KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > containers > JahiaContainer


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 13-Aug-2003, Jahia Solutions Sarl: Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41 package org.jahia.data.containers;
42
43 import java.io.Serializable JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Enumeration JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Hashtable JavaDoc;
48 import java.util.Map JavaDoc;
49 import java.util.Properties JavaDoc;
50 import java.util.Vector JavaDoc;
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 JavaDoc, Cloneable JavaDoc, 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 JavaDoc languageCode = "";
87
88     private boolean isContainerListsLoaded = false;
89     private boolean isFieldsLoaded = false;
90     private boolean propertiesLoaded = false;
91
92     private Properties JavaDoc ctnProperties = null;
93
94     /**
95      * @associates JahiaField
96      */

97     private Vector JavaDoc fields = new Vector JavaDoc();
98
99     /**
100      * @associates JahiaContainerList
101      */

102     private Vector JavaDoc containerLists = new Vector JavaDoc();
103
104     private Map JavaDoc children = new HashMap JavaDoc();
105
106     private ContainerFactoryProxy cFactoryProxy;
107
108     // no-parameter constructor required for JavaBean pattern compliance so
109
// that these objects may be used with JSPs
110
public JahiaContainer() {}
111
112     /***
113      * constructor
114      * EV 24.11.2000
115      *
116      */

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     } // end constructor
137

138     /**
139      * Set the containerFactory used to control how this container's will be loaded
140      *
141      * @param containerFactory
142      */

143     public void setFactoryProxy(ContainerFactoryProxy cntFactoryProxy){
144         this.cFactoryProxy = cntFactoryProxy;
145     }
146
147     /***
148      * accessor methods
149      * EV 24.11.2000
150      *
151      */

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     } //FIXME_MULTISITE Hollis jahiaID or siteID ?
163

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 JavaDoc 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 JavaDoc t) {
201             t.printStackTrace();
202         }
203         return acl;
204     }
205
206     public Enumeration JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
254      * @param val
255      */

256     public void setContainerListsLoaded ( boolean val ){
257         this.isContainerListsLoaded = val;
258     }
259
260     /**
261      *
262      * @param val
263      */

264     public void setFieldsLoaded ( boolean val ){
265         this.isFieldsLoaded = val;
266     }
267
268     /**
269      * Empty the list of fields
270      */

271     public void clearFields(){
272         if ( this.fields != null && this.fields.size()>0 ){
273             Enumeration JavaDoc 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 JavaDoc();
285     }
286
287     /**
288      * Empty the list of sub container lists
289      */

290     public void clearContainerLists(){
291         if ( this.containerLists !=null && this.containerLists.size()>0 ){
292             Enumeration JavaDoc 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 JavaDoc();
304     }
305
306     // end accessor methods
307

308     //-------------------------------------------------------------------------
309
/**
310      * Clone
311      */

312     public Object JavaDoc 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     //-------------------------------------------------------------------------
320
/***
321      * getDefinition
322      *
323      */

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 JavaDoc 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     } // end getDefinition
339

340     //-------------------------------------------------------------------------
341
/**
342      * get a field value
343      *
344      * @param fieldName the field name
345      * @exception JahiaException
346      * throws a critical jahia exception if field not found
347      */

348     public String JavaDoc getFieldValue( String JavaDoc fieldName )
349     throws JahiaException
350     {
351         return getFieldValue(fieldName,false,null);
352     }
353
354     //-------------------------------------------------------------------------
355
/**
356      * get a field value
357      *
358      * @param fieldName
359      * @param allowDiffVersionHighlight
360      * @return field value
361      * @exception JahiaException
362      * throws a critical jahia exception if field not found
363      */

364     public String JavaDoc getFieldValue( String JavaDoc 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     //-------------------------------------------------------------------------
389
/**
390      * get a field value
391      *
392      * @param fieldName the field name
393      * @param defaultValue
394      * @exception JahiaException
395      * throws a critical jahia exception if field not found
396      */

397     public String JavaDoc getFieldValue( String JavaDoc fieldName ,
398                                  String JavaDoc defaultValue ,
399                                  boolean allowDiffVersionHighlight ,
400                                  ParamBean jParams )
401     throws JahiaException
402     {
403         String JavaDoc value =
404                 getFieldValue(fieldName,allowDiffVersionHighlight,jParams);
405         if ( value == null ){
406             return defaultValue;
407         }
408         return value;
409     }
410
411     //-------------------------------------------------------------------------
412
/**
413      * get a field value
414      *
415      * @param fieldName the field name
416      * @param defaultValue
417      * @exception JahiaException
418      * throws a critical jahia exception if field not found
419      */

420     public String JavaDoc getFieldValue( String JavaDoc fieldName , String JavaDoc defaultValue )
421     throws JahiaException
422     {
423         String JavaDoc value = getFieldValue(fieldName);
424         if ( value == null ){
425             return defaultValue;
426         }
427         return value;
428     }
429
430     //-------------------------------------------------------------------------
431
/**
432      * get a field value
433      *
434      * @param fieldName the field name
435      * @exception JahiaException
436      * throws a critical jahia exception if field not found
437      */

438     public Object JavaDoc getFieldObject (String JavaDoc 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     //-------------------------------------------------------------------------
449
/***
450      * get a field by its fieldname
451      *
452      * @param fieldName the field name
453      *
454      * @exception JahiaException
455      * throws a critical jahia exception if field not found
456      *
457      */

458     public JahiaField getField (String JavaDoc fieldName)
459         throws JahiaException {
460         checkProxy(ContainerFactoryProxy.LOAD_FIELDS);
461         JahiaField theField = (JahiaField) children.get(fieldName);
462         return theField;
463     } // end getField
464

465     public JahiaField getFieldByName(String JavaDoc fieldName)
466         throws JahiaException {
467         return getField(fieldName);
468     }
469
470     //-------------------------------------------------------------------------
471
/***
472      * get a field by its id
473      *
474      * @param fieldID the field ID
475      *
476      * @exception JahiaException
477      * throws a critical jahia exception if field not found
478      *
479      */

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 JavaDoc 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     } // end getField
501

502     //-------------------------------------------------------------------------
503
/***
504      * setField
505      *
506      */

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     } // end addField
518

519     //-------------------------------------------------------------------------
520
/***
521      * addField
522      * EV 27.12.2000
523      *
524      */

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 JavaDoc npe) {
534                 logger.error("Error while inserting field " + theField.getID() +
535                              " into container children map", npe);
536             }
537         }
538     } // end addField
539

540     //-------------------------------------------------------------------------
541
/***
542      * getContainerList
543      * EV 27.12.2000
544      *
545      */

546     public JahiaContainerList getContainerList (String JavaDoc 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     } // end getContainerList
560

561     //-------------------------------------------------------------------------
562
/***
563      * addContainerList
564      * EV 27.12.20007
565      *
566      */

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 JavaDoc npe) {
579                 logger.error("Error while inserting container list " +
580                              theList.getID() +
581                              " into container children map", npe);
582             }
583         }
584     } // end addList
585

586     //-------------------------------------------------------------------------
587
/***
588      * Order the fields in the exact order in which they are declared in template.
589      * Create new declared fields for old containers.
590      *
591      * NK 04.06.2002
592      *
593      * @param jParams reference to the parameter bean
594      */

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         // let's order fields as they appear in template declaration !
606
Vector JavaDoc fieldOrder = new Vector JavaDoc();
607         Vector JavaDoc orderedFields = new Vector JavaDoc();
608         Hashtable JavaDoc fieldDefs = new Hashtable JavaDoc();
609
610         ContentPage sourcePage = ContentPage.getPage(this.getPageID());
611         if (sourcePage != null && this.fields != null) {
612             int pageDefID = sourcePage.getPageTemplateID(jParams);
613             Enumeration JavaDoc 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); //fake element
622
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 JavaDoc 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                     // seems that we encountered a field for which the definition has benn removed from the container declaration
639
// so we ignore this field
640
}
641             }
642
643             // We don't want to create new field if they already exist but in another
644
// language than this container current language code.
645
// so there is some extra check
646
if ( this.getID()>0 ){
647                 Vector JavaDoc fieldIds = ServicesRegistry.getInstance().
648                     getJahiaContainersService().getFieldIDsInContainer(this.
649                     getID());
650                 ArrayList JavaDoc locales = new ArrayList JavaDoc();
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 JavaDoc fieldId = (Integer JavaDoc)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 JavaDoc 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             // We check here if the container declaration has changed and if we have to create new
687
// declared field for this container.
688
Enumeration JavaDoc 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 JavaDoc 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                     // save the field
717
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     //-------------------------------------------------------------------------
733
/**
734      * Check if the user has read access for the specified container. Read access means
735      * the user can display container data (he needs to have the rights for each field/
736      * containerlist too)
737      *
738      * @param user Reference to the user.
739      *
740          * @return Return true if the user has read access for the specified container,
741      * or false in any other case.
742      */

743     public final boolean checkReadAccess (JahiaUser user) {
744         return checkAccess(user, JahiaBaseACL.READ_RIGHTS);
745     }
746
747     //-------------------------------------------------------------------------
748
/**
749      * Check if the user has Write access for the specified container. Write access means
750      * updating container data data (he needs to have the rights for each field/containerlist too)
751      *
752      * @param user Reference to the user.
753      *
754      * @return Return true if the user has write access for the specified container,
755      * or false in any other case.
756      */

757     public final boolean checkWriteAccess (JahiaUser user) {
758         return checkAccess(user, JahiaBaseACL.WRITE_RIGHTS);
759     }
760
761     //-------------------------------------------------------------------------
762
/**
763      * Check if the user has Admin access for the specified container. Admin access means
764      * setting container rights (he needs to have the rights for each field/containerlist too)
765      *
766      * @param user Reference to the user.
767      *
768      * @return Return true if the user has admin access for the specified container,
769      * or false in any other case.
770      */

771     public final boolean checkAdminAccess (JahiaUser user) {
772         return checkAccess(user, JahiaBaseACL.ADMIN_RIGHTS);
773     }
774
775     //-------------------------------------------------------------------------
776
private boolean checkAccess (JahiaUser user, int permission) {
777         if (user == null) {
778             return false;
779         }
780
781         boolean result = false;
782         try {
783             // Try to instanciate the ACL.
784
JahiaBaseACL containerACL = new JahiaBaseACL(aclID);
785
786             // Test the access rights
787
result = containerACL.getPermission(user, permission);
788
789             // destroy the object.
790
containerACL = null;
791         } catch (ACLNotFoundException ex) {
792             //JahiaConsole.println ("JahiaContainer", "Could not find the ACL ["+Integer.toString(aclID)+
793
// "] for container ["+Integer.toString(ID)+"]");
794
} catch (JahiaException ex) {
795             //JahiaConsole.println ("JahiaContainer", "JahiaException caught in checkAccess.");
796
}
797
798         //if (!result) {
799
// JahiaConsole.println ("JahiaContainer", "Permission denied for user ["+
800
// user.getName()+"] to container ["+Integer.toString(ID)+
801
// "] for access permission ["+Integer.toString(permission)+"]");
802
//}
803
return result;
804     }
805
806     /**
807      * Try to get the content object from the Jahia container
808      *
809      * @return The content container if success, otherwise null.
810      */

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 JavaDoc 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 JavaDoc 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 JavaDoc getProperty(String JavaDoc propertyName) {
849         if (getProperties() != null) {
850             return getProperties().getProperty(propertyName);
851         } else {
852             return null;
853         }
854     }
855
856     public void setProperty(String JavaDoc propertyName, String JavaDoc propertyValue) {
857         if (getProperties() == null) {
858             ctnProperties = new Properties JavaDoc();
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 } // end JahiaContainer
877
Popular Tags