KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
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 /**
26  * Sat, Jan 03, 2004 @ 11:16
27  * @author: Tuan Nguyen
28  * @email: tuan08@users.sourceforge.net
29  * @version: $Id: UIMembershipForm.java,v 1.11 2004/10/22 14:23:30 tuan08 Exp $
30  */

31 public class UIMembershipForm extends UIForm {
32   private static String JavaDoc USERNAME = "username" , MEMBERSHIP_NAME = "mname" ,
33                         GROUP_ID = "groupId" ;
34
35   public UIMembershipForm() throws Exception JavaDoc {
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 JavaDoc groupId) throws Exception JavaDoc {
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 JavaDoc userName) throws Exception JavaDoc {
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 JavaDoc {
64     OrganizationService service =
65       (OrganizationService) PortalContainer.getComponent(OrganizationService.class);
66     Collection JavaDoc memberships = service.findMembershipTypes() ;
67     Iterator JavaDoc i = memberships.iterator() ;
68     List JavaDoc roleOptions = new ArrayList JavaDoc(10) ;
69     while(i.hasNext()) {
70       MembershipType mt = (MembershipType) i.next();
71       String JavaDoc 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 JavaDoc {
85       UIMembershipForm uiForm = (UIMembershipForm) event.getSource() ;
86       OrganizationService service =
87         (OrganizationService) PortalContainer.getComponent(OrganizationService.class);
88         String JavaDoc userName = uiForm.getStringFieldValue(USERNAME) ;
89       User user = service.findUserByName(userName) ;
90       if(user == null) {
91         Object JavaDoc[] args = { userName } ;
92         throw new ExoMessageException("UIMembershipForm.user-not-found", args) ;
93       }
94       String JavaDoc groupId = uiForm.getStringFieldValue(GROUP_ID);
95       Group group = service.findGroupById(groupId) ;
96       if(group == null) {
97         Object JavaDoc[] 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 JavaDoc 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