1 5 package org.exoplatform.portlets.user.component; 6 7 import java.util.Iterator ; 8 import java.util.Map ; 9 import javax.faces.context.ExternalContext; 10 import javax.faces.context.FacesContext; 11 import javax.portlet.PortletRequest; 12 import org.exoplatform.container.PortalContainer; 13 import org.exoplatform.container.SessionContainer; 14 import org.exoplatform.faces.core.component.UIForm; 15 import org.exoplatform.faces.core.component.UINode; 16 import org.exoplatform.faces.core.component.UIForm.Container; 17 import org.exoplatform.faces.core.component.UIForm.StringField; 18 import org.exoplatform.faces.core.component.UIForm.TextAreaField; 19 import org.exoplatform.faces.core.event.ExoActionEvent; 20 import org.exoplatform.faces.core.event.ExoActionListener; 21 import org.exoplatform.services.organization.OrganizationService; 22 import org.exoplatform.services.organization.UserProfile; 23 24 28 public class UIUserProfile extends UINode { 29 final static public String [] PERSONAL_INFO_KEYS = 30 { 31 "user.name.given", "user.name.family", "user.name.nickName", "user.bdate" , 32 "user.gender" , "user.employer" , "user.department", "user.jobtitle", 33 } ; 34 35 final static public String [] HOME_INFO_KEYS = 36 { 37 "user.home-info.postal.name", "user.home-info.postal.street", 38 "user.home-info.postal.city", "user.home-info.postal.stateprov" , 39 "user.home-info.postal.postalcode", "user.home-info.postal.country", 40 "user.home-info.telecom.mobile.number", "user.home-info.telecom.telephone.number", 41 "user.home-info.online.email", "user.home-info.online.uri" 42 } ; 43 44 final static public String [] BUSINESE_INFO_KEYS = { 45 "user.business-info.postal.name", "user.business-info.postal.city", 46 "user.business-info.postal.stateprov", "user.business-info.postal.postalcode", 47 "user.business-info.postal.country", "user.business-info.telecom.mobile.number", 48 "user.business-info.telecom.telephone.number", "user.business-info.online.email", 49 "user.business-info.online.uri" 50 } ; 51 52 final static public String [] OTHER_KEYS = { 53 "user.other-info.avatar.url", "user.other-info.signature", 54 } ; 55 56 public UIUserProfile() throws Exception { 57 setRendererType("PyramidTabBarRenderer") ; 58 addChild(UIUserProfileSummary.class) ; 59 String userName = SessionContainer.getInstance().getOwner() ; 60 ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext() ; 61 if(!userName.equals(eContext.getRemoteUser())) return ; 63 64 Map requestMap = eContext.getRequestMap() ; 65 Map userInfoMap = (Map ) requestMap.get(PortletRequest.USER_INFO) ; 66 67 UINode uiAdmin = (UINode) addChild(UINode.class) ; 68 uiAdmin.setRendered(false) ; 69 uiAdmin.setRendererType("SimpleTabRenderer") ; 70 uiAdmin.setId("ProfileAdmin") ; 71 72 UIForm uiForm = new UIForm("personal") ; 73 uiForm.setId("PersonalInfo") ; 74 Container container = uiForm.addContainer("#{UIUserProfile.header.personal-info}") ; 75 for (int i =0 ; i < PERSONAL_INFO_KEYS.length; i++) { 76 String key = PERSONAL_INFO_KEYS[i] ; 77 container.add(new StringField(key,"#{" + key+ "}", (String )userInfoMap.get(key))) ; 78 } 79 80 container = uiForm.addContainer("#{UIUserProfile.header.user-home-info}") ; 81 for (int i =0 ; i < HOME_INFO_KEYS.length; i++) { 82 String key = HOME_INFO_KEYS[i] ; 83 container.add(new StringField(key,"#{" + key+ "}", (String )userInfoMap.get(key))) ; 84 } 85 86 uiForm.addButton(new UIForm.Button("#{UIUserProfile.button.save}", SAVE_ACTION)) ; 87 uiForm.addActionListener(SaveActionListener.class, SAVE_ACTION) ; 88 uiAdmin.getChildren().add(uiForm) ; 89 90 uiForm = new UIForm("busineseInfo") ; 91 uiForm.setId("BusineseInfo") ; 92 uiForm.setRendered(false) ; 93 container = uiForm.addContainer("#{UIUserProfile.header.user-businese-info}") ; 94 for (int i =0 ; i < BUSINESE_INFO_KEYS.length; i++) { 95 String key = BUSINESE_INFO_KEYS[i] ; 96 container.add(new StringField(key,"#{" + key+ "}", (String )userInfoMap.get(key))) ; 97 } 98 uiForm.addButton(new UIForm.Button("#{UIUserProfile.button.save}", SAVE_ACTION)) ; 99 uiForm.addActionListener(SaveActionListener.class, SAVE_ACTION) ; 100 uiAdmin.getChildren().add(uiForm) ; 101 102 uiForm = new UIForm("otherInfo") ; 103 uiForm.setId("OtherInfo") ; 104 uiForm.setRendered(false) ; 105 container = uiForm.addContainer("#{UIUserProfile.header.user-other-info}") ; 106 container.add(new StringField("user.other-info.avatar.url","#{user.other-info.avatar.url}", 107 (String )userInfoMap.get("user.other-info.avatar.url"))) ; 108 container.add(new TextAreaField("user.other-info.signature","#{user.other-info.signature}", 109 (String )userInfoMap.get("user.other-info.signature"))) ; 110 uiForm.addButton(new UIForm.Button("#{UIUserProfile.button.save}", SAVE_ACTION)) ; 111 uiForm.addActionListener(SaveActionListener.class, SAVE_ACTION) ; 112 uiAdmin.getChildren().add(uiForm) ; 113 UIAccountForm ui = new UIAccountForm() ; 114 ui.customizeUpdateAccountForm() ; 115 ui.setEditingUser(userName) ; 116 ui.setRendered(false) ; 117 uiAdmin.getChildren().add(ui) ; 118 } 119 120 121 static public class SaveActionListener extends ExoActionListener { 122 public void execute(ExoActionEvent event) throws Exception { 123 UIForm uiForm = (UIForm) event.getSource() ; 124 ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext() ; 125 Map requestMap = eContext.getRequestMap() ; 126 Map userInfoMap = (Map ) requestMap.get(PortletRequest.USER_INFO) ; 127 Iterator i = uiForm.getMapFields().values().iterator() ; 128 while (i.hasNext()) { 129 UIForm.StringField field = (UIForm.StringField) i.next() ; 130 String value = field.getValue() ; 131 if(value == null || value.length() == 0) { 132 userInfoMap.remove(field.getName()) ; 133 } else { 134 userInfoMap.put(field.getName(), value) ; 135 } 136 } 137 String userName = SessionContainer.getInstance().getOwner() ; 138 OrganizationService service = 139 (OrganizationService)PortalContainer.getComponent(OrganizationService.class) ; 140 UserProfile profile = service.findUserProfileByName(userName) ; 141 profile.setUserInfoMap(userInfoMap) ; 142 service.saveUserProfile(profile) ; 143 } 144 } 145 } | Popular Tags |