1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.apache.log4j.Logger; 32 import org.exolab.castor.jdo.Database; 33 import org.infoglue.cms.entities.kernel.BaseEntityVO; 34 import org.infoglue.cms.entities.management.Role; 35 import org.infoglue.cms.entities.management.RoleVO; 36 import org.infoglue.cms.entities.management.SystemUser; 37 import org.infoglue.cms.entities.management.impl.simple.RoleImpl; 38 import org.infoglue.cms.exception.Bug; 39 import org.infoglue.cms.exception.ConstraintException; 40 import org.infoglue.cms.exception.SystemException; 41 import org.infoglue.cms.util.ConstraintExceptionBuffer; 42 43 50 public class RoleController extends BaseController 51 { 52 private final static Logger logger = Logger.getLogger(RoleController.class.getName()); 53 54 57 58 public static RoleController getController() 59 { 60 return new RoleController(); 61 } 62 63 public Role getRoleWithId(Integer roleId, Database db) throws SystemException, Bug 64 { 65 return (Role) getObjectWithId(RoleImpl.class, roleId, db); 66 } 67 68 public Role getRoleWithName(String roleName, Database db) throws SystemException, Bug 69 { 70 return (Role)getObjectWithId(RoleImpl.class, roleName, db); 71 } 72 73 79 80 public RoleVO getRoleVOWithId(Integer roleId) throws SystemException, Bug 81 { 82 return (RoleVO) getVOWithId(RoleImpl.class, roleId); 83 } 84 85 public RoleVO getRoleVOWithId(String roleName) throws SystemException, Bug 86 { 87 return (RoleVO) getVOWithId(RoleImpl.class, roleName); 88 } 89 90 public RoleVO getRoleVOWithId(String roleName, Database db) throws SystemException, Bug 91 { 92 return (RoleVO) getVOWithId(RoleImpl.class, roleName, db); 93 } 94 95 102 103 public List getRoleVOList() throws SystemException, Bug 104 { 105 return getAllVOObjects(RoleImpl.class, "roleName"); 106 } 107 108 public List getRoleVOList(Database db) throws SystemException, Bug 109 { 110 return getAllVOObjects(RoleImpl.class, "roleName", db); 111 } 112 113 public RoleVO create(RoleVO roleVO) throws ConstraintException, SystemException 114 { 115 Role role = new RoleImpl(); 116 role.setValueObject(roleVO); 117 role = (Role) createEntity(role); 118 return role.getValueObject(); 119 } 120 121 public Role create(RoleVO roleVO, Database db) throws ConstraintException, SystemException, Exception 122 { 123 Role role = new RoleImpl(); 124 role.setValueObject(roleVO); 125 role = (Role) createEntity(role, db); 126 return role; 127 } 128 129 public void delete(RoleVO roleVO) throws ConstraintException, SystemException 130 { 131 deleteEntity(RoleImpl.class, roleVO.getRoleName()); 132 } 133 134 public void delete(RoleVO roleVO, Database db) throws ConstraintException, SystemException, Exception 135 { 136 deleteEntity(RoleImpl.class, roleVO.getRoleName(), db); 137 } 138 139 public void delete(String roleName) throws ConstraintException, SystemException 140 { 141 deleteEntity(RoleImpl.class, roleName); 142 } 143 144 public void delete(String roleName, Database db) throws ConstraintException, SystemException, Exception 145 { 146 deleteEntity(RoleImpl.class, roleName, db); 147 } 148 149 public List getRoleSystemUserVOList(String userName, Database db) throws SystemException, Bug 151 { 152 Collection systemUsers = null; 153 List systemUsersVO = new ArrayList (); 154 Role role = null; 155 156 try 157 { 158 role = getRoleWithName(userName, db); 159 systemUsers = role.getSystemUsers(); 160 161 Iterator it = systemUsers.iterator(); 162 while (it.hasNext()) 163 { 164 SystemUser systemUser = (SystemUser) it.next(); 165 systemUsersVO.add(systemUser.getValueObject()); 166 } 167 } 168 catch( Exception e) 169 { 170 throw new SystemException("An error occurred when we tried to fetch a list of users in this role. Reason:" + e.getMessage(), e); 171 } 172 173 return systemUsersVO; 174 } 175 176 public List getRoleSystemUserVOList(String roleName) throws SystemException, Bug 177 { 178 List systemUsersVO = null; 179 Database db = CastorDatabaseService.getDatabase(); 180 try 181 { 182 beginTransaction(db); 183 184 systemUsersVO = getRoleSystemUserVOList(roleName, db); 185 186 commitTransaction(db); 187 } 188 catch ( Exception e ) 189 { 190 rollbackTransaction(db); 191 throw new SystemException("An error occurred when we tried to fetch a list of users in this role. Reason:" + e.getMessage(), e); 192 } 193 return systemUsersVO; 194 } 195 196 public RoleVO update(RoleVO roleVO) throws ConstraintException, SystemException 197 { 198 return (RoleVO) updateEntity(RoleImpl.class, (BaseEntityVO) roleVO); 199 } 200 201 202 public RoleVO update(RoleVO roleVO, String [] systemUsers) throws ConstraintException, SystemException 203 { 204 Database db = CastorDatabaseService.getDatabase(); 205 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 206 207 Role role = null; 208 209 beginTransaction(db); 210 211 try 212 { 213 role = update(roleVO, systemUsers, db); 215 216 ceb.throwIfNotEmpty(); 218 219 commitTransaction(db); 220 } 221 catch(ConstraintException ce) 222 { 223 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 224 rollbackTransaction(db); 225 throw ce; 226 } 227 catch(Exception e) 228 { 229 logger.error("An error occurred so we should not complete the transaction:" + e, e); 230 rollbackTransaction(db); 231 throw new SystemException(e.getMessage()); 232 } 233 234 return role.getValueObject(); 235 } 236 237 public Role update(RoleVO roleVO, String [] systemUsers, Database db) throws ConstraintException, SystemException 238 { 239 Role role = getRoleWithName(roleVO.getRoleName(), db); 240 role.getSystemUsers().clear(); 241 242 if(systemUsers != null) 243 { 244 for (int i=0; i < systemUsers.length; i++) 245 { 246 SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(systemUsers[i], db); 247 248 role.getSystemUsers().add(systemUser); 249 systemUser.getRoles().add(role); 250 } 251 } 252 253 role.setValueObject(roleVO); 254 255 return role; 256 } 257 258 259 266 267 public List getRoleVOList(String userName) throws SystemException, Bug 268 { 269 List roleVOList = null; 270 271 Database db = CastorDatabaseService.getDatabase(); 272 try 273 { 274 beginTransaction(db); 275 276 SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(userName, db); 277 roleVOList = toVOList(systemUser.getRoles()); 278 279 commitTransaction(db); 280 } 281 catch(Exception e) 282 { 283 rollbackTransaction(db); 284 throw new SystemException("An error occurred when we tried to fetch a list of users in this role. Reason:" + e.getMessage(), e); 285 } 286 287 return roleVOList; 288 } 289 290 297 298 public Collection getRoleList(String userName, Database db) throws SystemException, Bug 299 { 300 Collection roleList = null; 301 302 SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(userName, db); 303 roleList = systemUser.getRoles(); 304 305 return roleList; 306 } 307 308 312 313 public BaseEntityVO getNewVO() 314 { 315 return new RoleVO(); 316 } 317 318 } 319 | Popular Tags |