KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > user > component > UIAccountForm


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.user.component;
6
7 import javax.faces.component.UIComponent;
8 import javax.faces.context.FacesContext;
9 import javax.faces.validator.Validator;
10 import javax.faces.validator.ValidatorException;
11 import org.exoplatform.faces.application.ExoFacesMessage;
12 import org.exoplatform.faces.core.component.InformationProvider;
13 import org.exoplatform.faces.core.component.UIForm;
14 import org.exoplatform.faces.core.event.ExoActionEvent;
15 import org.exoplatform.faces.core.event.ExoActionListener;
16 import org.exoplatform.faces.core.validator.EmailAddressValidator;
17 import org.exoplatform.faces.core.validator.EmptyFieldValidator;
18 import org.exoplatform.container.PortalContainer ;
19 import org.exoplatform.services.organization.OrganizationService;
20 import org.exoplatform.services.organization.User;
21 /**
22  * Sat, Jan 03, 2004 @ 11:16
23  * @author: Tuan Nguyen
24  * @email: tuan08@users.sourceforge.net
25  * @version: $Id: UIAccountForm.java,v 1.14 2004/08/17 13:07:02 tuan08 Exp $
26  */

27 public class UIAccountForm extends UIForm {
28   private static String JavaDoc USERNAME = "username" , PASSWORD1X = "password1x" ,
29                         PASSWORD2X = "password2x" , EMAIL = "email" ,
30                         FIRSTNAME = "firstname" , LASTNAME = "lastname" ;
31   private User user_ ;
32   
33   public UIAccountForm() throws Exception JavaDoc {
34     super("accountForm") ;
35     setId("UIAccountForm") ;
36     addContainer("#{UIAccountForm.header.new-account}").
37       add(new StringField(USERNAME, "#{UIAccountForm.label.user-name}", "")).
38       add(new StringPasswordField(PASSWORD1X, "#{UIAccountForm.label.password1x}", "")).
39       add(new StringPasswordField(PASSWORD2X, "#{UIAccountForm.label.password2x}", "")).
40       add(new StringField(FIRSTNAME, "#{UIAccountForm.label.first-name}", "")).
41       add(new StringField(LASTNAME, "#{UIAccountForm.label.last-name}", "")).
42       add(new StringField(EMAIL, "#{UIAccountForm.label.email}", "").
43           setValidator(EmailAddressValidator.class));
44     addFieldValidator(EmptyFieldValidator.class);
45     addFormValidator(ConfirmPasswordValidator.class);
46     addButton(new Button("#{UIAccountForm.button.save}", SAVE_ACTION)) ;
47   }
48    
49   public void customizeNewAccountForm() {
50     addActionListener(NewAccountListener.class, SAVE_ACTION) ;
51   }
52   
53   public void customizeUpdateAccountForm() {
54     addActionListener(UpdateAccountListener.class, SAVE_ACTION) ;
55     getStringField(USERNAME).setEditable(false);
56   }
57   
58   public void setEditingUser(String JavaDoc userName) throws Exception JavaDoc {
59     OrganizationService service =
60       (OrganizationService) PortalContainer.getComponent(OrganizationService.class) ;
61     user_ = service.findUserByName(userName) ;
62     setEditingUser(user_) ;
63   }
64   
65   public void setEditingUser(User user) throws Exception JavaDoc {
66     user_ = user;
67     setStringFieldValue(USERNAME, user_.getUserName());
68     setStringFieldValue(PASSWORD1X, user_.getPassword());
69     setStringFieldValue(PASSWORD2X, user_.getPassword());
70     setStringFieldValue(FIRSTNAME, user_.getFirstName());
71     setStringFieldValue(LASTNAME, user_.getLastName());
72     setStringFieldValue(EMAIL, user_.getEmail());
73   }
74   
75   static public class ConfirmPasswordValidator implements Validator {
76     public void validate(FacesContext context, UIComponent component,
77                          Object JavaDoc value) throws ValidatorException {
78       UIAccountForm uiForm = (UIAccountForm) component ;
79       String JavaDoc p1 = uiForm.getStringFieldValue(PASSWORD1X) ;
80       String JavaDoc p2 = uiForm.getStringFieldValue(PASSWORD2X) ;
81       if(!p1.equals(p2)) {
82         throw new ValidatorException(new ExoFacesMessage("#{UIAccountForm.msg.password-error}")) ;
83       }
84     }
85   }
86   
87   static public class NewAccountListener extends ExoActionListener {
88     public void execute(ExoActionEvent event) throws Exception JavaDoc {
89       UIAccountForm uicomp = (UIAccountForm) event.getComponent() ;
90       InformationProvider iprovider = findInformationProvider(uicomp);
91         String JavaDoc userName = uicomp.getStringFieldValue(USERNAME) ;
92       OrganizationService service =
93         (OrganizationService) PortalContainer.getComponent(OrganizationService.class) ;
94       User newUser = service.findUserByName(userName) ;
95       if (newUser == null) {
96         newUser = service.createUserInstance() ;
97         newUser.setUserName(userName) ;
98         newUser.setPassword(uicomp.getStringFieldValue(PASSWORD1X)) ;
99         newUser.setFirstName(uicomp.getStringFieldValue(FIRSTNAME)) ;
100         newUser.setLastName(uicomp.getStringFieldValue(LASTNAME)) ;
101         newUser.setEmail(uicomp.getStringFieldValue(EMAIL)) ;
102         service.createUser(newUser);
103         uicomp.resetFields() ;
104         iprovider.addMessage(new ExoFacesMessage("#{UIAccountForm.msg.create-user-success}")) ;
105       } else {
106         iprovider.addMessage(new ExoFacesMessage("#{UIAccountForm.msg.user-already-exist}")) ;
107       }
108     }
109   }
110   
111   static public class UpdateAccountListener extends ExoActionListener {
112     public void execute(ExoActionEvent event) throws Exception JavaDoc {
113       UIAccountForm uicomp = (UIAccountForm) event.getComponent() ;
114       uicomp.user_.setPassword(uicomp.getStringFieldValue(PASSWORD1X)) ;
115       uicomp.user_.setFirstName(uicomp.getStringFieldValue(FIRSTNAME)) ;
116       uicomp.user_.setLastName(uicomp.getStringFieldValue(LASTNAME)) ;
117       uicomp.user_.setEmail(uicomp.getStringFieldValue(EMAIL)) ;
118       OrganizationService service =
119         (OrganizationService) PortalContainer.getComponent(OrganizationService.class) ;
120       service.saveUser(uicomp.user_) ;
121       InformationProvider iprovider = findInformationProvider(uicomp);
122       iprovider.addMessage(new ExoFacesMessage("#{UIAccountForm.msg.update-user-success}")) ;
123     }
124   }
125 }
Popular Tags