1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.log4j.Logger; 31 import org.exolab.castor.jdo.Database; 32 import org.exolab.castor.jdo.OQLQuery; 33 import org.exolab.castor.jdo.QueryResults; 34 import org.infoglue.cms.entities.content.ContentVO; 35 import org.infoglue.cms.entities.kernel.BaseEntityVO; 36 import org.infoglue.cms.entities.management.Language; 37 import org.infoglue.cms.entities.management.Repository; 38 import org.infoglue.cms.entities.management.RepositoryLanguage; 39 import org.infoglue.cms.entities.management.RepositoryVO; 40 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl; 41 import org.infoglue.cms.entities.structure.SiteNodeVO; 42 import org.infoglue.cms.exception.Bug; 43 import org.infoglue.cms.exception.ConstraintException; 44 import org.infoglue.cms.exception.SystemException; 45 import org.infoglue.cms.security.InfoGluePrincipal; 46 import org.infoglue.cms.util.ConstraintExceptionBuffer; 47 import org.infoglue.deliver.util.CacheController; 48 49 public class RepositoryController extends BaseController 50 { 51 private final static Logger logger = Logger.getLogger(RepositoryController.class.getName()); 52 53 56 57 public static RepositoryController getController() 58 { 59 return new RepositoryController(); 60 } 61 62 public RepositoryVO create(RepositoryVO vo) throws ConstraintException, SystemException 63 { 64 Repository ent = new RepositoryImpl(); 65 ent.setValueObject(vo); 66 ent = (Repository) createEntity(ent); 67 return ent.getValueObject(); 68 } 69 70 73 74 public void delete(RepositoryVO repositoryVO, String userName) throws ConstraintException, SystemException 75 { 76 Database db = CastorDatabaseService.getDatabase(); 77 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 78 79 Repository repository = null; 80 81 beginTransaction(db); 82 83 try 84 { 85 repository = getRepositoryWithId(repositoryVO.getRepositoryId(), db); 86 87 RepositoryLanguageController.getController().deleteRepositoryLanguages(repository, db); 88 89 ContentVO contentVO = ContentControllerProxy.getController().getRootContentVO(repositoryVO.getRepositoryId(), userName, false); 90 if(contentVO != null) 91 ContentController.getContentController().delete(contentVO, db); 92 93 SiteNodeVO siteNodeVO = SiteNodeController.getController().getRootSiteNodeVO(repositoryVO.getRepositoryId()); 94 if(siteNodeVO != null) 95 SiteNodeController.getController().delete(siteNodeVO, db); 96 97 deleteEntity(RepositoryImpl.class, repositoryVO.getRepositoryId(), db); 98 99 ceb.throwIfNotEmpty(); 101 102 commitTransaction(db); 103 } 104 catch(ConstraintException ce) 105 { 106 logger.warn("An error occurred so we should not completes the transaction:" + ce, ce); 107 rollbackTransaction(db); 108 throw ce; 109 } 110 catch(Exception e) 111 { 112 logger.error("An error occurred so we should not completes the transaction:" + e, e); 113 rollbackTransaction(db); 114 throw new SystemException(e.getMessage()); 115 } 116 } 117 118 119 public RepositoryVO update(RepositoryVO vo) throws ConstraintException, SystemException 120 { 121 return (RepositoryVO) updateEntity(RepositoryImpl.class, (BaseEntityVO) vo); 122 } 123 124 public RepositoryVO update(RepositoryVO repositoryVO, String [] languageValues) throws ConstraintException, SystemException 125 { 126 Database db = CastorDatabaseService.getDatabase(); 127 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 128 129 beginTransaction(db); 130 131 try 132 { 133 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryVO.getRepositoryId(), db); 134 135 RepositoryLanguageController.getController().deleteRepositoryLanguages(repository, db); 136 137 List repositoryLanguageList = new ArrayList (); 139 if(languageValues != null) 140 { 141 for (int i=0; i < languageValues.length; i++) 142 { 143 Language language = LanguageController.getController().getLanguageWithId(new Integer (languageValues[i]), db); 144 RepositoryLanguage repositoryLanguage = RepositoryLanguageController.getController().create(repositoryVO.getRepositoryId(), new Integer (languageValues[i]), new Integer (i), db); 145 repositoryLanguageList.add(repositoryLanguage); 146 language.getRepositoryLanguages().add(repositoryLanguage); 147 } 148 } 149 150 repository.setValueObject(repositoryVO); 151 repository.setRepositoryLanguages(repositoryLanguageList); 152 153 repositoryVO = repository.getValueObject(); 154 155 ceb.throwIfNotEmpty(); 157 158 commitTransaction(db); 159 } 160 catch(ConstraintException ce) 161 { 162 logger.warn("An error occurred so we should not completes the transaction:" + ce, ce); 163 rollbackTransaction(db); 164 throw ce; 165 } 166 catch(Exception e) 167 { 168 logger.error("An error occurred so we should not completes the transaction:" + e, e); 169 rollbackTransaction(db); 170 throw new SystemException(e.getMessage()); 171 } 172 173 return repositoryVO; 174 } 175 176 public Repository getRepositoryWithId(Integer id, Database db) throws SystemException, Bug 178 { 179 return (Repository) getObjectWithId(RepositoryImpl.class, id, db); 180 } 181 182 public RepositoryVO getRepositoryVOWithId(Integer repositoryId) throws ConstraintException, SystemException, Bug 183 { 184 return (RepositoryVO) getVOWithId(RepositoryImpl.class, repositoryId); 185 } 186 187 188 196 197 public RepositoryVO getRepositoryVOWithName(String name) throws SystemException, Bug 198 { 199 RepositoryVO repositoryVO = null; 200 201 Database db = CastorDatabaseService.getDatabase(); 202 203 try 204 { 205 beginTransaction(db); 206 207 Repository repository = getRepositoryWithName(name, db); 208 if(repository != null) 209 repositoryVO = repository.getValueObject(); 210 211 commitTransaction(db); 212 } 213 catch (Exception e) 214 { 215 logger.info("An error occurred so we should not complete the transaction:" + e); 216 rollbackTransaction(db); 217 throw new SystemException(e.getMessage()); 218 } 219 220 return repositoryVO; 221 } 222 223 232 233 public Repository getRepositoryWithName(String name, Database db) throws SystemException, Bug 234 { 235 Repository repository = null; 236 237 try 238 { 239 OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl f WHERE f.name = $1"); 240 oql.bind(name); 241 242 QueryResults results = oql.execute(); 243 this.logger.info("Fetching entity in read/write mode" + name); 244 245 if (results.hasMore()) 246 { 247 repository = (Repository)results.next(); 248 } 249 250 results.close(); 251 oql.close(); 252 } 253 catch(Exception e) 254 { 255 throw new SystemException("An error occurred when we tried to fetch a named repository. Reason:" + e.getMessage(), e); 256 } 257 258 return repository; 259 } 260 261 265 266 public List getRepositoryVOList() throws ConstraintException, SystemException, Bug 267 { 268 String key = "repositoryVOList"; 269 logger.info("key:" + key); 270 List cachedRepositoryVOList = (List )CacheController.getCachedObject("repositoryCache", key); 271 if(cachedRepositoryVOList != null) 272 { 273 logger.info("There was an cached authorization:" + cachedRepositoryVOList.size()); 274 return cachedRepositoryVOList; 275 } 276 277 List repositoryVOList = getAllVOObjects(RepositoryImpl.class, "repositoryId"); 278 279 CacheController.cacheObject("repositoryCache", key, repositoryVOList); 280 281 return repositoryVOList; 282 } 283 284 285 289 290 public List getAuthorizedRepositoryVOList(InfoGluePrincipal infoGluePrincipal, boolean isBindingDialog) throws ConstraintException, SystemException, Bug 291 { 292 List accessableRepositories = new ArrayList (); 293 294 List allRepositories = this.getRepositoryVOList(); 295 Iterator i = allRepositories.iterator(); 296 while(i.hasNext()) 297 { 298 RepositoryVO repositoryVO = (RepositoryVO)i.next(); 299 if(getIsAccessApproved(repositoryVO.getRepositoryId(), infoGluePrincipal, isBindingDialog)) 300 { 301 accessableRepositories.add(repositoryVO); 302 } 303 } 304 305 return accessableRepositories; 306 } 307 308 309 310 311 314 315 public RepositoryVO getFirstRepositoryVO() throws SystemException, Bug 316 { 317 Database db = CastorDatabaseService.getDatabase(); 318 RepositoryVO repositoryVO = null; 319 320 try 321 { 322 beginTransaction(db); 323 324 OQLQuery oql = db.getOQLQuery("SELECT r FROM org.infoglue.cms.entities.management.impl.simple.RepositoryImpl r ORDER BY r.repositoryId"); 325 QueryResults results = oql.execute(); 326 this.logger.info("Fetching entity in read/write mode"); 327 328 if (results.hasMore()) 329 { 330 Repository repository = (Repository)results.next(); 331 repositoryVO = repository.getValueObject(); 332 } 333 334 results.close(); 335 oql.close(); 336 337 commitTransaction(db); 338 } 339 catch ( Exception e) 340 { 341 throw new SystemException("An error occurred when we tried to fetch a list of roles in the repository. Reason:" + e.getMessage(), e); 342 } 343 return repositoryVO; 344 } 345 346 347 348 351 public void delete(Integer repositoryId, Database db) throws SystemException, Bug 352 { 353 try 354 { 355 db.remove(getRepositoryWithId(repositoryId, db)); 356 } 357 catch(Exception e) 358 { 359 throw new SystemException("An error occurred when we tried to delete Repository in the database. Reason: " + e.getMessage(), e); 360 } 361 } 362 363 364 367 368 public boolean getIsAccessApproved(Integer repositoryId, InfoGluePrincipal infoGluePrincipal, boolean isBindingDialog) throws SystemException 369 { 370 logger.info("getIsAccessApproved for " + repositoryId + " AND " + infoGluePrincipal + " AND " + isBindingDialog); 371 boolean hasAccess = false; 372 373 Database db = CastorDatabaseService.getDatabase(); 374 375 beginTransaction(db); 376 377 try 378 { 379 if(isBindingDialog) 380 hasAccess = (AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Repository.Read", repositoryId.toString()) || AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Repository.ReadForBinding", repositoryId.toString())); 381 else 382 hasAccess = AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Repository.Read", repositoryId.toString()); 383 384 commitTransaction(db); 385 } 386 catch(Exception e) 387 { 388 logger.error("An error occurred so we should not complete the transaction:" + e, e); 389 rollbackTransaction(db); 390 throw new SystemException(e.getMessage()); 391 } 392 393 return hasAccess; 394 } 395 396 397 401 402 public BaseEntityVO getNewVO() 403 { 404 return new RepositoryVO(); 405 } 406 407 } 408 409 | Popular Tags |