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.Group; 35 import org.infoglue.cms.entities.management.GroupVO; 36 import org.infoglue.cms.entities.management.SystemUser; 37 import org.infoglue.cms.entities.management.impl.simple.GroupImpl; 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 import org.infoglue.deliver.util.CacheController; 43 44 51 public class GroupController extends BaseController 52 { 53 private final static Logger logger = Logger.getLogger(GroupController.class.getName()); 54 55 58 59 public static GroupController getController() 60 { 61 return new GroupController(); 62 } 63 64 public Group getGroupWithId(Integer groupId, Database db) throws SystemException, Bug 65 { 66 return (Group) getObjectWithId(GroupImpl.class, groupId, db); 67 } 68 69 public Group getGroupWithName(String groupName, Database db) throws SystemException, Bug 70 { 71 return (Group)getObjectWithId(GroupImpl.class, groupName, db); 72 } 73 74 80 81 public GroupVO getGroupVOWithId(Integer groupId) throws SystemException, Bug 82 { 83 return (GroupVO) getVOWithId(GroupImpl.class, groupId); 84 } 85 86 public GroupVO getGroupVOWithId(String groupName) throws SystemException, Bug 87 { 88 return (GroupVO) getVOWithId(GroupImpl.class, groupName); 89 } 90 91 public GroupVO getGroupVOWithId(String groupName, Database db) throws SystemException, Bug 92 { 93 return (GroupVO) getVOWithId(GroupImpl.class, groupName, db); 94 } 95 96 103 104 public List getGroupVOList() throws SystemException, Bug 105 { 106 return getAllVOObjects(GroupImpl.class, "groupName"); 107 } 108 109 public List getGroupVOList(Database db) throws SystemException, Bug 110 { 111 String cacheKey = "allGroupVO"; 112 logger.info("cacheKey in getGroupVOList:" + cacheKey); 113 List groupVOList = (List )CacheController.getCachedObject("groupVOListCache", cacheKey); 114 if(groupVOList != null) 115 { 116 logger.info("There was an cached list of GroupVO:" + groupVOList.size()); 117 } 118 else 119 { 120 groupVOList = getAllVOObjects(GroupImpl.class, "groupName", db); 121 if(groupVOList != null) 122 CacheController.cacheObject("groupVOListCache", cacheKey, groupVOList); 123 } 124 125 return groupVOList; 126 } 127 128 public GroupVO create(GroupVO groupVO) throws ConstraintException, SystemException 129 { 130 Group group = new GroupImpl(); 131 group.setValueObject(groupVO); 132 group = (Group) createEntity(group); 133 return group.getValueObject(); 134 } 135 136 public Group create(GroupVO groupVO, Database db) throws ConstraintException, SystemException, Exception 137 { 138 Group group = new GroupImpl(); 139 group.setValueObject(groupVO); 140 group = (Group) createEntity(group, db); 141 return group; 142 } 143 144 public void delete(GroupVO groupVO) throws ConstraintException, SystemException 145 { 146 deleteEntity(GroupImpl.class, groupVO.getGroupName()); 147 } 148 149 public void delete(String groupName) throws ConstraintException, SystemException 150 { 151 deleteEntity(GroupImpl.class, groupName); 152 } 153 154 public void delete(String groupName, Database db) throws ConstraintException, SystemException, Exception 155 { 156 deleteEntity(GroupImpl.class, groupName, db); 157 } 158 159 public List getGroupSystemUserVOList(String userName, Database db) throws SystemException, Bug 161 { 162 Collection systemUsers = null; 163 List systemUsersVO = new ArrayList (); 164 Group group = null; 165 166 try 167 { 168 group = getGroupWithName(userName, db); 169 systemUsers = group.getSystemUsers(); 170 171 Iterator it = systemUsers.iterator(); 172 while (it.hasNext()) 173 { 174 SystemUser systemUser = (SystemUser) it.next(); 175 systemUsersVO.add(systemUser.getValueObject()); 176 } 177 } 178 catch( Exception e) 179 { 180 throw new SystemException("An error occurred when we tried to fetch a list of users in this group. Reason:" + e.getMessage(), e); 181 } 182 183 return systemUsersVO; 184 } 185 186 public List getGroupSystemUserVOList(String groupName) throws SystemException, Bug 187 { 188 List systemUsersVO = null; 189 Database db = CastorDatabaseService.getDatabase(); 190 try 191 { 192 beginTransaction(db); 193 194 systemUsersVO = getGroupSystemUserVOList(groupName, db); 195 196 commitTransaction(db); 197 } 198 catch ( Exception e ) 199 { 200 rollbackTransaction(db); 201 throw new SystemException("An error occurred when we tried to fetch a list of users in this group. Reason:" + e.getMessage(), e); 202 } 203 return systemUsersVO; 204 } 205 206 public GroupVO update(GroupVO groupVO) throws ConstraintException, SystemException 207 { 208 return (GroupVO) updateEntity(GroupImpl.class, (BaseEntityVO) groupVO); 209 } 210 211 public GroupVO update(GroupVO groupVO, Database db) throws ConstraintException, SystemException 212 { 213 return (GroupVO) updateEntity(GroupImpl.class, (BaseEntityVO) groupVO, db); 214 } 215 216 217 public GroupVO update(GroupVO groupVO, String [] systemUsers) throws ConstraintException, SystemException 218 { 219 Database db = CastorDatabaseService.getDatabase(); 220 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 221 222 Group group = null; 223 224 beginTransaction(db); 225 226 try 227 { 228 230 group = update(groupVO, systemUsers, db); 231 232 ceb.throwIfNotEmpty(); 234 235 commitTransaction(db); 236 } 237 catch(ConstraintException ce) 238 { 239 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 240 rollbackTransaction(db); 241 throw ce; 242 } 243 catch(Exception e) 244 { 245 logger.error("An error occurred so we should not complete the transaction:" + e, e); 246 rollbackTransaction(db); 247 throw new SystemException(e.getMessage()); 248 } 249 250 return group.getValueObject(); 251 } 252 253 public Group update(GroupVO groupVO, String [] systemUsers, Database db) throws ConstraintException, SystemException 254 { 255 Group group = getGroupWithName(groupVO.getGroupName(), db); 256 group.getSystemUsers().clear(); 257 258 if(systemUsers != null) 259 { 260 for (int i=0; i < systemUsers.length; i++) 261 { 262 SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(systemUsers[i], db); 263 264 group.getSystemUsers().add(systemUser); 265 systemUser.getGroups().add(group); 266 } 267 } 268 269 group.setValueObject(groupVO); 270 271 return group; 272 } 273 274 275 282 283 public List getGroupVOList(String userName) throws SystemException, Bug 284 { 285 List groupVOList = null; 286 287 Database db = CastorDatabaseService.getDatabase(); 288 try 289 { 290 beginTransaction(db); 291 292 SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(userName, db); 293 groupVOList = toVOList(systemUser.getGroups()); 294 295 commitTransaction(db); 296 } 297 catch(Exception e) 298 { 299 rollbackTransaction(db); 300 throw new SystemException("An error occurred when we tried to fetch a list of users in this group. Reason:" + e.getMessage(), e); 301 } 302 303 return groupVOList; 304 } 305 306 313 314 public Collection getGroupList(String userName, Database db) throws SystemException, Bug 315 { 316 Collection groupList = null; 317 318 SystemUser systemUser = SystemUserController.getController().getSystemUserWithName(userName, db); 319 groupList = systemUser.getGroups(); 320 321 return groupList; 322 } 323 324 328 329 public BaseEntityVO getNewVO() 330 { 331 return new GroupVO(); 332 } 333 334 } 335 | Popular Tags |