1 5 package org.exoplatform.portlets.user.component; 6 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 import org.exoplatform.Constants; 13 import org.exoplatform.container.PortalContainer; 14 import org.exoplatform.commons.exception.ExoMessageException; 15 import org.exoplatform.faces.core.component.UIForm; 16 import org.exoplatform.faces.core.component.model.SelectItem; 17 import org.exoplatform.faces.core.event.CheckRoleInterceptor; 18 import org.exoplatform.faces.core.event.ExoActionEvent; 19 import org.exoplatform.faces.core.event.ExoActionListener; 20 import org.exoplatform.services.organization.*; 21 import org.exoplatform.services.organization.Group; 22 import org.exoplatform.services.organization.Membership; 23 import org.exoplatform.services.organization.MembershipType; 24 import org.exoplatform.services.organization.OrganizationService; 25 31 public class UIMembershipForm extends UIForm { 32 private static String USERNAME = "username" , MEMBERSHIP_NAME = "mname" , 33 GROUP_ID = "groupId" ; 34 35 public UIMembershipForm() throws Exception { 36 super("membership") ; 37 setId("UIMembershipForm"); 38 setClazz("UICompactForm") ; 39 addContainer("#{UIMembershipForm.header.membership}"). 40 add(new StringField(USERNAME, "#{UIMembershipForm.label.user-name}", "")). 41 add(new SelectBoxField(MEMBERSHIP_NAME , "#{UIMembershipForm.label.membership-name}", "", Constants.EMPTY_LIST)). 42 add(new StringField(GROUP_ID, "#{UIMembershipForm.label.group-id}", "")); 43 addButton(new Button("#{UIMembershipForm.button.save}", SAVE_ACTION)) ; 44 addActionListener(SaveActionListener.class, SAVE_ACTION) ; 45 } 46 47 public void populateFormByGroup(String groupId) throws Exception { 48 populateCommon() ; 49 StringField groupIdField = getStringField(GROUP_ID) ; 50 groupIdField.setValue(groupId) ; 51 groupIdField.setEditable(false) ; 52 setStringFieldValue(USERNAME, "") ; 53 } 54 55 public void populateFormByUser(String userName) throws Exception { 56 populateCommon() ; 57 StringField usernameField = getStringField(USERNAME) ; 58 usernameField.setValue(userName) ; 59 usernameField.setEditable(false) ; 60 setStringFieldValue(GROUP_ID, "") ; 61 } 62 63 private void populateCommon() throws Exception { 64 OrganizationService service = 65 (OrganizationService) PortalContainer.getComponent(OrganizationService.class); 66 Collection memberships = service.findMembershipTypes() ; 67 Iterator i = memberships.iterator() ; 68 List roleOptions = new ArrayList (10) ; 69 while(i.hasNext()) { 70 MembershipType mt = (MembershipType) i.next(); 71 String name = mt.getName() ; 72 roleOptions.add(new SelectItem(name, name)) ; 73 } 74 SelectBoxField sfield = (SelectBoxField)getField(MEMBERSHIP_NAME) ; 75 sfield.setValue("") ; 76 sfield.setOptions(roleOptions) ; 77 } 78 79 static public class SaveActionListener extends ExoActionListener { 80 public SaveActionListener( ) { 81 addInterceptor(new CheckRoleInterceptor("admin")) ; 82 } 83 84 public void execute(ExoActionEvent event) throws Exception { 85 UIMembershipForm uiForm = (UIMembershipForm) event.getSource() ; 86 OrganizationService service = 87 (OrganizationService) PortalContainer.getComponent(OrganizationService.class); 88 String userName = uiForm.getStringFieldValue(USERNAME) ; 89 User user = service.findUserByName(userName) ; 90 if(user == null) { 91 Object [] args = { userName } ; 92 throw new ExoMessageException("UIMembershipForm.user-not-found", args) ; 93 } 94 String groupId = uiForm.getStringFieldValue(GROUP_ID); 95 Group group = service.findGroupById(groupId) ; 96 if(group == null) { 97 Object [] args = { groupId } ; 98 throw new ExoMessageException("UIMembershipForm.group-not-found", args) ; 99 } 100 Membership membership = service.createMembershipInstance(); 101 membership.setMembershipType(uiForm.getStringFieldValue(MEMBERSHIP_NAME)); 102 service.linkMembership(userName, group, membership); 103 Object parent = uiForm.getParent() ; 104 if(parent instanceof UIUserInfo) { 105 UIUserInfo uiParent = (UIUserInfo) parent ; 106 uiParent.update(); 107 } else if(parent instanceof UIViewUserInGroup) { 108 UIViewUserInGroup uiParent = (UIViewUserInGroup) parent ; 109 uiParent.update() ; 110 } 111 } 112 } 113 } | Popular Tags |