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 import java.util.Locale ; 31 32 import org.apache.log4j.Logger; 33 import org.exolab.castor.jdo.Database; 34 import org.exolab.castor.jdo.OQLQuery; 35 import org.exolab.castor.jdo.QueryResults; 36 import org.infoglue.cms.entities.kernel.BaseEntityVO; 37 import org.infoglue.cms.entities.management.Language; 38 import org.infoglue.cms.entities.management.LanguageVO; 39 import org.infoglue.cms.entities.management.Repository; 40 import org.infoglue.cms.entities.management.RepositoryLanguage; 41 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl; 42 import org.infoglue.cms.entities.management.impl.simple.RepositoryLanguageImpl; 43 import org.infoglue.cms.exception.Bug; 44 import org.infoglue.cms.exception.ConstraintException; 45 import org.infoglue.cms.exception.SystemException; 46 import org.infoglue.cms.util.ConstraintExceptionBuffer; 47 import org.infoglue.deliver.util.CacheController; 48 49 54 55 public class LanguageController extends BaseController 56 { 57 private final static Logger logger = Logger.getLogger(LanguageController.class.getName()); 58 59 62 63 public static LanguageController getController() 64 { 65 return new LanguageController(); 66 } 67 68 71 77 78 81 82 public LanguageVO getLanguageVOWithId(Integer languageId) throws SystemException, Exception 83 { 84 String key = "" + languageId; 85 logger.info("key:" + key); 86 LanguageVO languageVO = (LanguageVO)CacheController.getCachedObject("languageCache", key); 87 if(languageVO != null) 88 { 89 logger.info("There was an cached languageVO:" + languageVO); 90 } 91 else 92 { 93 languageVO = (LanguageVO)getVOWithId(LanguageImpl.class, languageId); 94 95 CacheController.cacheObject("languageCache", key, languageVO); 96 } 97 98 return languageVO; 99 } 100 101 102 105 106 public LanguageVO getLanguageVOWithId(Integer languageId, Database db) throws SystemException, Bug 107 { 108 return (LanguageVO)getVOWithId(LanguageImpl.class, languageId, db); 109 } 110 111 114 115 public Locale getLocaleWithId(Integer languageId) 116 { 117 Locale locale = Locale.getDefault(); 118 119 if (languageId != null) 120 { 121 try 122 { 123 LanguageVO languageVO = getLanguageVOWithId(languageId); 124 locale = new Locale (languageVO.getLanguageCode()); 125 } 126 catch (Exception e) 127 { 128 logger.error("An error occurred in getLocaleWithId: getting locale with languageid:" + languageId + "," + e, e); 129 } 130 } 131 132 return locale; 133 } 134 135 143 144 public LanguageVO getLanguageVOWithName(String name) throws SystemException, Bug 145 { 146 LanguageVO languageVO = null; 147 148 Database db = CastorDatabaseService.getDatabase(); 149 150 try 151 { 152 beginTransaction(db); 153 154 Language language = getLanguageWithName(name, db); 155 if(language != null) 156 languageVO = language.getValueObject(); 157 158 commitTransaction(db); 159 } 160 catch (Exception e) 161 { 162 logger.info("An error occurred so we should not complete the transaction:" + e); 163 rollbackTransaction(db); 164 throw new SystemException(e.getMessage()); 165 } 166 167 return languageVO; 168 } 169 170 179 180 public Language getLanguageWithName(String name, Database db) throws SystemException, Bug 181 { 182 Language language = null; 183 184 try 185 { 186 OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.LanguageImpl f WHERE f.name = $1"); 187 oql.bind(name); 188 189 QueryResults results = oql.execute(); 190 this.logger.info("Fetching entity in read/write mode" + name); 191 192 if (results.hasMore()) 193 { 194 language = (Language)results.next(); 195 } 196 197 results.close(); 198 oql.close(); 199 } 200 catch(Exception e) 201 { 202 throw new SystemException("An error occurred when we tried to fetch a named language. Reason:" + e.getMessage(), e); 203 } 204 205 return language; 206 } 207 208 216 217 public LanguageVO getLanguageVOWithCode(String code, Database db) throws SystemException, Bug 218 { 219 LanguageVO languageVO = null; 220 221 Language language = getLanguageWithCode(code, db); 222 if(language != null) 223 languageVO = language.getValueObject(); 224 225 return languageVO; 226 } 227 228 236 237 public LanguageVO getLanguageVOWithCode(String code) throws SystemException, Bug 238 { 239 LanguageVO languageVO = null; 240 241 Database db = CastorDatabaseService.getDatabase(); 242 243 try 244 { 245 beginTransaction(db); 246 247 Language language = getLanguageWithCode(code, db); 248 if(language != null) 249 languageVO = language.getValueObject(); 250 251 commitTransaction(db); 252 } 253 catch (Exception e) 254 { 255 logger.info("An error occurred so we should not complete the transaction:" + e); 256 rollbackTransaction(db); 257 throw new SystemException(e.getMessage()); 258 } 259 260 return languageVO; 261 } 262 263 272 273 public Language getLanguageWithCode(String code, Database db) throws SystemException, Bug 274 { 275 Language language = null; 276 277 try 278 { 279 OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.LanguageImpl f WHERE f.languageCode = $1"); 280 oql.bind(code); 281 282 QueryResults results = oql.execute(); 283 this.logger.info("Fetching entity in read/write mode" + code); 284 285 if (results.hasMore()) 286 { 287 language = (Language)results.next(); 288 } 289 290 results.close(); 291 oql.close(); 292 } 293 catch(Exception e) 294 { 295 throw new SystemException("An error occurred when we tried to fetch a named language. Reason:" + e.getMessage(), e); 296 } 297 298 return language; 299 } 300 301 public LanguageVO create(LanguageVO languageVO) throws ConstraintException, SystemException 302 { 303 Language ent = new LanguageImpl(); 304 ent.setValueObject(languageVO); 305 ent = (Language) createEntity(ent); 306 return ent.getValueObject(); 307 } 308 309 public LanguageVO create(Database db, LanguageVO languageVO) throws ConstraintException, SystemException, Exception 310 { 311 Language ent = new LanguageImpl(); 312 ent.setValueObject(languageVO); 313 db.create(ent); 314 return ent.getValueObject(); 315 } 316 317 320 321 public void delete(LanguageVO languageVO) throws ConstraintException, SystemException 322 { 323 Database db = CastorDatabaseService.getDatabase(); 324 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 325 326 Language language = null; 327 328 try 329 { 330 beginTransaction(db); 331 332 delete(db, languageVO); 333 334 commitTransaction(db); 335 } 336 catch(Exception e) 337 { 338 logger.error("An error occurred so we should not complete the transaction:" + e, e); 339 rollbackTransaction(db); 340 throw new SystemException(e.getMessage()); 341 } 342 } 343 344 347 348 public void delete(Database db, LanguageVO languageVO) throws ConstraintException, SystemException 349 { 350 Language language = getLanguageWithId(languageVO.getId(), db); 351 RepositoryLanguageController.getController().deleteAllRepositoryLanguageWithLanguage(language, db); 352 353 deleteEntity(LanguageImpl.class, languageVO.getLanguageId(), db); 354 } 355 356 357 public Language getLanguageWithId(Integer languageId, Database db) throws SystemException, Bug 358 { 359 return (Language) getObjectWithId(LanguageImpl.class, languageId, db); 360 } 361 362 363 public Language getLanguageWithId(Integer languageId) throws ConstraintException, SystemException, Bug 364 { 365 Database db = CastorDatabaseService.getDatabase(); 366 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 367 368 Language language = null; 369 370 try 371 { 372 beginTransaction(db); 373 374 language = getLanguageWithId(languageId, db); 375 376 ceb.throwIfNotEmpty(); 378 379 commitTransaction(db); 380 } 381 catch(Exception e) 382 { 383 logger.error("An error occurred so we should not complete the transaction:" + e, e); 384 rollbackTransaction(db); 385 throw new SystemException(e.getMessage()); 386 } 387 388 return language; 389 } 390 391 392 public LanguageVO getLanguageVOWithRepositoryLanguageId(Integer repositoryLanguageId) throws ConstraintException, SystemException, Bug 393 { 394 Database db = CastorDatabaseService.getDatabase(); 395 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 396 397 LanguageVO languageVO = null; 398 399 try 400 { 401 beginTransaction(db); 402 403 languageVO = getLanguageWithRepositoryLanguageId(repositoryLanguageId, db).getValueObject(); 404 405 ceb.throwIfNotEmpty(); 407 408 commitTransaction(db); 409 } 410 catch(Exception e) 411 { 412 logger.error("An error occurred so we should not complete the transaction:" + e, e); 413 rollbackTransaction(db); 414 throw new SystemException(e.getMessage()); 415 } 416 417 return languageVO; 418 } 419 420 public Language getLanguageWithRepositoryLanguageId(Integer repositoryLanguageId, Database db) throws ConstraintException, SystemException, Bug 421 { 422 RepositoryLanguage repositoryLanguage = (RepositoryLanguage) getObjectWithId(RepositoryLanguageImpl.class, repositoryLanguageId, db); 423 Language language = repositoryLanguage.getLanguage(); 424 425 return language; 426 } 427 428 429 public List getLanguageVOList(Integer repositoryId) throws ConstraintException, SystemException 430 { 431 Database db = CastorDatabaseService.getDatabase(); 432 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 433 434 List languageVOList = new ArrayList (); 435 436 beginTransaction(db); 437 438 try 439 { 440 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db); 441 Collection repositoryLanguageList = repository.getRepositoryLanguages(); 442 Iterator repositoryLanguageIterator = repositoryLanguageList.iterator(); 443 while(repositoryLanguageIterator.hasNext()) 444 { 445 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguageIterator.next(); 446 languageVOList.add(repositoryLanguage.getLanguage().getValueObject()); 447 } 448 449 ceb.throwIfNotEmpty(); 451 452 commitTransaction(db); 453 } 454 catch(ConstraintException ce) 455 { 456 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 457 rollbackTransaction(db); 458 throw ce; 459 } 460 catch(Exception e) 461 { 462 logger.error("An error occurred so we should not complete the transaction:" + e, e); 463 rollbackTransaction(db); 464 throw new SystemException(e.getMessage()); 465 } 466 467 return languageVOList; 468 } 469 470 public List getLanguageVOList(Integer repositoryId, Database db) throws ConstraintException, SystemException 471 { 472 List languageVOList = new ArrayList (); 473 474 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db); 475 Collection repositoryLanguageList = repository.getRepositoryLanguages(); 476 Iterator repositoryLanguageIterator = repositoryLanguageList.iterator(); 477 while(repositoryLanguageIterator.hasNext()) 478 { 479 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguageIterator.next(); 480 languageVOList.add(repositoryLanguage.getLanguage().getValueObject()); 481 } 482 483 return languageVOList; 484 } 485 486 487 488 public List getLanguageList(Integer repositoryId, Database db) throws ConstraintException, SystemException 489 { 490 List languageList = new ArrayList (); 491 492 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db); 493 Collection repositoryLanguageList = repository.getRepositoryLanguages(); 494 Iterator repositoryLanguageIterator = repositoryLanguageList.iterator(); 495 while(repositoryLanguageIterator.hasNext()) 496 { 497 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguageIterator.next(); 498 languageList.add(repositoryLanguage.getLanguage()); 499 } 500 501 return languageList; 502 } 503 504 public List getLanguageVOList(Database db) throws SystemException, Bug 505 { 506 return getAllVOObjects(LanguageImpl.class, "languageId", db); 507 } 508 509 public List getLanguageVOList() throws SystemException, Bug 510 { 511 return getAllVOObjects(LanguageImpl.class, "languageId"); 512 } 513 514 518 519 public LanguageVO getMasterLanguage(Integer repositoryId) throws SystemException, Exception 520 { 521 LanguageVO languageVO = null; 522 523 String languageKey = "" + repositoryId; 524 logger.info("languageKey:" + languageKey); 525 languageVO = (LanguageVO)CacheController.getCachedObject("masterLanguageCache", languageKey); 526 if(languageVO != null) 527 { 528 logger.info("There was an cached master language:" + languageVO.getName()); 529 } 530 else 531 { 532 Database db = CastorDatabaseService.getDatabase(); 533 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 534 535 Language language = null; 536 537 beginTransaction(db); 538 539 try 540 { 541 language = getMasterLanguage(db, repositoryId); 542 543 ceb.throwIfNotEmpty(); 545 546 if(language != null) 547 { 548 languageVO = language.getValueObject(); 549 CacheController.cacheObject("masterLanguageCache", languageKey, languageVO); 550 } 551 552 commitTransaction(db); 553 } 554 catch(Exception e) 555 { 556 logger.error("An error occurred so we should not complete the transaction:" + e, e); 557 rollbackTransaction(db); 558 throw new SystemException(e.getMessage()); 559 } 560 } 561 562 return languageVO; 563 } 564 565 566 570 571 public LanguageVO getMasterLanguage(Integer repositoryId, Database db) throws SystemException, Exception 572 { 573 LanguageVO languageVO = null; 574 575 String languageKey = "" + repositoryId; 576 logger.info("languageKey:" + languageKey); 577 languageVO = (LanguageVO)CacheController.getCachedObject("masterLanguageCache", languageKey); 578 if(languageVO != null) 579 { 580 logger.info("There was an cached master language:" + languageVO.getName()); 581 } 582 else 583 { 584 Language language = getMasterLanguage(db, repositoryId); 585 586 if(language != null) 587 { 588 languageVO = language.getValueObject(); 589 CacheController.cacheObject("masterLanguageCache", languageKey, languageVO); 590 } 591 } 592 593 return languageVO; 594 } 595 596 597 600 601 public Language getMasterLanguage(Database db, Integer repositoryId) throws SystemException, Exception 602 { 603 Language language = null; 604 605 OQLQuery oql = db.getOQLQuery( "SELECT l FROM org.infoglue.cms.entities.management.impl.simple.LanguageImpl l WHERE l.repositoryLanguages.repository.repositoryId = $1 ORDER BY l.repositoryLanguages.sortOrder, l.languageId"); 606 oql.bind(repositoryId); 607 608 QueryResults results = oql.execute(Database.ReadOnly); 609 610 if (results.hasMore()) 611 { 612 language = (Language)results.next(); 613 } 614 615 results.close(); 616 oql.close(); 617 618 return language; 619 } 620 621 624 625 public void deleteLanguage(Integer languageId, Database db) throws SystemException, Bug 626 { 627 try 628 { 629 db.remove(getLanguageWithId(languageId, db)); 630 } 631 catch(Exception e) 632 { 633 throw new SystemException("An error occurred when we tried to delete Language in the database. Reason: " + e.getMessage(), e); 634 } 635 } 636 637 public LanguageVO update(LanguageVO languageVO) throws ConstraintException, SystemException 638 { 639 Database db = CastorDatabaseService.getDatabase(); 640 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 641 642 Language language = null; 643 644 beginTransaction(db); 645 646 try 647 { 648 language = getLanguageWithId(languageVO.getLanguageId(), db); 650 language.setValueObject(languageVO); 651 652 ceb.throwIfNotEmpty(); 654 655 commitTransaction(db); 656 } 657 catch(ConstraintException ce) 658 { 659 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 660 rollbackTransaction(db); 661 throw ce; 662 } 663 catch(Exception e) 664 { 665 logger.error("An error occurred so we should not complete the transaction:" + e, e); 666 rollbackTransaction(db); 667 throw new SystemException(e.getMessage()); 668 } 669 670 671 return language.getValueObject(); 672 } 673 674 678 679 public BaseEntityVO getNewVO() 680 { 681 return new LanguageVO(); 682 } 683 684 } 685 686 | Popular Tags |