1 package hero.session; 2 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 ; 40 import java.util.Collection ; 41 import java.util.Iterator ; 42 43 import javax.ejb.CreateException ; 44 import javax.ejb.FinderException ; 45 import javax.ejb.RemoveException ; 46 import javax.ejb.SessionBean ; 47 import javax.ejb.SessionContext ; 48 49 import org.apache.log4j.Category; 50 51 111 112 public class UserRegistrationBean implements SessionBean { 113 114 private static final Category trace = Category.getInstance(UserRegistrationBean.class); 116 117 121 private SessionContext mContext; 122 123 private BnUserLocalHome userhome; 124 private BnAuthRoleLocalHome rolehome; 125 126 130 138 public void ejbCreate() throws CreateException { 139 trace.debug("create"); 140 } 141 142 150 public BnUserLocal userCreate(String 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 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 f) { 165 throw new HeroException(name+" is already exist"); 166 } 167 } 168 } 169 170 180 public void userCreate(String name, String password, String 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 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 f) { 196 throw new HeroException(name+" is already exist"); 197 } 198 } 199 } 200 201 212 public void userCreate(String name,String password,String email,String 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 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 f) { 229 throw new HeroException(name + " is already exist"); 230 } 231 } 232 } 233 243 public void deleteUser(String userName) throws HeroException { 244 trace.info(" Parameter: userName=" + userName); 245 246 try { 247 BnUserLocal user = userhome.findByName(userName); 248 Collection projects = user.getBnProjects(); 249 Iterator 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 fe) { 258 throw new HeroException(fe.getMessage()); 259 } catch (RemoveException rm) { 260 throw new HeroException(rm.getMessage()); 261 } 262 } 263 264 274 public void roleCreate(String name, String 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 f) { 282 throw new HeroException(name+" is already exist"); 283 } 284 } 285 286 295 public void setUserRole(String userName, String 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 roles = user.getBnAuthRoles(); 301 roles.add(role); 302 } catch (Exception f) { 303 throw new HeroException(userName+" Error in setUserAuthRole..."); 304 } 305 } 306 307 318 public void setUserProperty(String userName, String key, String 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 ne) { 333 throw new HeroException(ne.getMessage()); 334 } 335 Collection c=null; 336 try { 337 c=pHome.findByTheKey(mUser.getId(),key); 338 } catch (FinderException 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 ce) { 355 throw new HeroException(ce.getMessage()); 356 } 357 } 358 362 363 } catch (Exception f) { 364 throw new HeroException("Error in setUserProperty"); 365 } 366 } 367 368 371 372 public void setSessionContext(javax.ejb.SessionContext context) throws RemoteException { 373 this.mContext=context; 374 try { 375 userhome = BnUserUtil.getLocalHome(); 376 rolehome = BnAuthRoleUtil.getLocalHome(); 377 }catch(Exception e){ 378 throw new RemoteException (e.getMessage()); 379 } 380 } 381 382 385 public void unsetSessionContext() throws RemoteException { 386 } 387 388 391 public void ejbRemove() throws java.rmi.RemoteException { 392 } 393 394 397 public void ejbActivate() throws java.rmi.RemoteException { 398 } 399 400 403 public void ejbPassivate() throws java.rmi.RemoteException { 404 } 405 406 } 407 | Popular Tags |