/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.harvard.iq.dataverse;

import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.hibernate.validator.constraints.NotBlank;

/**
 *
 * @author xyang
 */
@ViewScoped
@Named("DataverseUserPage")
public class DataverseUserPage implements java.io.Serializable {

    @EJB
    DataverseUserServiceBean dataverseUserService;

    private DataverseUser dataverseUser = new DataverseUser();
    private boolean editMode = false;
    
    @NotBlank(message = "Please enter a password for your account.")    
    private String inputPassword;
    
    public DataverseUser getDataverseUser() {
        return dataverseUser;
    }

    public void setDataverseUser(DataverseUser dataverseUser) {
        this.dataverseUser = dataverseUser;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public void init() {
        editMode = true;;
    }
    public void edit(ActionEvent e) {
        editMode = true;
    }
    
    public String getInputPassword() {
        return inputPassword;
    }

    public void setInputPassword(String inputPassword) {
        this.inputPassword = inputPassword;
    }

    public void save(ActionEvent e) {
        if (inputPassword!=null) {
            dataverseUser.setEncryptedPassword(dataverseUserService.encryptPassword(inputPassword));
        }
        dataverseUser = dataverseUserService.save(dataverseUser);
        editMode = false;
    }

    public void cancel(ActionEvent e) {
        init();
        editMode = true;
    }
}
@@ -236,16 +236,182 @@
 jb.EJB;%0A
+import javax.faces.application.FacesMessage;%0Aimport javax.faces.component.UIComponent;%0Aimport javax.faces.component.UIInput;%0Aimport javax.faces.context.FacesContext;%0A
 import j
@@ -1327,139 +1327,8 @@
  %7D%0A%0A
-    public void init() %7B%0A        editMode = true;;%0A    %7D%0A    public void edit(ActionEvent e) %7B%0A        editMode = true;%0A    %7D%0A    %0A
     
@@ -1498,32 +1498,640 @@
 Password;%0A    %7D%0A
+    %0A    public void init() %7B%0A        editMode = true;;%0A    %7D%0A    public void edit(ActionEvent e) %7B%0A        editMode = true;%0A    %7D%0A    %0A    public void validateUserName(FacesContext context, UIComponent toValidate, Object value) %7B%0A        String userName = (String) value;%0A        DataverseUser user = dataverseUserService.findByUserName(userName);%0A        if (user!=null) %7B%0A            ((UIInput)toValidate).setValid(false);%0A            FacesMessage message = new FacesMessage(%22This Username is already taken.%22);%0A            context.addMessage(toValidate.getClientId(context), message);%0A        %7D%0A    %7D%0A    
 %0A    public void

Check if a username exists.