1 40 41 package org.jahia.data.containers; 42 43 import java.io.Serializable ; 44 import java.util.ArrayList ; 45 import java.util.Enumeration ; 46 import java.util.HashMap ; 47 import java.util.Hashtable ; 48 import java.util.Map ; 49 import java.util.Properties ; 50 import java.util.Vector ; 51 52 import org.jahia.content.PropertiesInterface; 53 import org.jahia.data.fields.JahiaContentFieldFacade; 54 import org.jahia.data.fields.JahiaField; 55 import org.jahia.data.fields.JahiaFieldDefinition; 56 import org.jahia.data.fields.LoadFlags; 57 import org.jahia.exceptions.JahiaException; 58 import org.jahia.params.ParamBean; 59 import org.jahia.registries.JahiaContainerDefinitionsRegistry; 60 import org.jahia.registries.ServicesRegistry; 61 import org.jahia.services.acl.ACLNotFoundException; 62 import org.jahia.services.acl.ACLResourceInterface; 63 import org.jahia.services.acl.JahiaBaseACL; 64 import org.jahia.services.containers.ContainerFactoryProxy; 65 import org.jahia.services.containers.ContentContainer; 66 import org.jahia.services.pages.ContentPage; 67 import org.jahia.services.usermanager.JahiaUser; 68 import org.jahia.services.version.EntryLoadRequest; 69 import org.jahia.services.version.JahiaSaveVersion; 70 import org.jahia.utils.LanguageCodeConverters; 71 72 public class JahiaContainer implements Serializable , Cloneable , ACLResourceInterface, PropertiesInterface { 73 74 private static org.apache.log4j.Logger logger = 75 org.apache.log4j.Logger.getLogger(JahiaContainer.class); 76 77 private int ID; 78 private int jahiaID; 79 private int pageID; 80 private int listID; 81 private int rank; 82 private int ctndefid; 83 private int aclID; 84 private int versionID; 85 private int workflowState; 86 private String languageCode = ""; 87 88 private boolean isContainerListsLoaded = false; 89 private boolean isFieldsLoaded = false; 90 private boolean propertiesLoaded = false; 91 92 private Properties ctnProperties = null; 93 94 97 private Vector fields = new Vector (); 98 99 102 private Vector containerLists = new Vector (); 103 104 private Map children = new HashMap (); 105 106 private ContainerFactoryProxy cFactoryProxy; 107 108 public JahiaContainer() {} 111 112 117 public JahiaContainer (int anID, 118 int aJahiaID, 119 int aPageID, 120 int aListID, 121 int aRank, 122 int anAclID, 123 int aCtndefid, 124 int aVersionID, 125 int aWorkflowState) { 126 this.ID = anID; 127 this.jahiaID = aJahiaID; 128 this.pageID = aPageID; 129 this.listID = aListID; 130 this.rank = aRank; 131 this.aclID = anAclID; 132 this.ctndefid = aCtndefid; 133 this.versionID = aVersionID; 134 this.workflowState = aWorkflowState; 135 136 } 138 143 public void setFactoryProxy(ContainerFactoryProxy cntFactoryProxy){ 144 this.cFactoryProxy = cntFactoryProxy; 145 } 146 147 152 public final int getID () { 153 return ID; 154 } 155 156 public final int getJahiaID () { 157 return jahiaID; 158 } 159 160 public final int getSiteID () { 161 return jahiaID; 162 } 164 public final int getPageID () { 165 return pageID; 166 } 167 168 public final int getListID () { 169 return listID; 170 } 171 172 public final int getRank () { 173 return rank; 174 } 175 176 public final int getctndefid () { 177 return ctndefid; 178 } 179 180 public final int getAclID () { 181 return aclID; 182 } 183 184 public final int getVersionID () { 185 return versionID; 186 } 187 188 public final int getWorkflowState () { 189 return workflowState; 190 } 191 192 public final String getLanguageCode () { 193 return languageCode; 194 } 195 196 public final JahiaBaseACL getACL () { 197 JahiaBaseACL acl = null; 198 try { 199 acl = new JahiaBaseACL(getAclID()); 200 } catch (Throwable t) { 201 t.printStackTrace(); 202 } 203 return acl; 204 } 205 206 public Enumeration getFields () { 207 checkProxy(ContainerFactoryProxy.LOAD_FIELDS); 208 return fields.elements(); 209 } 210 211 public int getNbFields () { 212 checkProxy(ContainerFactoryProxy.LOAD_FIELDS); 213 return fields.size(); 214 } 215 216 public Enumeration getContainerLists () { 217 checkProxy(ContainerFactoryProxy.LOAD_SUBCONTAINER_LISTS); 218 return containerLists.elements(); 219 } 220 221 public void setID (int anID) { 222 this.ID = anID; 223 } 224 225 public void setctndefid (int aCtndefid) { 226 this.ctndefid = aCtndefid; 227 } 228 229 public void setAclID (int anAclID) { 230 this.aclID = anAclID; 231 } 232 233 public void setListID (int aListID) { 234 this.listID = aListID; 235 } 236 237 public void setRank (int aRank) { 238 this.rank = aRank; 239 } 240 241 public void setLanguageCode (String aLanguageCode) { 242 this.languageCode = aLanguageCode; 243 } 244 245 public boolean isContainerListsLoaded(){ 246 return this.isContainerListsLoaded; 247 } 248 249 public boolean isFieldsLoaded(){ 250 return this.isFieldsLoaded; 251 } 252 253 256 public void setContainerListsLoaded ( boolean val ){ 257 this.isContainerListsLoaded = val; 258 } 259 260 264 public void setFieldsLoaded ( boolean val ){ 265 this.isFieldsLoaded = val; 266 } 267 268 271 public void clearFields(){ 272 if ( this.fields != null && this.fields.size()>0 ){ 273 Enumeration enumeration = this.fields.elements(); 274 JahiaField f = null; 275 while ( enumeration.hasMoreElements() ){ 276 f = (JahiaField)enumeration.nextElement(); 277 try { 278 this.children.remove(f.getDefinition().getName()); 279 } catch ( JahiaException je ){ 280 logger.debug("Exception",je); 281 } 282 } 283 } 284 this.fields = new Vector (); 285 } 286 287 290 public void clearContainerLists(){ 291 if ( this.containerLists !=null && this.containerLists.size()>0 ){ 292 Enumeration enumeration = this.containerLists.elements(); 293 JahiaContainerList cList = null; 294 while ( enumeration.hasMoreElements() ){ 295 cList = (JahiaContainerList)enumeration.nextElement(); 296 try { 297 this.children.remove(cList.getDefinition().getName()); 298 } catch ( JahiaException je ){ 299 logger.debug("Exception",je); 300 } 301 } 302 } 303 this.containerLists = new Vector (); 304 } 305 306 308 312 public Object clone () { 313 JahiaContainer container = new JahiaContainer(ID, jahiaID, pageID, 314 listID, rank, aclID, ctndefid, versionID, workflowState); 315 container.setLanguageCode(languageCode); 316 return container; 317 } 318 319 324 public JahiaContainerDefinition getDefinition () 325 throws JahiaException { 326 JahiaContainerDefinition theDef = JahiaContainerDefinitionsRegistry. 327 getInstance( 328 ).getDefinition(ctndefid); 329 if (theDef != null) { 330 return theDef; 331 } else { 332 String msg = "JahiaContainer definition " + ctndefid + 333 " not found in definition registry !"; 334 throw new JahiaException("Synchronisation error in database", 335 msg, JahiaException.DATABASE_ERROR, 336 JahiaException.CRITICAL_SEVERITY); 337 } 338 } 340 348 public String getFieldValue( String fieldName ) 349 throws JahiaException 350 { 351 return getFieldValue(fieldName,false,null); 352 } 353 354 364 public String getFieldValue( String fieldName , 365 boolean allowDiffVersionHighlight, 366 ParamBean jParams) 367 throws JahiaException 368 { 369 checkProxy(ContainerFactoryProxy.LOAD_FIELDS); 370 JahiaField theField = null; 371 for (int i=0; i < fields.size(); i++) { 372 JahiaField aField = (JahiaField) fields.elementAt(i); 373 if (aField.getDefinition().getName().equals(fieldName)) { 374 theField = aField; 375 break; 376 } 377 } 378 if ( theField != null ){ 379 if ( !allowDiffVersionHighlight ){ 380 return theField.getValue(); 381 } else { 382 return theField.getHighLightDiffValue(jParams); 383 } 384 } 385 return null; 386 } 387 388 397 public String getFieldValue( String fieldName , 398 String defaultValue , 399 boolean allowDiffVersionHighlight , 400 ParamBean jParams ) 401 throws JahiaException 402 { 403 String value = 404 getFieldValue(fieldName,allowDiffVersionHighlight,jParams); 405 if ( value == null ){ 406 return defaultValue; 407 } 408 return value; 409 } 410 411 420 public String getFieldValue( String fieldName , String defaultValue ) 421 throws JahiaException 422 { 423 String value = getFieldValue(fieldName); 424 if ( value == null ){ 425 return defaultValue; 426 } 427 return value; 428 } 429 430 438 public Object getFieldObject (String fieldName) 439 throws JahiaException { 440 checkProxy(ContainerFactoryProxy.LOAD_FIELDS); 441 JahiaField theField = (JahiaField) children.get(fieldName); 442 if (theField != null) { 443 return theField.getObject(); 444 } 445 return null; 446 } 447 448 458 public JahiaField getField (String fieldName) 459 throws JahiaException { 460 checkProxy(ContainerFactoryProxy.LOAD_FIELDS); 461 JahiaField theField = (JahiaField) children.get(fieldName); 462 return theField; 463 } 465 public JahiaField getFieldByName(String fieldName) 466 throws JahiaException { 467 return getField(fieldName); 468 } 469 470 480 public JahiaField getField (int fieldID) 481 throws JahiaException { 482 checkProxy(ContainerFactoryProxy.LOAD_FIELDS); 483 JahiaField theField = null; 484 for (int i = 0; i < fields.size(); i++) { 485 JahiaField aField = (JahiaField) fields.elementAt(i); 486 if (aField.getID() == fieldID) { 487 theField = aField; 488 break; 489 } 490 } 491 if (theField == null) { 492 String msg = "Field " + fieldID + " cannot be found in " + 493 getDefinition().getName() + "! fields.size=" + fields.size(); 494 JahiaException je = new JahiaException(msg, 495 "JahiaContainer : " + msg, 496 JahiaException.TEMPLATE_ERROR, JahiaException.CRITICAL_SEVERITY); 497 logger.error(msg, je); 498 } 499 return theField; 500 } 502 507 public synchronized void setField (JahiaField theField) 508 throws JahiaException { 509 checkProxy(ContainerFactoryProxy.LOAD_FIELDS); 510 for (int i = 0; i < fields.size(); i++) { 511 JahiaField aField = (JahiaField) fields.elementAt(i); 512 if (aField.getID() == theField.getID()) { 513 fields.setElementAt(theField, i); 514 this.children.put(theField.getDefinition().getName(),theField); 515 } 516 } 517 } 519 525 public synchronized void addField (JahiaField theField) { 526 if (theField != null) { 527 fields.add(theField); 528 try { 529 children.put(theField.getDefinition().getName(), theField); 530 } catch (JahiaException je) { 531 logger.error("Error while inserting field " + theField.getID() + 532 " into container children map", je); 533 } catch (NullPointerException npe) { 534 logger.error("Error while inserting field " + theField.getID() + 535 " into container children map", npe); 536 } 537 } 538 } 540 546 public JahiaContainerList getContainerList (String listName) 547 throws JahiaException { 548 checkProxy(ContainerFactoryProxy.LOAD_SUBCONTAINER_LISTS); 549 logger.debug("looking for child container list: " + listName); 550 JahiaContainerList theList = (JahiaContainerList) children.get(listName); 551 if (theList == null) { 552 JahiaContainerDefinition theDef = JahiaContainerDefinitionsRegistry. 553 getInstance().getDefinition(this. 554 jahiaID, listName); 555 theList = new JahiaContainerList(0, this.getID(), this.getPageID(), 556 theDef.getID(), 0); 557 } 558 return theList; 559 } 561 567 public synchronized void addContainerList (JahiaContainerList theList) 568 throws JahiaException { 569 checkProxy(ContainerFactoryProxy.LOAD_SUBCONTAINER_LISTS); 570 if (theList != null) { 571 containerLists.add(theList); 572 try { 573 children.put(theList.getDefinition().getName(), theList); 574 } catch (JahiaException je) { 575 logger.error("Error while inserting container list " + 576 theList.getID() + 577 " into container children map", je); 578 } catch (NullPointerException npe) { 579 logger.error("Error while inserting container list " + 580 theList.getID() + 581 " into container children map", npe); 582 } 583 } 584 } 586 595 public synchronized void fieldsStructureCheck (ParamBean jParams) 596 throws JahiaException { 597 598 checkProxy(ContainerFactoryProxy.LOAD_FIELDS); 599 if (this.languageCode == null || this.languageCode.trim().equals("")) { 600 throw new JahiaException( 601 "Not a valid Language Code ( empty Str or null )", 602 "Not a valid Language Code ( empty Str or null )", 603 JahiaException.DATA_ERROR, JahiaException.CRITICAL_SEVERITY); 604 } 605 Vector fieldOrder = new Vector (); 607 Vector orderedFields = new Vector (); 608 Hashtable fieldDefs = new Hashtable (); 609 610 ContentPage sourcePage = ContentPage.getPage(this.getPageID()); 611 if (sourcePage != null && this.fields != null) { 612 int pageDefID = sourcePage.getPageTemplateID(jParams); 613 Enumeration structure = this.getDefinition().getStructure( 614 "", pageDefID, JahiaContainerStructure.JAHIA_FIELD); 615 while (structure.hasMoreElements()) { 616 JahiaContainerStructure theStruct = 617 (JahiaContainerStructure) structure.nextElement(); 618 JahiaFieldDefinition theDef = 619 (JahiaFieldDefinition) theStruct.getObjectDef(); 620 fieldOrder.add(theDef.getName()); 621 orderedFields.add(null); fieldDefs.put(theDef.getName(), theDef); 623 } 624 625 JahiaField field = null; 626 for (int i = 0; i < fields.size(); i++) { 627 field = (JahiaField) fields.get(i); 628 String fieldDefName = field.getDefinition().getName(); 629 int fieldPos = fieldOrder.indexOf(fieldDefName); 630 if (fieldPos != -1) { 631 logger.debug("JahiaContainer.fieldsStructureCheck : Field " + 632 fieldDefName + 633 " has pos : " + fieldPos); 634 orderedFields.setElementAt(field, fieldPos); 635 children.put(fieldDefName, field); 636 fieldDefs.remove(fieldDefName); 637 } else { 638 } 641 } 642 643 if ( this.getID()>0 ){ 647 Vector fieldIds = ServicesRegistry.getInstance(). 648 getJahiaContainersService().getFieldIDsInContainer(this. 649 getID()); 650 ArrayList locales = new ArrayList (); 651 locales.add(LanguageCodeConverters.languageCodeToLocale(this.getLanguageCode())); 652 EntryLoadRequest loadRequest = 653 new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 654 0,locales); 655 for ( int i=0 ; i<fieldIds.size(); i++ ){ 656 Integer fieldId = (Integer )fieldIds.get(i); 657 boolean found = false; 658 for (int j = 0; j < fields.size(); j++) { 659 JahiaField f = (JahiaField)fields.get(j); 660 if ( f.getID() == fieldId.intValue() ){ 661 found = true; 662 } 663 } 664 if ( !found ){ 665 JahiaContentFieldFacade fFacade = 666 new JahiaContentFieldFacade(fieldId.intValue(), 667 LoadFlags.ALL, 668 jParams, 669 locales, 670 true); 671 field = fFacade.getField(loadRequest,true); 672 String fieldDefName = field.getDefinition().getName(); 673 int fieldPos = fieldOrder.indexOf(fieldDefName); 674 if (fieldPos != -1) { 675 logger.debug("JahiaContainer.fieldsStructureCheck : Field " + 676 fieldDefName + 677 " has pos : " + fieldPos); 678 orderedFields.setElementAt(field, fieldPos); 679 children.put(fieldDefName, field); 680 fieldDefs.remove(fieldDefName); 681 } 682 } 683 } 684 } 685 686 Enumeration enumeration = fieldDefs.elements(); 689 JahiaFieldDefinition fieldDef = null; 690 while (enumeration.hasMoreElements()) { 691 fieldDef = (JahiaFieldDefinition) enumeration.nextElement(); 692 693 logger.debug("JahiaContainer.fieldsStructureCheck : Field " + 694 fieldDef.getName() + 695 " is missing, we have to create a new one "); 696 int fieldType = fieldDef.getType(pageDefID); 697 698 int connectType = 0; 699 int localRank = 0; 700 int localAclID = 0; 701 String fieldValue = fieldDef.getDefaultValue(pageDefID); 702 JahiaSaveVersion saveVersion = ServicesRegistry.getInstance(). 703 getJahiaVersionService(). 704 getSiteSaveVersion(this. 705 getJahiaID()); 706 field = ServicesRegistry.getInstance(). 707 getJahiaFieldService(). 708 createJahiaField(0, this.getJahiaID(), this.getPageID(), 709 this.getID(), fieldDef.getID(), 710 fieldType, 711 connectType, fieldValue, localRank, localAclID, 712 saveVersion.getVersionID(), 713 saveVersion.getWorkflowState(), 714 this.languageCode); 715 if (field != null) { 716 ServicesRegistry.getInstance().getJahiaFieldService(). 718 saveField(field, this.getAclID(), jParams); 719 720 int fieldPos = fieldOrder.indexOf(fieldDef.getName()); 721 logger.debug("JahiaContainer.fieldsStructureCheck : Field " + 722 fieldDef.getName() + 723 " has pos : " + fieldPos); 724 orderedFields.setElementAt(field, fieldPos); 725 children.put(fieldDef.getName(), field); 726 } 727 } 728 this.fields = orderedFields; 729 } 730 } 731 732 743 public final boolean checkReadAccess (JahiaUser user) { 744 return checkAccess(user, JahiaBaseACL.READ_RIGHTS); 745 } 746 747 757 public final boolean checkWriteAccess (JahiaUser user) { 758 return checkAccess(user, JahiaBaseACL.WRITE_RIGHTS); 759 } 760 761 771 public final boolean checkAdminAccess (JahiaUser user) { 772 return checkAccess(user, JahiaBaseACL.ADMIN_RIGHTS); 773 } 774 775 private boolean checkAccess (JahiaUser user, int permission) { 777 if (user == null) { 778 return false; 779 } 780 781 boolean result = false; 782 try { 783 JahiaBaseACL containerACL = new JahiaBaseACL(aclID); 785 786 result = containerACL.getPermission(user, permission); 788 789 containerACL = null; 791 } catch (ACLNotFoundException ex) { 792 } catch (JahiaException ex) { 795 } 797 798 return result; 804 } 805 806 811 public ContentContainer getContentContainer() { 812 ContentContainer contentContainer = null; 813 try { 814 contentContainer = ContentContainer.getContainer(ID); 815 } catch (JahiaException je) { 816 logger.debug(je); 817 } 818 return contentContainer; 819 } 820 821 public void setProperties(Properties newProperties) { 822 ctnProperties = newProperties; 823 try { 824 ServicesRegistry.getInstance().getJahiaContainersService(). 825 setContainerProperties(ID, getJahiaID(), ctnProperties); 826 propertiesLoaded = true; 827 } catch (JahiaException je) { 828 logger.error("Error while setting new properties for container " + ID, je); 829 } 830 } 831 832 public Properties getProperties() { 833 if (propertiesLoaded) { 834 return ctnProperties; 835 } else { 836 try { 837 ctnProperties = ServicesRegistry.getInstance(). 838 getJahiaContainersService(). 839 getContainerProperties(ID); 840 } catch (JahiaException je) { 841 logger.error("Error while loading container " + ID + " properties", je); 842 } 843 propertiesLoaded = true; 844 return ctnProperties; 845 } 846 } 847 848 public String getProperty(String propertyName) { 849 if (getProperties() != null) { 850 return getProperties().getProperty(propertyName); 851 } else { 852 return null; 853 } 854 } 855 856 public void setProperty(String propertyName, String propertyValue) { 857 if (getProperties() == null) { 858 ctnProperties = new Properties (); 859 propertiesLoaded = true; 860 } 861 try { 862 getProperties().setProperty(propertyName, propertyValue); 863 ServicesRegistry.getInstance().getJahiaContainersService(). 864 setContainerProperty(ID, getJahiaID(), propertyName, propertyValue); 865 } catch (JahiaException e) { 866 logger.error("Error while setting new property for container " + ID, e); 867 } 868 } 869 870 private void checkProxy(int loadFlag){ 871 if ( this.cFactoryProxy != null ){ 872 this.cFactoryProxy.load(this,loadFlag); 873 } 874 } 875 876 } | Popular Tags |