1 23 24 package org.infoglue.deliver.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.controllers.kernel.impl.simple.SiteNodeVersionController; 37 import org.infoglue.cms.entities.content.ContentVO; 38 import org.infoglue.cms.entities.content.ContentVersionVO; 39 import org.infoglue.cms.entities.management.Language; 40 import org.infoglue.cms.entities.management.LanguageVO; 41 import org.infoglue.cms.entities.management.Repository; 42 import org.infoglue.cms.entities.management.RepositoryLanguage; 43 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl; 44 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl; 45 import org.infoglue.cms.entities.structure.SiteNode; 46 import org.infoglue.cms.entities.structure.SiteNodeVersion; 47 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl; 48 import org.infoglue.cms.exception.SystemException; 49 import org.infoglue.cms.security.InfoGluePrincipal; 50 import org.infoglue.cms.util.CmsPropertyHandler; 51 import org.infoglue.deliver.applications.databeans.DeliveryContext; 52 import org.infoglue.deliver.util.CacheController; 53 54 55 public class LanguageDeliveryController extends BaseDeliveryController 56 { 57 private final static Logger logger = Logger.getLogger(LanguageDeliveryController.class.getName()); 58 59 62 63 private LanguageDeliveryController() 64 { 65 } 66 67 70 71 public static LanguageDeliveryController getLanguageDeliveryController() 72 { 73 return new LanguageDeliveryController(); 74 } 75 76 77 80 81 public LanguageVO getLanguageVO(Database db, Integer languageId) throws SystemException, Exception 82 { 83 if(languageId == null || languageId.intValue() == 0) 84 return null; 85 86 String key = "" + languageId; 87 logger.info("key:" + key); 88 LanguageVO languageVO = (LanguageVO)CacheController.getCachedObject("languageCache", key); 89 if(languageVO != null) 90 { 91 logger.info("There was an cached languageVO:" + languageVO); 92 } 93 else 94 { 95 Language language = (Language)getObjectWithId(LanguageImpl.class, languageId, db); 96 97 if(language != null) 98 languageVO = language.getValueObject(); 99 100 CacheController.cacheObject("languageCache", key, languageVO); 101 } 102 103 return languageVO; 104 } 105 106 114 115 public List getAvailableLanguagesForRepository(Database db, Integer repositoryId) throws SystemException, Exception 116 { 117 String key = "" + repositoryId + "_allLanguages"; 118 logger.info("key:" + key); 119 List list = (List )CacheController.getCachedObject("languageCache", key); 120 if(list != null) 121 { 122 logger.info("There was an cached list:" + list); 123 } 124 else 125 { 126 list = new ArrayList (); 127 128 Repository repository = (Repository) getObjectWithId(RepositoryImpl.class, repositoryId, db); 129 if (repository != null) 130 { 131 for (Iterator i = repository.getRepositoryLanguages().iterator();i.hasNext();) 132 { 133 RepositoryLanguage repositoryLanguage = (RepositoryLanguage) i.next(); 134 Language language = repositoryLanguage.getLanguage(); 135 if (language != null) 136 list.add(language.getValueObject()); 137 } 138 } 139 140 if(list.size() > 0) 141 CacheController.cacheObject("languageCache", key, list); 142 } 143 144 return list; 145 } 146 147 150 151 public List getAvailableLanguages(Database db, Integer siteNodeId) throws SystemException, Exception 152 { 153 logger.info("getAvailableLanguages for " + siteNodeId + " start."); 154 155 List languageVOList = new ArrayList (); 156 157 logger.info("siteNodeId:" + siteNodeId); 158 159 SiteNode siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db); 160 161 if(siteNode != null) 162 { 163 Repository repository = siteNode.getRepository(); 164 if(repository != null) 165 { 166 logger.info("repository:" + repository.getName()); 167 168 Collection repositoryLanguages = repository.getRepositoryLanguages(); 169 logger.info("repositoryLanguages:" + repositoryLanguages.size()); 170 Iterator repositoryLanguagesIterator = repositoryLanguages.iterator(); 171 while(repositoryLanguagesIterator.hasNext()) 172 { 173 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)repositoryLanguagesIterator.next(); 174 Language language = repositoryLanguage.getLanguage(); 175 if(language != null) 176 { 177 logger.info("Adding " + language.getName() + " to the list of available languages"); 178 languageVOList.add(language.getValueObject()); 179 } 180 } 181 } 182 } 183 184 logger.info("getAvailableLanguages for " + siteNodeId + " end."); 185 186 return languageVOList; 187 } 188 189 190 194 195 public LanguageVO getMasterLanguage(Database db, String repositoryName) throws SystemException, Exception 196 { 197 Language language = null; 198 199 OQLQuery oql = db.getOQLQuery( "SELECT l FROM org.infoglue.cms.entities.management.impl.simple.LanguageImpl l WHERE l.repositoryLanguages.repository.name = $1 ORDER BY l.repositoryLanguages.sortOrder, l.languageId"); 200 oql.bind(repositoryName); 201 202 QueryResults results = oql.execute(Database.ReadOnly); 203 204 if (results.hasMore()) 205 { 206 language = (Language)results.next(); 207 } 208 209 results.close(); 210 oql.close(); 211 212 return (language == null) ? null : language.getValueObject(); 213 } 214 215 216 220 221 public LanguageVO getMasterLanguageForRepository(Database db, Integer repositoryId) throws SystemException, Exception 222 { 223 String languageKey = "" + repositoryId; 224 logger.info("languageKey in getMasterLanguageForRepository:" + languageKey); 225 LanguageVO languageVO = (LanguageVO)CacheController.getCachedObject("masterLanguageCache", languageKey); 226 if(languageVO != null) 227 { 228 logger.info("There was an cached master language:" + languageVO.getName()); 229 } 230 else 231 { 232 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"); 233 oql.bind(repositoryId); 234 235 QueryResults results = oql.execute(Database.ReadOnly); 236 237 if (results.hasMore()) 238 { 239 Language language = (Language)results.next(); 240 languageVO = language.getValueObject(); 241 } 242 243 results.close(); 244 oql.close(); 245 246 CacheController.cacheObject("masterLanguageCache", languageKey, languageVO); 247 } 248 249 return languageVO; 250 } 251 252 256 257 public LanguageVO getMasterLanguageForRepository(Integer repositoryId, Database db) throws SystemException, Exception 258 { 259 LanguageVO languageVO = null; 260 261 String languageKey = "" + repositoryId; 262 logger.info("languageKey in getMasterLanguageForRepository:" + languageKey); 263 languageVO = (LanguageVO)CacheController.getCachedObject("masterLanguageCache", languageKey); 264 if(languageVO != null) 265 { 266 logger.info("There was an cached master language:" + languageVO.getName()); 267 } 268 else 269 { 270 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"); 271 oql.bind(repositoryId); 272 273 QueryResults results = oql.execute(Database.ReadOnly); 274 275 if (results.hasMore()) 276 { 277 Language language = (Language)results.next(); 278 languageVO = language.getValueObject(); 279 } 280 281 results.close(); 282 oql.close(); 283 284 CacheController.cacheObject("masterLanguageCache", languageKey, languageVO); 285 } 286 287 return languageVO; 288 } 289 290 291 295 296 public LanguageVO getMasterLanguageForSiteNode(Database db, Integer siteNodeId) throws SystemException, Exception 297 { 298 String languageKey = "siteNodeId_" + siteNodeId; 299 logger.info("languageKey in getMasterLanguageForSiteNode:" + languageKey); 300 LanguageVO languageVO = (LanguageVO)CacheController.getCachedObject("masterLanguageCache", languageKey); 301 if(languageVO != null) 302 { 303 logger.info("There was an cached master language:" + languageVO.getName()); 304 } 305 else 306 { 307 SiteNode siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db); 308 Integer repositoryId = siteNode.getRepository().getRepositoryId(); 309 310 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"); 311 oql.bind(repositoryId); 312 313 QueryResults results = oql.execute(Database.ReadOnly); 314 315 if (results.hasMore()) 316 { 317 Language language = (Language)results.next(); 318 languageVO = language.getValueObject(); 319 } 320 321 results.close(); 322 oql.close(); 323 324 CacheController.cacheObject("masterLanguageCache", languageKey, languageVO); 325 } 326 327 return languageVO; 328 } 329 330 331 334 335 public Locale getLocaleWithId(Database db, Integer languageId) 336 { 337 String key = "" + languageId; 338 logger.info("key:" + key); 339 Locale locale = (Locale )CacheController.getCachedObject("localeCache", key); 340 if(locale != null) 341 { 342 logger.info("There was an cached locale:" + locale); 343 } 344 else 345 { 346 locale = Locale.getDefault(); 347 348 if (languageId != null) 349 { 350 try 351 { 352 LanguageVO languageVO = getLanguageVO(db, languageId); 353 locale = new Locale (languageVO.getLanguageCode()); 354 } 355 catch (Exception e) 356 { 357 logger.error("An error occurred in getLocaleWithId: getting locale with languageid:" + languageId + "," + e, e); 358 } 359 } 360 361 CacheController.cacheObject("localeCache", key, locale); 362 } 363 364 return locale; 365 } 366 367 370 371 public Locale getLocaleWithCode(String languageCode) 372 { 373 String key = "" + languageCode; 374 logger.info("key:" + key); 375 Locale locale = (Locale )CacheController.getCachedObject("localeCache", key); 376 if(locale != null) 377 { 378 logger.info("There was an cached locale:" + locale); 379 } 380 else 381 { 382 locale = Locale.getDefault(); 383 384 if (languageCode != null) 385 { 386 try 387 { 388 locale = new Locale (languageCode); 389 } 390 catch (Exception e) 391 { 392 logger.error("An error occurred in getLocaleWithCode: getting locale with languageCode:" + languageCode + "," + e, e); 393 } 394 } 395 396 CacheController.cacheObject("localeCache", key, locale); 397 } 398 399 return locale; 400 } 401 402 403 406 407 public LanguageVO getLanguageWithCode(Database db, String languageCode) throws SystemException, Exception 408 { 409 String key = "" + languageCode; 410 logger.info("key:" + key); 411 LanguageVO languageVO = (LanguageVO)CacheController.getCachedObject("languageCache", key); 412 if(languageVO != null) 413 { 414 logger.info("There was an cached languageVO:" + languageVO); 415 } 416 else 417 { 418 Language language = null; 419 420 OQLQuery oql = db.getOQLQuery( "SELECT l FROM org.infoglue.cms.entities.management.impl.simple.LanguageImpl l WHERE l.languageCode = $1"); 421 oql.bind(languageCode); 422 423 QueryResults results = oql.execute(Database.ReadOnly); 424 425 if (results.hasMore()) 426 { 427 language = (Language)results.next(); 428 languageVO = language.getValueObject(); 429 } 430 431 results.close(); 432 oql.close(); 433 434 CacheController.cacheObject("languageCache", key, languageVO); 435 } 436 437 return languageVO; 438 } 439 440 441 444 445 public LanguageVO getLanguageIfRepositorySupportsIt(Database db, String languageCodes, Integer siteNodeId) throws SystemException, Exception 446 { 447 if (languageCodes == null) return null; 448 int index = Integer.MAX_VALUE; 449 int currentIndex = 0; 450 logger.info("Coming in with languageCodes:" + languageCodes); 451 452 Language language = null; 453 454 SiteNode siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db); 455 Repository repository = siteNode.getRepository(); 456 if(repository != null) 457 { 458 Collection languages = repository.getRepositoryLanguages(); 459 Iterator languageIterator = languages.iterator(); 460 while(languageIterator.hasNext()) 461 { 462 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)languageIterator.next(); 463 Language currentLanguage = repositoryLanguage.getLanguage(); 464 logger.info("CurrentLanguageCode:" + currentLanguage.getLanguageCode()); 465 currentIndex = languageCodes.toLowerCase().indexOf(currentLanguage.getLanguageCode().toLowerCase()); 466 if( currentIndex > -1 && currentIndex < index) 467 { 468 index = currentIndex; 469 logger.info("Found the language in the list of supported languages for this site: " + currentLanguage.getName() + " - priority:" + index); 470 language = currentLanguage; 471 if (index==0) break; } 473 } 474 } 475 476 return (language == null) ? null : language.getValueObject(); 477 } 478 479 482 483 public LanguageVO getLanguageIfSiteNodeSupportsIt(Database db, String languageCodes, Integer siteNodeId, InfoGluePrincipal principal) throws SystemException, Exception 484 { 485 if (languageCodes == null) 486 return null; 487 488 String key = "" + siteNodeId + "_" + languageCodes; 489 LanguageVO languageVO = (LanguageVO)CacheController.getCachedObject("siteNodeLanguageCache", key); 490 if(languageVO != null) 491 return languageVO; 492 493 int index = Integer.MAX_VALUE; 494 int currentIndex = 0; 495 logger.info("Coming in with languageCodes:" + languageCodes); 496 497 Language language = null; 498 499 SiteNode siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db); 500 501 Repository repository = siteNode.getRepository(); 502 if(repository != null) 503 { 504 Collection languages = repository.getRepositoryLanguages(); 505 Iterator languageIterator = languages.iterator(); 506 while(languageIterator.hasNext()) 507 { 508 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)languageIterator.next(); 509 Language currentLanguage = repositoryLanguage.getLanguage(); 510 logger.info("CurrentLanguageCode:" + currentLanguage.getLanguageCode()); 511 512 NodeDeliveryController ndc = NodeDeliveryController.getNodeDeliveryController(siteNodeId, currentLanguage.getId(), new Integer (-1)); 513 514 currentIndex = languageCodes.toLowerCase().indexOf(currentLanguage.getLanguageCode().toLowerCase()); 515 if(getIsValidLanguage(db, ndc, siteNode, currentLanguage.getId()) && currentIndex > -1 && currentIndex < index) 516 { 517 index = currentIndex; 518 logger.info("Found the language in the list of supported languages for this site: " + currentLanguage.getName() + " - priority:" + index); 519 520 DeliveryContext deliveryContext = DeliveryContext.getDeliveryContext(); 521 ContentVO contentVO = ndc.getBoundContent(db, principal, siteNodeId, currentLanguage.getId(), false, BasicTemplateController.META_INFO_BINDING_NAME, deliveryContext); 522 if(contentVO != null) 523 { 524 ContentVersionVO contentVersionVO = ContentDeliveryController.getContentDeliveryController().getContentVersionVO(db, siteNodeId, contentVO.getId(), currentLanguage.getId(), false, deliveryContext, principal); 525 if(contentVersionVO != null) 526 { 527 language = currentLanguage; 528 logger.info("Language now: " + language.getName()); 529 } 530 } 531 532 if (index==0) break; } 534 } 535 } 536 537 if(language != null) 538 CacheController.cacheObject("siteNodeLanguageCache", key, language.getValueObject()); 539 540 logger.info("Returning language: " + language); 541 542 return (language == null) ? null : language.getValueObject(); 543 } 544 545 546 549 550 public LanguageVO getLanguageIfSiteNodeSupportsIt(Database db, Integer languageId, Integer siteNodeId) throws SystemException, Exception 551 { 552 if (languageId == null) 553 return null; 554 555 String key = "" + siteNodeId + "_" + languageId; 556 LanguageVO languageVO = (LanguageVO)CacheController.getCachedObject("siteNodeLanguageCache", key); 557 if(languageVO != null) 558 return languageVO; 559 560 NodeDeliveryController ndc = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, new Integer (-1)); 561 562 logger.info("Coming in with languageId:" + languageId); 563 564 Language language = null; 565 566 SiteNode siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db); 567 568 if(!getIsValidLanguage(db, ndc, siteNode, languageId)) 569 return null; 570 571 Repository repository = siteNode.getRepository(); 572 if(repository != null) 573 { 574 Collection languages = repository.getRepositoryLanguages(); 575 576 Iterator languageIterator = languages.iterator(); 577 while(languageIterator.hasNext()) 578 { 579 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)languageIterator.next(); 580 Language currentLanguage = repositoryLanguage.getLanguage(); 581 logger.info("CurrentLanguage:" + currentLanguage.getId()); 582 if(currentLanguage.getId().intValue() == languageId.intValue()) 583 { 584 logger.info("Found the language in the list of supported languages for this site: " + currentLanguage.getName()); 585 if(getIsValidLanguage(db, ndc, siteNode, currentLanguage.getId())) 586 { 587 language = currentLanguage; 588 break; 589 } 590 604 } 605 } 606 } 607 608 if(language != null) 609 CacheController.cacheObject("siteNodeLanguageCache", key, language.getValueObject()); 610 611 logger.info("Returning language: " + language); 612 613 return (language == null) ? null : language.getValueObject(); 614 } 615 616 619 620 public List getLanguagesForSiteNode(Database db, Integer siteNodeId, InfoGluePrincipal principal) throws SystemException, Exception 621 { 622 String key = "" + siteNodeId; 623 List languageVOList = (List )CacheController.getCachedObject("siteNodeLanguageCache", key); 624 if(languageVOList != null) 625 return languageVOList; 626 627 logger.info("Coming in with siteNodeId:" + siteNodeId); 628 629 languageVOList = new ArrayList (); 630 631 SiteNode siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db); 632 633 Repository repository = siteNode.getRepository(); 634 if(repository != null) 635 { 636 Collection languages = repository.getRepositoryLanguages(); 637 Iterator languageIterator = languages.iterator(); 638 while(languageIterator.hasNext()) 639 { 640 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)languageIterator.next(); 641 Language currentLanguage = repositoryLanguage.getLanguage(); 642 logger.info("CurrentLanguageCode:" + currentLanguage.getLanguageCode()); 643 644 NodeDeliveryController ndc = NodeDeliveryController.getNodeDeliveryController(siteNodeId, currentLanguage.getId(), new Integer (-1)); 645 646 if(getIsValidLanguage(db, ndc, siteNode, currentLanguage.getId())) 647 { 648 logger.info("Found the language in the list of supported languages for this site: " + currentLanguage.getName()); 649 languageVOList.add(currentLanguage.getValueObject()); 650 } 651 } 652 } 653 654 if(languageVOList != null) 655 CacheController.cacheObject("siteNodeLanguageCache", key, languageVOList); 656 657 logger.info("Returning languageVOList: " + languageVOList.size()); 658 659 return languageVOList; 660 } 661 662 663 public boolean getIsValidLanguage(Database db, NodeDeliveryController ndc, SiteNode siteNode, Integer languageId) throws Exception 664 { 665 boolean isValidLanguage = true; 666 667 SiteNodeVersion siteNodeVersion = ndc.getActiveSiteNodeVersion(siteNode.getId(), db); 668 Integer disabledLanguagesSiteNodeVersionId = ndc.getDisabledLanguagesSiteNodeVersionId(db, siteNode.getId()); 669 logger.info("disabledLanguagesSiteNodeVersionId:" + disabledLanguagesSiteNodeVersionId); 670 671 if(disabledLanguagesSiteNodeVersionId != null) 672 { 673 SiteNodeVersion disabledLanguagesSiteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(disabledLanguagesSiteNodeVersionId, db); 674 675 String disabledLanguagesString = CmsPropertyHandler.getPropertySetValue("siteNode_" + disabledLanguagesSiteNodeVersion.getValueObject().getSiteNodeId() + "_disabledLanguages"); 676 logger.info("disabledLanguagesString:" + disabledLanguagesString); 677 678 if(disabledLanguagesString != null && !disabledLanguagesString.equalsIgnoreCase("")) 679 { 680 String [] disabledLanguagesStringArray = disabledLanguagesString.split(","); 681 for(int i=0; i<disabledLanguagesStringArray.length; i++) 682 { 683 logger.info("languageId.intValue():" + languageId.intValue()); 684 logger.info("disabledLanguagesStringArray:" + disabledLanguagesStringArray); 685 if(languageId.intValue() == new Integer (disabledLanguagesStringArray[i]).intValue()) 686 { 687 isValidLanguage = false; 688 logger.info("isValidLanguage:" + isValidLanguage); 689 break; 690 } 691 } 692 } 693 694 } 695 logger.info("languageId:" + languageId + " was valid:" + isValidLanguage); 696 697 return isValidLanguage; 698 } 699 700 701 702 } | Popular Tags |