package org.osiam.addons.administration.model.command;

import org.osiam.resources.scim.Email;
import org.osiam.resources.scim.Name;
import org.osiam.resources.scim.UpdateUser;
import org.osiam.resources.scim.User;

/**
 * Command object for the user update view.
 * 
 * @author Timo Kanera, tarent solutions GmbH
 */
public class UpdateUserCommand {

    private User user;
   
    private String id;
    private String firstName;
    private String lastName;
    private String email;

    /**
     * Creates a new UpdateUserCommand based on the given {@link User}.
     * 
     * @param user
     *        the user
     */
    public UpdateUserCommand(User user) {
        this.user = user;
        setId(user.getId());
        if (user.getName() != null) {
            setFirstName(user.getName().getGivenName());
            setLastName(user.getName().getFamilyName());
        }
        Email primaryEmail = getPrimaryEMail(user);
        if (primaryEmail != null) {
            setEmail(primaryEmail.getValue());
        }
    }

    /**
     * Creates a new UpdateUserCommand.
     */
    public UpdateUserCommand() {
    }

    /**
     * Returns the first name.
     * 
     * @return the first name
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets the first name.
     * 
     * @param firstName
     *        the first name to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Returns the last name.
     * 
     * @return the last name
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets the last name.
     * 
     * @param lastName
     *        the last name to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Returns the e-mail address.
     * 
     * @return the the e-mail address
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets the e-mail address.
     * 
     * @param email
     *        the e-mail address to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Returns the user ID.
     * 
     * @return the user ID
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the user ID.
     * 
     * @param id the user ID to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Sets the user.
     * 
     * @param user
     *        the {@link User} to set.
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * Returns a SCIM {@link UpdateUser} based on this command.
     * 
     * @return the requested {@link UpdateUser}
     */
    public UpdateUser asUpdateUser() {
        Name name = new Name.Builder().setGivenName(getFirstName()).setFamilyName(getLastName()).build();
        UpdateUser.Builder builder = new UpdateUser.Builder().updateName(name);

        Email previousEmail = getPrimaryEMail(user);
        if (previousEmail != null) {
            builder.updateEmail(previousEmail, new Email.Builder(previousEmail).setValue(getEmail()).build());
        } else {
            builder.addEmail(new Email.Builder().setPrimary(true).setValue(getEmail()).build());
        }

        return builder.build();
    }

    private Email getPrimaryEMail(User user) {
        for (Email eMail : user.getEmails()) {
            if (eMail.isPrimary()) {
                return eMail;
            }
        }
        return null;
    }
}

@@ -49,16 +49,62 @@
 mmand;%0A%0A
+import org.osiam.resources.helper.SCIMHelper;%0A
 import o
@@ -255,16 +255,57 @@
 .User;%0A%0A
+import com.google.common.base.Optional;%0A%0A
 /**%0A * C
@@ -455,19 +455,16 @@
 r user;%0A
-   
 %0A    pri
@@ -970,21 +970,31 @@
         
+Optional%3C
 Email
+%3E
  primary
@@ -993,32 +993,43 @@
  primaryEmail = 
+SCIMHelper.
 getPrimaryEMail(
@@ -1014,34 +1014,41 @@
 elper.getPrimary
-EM
+OrFirstEm
 ail(user);%0A     
@@ -1062,32 +1062,36 @@
 primaryEmail
- != null
+.isPresent()
 ) %7B%0A        
@@ -1116,16 +1116,22 @@
 ryEmail.
+get().
 getValue
@@ -2490,16 +2490,30 @@
 param id
+%0A     *       
  the use
@@ -3140,21 +3140,31 @@
         
+Optional%3C
 Email
+%3E
  previou
@@ -3172,16 +3172,27 @@
 Email = 
+SCIMHelper.
 getPrima
@@ -3189,26 +3189,33 @@
 r.getPrimary
-EM
+OrFirstEm
 ail(user);%0A 
@@ -3238,24 +3238,28 @@
 ousEmail
- != null
+.isPresent()
 ) %7B%0A    
@@ -3299,16 +3299,22 @@
 ousEmail
+.get()
 , new Em
@@ -3338,16 +3338,22 @@
 ousEmail
+.get()
 ).setVal
@@ -3358,32 +3358,53 @@
 alue(getEmail())
+%0A                    
 .build());%0A     
@@ -3564,220 +3564,7 @@
    %7D
-%0A%0A    private Email getPrimaryEMail(User user) %7B%0A        for (Email eMail : user.getEmails()) %7B%0A            if (eMail.isPrimary()) %7B%0A                return eMail;%0A            %7D%0A        %7D%0A        return null;%0A    %7D
 %0A%7D%0A

use SCIMHelper.getPrimaryOrFirstEmail() to retrieve the primary email of the user