KickJava   Java API By Example, From Geeks To Geeks.

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


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
// JahiaContainerList
15
// EV 27.12.2000
16
//
17

18 package org.jahia.data.containers;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.log4j.Logger;
26 import org.jahia.exceptions.JahiaException;
27 import org.jahia.registries.JahiaContainerDefinitionsRegistry;
28 import org.jahia.services.acl.ACLNotFoundException;
29 import org.jahia.services.acl.JahiaBaseACL;
30 import org.jahia.services.containers.ContainerListFactoryProxy;
31 import org.jahia.services.containers.ContentContainerList;
32 import org.jahia.services.usermanager.JahiaUser;
33 import java.io.Serializable JavaDoc;
34
35 /**
36  * <p>Title: A JahiaContainerList is a list of JahiaContainer objects</p>
37  * <p>Description: The JahiaContainerList object managed a list of
38  * JahiaContainer object, that themselves contain JahiaField objects.</p>
39  * <p>Copyright: Copyright (c) 2002</p>
40  * <p>Company: Jahia Ltd</p>
41  * @author Eric Vassalli
42  * @version 1.0
43  */

44
45 public class JahiaContainerList implements Cloneable JavaDoc, Serializable JavaDoc {
46
47     private static Logger logger = Logger.getLogger(JahiaContainerList.class);
48
49     private int ID;
50     private int parentEntryID;
51     private int pageID;
52     private int ctndefid;
53     private int aclID;
54     private int fullDataSize = 0;
55
56     private boolean isContainersLoaded = false; // by default the container list is loaded without its containers.
57

58     private Properties JavaDoc ctnListProperties = new Properties JavaDoc();
59
60     private JahiaContainerListPagination ctnListPagination;
61
62
63     /**
64      * @associates JahiaContainer
65      */

66     private ArrayList JavaDoc containers = new ArrayList JavaDoc();
67
68     private ContainerListFactoryProxy cListFactoryProxy;
69
70     //-------------------------------------------------------------------------
71
public JahiaContainerList( int anID,
72                                 int aParentEntryID,
73                                 int aPageID,
74                                 int aCtndefid,
75                                 int anAclID )
76     {
77         this.ID = anID;
78         this.parentEntryID = aParentEntryID;
79         this.pageID = aPageID;
80         this.ctndefid = aCtndefid;
81         this.aclID = anAclID;
82     } // end constructor
83

84     /**
85      * Set the containerListFactory used to control how this containerlist's
86      * containers will be loaded.
87      *
88      * @param containerFactory
89      */

90     public void setFactoryProxy(ContainerListFactoryProxy aCListFactoryProxy){
91         this.cListFactoryProxy = aCListFactoryProxy;
92     }
93
94     //-------------------------------------------------------------------------
95
public int getID() { return ID; }
96     public int getParentEntryID() { return parentEntryID; }
97     public int getPageID() { return pageID; }
98     public int getctndefid() { return ctndefid; }
99     public final int getAclID() { return aclID; }
100
101     /**
102      * Return true if this container list is loaded with its containers
103      *
104      * @author NK
105      */

106     public boolean isContainersLoaded()
107     {
108         return isContainersLoaded;
109     }
110
111     public JahiaContainerListPagination getCtnListPagination() {
112         checkProxy();
113         return ctnListPagination;
114     }
115
116     public final JahiaBaseACL getACL() {
117         JahiaBaseACL acl = null;
118         try {
119             acl = new JahiaBaseACL(getAclID());
120         } catch ( Throwable JavaDoc t ) {
121             t.printStackTrace();
122         }
123         return acl;
124     }
125
126     public void setID( int anID ) { this.ID = anID; }
127     public void setAclID( int anAclID ) { this.aclID = anAclID; }
128     public void setParentEntryID(int aParentEntryID) { this.parentEntryID = aParentEntryID; }
129
130     public void setCtnListPagination( int windowSize, int windowOffset )
131     {
132         this.ctnListPagination = new JahiaContainerListPagination(this.getFullSize(),windowSize,windowOffset);
133     }
134
135     public void setCtnListPagination( JahiaContainerListPagination cListPagination )
136     {
137         this.ctnListPagination = cListPagination;
138     }
139
140     /**
141      * By default the container list is loaded without its containers.
142      * Set this state to true to indeicate you loaded its containers.
143      *
144      * @author NK
145      */

146     public void setIsContainersLoaded( boolean value )
147     {
148         this.isContainersLoaded = value;
149     }
150
151
152     // end accessor methods
153

154
155
156     //-------------------------------------------------------------------------
157
/**
158      */

159     public JahiaContainer getContainer( int index ) throws JahiaException
160     {
161         checkProxy();
162         if (index < containers.size()) {
163             return (JahiaContainer) containers.get(index);
164         } else {
165             String JavaDoc errorMsg = "Error in JahiaContainerList : trying to get entry " + index + " for container " + getDefinition().getName();
166             logger.error(errorMsg + " -> BAILING OUT");
167             throw new JahiaException( "Error in database synchronisation",
168                                         errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY );
169         }
170     } // end getContainer
171

172
173
174     /**
175      * @return an Enumeration of the containers in the container list.
176      */

177     public Enumeration JavaDoc getContainers()
178     {
179         checkProxy();
180         Vector JavaDoc tempVector = new Vector JavaDoc(containers);
181         return tempVector.elements();
182     } // end getContainers
183

184     /**
185      * Returns the size of the container list (ie the number of elements in
186      * the list). This now returns only the size that has been loaded. For
187      * real full size of data set, see the full size call below.
188      * @returns an integer specifying the number of elements in the container
189      * list in memory for the current view.
190      */

191     public int size() {
192         checkProxy();
193         if ( containers != null )
194         {
195             return containers.size();
196         }
197         return 0;
198     }
199
200     /**
201      * Sets the full size of the container list stored in the database, not
202      * just in memory.
203      * @param fullDataSize an integer containing a value of the full data size.
204      */

205     public void setFullSize(int aFullDataSize) {
206         if (aFullDataSize >= 0) {
207             this.fullDataSize = aFullDataSize;
208         }
209     }
210
211     /**
212      * Returns the full size of the data set in the datasource. This function
213      * has been added because of the introduction of scrollable container lists
214      * which load only the set for the view.
215      * @return the number of elements of this containerList in the database
216      */

217     public int getFullSize() {
218         checkProxy();
219         return this.fullDataSize;
220     }
221
222     /***
223         * getDefinition
224         *
225         */

226     public JahiaContainerDefinition getDefinition()
227     throws JahiaException
228     {
229         JahiaContainerDefinition theDef = JahiaContainerDefinitionsRegistry.getInstance(
230             ).getDefinition( ctndefid );
231         if (theDef != null) {
232             return theDef;
233         } else {
234             String JavaDoc msg = "JahiaContainer definition " + ctndefid + " not found in definition registry !";
235             throw new JahiaException( "Synchronisation error in database",
236                                         msg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY );
237         }
238     } // end getDefinition
239

240
241
242     /***
243         * adds a container to the containerList
244         * @author EV 27.12.2000
245         *
246         */

247     public void addContainer( JahiaContainer theContainer )
248     {
249         checkProxy();
250         /*
251         int insertPos = containers.size();
252         for (int i=0; i < containers.size(); i++) {
253             JahiaContainer aContainer = (JahiaContainer)containers.elementAt(i);
254             if (theContainer.getRank() < aContainer.getRank()) {
255                 insertPos = i;
256                 break;
257             }
258             aContainer = null;
259         }
260         containers.insertElementAt( theContainer, insertPos );
261         */

262         containers.add(theContainer);
263
264     } // end addContainer
265

266
267     //-------------------------------------------------------------------------
268
/**
269      * Returns true if the acl set at container list level allow a field of a given
270      * field def name to be editable or not
271      *
272      * @param String fieldDefName
273      * @param JahiaUser the user
274      * @return boolean true if the acl return true , false else
275      * @author Khue Nguyen
276      */

277     public final boolean isFieldEditable(String JavaDoc fieldDefName, JahiaUser user){
278         boolean result = false;
279         if ( fieldDefName == null || user == null )
280             return false;
281
282         String JavaDoc val = this.getProperty("view_field_acl_"+fieldDefName);
283         if ( val != null ){
284             try {
285                 int anAclID = Integer.parseInt(val);
286                 JahiaBaseACL theACL = null;
287                 try {
288                     theACL = new JahiaBaseACL (anAclID);
289                 }
290                 catch (ACLNotFoundException ex) {
291                 }
292                 catch (JahiaException ex) {
293                 }
294                 return theACL.getPermission(user,JahiaBaseACL.WRITE_RIGHTS);
295             } catch ( Throwable JavaDoc t ){
296             }
297         }
298         return result;
299     }
300
301     //-------------------------------------------------------------------------
302
/**
303      * Returns true if the acl set at container list level allow a field of a given
304      * field def name to be visible ( READ permission )
305      *
306      * @param String fieldDefName
307      * @param JahiaUser the user
308      * @return boolean true if the acl return true , false else
309      * @author Khue Nguyen
310      */

311     public final boolean isFieldReadable(String JavaDoc fieldDefName, JahiaUser user){
312         if ( fieldDefName == null || user == null )
313             return false;
314
315         String JavaDoc val = this.getProperty("view_field_acl_"+fieldDefName);
316         if ( val != null ){
317             try {
318                 int anAclID = Integer.parseInt(val);
319                 JahiaBaseACL theACL = null;
320                 theACL = new JahiaBaseACL (anAclID);
321                 return theACL.getPermission(user,JahiaBaseACL.READ_RIGHTS);
322             } catch ( Throwable JavaDoc t ){
323                 // One acl defined but there's an error assume false for security reason
324
logger.error("error dureing guessing of readable field ",t);
325                 return false;
326             }
327         }
328         // NO Acl defined so assume true
329
return true;
330     }
331
332
333     //-------------------------------------------------------------------------
334
/**
335      * Check if the user has read access for the specified containerList. Read access means
336      * the user can display containerList data.
337      *
338      * @param user Reference to the user.
339      *
340      * @return Return true if the user has read access for the specified containerList,
341      * or false in any other case.
342      */

343     public final boolean checkReadAccess (JahiaUser user)
344     {
345         return checkAccess (user, JahiaBaseACL.READ_RIGHTS);
346     }
347
348     //-------------------------------------------------------------------------
349
/**
350      * Check if the user has Write access for the specified container. Write access means
351      * the user can add/delete/update containers in the containerList.
352      *
353      * @param user Reference to the user.
354      *
355      * @return Return true if the user has write access for the specified containerList,
356      * or false in any other case.
357      */

358     public final boolean checkWriteAccess (JahiaUser user)
359     {
360         return checkAccess (user, JahiaBaseACL.WRITE_RIGHTS);
361     }
362
363
364     //-------------------------------------------------------------------------
365
/**
366      * Check if the user has Admin access for the specified container. Admin access means
367      * the user can set rights on the containerList.
368      *
369      * @param user Reference to the user.
370      *
371      * @return Return true if the user has admin access for the specified containerList,
372      * or false in any other case.
373      */

374     public final boolean checkAdminAccess (JahiaUser user)
375     {
376         return checkAccess (user, JahiaBaseACL.ADMIN_RIGHTS);
377     }
378
379
380     //-------------------------------------------------------------------------
381
private boolean checkAccess (JahiaUser user, int permission)
382     {
383         if (user == null) {
384             return false;
385         }
386
387         // NK :Fake container list with acl id = -1 are not yet created in storage
388
// and their ACL are therefore fake too, so we return true
389
if ( aclID == 0 )
390             return true;
391
392
393         //JahiaConsole.println ("->> containerList checkAccess : containerList ["+Integer.toString (ID)+
394
// "], permission ["+Integer.toString (permission)+"], user ["+user.getName()+"]");
395

396         boolean result = false;
397         try
398         {
399             // Try to instanciate the ACL.
400
JahiaBaseACL containerListACL = new JahiaBaseACL (aclID);
401
402             // Test the access rights
403
result = containerListACL.getPermission (user, permission);
404
405             // destroy the object.
406
containerListACL = null;
407         }
408         catch (JahiaException ex) {
409             logger.debug("Problem getting ACL on container list.", ex);
410         }
411
412         //if (!result) {
413
// JahiaConsole.println ("JahiaContainerList", "Permission denied for user ["+
414
// user.getName()+"] to containerList ["+Integer.toString(ID)+
415
// "] for access permission ["+Integer.toString(permission)+"]");
416
//}
417
return result;
418     }
419
420
421     public void setProperties(Properties JavaDoc newProperties) {
422         this.ctnListProperties = newProperties;
423     }
424
425     public Properties JavaDoc getProperties() {
426         return this.ctnListProperties;
427     }
428
429     public String JavaDoc getProperty(String JavaDoc propertyName) {
430         if (this.ctnListProperties != null) {
431             return this.ctnListProperties.getProperty(propertyName);
432         } else {
433             return null;
434         }
435     }
436
437     public void setProperty(String JavaDoc propertyName, String JavaDoc propertyValue) {
438         if (this.ctnListProperties != null) {
439             this.ctnListProperties.setProperty(propertyName, propertyValue);
440         } else {
441             logger.error("ERROR: Properties object is not defined, ignoring property insertion.");
442         }
443     }
444
445     /**
446      * Merge a set of properties passed in parameters with the internal
447      * properties set. Returns true if the resulting set of internal properties
448      * must be serialized.
449
450      * @param newProperties
451      * @return true if the result of the merge is a different set of properties
452      * that needs to be serialized.
453      */

454     public boolean mergeProperties(Properties JavaDoc newProperties) {
455         boolean mustSave = false;
456         Enumeration JavaDoc newPropKeys = newProperties.keys();
457         while (newPropKeys.hasMoreElements()) {
458             String JavaDoc curNewPropName = (String JavaDoc) newPropKeys.nextElement();
459             String JavaDoc curNewPropValue = newProperties.getProperty(curNewPropName);
460             if (this.ctnListProperties.containsKey(curNewPropName)) {
461                 String JavaDoc internalPropValue = this.ctnListProperties.getProperty(curNewPropName);
462                 if (!internalPropValue.equals(curNewPropValue)) {
463                     // properties are not equals, lets set it.
464
this.ctnListProperties.setProperty(curNewPropName, curNewPropValue);
465                     mustSave = true;
466                 }
467             } else {
468                 // this is a new property.
469
this.ctnListProperties.setProperty(curNewPropName, curNewPropValue);
470                 mustSave = true;
471             }
472         }
473         return mustSave;
474     }
475
476     /**
477      * Try to get the content object from the Jahia container list
478      *
479      * @return The content container list if success, otherwise null.
480      */

481     public ContentContainerList getContentContainerList() {
482         ContentContainerList contentContainerList = null;
483         try {
484             if (ID != 0) {
485                 contentContainerList = ContentContainerList.getContainerList(ID);
486             } else {
487                 // this case can happen if we are handling a "fake"
488
// container list created to generate the add container URL,
489
// etc...
490
return null;
491             }
492         } catch (JahiaException je) {
493             logger.error(
494                 "Error while trying to retrieve ContentContainerList from JahiaContainerList",
495                 je);
496         }
497         return contentContainerList;
498     }
499
500     //-------------------------------------------------------------------------
501
/**
502      * Clone
503      */

504     public Object JavaDoc clone () {
505         JahiaContainerList containerList =
506             new JahiaContainerList(this.ID,this.parentEntryID,this.pageID,
507                                    this.ctndefid,this.aclID);
508         if ( this.ctnListProperties != null ){
509             containerList.setProperties( (Properties JavaDoc)this.ctnListProperties.
510                                         clone());
511         }
512         return containerList;
513     }
514
515     private void checkProxy(){
516         if ( this.cListFactoryProxy != null ){
517             this.cListFactoryProxy.load(this);
518         }
519     }
520
521     public void clearContainers(){
522         this.containers = new ArrayList JavaDoc();
523     }
524 }
525
Popular Tags