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.exolab.castor.jdo.OQLQuery; 34 import org.exolab.castor.jdo.QueryResults; 35 import org.infoglue.cms.entities.kernel.BaseEntityVO; 36 import org.infoglue.cms.entities.management.Language; 37 import org.infoglue.cms.entities.management.LanguageVO; 38 import org.infoglue.cms.entities.management.Repository; 39 import org.infoglue.cms.entities.management.RepositoryLanguage; 40 import org.infoglue.cms.entities.management.RepositoryLanguageVO; 41 import org.infoglue.cms.entities.management.impl.simple.RepositoryLanguageImpl; 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.util.ConstraintExceptionBuffer; 46 import org.infoglue.deliver.util.CacheController; 47 48 public class RepositoryLanguageController extends BaseController 49 { 50 private final static Logger logger = Logger.getLogger(RepositoryLanguageController.class.getName()); 51 52 55 56 public static RepositoryLanguageController getController() 57 { 58 return new RepositoryLanguageController(); 59 } 60 61 public RepositoryLanguage getRepositoryLanguageWithId(Integer id, Database db) throws SystemException, Bug 62 { 63 return (RepositoryLanguage) getObjectWithId(RepositoryLanguageImpl.class, id, db); 64 } 65 66 67 public RepositoryLanguage getRepositoryLanguageWithId(Integer repositoryLanguageId) throws ConstraintException, SystemException, Bug 68 { 69 Database db = CastorDatabaseService.getDatabase(); 70 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 71 72 RepositoryLanguage repositoryLanguage = null; 73 74 try 75 { 76 beginTransaction(db); 77 78 repositoryLanguage = getRepositoryLanguageWithId(repositoryLanguageId, db); 79 80 ceb.throwIfNotEmpty(); 82 83 commitTransaction(db); 84 } 85 catch(ConstraintException ce) 86 { 87 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 88 rollbackTransaction(db); 89 throw ce; 90 } 91 catch(Exception e) 92 { 93 logger.error("An error occurred so we should not complete the transaction:" + e, e); 94 rollbackTransaction(db); 95 throw new SystemException(e.getMessage()); 96 } 97 98 return repositoryLanguage; 99 } 100 101 102 public List getRepositoryLanguageVOList() throws SystemException, Bug 103 { 104 return getAllVOObjects(RepositoryLanguageImpl.class, "repositoryLanguageImplId"); 105 } 106 107 108 public List getRepositoryLanguageVOListWithLanguageId(Integer languageId) throws SystemException, Bug 109 { 110 Database db = CastorDatabaseService.getDatabase(); 111 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 112 113 ArrayList repositoryLanguageList = new ArrayList (); 114 115 beginTransaction(db); 116 117 try 118 { 119 OQLQuery oql = db.getOQLQuery("SELECT rl FROM org.infoglue.cms.entities.management.impl.simple.RepositoryLanguageImpl rl WHERE rl.language = $1 order by rl.sortOrder, rl.language.languageId"); 120 oql.bind(languageId); 121 122 QueryResults results = oql.execute(Database.ReadOnly); 123 124 while (results.hasMore()) 125 { 126 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)results.next(); 127 repositoryLanguageList.add(repositoryLanguage.getValueObject()); 128 } 129 130 results.close(); 131 oql.close(); 132 133 ceb.throwIfNotEmpty(); 134 commitTransaction(db); 135 } 136 catch(Exception e) 137 { 138 logger.error("An error occurred so we should not completes the transaction:" + e, e); 139 rollbackTransaction(db); 140 throw new SystemException(e.getMessage()); 141 } 142 143 return repositoryLanguageList; 144 } 145 146 147 public List getRepositoryLanguageVOListWithRepositoryId(Integer repositoryId) throws SystemException, Bug 148 { 149 List repositoryLanguageList = new ArrayList (); 150 151 162 Database db = CastorDatabaseService.getDatabase(); 163 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 164 165 beginTransaction(db); 166 167 try 168 { 169 repositoryLanguageList = getRepositoryLanguageVOListWithRepositoryId(repositoryId, db); 170 171 176 ceb.throwIfNotEmpty(); 177 commitTransaction(db); 178 } 179 catch(Exception e) 180 { 181 logger.error("An error occurred so we should not completes the transaction:" + e, e); 182 rollbackTransaction(db); 183 throw new SystemException(e.getMessage()); 184 } 185 187 return repositoryLanguageList; 188 } 189 190 191 public List getRepositoryLanguageVOListWithRepositoryId(Integer repositoryId, Database db) throws SystemException, Bug, Exception 192 { 193 ArrayList repositoryLanguageList = new ArrayList (); 194 195 OQLQuery oql = db.getOQLQuery("SELECT rl FROM org.infoglue.cms.entities.management.impl.simple.RepositoryLanguageImpl rl WHERE rl.repository = $1 ORDER BY rl.sortOrder, rl.language.languageId"); 196 oql.bind(repositoryId); 197 198 QueryResults results = oql.execute(Database.ReadOnly); 199 200 while (results.hasMore()) 201 { 202 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)results.next(); 203 repositoryLanguageList.add(repositoryLanguage); 204 } 205 206 results.close(); 207 oql.close(); 208 209 return repositoryLanguageList; 210 } 211 212 213 public List getAvailableLanguageVOListForRepositoryId(Integer repositoryId) throws ConstraintException, SystemException, Exception 214 { 215 List repositoryLanguageVOList = null; 216 217 String repositoryLanguageListKey = "" + repositoryId; 218 logger.info("repositoryLanguageListKey:" + repositoryLanguageListKey); 219 repositoryLanguageVOList = (List )CacheController.getCachedObject("repositoryLanguageListCache", repositoryLanguageListKey); 220 if(repositoryLanguageVOList != null) 221 { 222 logger.info("There was an cached list:" + repositoryLanguageVOList); 223 } 224 else 225 { 226 Database db = CastorDatabaseService.getDatabase(); 227 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 228 229 beginTransaction(db); 230 231 try 232 { 233 List availableRepositoryLanguageList = RepositoryLanguageController.getController().getRepositoryLanguageListWithRepositoryId(repositoryId, db); 234 235 repositoryLanguageVOList = new ArrayList (); 236 Iterator i = availableRepositoryLanguageList.iterator(); 237 while(i.hasNext()) 238 { 239 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)i.next(); 240 repositoryLanguageVOList.add(repositoryLanguage.getLanguage().getValueObject()); 241 } 242 243 CacheController.cacheObject("repositoryLanguageListCache", repositoryLanguageListKey, repositoryLanguageVOList); 244 245 ceb.throwIfNotEmpty(); 246 commitTransaction(db); 247 } 248 catch(Exception e) 249 { 250 logger.error("An error occurred so we should not completes the transaction:" + e, e); 251 rollbackTransaction(db); 252 throw new SystemException(e.getMessage()); 253 } 254 } 255 256 return repositoryLanguageVOList; 257 } 258 259 public List getRepositoryLanguageListWithRepositoryId(Integer repositoryId, Database db) throws SystemException, Bug, Exception 260 { 261 ArrayList repositoryLanguageList = new ArrayList (); 262 263 OQLQuery oql = db.getOQLQuery("SELECT rl FROM org.infoglue.cms.entities.management.impl.simple.RepositoryLanguageImpl rl WHERE rl.repository = $1 ORDER BY rl.sortOrder, rl.language.languageId"); 264 oql.bind(repositoryId); 265 266 QueryResults results = oql.execute(); 267 this.logger.info("Fetching entity in read/write mode"); 268 269 while (results.hasMore()) 270 { 271 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)results.next(); 272 repositoryLanguageList.add(repositoryLanguage); 273 } 274 275 results.close(); 276 oql.close(); 277 278 return repositoryLanguageList; 279 } 280 281 284 285 public void delete(RepositoryLanguageVO vo) throws ConstraintException, SystemException 286 { 287 deleteEntity(RepositoryLanguageImpl.class, vo.getRepositoryLanguageId()); 288 } 289 290 311 312 313 public void deleteRepositoryLanguages(Repository repository, Database db) throws SystemException, Bug 314 { 315 try 316 { 317 Collection repositoryLanguages = repository.getRepositoryLanguages(); 318 Iterator iterator = repositoryLanguages.iterator(); 319 while(iterator.hasNext()) 320 { 321 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)iterator.next(); 322 Language language = repositoryLanguage.getLanguage(); 323 language.getRepositoryLanguages().remove(repositoryLanguage); 324 325 db.remove(repositoryLanguage); 326 } 328 } 329 catch(Exception e) 330 { 331 throw new SystemException("An error occurred when we tried to find the matching RepositoryLanguage in the database. Reason: " + e.getMessage(), e); 332 } 333 } 334 335 356 357 360 361 public RepositoryLanguage deleteAllRepositoryLanguageWithLanguageId(Integer languageId) throws SystemException, Bug 362 { 363 try 364 { 365 List repositoryLanguages = getRepositoryLanguageVOListWithLanguageId(languageId); 366 Iterator iterator = repositoryLanguages.iterator(); 367 while(iterator.hasNext()) 368 { 369 RepositoryLanguageVO repositoryLanguage = (RepositoryLanguageVO)iterator.next(); 370 deleteEntity(RepositoryLanguageImpl.class, repositoryLanguage.getRepositoryLanguageId()); 371 } 372 } 373 catch(Exception e) 374 { 375 throw new SystemException("An error occurred when we tried to find the matching RepositoryLanguage in the database. Reason: " + e.getMessage(), e); 376 } 377 378 return null; 379 } 380 381 384 385 public RepositoryLanguage deleteAllRepositoryLanguageWithLanguage(Language language, Database db) throws SystemException, Bug 386 { 387 try 388 { 389 Collection repositoryLanguages = language.getRepositoryLanguages(); 390 Iterator iterator = repositoryLanguages.iterator(); 391 while(iterator.hasNext()) 392 { 393 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)iterator.next(); 394 Repository repository = repositoryLanguage.getRepository(); 395 repository.getRepositoryLanguages().remove(repositoryLanguage); 396 db.remove(repositoryLanguage); 397 } 398 } 399 catch(Exception e) 400 { 401 throw new SystemException("An error occurred when we tried to delete a RepositoryLanguage in the database. Reason: " + e.getMessage(), e); 402 } 403 404 return null; 405 } 406 407 408 public RepositoryLanguage create(Integer repositoryId, Integer languageId, Integer sortOrder, Database db) throws Exception 409 { 410 RepositoryLanguage repositoryLanguage = new RepositoryLanguageImpl(); 411 412 repositoryLanguage.setIsPublished(new Boolean (false)); 413 repositoryLanguage.setLanguage(LanguageController.getController().getLanguageWithId(languageId, db)); 414 repositoryLanguage.setRepository(RepositoryController.getController().getRepositoryWithId(repositoryId, db)); 415 repositoryLanguage.setSortOrder(sortOrder); 416 417 db.create(repositoryLanguage); 418 419 return repositoryLanguage; 420 } 421 422 public void publishRepositoryLanguage(RepositoryLanguageVO repositoryLanguageVO) throws ConstraintException, SystemException 423 { 424 Database db = CastorDatabaseService.getDatabase(); 425 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 426 427 RepositoryLanguage repositoryLanguage = null; 428 429 beginTransaction(db); 430 431 try 432 { 433 repositoryLanguage = getRepositoryLanguageWithId(repositoryLanguageVO.getRepositoryLanguageId(), db); 434 repositoryLanguage.setIsPublished(new Boolean (true)); 435 commitTransaction(db); 436 } 437 catch(Exception e) 438 { 439 logger.error("An error occurred so we should not complete the transaction:" + e, e); 440 rollbackTransaction(db); 441 throw new SystemException(e.getMessage()); 442 } 443 } 444 445 446 public void unpublishRepositoryLanguage(RepositoryLanguageVO repositoryLanguageVO) throws ConstraintException, SystemException 447 { 448 Database db = CastorDatabaseService.getDatabase(); 449 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 450 451 RepositoryLanguage repositoryLanguage = null; 452 453 beginTransaction(db); 454 455 try 456 { 457 repositoryLanguage = getRepositoryLanguageWithId(repositoryLanguageVO.getRepositoryLanguageId(), db); 459 repositoryLanguage.setIsPublished(new Boolean (false)); 460 commitTransaction(db); 461 } 462 catch(Exception e) 463 { 464 logger.error("An error occurred so we should not complete the transaction:" + e, e); 465 rollbackTransaction(db); 466 throw new SystemException(e.getMessage()); 467 } 468 } 469 470 471 public void moveRepositoryLanguage(RepositoryLanguageVO repositoryLanguageVO, boolean down) throws ConstraintException, SystemException 472 { 473 Database db = CastorDatabaseService.getDatabase(); 474 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 475 476 beginTransaction(db); 477 478 try 479 { 480 RepositoryLanguage originalRepositoryLanguage = this.getRepositoryLanguageWithId(repositoryLanguageVO.getRepositoryLanguageId(), db); 481 List repositoryLanguages = this.getRepositoryLanguageListWithRepositoryId(originalRepositoryLanguage.getRepository().getId(), db); 482 483 for(int i=0; i<repositoryLanguages.size(); i++) 484 { 485 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguages.get(i); 486 if(repositoryLanguage.getRepositoryLanguageId().intValue() == repositoryLanguageVO.getRepositoryLanguageId().intValue()) 487 { 488 if(down && i != repositoryLanguages.size() - 1) 489 { 490 RepositoryLanguage nextRepositoryLanguage = (RepositoryLanguage)repositoryLanguages.get(i+1); 491 Integer currentSortOrder = repositoryLanguage.getSortOrder(); 492 repositoryLanguage.setSortOrder(nextRepositoryLanguage.getSortOrder()); 493 nextRepositoryLanguage.setSortOrder(currentSortOrder); 494 } 495 else if(!down && i != 0) 496 { 497 RepositoryLanguage previousRepositoryLanguage = (RepositoryLanguage)repositoryLanguages.get(i-1); 498 Integer currentSortOrder = repositoryLanguage.getSortOrder(); 499 repositoryLanguage.setSortOrder(previousRepositoryLanguage.getSortOrder()); 500 previousRepositoryLanguage.setSortOrder(currentSortOrder); 501 } 502 } 503 } 504 505 commitTransaction(db); 506 } 507 catch(Exception e) 508 { 509 logger.error("An error occurred so we should not complete the transaction:" + e, e); 510 rollbackTransaction(db); 511 throw new SystemException(e.getMessage()); 512 } 513 } 514 515 516 public void createRepositoryLanguage(Integer repositoryId, Integer languageId, Integer sortOrder) throws ConstraintException, SystemException 517 { 518 Database db = CastorDatabaseService.getDatabase(); 519 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 520 521 RepositoryLanguage repositoryLanguage = null; 522 523 beginTransaction(db); 524 525 try 526 { 527 repositoryLanguage = create(repositoryId, languageId, sortOrder, db); 528 commitTransaction(db); 529 } 530 catch(Exception e) 531 { 532 logger.error("An error occurred so we should not complete the transaction:" + e, e); 533 rollbackTransaction(db); 534 throw new SystemException(e.getMessage()); 535 } 536 } 537 538 539 public void updateRepositoryLanguages(Integer repositoryId, String [] languageValues) throws ConstraintException, SystemException 540 { 541 Database db = CastorDatabaseService.getDatabase(); 542 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 543 544 beginTransaction(db); 545 546 try 547 { 548 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db); 549 deleteRepositoryLanguages(repository, db); 550 551 List repositoryLanguageList = new ArrayList (); 553 if(languageValues != null) 554 { 555 for (int i=0; i < languageValues.length; i++) 556 { 557 Language language = LanguageController.getController().getLanguageWithId(new Integer (languageValues[i]), db); 558 RepositoryLanguage repositoryLanguage = create(repository.getId(), new Integer (languageValues[i]), new Integer (i), db); 559 repositoryLanguageList.add(repositoryLanguage); 560 } 561 } 562 563 repository.setRepositoryLanguages(repositoryLanguageList); 566 567 ceb.throwIfNotEmpty(); 569 570 commitTransaction(db); 571 } 572 catch(ConstraintException ce) 573 { 574 logger.warn("An error occurred so we should not completes the transaction:" + ce, ce); 575 rollbackTransaction(db); 576 throw ce; 577 } 578 catch(Exception e) 579 { 580 logger.error("An error occurred so we should not completes the transaction:" + e, e); 581 rollbackTransaction(db); 582 throw new SystemException(e.getMessage()); 583 } 584 } 585 586 public List getRemainingLanguages(Integer repositoryId) throws ConstraintException, SystemException 587 { 588 Database db = CastorDatabaseService.getDatabase(); 589 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 590 591 List remainingLanguages = new ArrayList (); 592 593 beginTransaction(db); 594 595 try 596 { 597 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db); 598 Collection repositoryLanguageList = repository.getRepositoryLanguages(); 599 List languageList = LanguageController.getController().getLanguageVOList(); 600 remainingLanguages.addAll(languageList); 601 602 Iterator languageIterator = languageList.iterator(); 603 while(languageIterator.hasNext()) 604 { 605 LanguageVO languageVO = (LanguageVO)languageIterator.next(); 606 logger.info("Language:" + languageVO.getName()); 607 Iterator repositoryLanguageIterator = repositoryLanguageList.iterator(); 608 while(repositoryLanguageIterator.hasNext()) 609 { 610 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguageIterator.next(); 611 logger.info("Comparing" + languageVO.getLanguageId().intValue() + " and " + repositoryLanguage.getLanguage().getLanguageId().intValue()); 612 if(languageVO.getLanguageId().intValue() == repositoryLanguage.getLanguage().getLanguageId().intValue()) 613 { 614 remainingLanguages.remove(languageVO); 615 } 616 } 617 } 618 619 ceb.throwIfNotEmpty(); 621 622 commitTransaction(db); 623 } 624 catch(ConstraintException ce) 625 { 626 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 627 rollbackTransaction(db); 628 throw ce; 629 } 630 catch(Exception e) 631 { 632 logger.error("An error occurred so we should not complete the transaction:" + e, e); 633 e.printStackTrace(); 634 rollbackTransaction(db); 635 throw new SystemException(e.getMessage()); 636 } 637 638 return remainingLanguages; 639 } 640 641 642 646 647 public BaseEntityVO getNewVO() 648 { 649 return new RepositoryLanguageVO(); 650 } 651 652 } 653 654 655 656 657 | Popular Tags |