KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > session > UserRegistrationBean


1 package hero.session;
2 /*
3 * 02/01/2002 - 15:24:07
4 *
5 * UserRegistrationEJB.java -
6 * Copyright (C) 2002 Ecoo Team
7 * valdes@loria.fr
8 *
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */

24 import hero.interfaces.BnAuthRoleLocal;
25 import hero.interfaces.BnAuthRoleLocalHome;
26 import hero.interfaces.BnAuthRoleUtil;
27 import hero.interfaces.BnAuthRoleValue;
28 import hero.interfaces.BnUserLocal;
29 import hero.interfaces.BnUserLocalHome;
30 import hero.interfaces.BnUserPropertyLocal;
31 import hero.interfaces.BnUserPropertyLocalHome;
32 import hero.interfaces.BnUserPropertyUtil;
33 import hero.interfaces.BnUserUtil;
34 import hero.interfaces.BnUserValue;
35 import hero.interfaces.BnProjectLocal;
36 import hero.interfaces.InvalidValueException;
37 import hero.util.HeroException;
38
39 import java.rmi.RemoteException JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 import javax.ejb.CreateException JavaDoc;
44 import javax.ejb.FinderException JavaDoc;
45 import javax.ejb.RemoveException JavaDoc;
46 import javax.ejb.SessionBean JavaDoc;
47 import javax.ejb.SessionContext JavaDoc;
48
49 import org.apache.log4j.Category;
50
51 /**
52  * The User Registration Bean, is an stateless session bean that provides the API to create users in
53  * Bonita database (new user account). This API allows some functionalities oriented to user
54  * configuration: set authorization roles, set Bonita roles, set preferences...
55  *
56  * <br><br>
57  * <strong>The following lines shows a sample code to use this API in your application:<br><br></strong>
58  * <br>
59  * First of all you have to import the User Registration files:<br><br>
60  *
61  * import hero.interfaces.UserRegistrationLocalHome;<br>
62  * import hero.interfaces.UserRegistrationLocal;<br>
63  * import hero.interfaces.UserRegistrationHome;<br>
64  * import hero.interfaces.UserRegistration;<br>
65  * import hero.interfaces.UserRegistrationUtil;<br>
66   *
67  * <br>
68  * Now, it is time to create the User Registration instance:<br>
69  * <br>
70  * Like this if you want to use local interfaces:<br><br>
71  *
72  * hero.interfaces.UserRegistrationLocalHome userh = (UserRegistrationLocalHome)hero.interfaces.UserRegistrationUtil.getLocalHome();<br>
73  * hero.interfaces.UserRegistrationLocal user = userh.create();<br><br>
74  *
75  * or like this if you use remote interfaces:<br>
76  * <br>
77  *
78  * hero.interfaces.UserRegistrationHome userh = (UserRegistrationHome)hero.interfaces.UserRegistrationUtil.getHome();<br>
79  * hero.interfaces.UserRegistration user = userh.create();
80  * <br>
81  * <br>
82  * Now, we can create a new user account by using userCreate methods (with or without the instant
83  * messaging address).<br><br>
84  *
85  * user.userCreate(name,password,email);<br>
86  * or
87  * user.userCreate(name,password,email,jabber);<br>
88  *
89  *
90  * @ejb:bean name="UserRegistration"
91  * display-name="UserRegistration Bean"
92  * type="Stateless"
93  * transaction-type="Container"
94  * jndi-name="ejb/hero/UserRegistration"
95  * local-jndi-name="ejb/hero/UserRegistration_L"
96  *
97  * @ejb:ejb-ref ejb-name="UserRegistration"
98  * ref-name="myhero/UserRegistration"
99  * @ejb:transaction type="Required"
100  * @ejb:transaction-type type="Container"
101  * @ejb.permission unchecked="yes"
102  * @ejb.security-identity run-as="SuperAdmin"
103  * @jonas.bean
104  * ejb-name="UserRegistration"
105  * jndi-name="ejb/hero/UserRegistration"
106  *
107  * @copyright INRIA
108  * @author Miguel Valdes
109  *
110  **/

111
112 public class UserRegistrationBean implements SessionBean JavaDoc {
113   
114    // Utility variable
115
private static final Category trace = Category.getInstance(UserRegistrationBean.class);
116
117    // -------------------------------------------------------------------------
118
// Members
119
// -------------------------------------------------------------------------
120

121    private SessionContext JavaDoc mContext;
122
123     private BnUserLocalHome userhome;
124     private BnAuthRoleLocalHome rolehome;
125
126    // -------------------------------------------------------------------------
127
// Methods
128
// -------------------------------------------------------------------------
129

130    /**
131    * Create the User Session Bean. This method is the first one to invoke in order to
132    * creates a new user account.
133    *
134    * @throws CreateException
135    *
136    * @ejb:create-method view-type="both"
137    **/

138     public void ejbCreate() throws CreateException JavaDoc {
139     trace.debug("create");
140     }
141     
142     /**
143      * Creates user account. You have to call this method after "create" call (this API is an stateles
144      * session bean). This method is oriented to Bonita external users configuration
145      * @param name name of the new user
146      * @ejb:interface-method view-type="both"
147      * @throws HeroException
148      *
149      **/

150     public BnUserLocal userCreate(String JavaDoc name) throws HeroException {
151         trace.info(" Parameter: name=" + name);
152         try {
153             BnUserLocal ul = userhome.findByName(name);
154             throw new HeroException("User "+ name+ " already exist ");
155         } catch (javax.ejb.FinderException JavaDoc nn) {
156         try{
157            BnUserValue newUser=new BnUserValue();
158            newUser.setName(name);
159            BnUserLocal user = userhome.create(newUser);
160            this.setUserRole(name,hero.interfaces.Constants.USER_SECURITY_ROLE);
161            return user;
162         } catch (InvalidValueException ie) {
163            throw new HeroException(ie.getMessage());
164         } catch (CreateException JavaDoc f) {
165            throw new HeroException(name+" is already exist");
166         }
167      }
168     }
169
170     /**
171      * Creates user account. You have to call this method after "create" call (this API is an stateles
172      * session bean).
173      * @param name name of the new user
174      * @param password password of the new user
175      * @param email email of the new user
176      * @ejb:interface-method view-type="both"
177      * @throws HeroException
178      *
179      **/

180     public void userCreate(String JavaDoc name, String JavaDoc password, String JavaDoc email) throws HeroException {
181         trace.info(" Parameter: name=" + name + " password = "+password+ " mail = " + email);
182         try {
183             BnUserLocal ul = userhome.findByName(name);
184             throw new HeroException("User "+ name+ " already exist ");
185         } catch (javax.ejb.FinderException JavaDoc nn) {
186         try{
187            BnUserValue newUser=new BnUserValue();
188            newUser.setName(name);
189            newUser.setPassword(password);
190            newUser.setEmail(email);
191            BnUserLocal user = userhome.create(newUser);
192            this.setUserRole(name,hero.interfaces.Constants.USER_SECURITY_ROLE);
193         } catch (InvalidValueException ie) {
194            throw new HeroException(ie.getMessage());
195         } catch (CreateException JavaDoc f) {
196            throw new HeroException(name+" is already exist");
197         }
198      }
199     }
200
201     /**
202      * Creates user account with an instant messaging address. You have to call this method after "create" call (this API is an stateles
203      * session bean).
204      * @param name name of the new user
205      * @param password password of the new user
206      * @param email email of the new user
207      * @param jabber jabber address of the new user
208      * @ejb:interface-method view-type="both"
209      * @throws HeroException
210      *
211     **/

212      public void userCreate(String JavaDoc name,String JavaDoc password,String JavaDoc email,String JavaDoc jabber) throws HeroException {
213         trace.info(" Parameter: name=" + name + " password = "+password+ " mail = " + email+ " jabber = " + jabber);
214         try {
215             BnUserLocal ul = userhome.findByName(name);
216             throw new HeroException("User " + name + " already exist ");
217         } catch (javax.ejb.FinderException JavaDoc nn) {
218             try {
219                 BnUserValue newUser = new BnUserValue();
220                 newUser.setName(name);
221                 newUser.setPassword(password);
222                 newUser.setEmail(email);
223                 newUser.setJabber(jabber);
224                 BnUserLocal user = userhome.create(newUser);
225                 this.setUserRole(name, hero.interfaces.Constants.USER_SECURITY_ROLE);
226             } catch (InvalidValueException ie) {
227                 throw new HeroException(ie.getMessage());
228             } catch (CreateException JavaDoc f) {
229                 throw new HeroException(name + " is already exist");
230             }
231         }
232     }
233     /**
234      * Detete a user from Bonita database. If the user is included in active projects this
235      * methods throws an exception.
236      *
237      * @param userName the name of the user
238      * @ejb:interface-method view-type="both"
239      * @ejb:transaction type="Required"
240      * @throws HeroException
241      *
242     **/

243     public void deleteUser(String JavaDoc userName) throws HeroException {
244         trace.info(" Parameter: userName=" + userName);
245         
246         try {
247             BnUserLocal user = userhome.findByName(userName);
248             Collection JavaDoc projects = user.getBnProjects();
249             Iterator JavaDoc i = projects.iterator();
250             while (i.hasNext())
251             {
252                 BnProjectLocal project = (BnProjectLocal)i.next();
253                 if (project.getState()!=hero.interfaces.Constants.Pj.TERMINATED)
254                     throw new HeroException("Cannot remove user: This user takes part in some workflow projects ");
255             }
256             user.remove();
257         } catch (FinderException JavaDoc fe) {
258             throw new HeroException(fe.getMessage());
259         } catch (RemoveException JavaDoc rm) {
260             throw new HeroException(rm.getMessage());
261         }
262     }
263
264     /**
265      * Creates a new authorization role to the system. This kind of role is used to control the user
266      * access to different APIs. You have to call this method after "create" call (this API is an stateles
267      * session bean).
268      * @param name name of the new role
269      * @param roleGroup the name of the group
270      * @ejb:interface-method view-type="both"
271      * @throws HeroException
272      *
273      **/

274     public void roleCreate(String JavaDoc name, String JavaDoc roleGroup) throws HeroException {
275         trace.info(" Parameter: name=" + name + " roleGroup = " + roleGroup);
276     try {
277         BnAuthRoleValue newRole=new BnAuthRoleValue();
278         newRole.setName(name);
279         newRole.setBnRoleGroup(roleGroup);
280         BnAuthRoleLocal role = rolehome.create(newRole);
281     } catch (CreateException JavaDoc f) {
282         throw new HeroException(name+" is already exist");
283     }
284     }
285
286     /**
287      * Set a new authorization role to the user. You have to call this method after "create" call
288      * (this API is an stateles session bean).
289      * @param username name of the user
290      * @param rolename name of the role
291      * @ejb:interface-method view-type="both"
292      * @throws HeroException
293      *
294      **/

295     public void setUserRole(String JavaDoc userName, String JavaDoc roleName) throws HeroException {
296     trace.info(" Parameter: userName=" + userName + " roleName = " + roleName);
297     try {
298         BnUserLocal user = userhome.findByName(userName);
299         BnAuthRoleLocal role = rolehome.findByName(roleName);
300         Collection JavaDoc roles = user.getBnAuthRoles();
301         roles.add(role);
302     } catch (Exception JavaDoc f) {
303         throw new HeroException(userName+" Error in setUserAuthRole...");
304     }
305     }
306
307     /**
308      * Set a new property to the user. User properties will be used to define user preferences.
309      * You have to call this method after "create" call (this API is an stateles session bean).
310      *
311      * @param userName the name of the user
312      * @param key the key of user property
313      * @param value the value of user property
314      * @ejb:interface-method view-type="both"
315      * @throws HeroException
316      *
317      **/

318     public void setUserProperty(String JavaDoc userName, String JavaDoc key, String JavaDoc value) throws HeroException {
319     trace.info(" Parameter: userName=" + userName + " key = " + key + " value = " + value);
320
321     BnUserLocalHome userhome;
322     BnUserLocal mUser;
323     BnUserPropertyLocalHome pHome;
324     BnUserPropertyLocal propertyLocal=null;
325
326     try {
327         userhome=BnUserUtil.getLocalHome();
328         mUser=userhome.findByName(userName);
329     
330         try {
331         pHome=BnUserPropertyUtil.getLocalHome();
332         } catch (javax.naming.NamingException JavaDoc ne) {
333         throw new HeroException(ne.getMessage());
334         }
335         Collection JavaDoc c=null;
336         try {
337         c=pHome.findByTheKey(mUser.getId(),key);
338         } catch (FinderException JavaDoc fe) {
339         throw new HeroException(fe.getMessage());
340         }
341         
342         if (!c.isEmpty()){
343         
344         propertyLocal=(BnUserPropertyLocal)(c.toArray())[0];
345         propertyLocal.setTheValue(value);
346         
347         }else{
348         try{
349             propertyLocal=pHome.create(key,value);
350             propertyLocal.setBnUser(mUser);
351             
352         } catch(InvalidValueException ie){
353             throw new HeroException(ie.getMessage());
354         } catch (javax.ejb.CreateException JavaDoc ce) {
355             throw new HeroException(ce.getMessage());
356         }
357         }
358 /*
359         hero.interfaces.UserSessionLocalHome usersessionhome = (UserSessionLocalHome)hero.interfaces.UserSessionUtil.getLocalHome();
360         UserSessionLocal user = usersessionhome.create();
361         user.setUserProperty(key,value);*/

362
363     } catch (Exception JavaDoc f) {
364         throw new HeroException("Error in setUserProperty");
365     }
366     }
367
368     /**
369     * Internal Enterprise Java Beans method.
370     **/

371     
372     public void setSessionContext(javax.ejb.SessionContext JavaDoc context) throws RemoteException JavaDoc {
373     this.mContext=context;
374     try {
375         userhome = BnUserUtil.getLocalHome();
376         rolehome = BnAuthRoleUtil.getLocalHome();
377     }catch(Exception JavaDoc e){
378         throw new RemoteException JavaDoc(e.getMessage());
379     }
380     }
381
382     /**
383     * Internal Enterprise Java Beans method.
384     **/

385     public void unsetSessionContext() throws RemoteException JavaDoc {
386     }
387     
388     /**
389     * Internal Enterprise Java Beans method.
390     **/

391     public void ejbRemove() throws java.rmi.RemoteException JavaDoc {
392     }
393     
394     /**
395     * Internal Enterprise Java Beans method.
396     **/

397     public void ejbActivate() throws java.rmi.RemoteException JavaDoc {
398     }
399     
400     /**
401     * Internal Enterprise Java Beans method.
402     **/

403     public void ejbPassivate() throws java.rmi.RemoteException JavaDoc {
404     }
405
406 }
407
Popular Tags