KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > beans > ContainerBean


1 package org.jahia.data.beans;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Properties JavaDoc;
8
9 import org.apache.log4j.Logger;
10 import org.jahia.content.JahiaObject;
11 import org.jahia.content.PropertiesInterface;
12 import org.jahia.data.containers.JahiaContainer;
13 import org.jahia.data.containers.JahiaContainerDefinition;
14 import org.jahia.data.containers.JahiaContainerList;
15 import org.jahia.data.fields.FieldTypes;
16 import org.jahia.data.fields.JahiaField;
17 import org.jahia.data.fields.JahiaFieldDefinition;
18 import org.jahia.exceptions.JahiaException;
19 import org.jahia.gui.GuiBean;
20 import org.jahia.gui.HTMLToolBox;
21 import org.jahia.params.ParamBean;
22 import org.jahia.registries.ServicesRegistry;
23 import org.jahia.services.acl.JahiaBaseACL;
24 import org.jahia.services.containers.ContainerFactory;
25 import org.jahia.services.containers.ContainerFactoryProxy;
26 import org.jahia.services.containers.ContentContainer;
27 import org.jahia.services.containers.ContentContainerList;
28 import org.jahia.services.lock.LockKey;
29 import org.jahia.services.lock.LockService;
30 import org.jahia.services.pages.ContentPage;
31 import org.jahia.services.version.EntryLoadRequest;
32 import org.jahia.utils.InsertionSortedMap;
33
34 /**
35  * <p>Title: Container JavaBean compliant JahiaContainer facade</p>
36  * <p>Description: </p>
37  * <p>Copyright: Copyright (c) 2002</p>
38  * <p>Company: Jahia Ltd</p>
39  * @author Serge Huber
40  * @version 1.0
41  */

42
43 public class ContainerBean extends ContentBean implements PropertiesInterface {
44
45     private static Logger logger = Logger.getLogger(ContainerBean.class);
46
47     private JahiaContainer jahiaContainer;
48     private ParamBean paramBean;
49     private FieldsWrapper fieldsWrapper = null;
50     private Map JavaDoc fields = null;
51     private Map JavaDoc actionURIs = null;
52     private boolean completelyLocked = false;
53
54     static {
55         registerType(ContentContainer.class.getName(), ContainerBean.class.getName());
56     }
57
58     public ContainerBean () {
59     }
60
61     public ContainerBean (JahiaContainer aContainer, ParamBean params) {
62         this.jahiaContainer = aContainer;
63         this.paramBean = params;
64     }
65
66     public static AbstractJahiaObjectBean getChildInstance (JahiaObject
67         jahiaObject,
68         ParamBean paramBean) {
69         ContentContainer contentContainer = (ContentContainer) jahiaObject;
70         try {
71             return new ContainerBean(contentContainer.getJahiaContainer(
72                 paramBean, paramBean.getEntryLoadRequest()), paramBean);
73         } catch (JahiaException je) {
74             logger.error("Error while converting content container to jahia container", je);
75             return null;
76         }
77     }
78
79     public int getId () {
80         return jahiaContainer.getID();
81     }
82
83     public JahiaContainer getJahiaContainer () {
84         return jahiaContainer;
85     }
86
87     public ContentContainer getContentContainer () {
88         return jahiaContainer.getContentContainer();
89     }
90
91     public JahiaContainerDefinition getDefinition () {
92         try {
93             return jahiaContainer.getDefinition();
94         } catch (JahiaException je) {
95             logger.error(
96                 "Error while accessing container list definition for container " +
97                 getId() + ":", je);
98             return null;
99         }
100     }
101
102     public FieldBean getField (String JavaDoc name) {
103         try {
104             JahiaField jahiaField = jahiaContainer.getField(name);
105             if (jahiaField == null) {
106                 return null;
107             }
108             FieldBean fieldBean = new FieldBean(jahiaField, paramBean);
109             return fieldBean;
110         } catch (JahiaException je) {
111             logger.error("Error while retrieving field " + name +
112                          " for container " + getId() + ":", je);
113             return null;
114         }
115     }
116
117     //-------------------------------------------------------------------------
118
/**
119      * get a field value
120      *
121      * @param fieldName the field name
122      * @exception throws a critical jahia exception if field not found
123      */

124     public Object JavaDoc getFieldValue( String JavaDoc fieldName )
125     throws JahiaException
126     {
127         return getFieldValue(fieldName,false,null);
128     }
129
130     //-------------------------------------------------------------------------
131
/**
132      * get a field value
133      *
134      * @param fieldName
135      * @param allowDiffVersionHighlight
136      * @return
137      * @exception throws a critical jahia exception if field not found
138      */

139     public Object JavaDoc getFieldValue( String JavaDoc fieldName ,
140                                  boolean allowDiffVersionHighlight,
141                                  ParamBean jParams)
142     throws JahiaException
143     {
144         FieldBean theField = this.getField(fieldName);
145         if ( theField != null ){
146             if ( !allowDiffVersionHighlight ){
147                 return theField.getValue();
148             } else {
149                 return theField.getHighLightDiffValue();
150             }
151         }
152         return null;
153     }
154
155     //-------------------------------------------------------------------------
156
/**
157      * get a field value
158      *
159      * @param fieldName the field name
160      * @param defaultValue
161      * @exception throws a critical jahia exception if field not found
162      */

163     public Object JavaDoc getFieldValue( String JavaDoc fieldName ,
164                                  String JavaDoc defaultValue ,
165                                  boolean allowDiffVersionHighlight ,
166                                  ParamBean jParams )
167     throws JahiaException
168     {
169         String JavaDoc value = (String JavaDoc)
170                 getFieldValue(fieldName,allowDiffVersionHighlight,jParams);
171         if ( value == null ){
172             return defaultValue;
173         }
174         return value;
175     }
176
177     //-------------------------------------------------------------------------
178
/**
179      * get a field value
180      *
181      * @param fieldName the field name
182      * @param defaultValue
183      * @exception throws a critical jahia exception if field not found
184      */

185     public Object JavaDoc getFieldValue( String JavaDoc fieldName , String JavaDoc defaultValue )
186     throws JahiaException
187     {
188         String JavaDoc value = (String JavaDoc)getFieldValue(fieldName);
189         if ( value == null ){
190             return defaultValue;
191         }
192         return value;
193     }
194
195     //-------------------------------------------------------------------------
196
/**
197      * get a field value
198      *
199      * @param fieldName the field name
200      * @exception throws a critical jahia exception if field not found
201      */

202     public Object JavaDoc getFieldObject (String JavaDoc fieldName)
203         throws JahiaException {
204         FieldBean theField = this.getField (fieldName);
205         if (theField != null) {
206             return theField.getObject();
207         }
208         return null;
209     }
210
211
212     public Map JavaDoc getFields() {
213         if (fields != null) {
214             return fields;
215         }
216         fields = new HashMap JavaDoc();
217         Enumeration JavaDoc fieldEnum = jahiaContainer.getFields();
218         while (fieldEnum.hasMoreElements()) {
219             JahiaField curJahiaField = (JahiaField) fieldEnum.nextElement();
220             FieldBean curFieldBean = new FieldBean(curJahiaField, paramBean);
221             fields.put(curFieldBean.getName(), curFieldBean);
222         }
223         return fields;
224     }
225
226     public Map JavaDoc getLazyFields() {
227        if (fieldsWrapper == null){
228            fieldsWrapper = new FieldsWrapper();
229        }
230        return fieldsWrapper;
231     }
232     
233     public ContainerListBean getContainerList (String JavaDoc name) {
234         try {
235             JahiaContainerList jahiaContainerList = jahiaContainer.
236                 getContainerList(name);
237             if (jahiaContainerList == null) {
238                 return null;
239             }
240             ContainerListBean containerListBean = new ContainerListBean(
241                 jahiaContainerList, paramBean);
242             return containerListBean;
243         } catch (JahiaException je) {
244             logger.error("Error while retrieving sub container list " + name +
245                          " for container " + getId() + ":", je);
246             return null;
247         }
248     }
249
250     public int getSiteID () {
251         return jahiaContainer.getSiteID();
252     }
253
254     public int getNbFields () {
255         return jahiaContainer.getNbFields();
256     }
257
258     public int getContainerDefinitionID () {
259         return jahiaContainer.getctndefid();
260     }
261
262     public JahiaBaseACL getACL () {
263         return jahiaContainer.getACL();
264     }
265
266     public int getAclID() {
267         return jahiaContainer.getAclID();
268     }
269
270     public int getPageID() {
271         return jahiaContainer.getPageID();
272     }
273
274     public int getContainerListID() {
275         return jahiaContainer.getListID();
276     }
277
278     public int getVersionID() {
279         return jahiaContainer.getVersionID();
280     }
281
282     public int getWorkflowState() {
283         return jahiaContainer.getWorkflowState();
284     }
285
286     public String JavaDoc getLanguageCode() {
287         return jahiaContainer.getLanguageCode();
288     }
289
290     public int getRank() {
291         return jahiaContainer.getRank();
292     }
293
294     public Properties JavaDoc getProperties() {
295         return jahiaContainer.getProperties();
296     }
297
298     public void setProperties(Properties JavaDoc properties) {
299         jahiaContainer.setProperties(properties);
300     }
301
302     public String JavaDoc getProperty(String JavaDoc propertyName) {
303         return jahiaContainer.getProperty(propertyName);
304     }
305
306     public void setProperty(String JavaDoc propertyName, String JavaDoc propertyValue) {
307         jahiaContainer.setProperty(propertyName, propertyValue);
308     }
309
310     public Map JavaDoc getActionURIBeans() {
311         if (actionURIs == null) {
312             buildActionURIs();
313         }
314         return actionURIs;
315     }
316
317     public boolean isCompletelyLocked() {
318         if (actionURIs == null) {
319             buildActionURIs();
320         }
321         return completelyLocked;
322     }
323
324     public boolean isPartiallyLocked() {
325         if (actionURIs == null) {
326             buildActionURIs();
327         }
328         if (!completelyLocked) {
329             Iterator JavaDoc actionURIIter = actionURIs.entrySet().iterator();
330             boolean partiallyLocked = false;
331             while (actionURIIter.hasNext()) {
332                 Map.Entry JavaDoc curActionURIEntry = (Map.Entry JavaDoc) actionURIIter.next();
333                 ActionURIBean curActionURIBean = (ActionURIBean) curActionURIEntry.getValue();
334                 if (curActionURIBean.isLocked()) {
335                     partiallyLocked = true;
336                 }
337             }
338             return partiallyLocked;
339         } else {
340             return false;
341         }
342     }
343
344     public boolean isActionURIsEmpty() {
345         if (!paramBean.getOperationMode().equals(ParamBean.EDIT)) {
346             return true;
347         }
348         if (actionURIs == null) {
349             buildActionURIs();
350         }
351         return actionURIs.isEmpty();
352     }
353
354     public ContentBean getParent() {
355         return getParentContainerList();
356     }
357
358     public ContainerListBean getParentContainerList() {
359         ContainerListBean parentContainerListBean = null;
360         int containerListID = getContainerListID();
361         try {
362             ContentContainerList parentContainerList = ContentContainerList.
363                 getContainerList(containerListID);
364             JahiaContainerList parentJahiaContainerList = parentContainerList.getJahiaContainerList(paramBean, paramBean.getEntryLoadRequest());
365             parentContainerListBean = new ContainerListBean(parentJahiaContainerList, paramBean);
366         } catch (JahiaException je) {
367             logger.error("Error while trying to retrieve parent container list ID=" + containerListID, je);
368         }
369         return parentContainerListBean;
370     }
371
372     private void buildActionURIs() {
373         actionURIs = new InsertionSortedMap();
374         GuiBean guiBean = new GuiBean(paramBean);
375         HTMLToolBox htmlToolBox = new HTMLToolBox(guiBean, paramBean);
376         completelyLocked = true;
377         try {
378             String JavaDoc curURL = guiBean.drawUpdateContainerUrl(jahiaContainer);
379             String JavaDoc curLauncherURI = htmlToolBox.drawUpdateContainerLauncher(jahiaContainer);
380             ActionURIBean curActionURIBean = new ActionURIBean("update", curURL, curLauncherURI);
381             LockService lockRegistry = ServicesRegistry.getInstance().getLockService();
382             LockKey lockKey = LockKey.composeLockKey(LockKey.UPDATE_CONTAINER_TYPE, jahiaContainer.getID(), jahiaContainer.getPageID());
383             if (!lockRegistry.isAcquireable(lockKey, paramBean.getUser(), paramBean.getSessionID())) {
384                 curActionURIBean.setLocked(true);
385             } else {
386                 completelyLocked = false;
387             }
388             if (!lockRegistry.canRelease(lockKey, paramBean.getUser(), paramBean.getSessionID())) {
389                 curActionURIBean.setReleaseable(true);
390             }
391             if ((curActionURIBean.getUri() != null) && (!"".equals(curActionURIBean.getUri()))) {
392                 actionURIs.put(curActionURIBean.getName(), curActionURIBean);
393             }
394             // before generating delete URL we must check for any page fields
395
// that ARE the current page. We are not allowed to perform a
396
// delete operation on the page we are currently on.
397
if (!isFieldWithCurrentPagePresent()) {
398                 curURL = guiBean.drawDeleteContainerUrl(jahiaContainer);
399                 curLauncherURI = htmlToolBox.drawDeleteContainerLauncher(
400                     jahiaContainer);
401                 curActionURIBean = new ActionURIBean("delete", curURL,
402                     curLauncherURI);
403                 lockKey = LockKey.composeLockKey(LockKey.DELETE_CONTAINER_TYPE,
404                                                  jahiaContainer.getID(), jahiaContainer.getPageID());
405                 if (!lockRegistry.isAcquireable(lockKey, paramBean.getUser(),
406                                                 paramBean.getSessionID())) {
407                     curActionURIBean.setLocked(true);
408                 } else {
409                     completelyLocked = false;
410                 }
411                 if (!lockRegistry.canRelease(lockKey, paramBean.getUser(),
412                                              paramBean.getSessionID())) {
413                     curActionURIBean.setReleaseable(true);
414                 }
415                 if ( (curActionURIBean.getUri() != null) &&
416                     (!"".equals(curActionURIBean.getUri()))) {
417                     actionURIs.put(curActionURIBean.getName(), curActionURIBean);
418                 }
419             }
420         } catch (JahiaException je) {
421             logger.error("Error while retrieving action URI map for container " + getId(), je);
422         }
423     }
424
425     private boolean isFieldWithCurrentPagePresent () {
426         Map JavaDoc fieldMap = getFields();
427         Iterator JavaDoc fieldIter = fieldMap.entrySet().iterator();
428         while (fieldIter.hasNext()) {
429             Map.Entry JavaDoc curEntry = (Map.Entry JavaDoc) fieldIter.next();
430             FieldBean curFieldBean = (FieldBean) curEntry.getValue();
431             if (curFieldBean.getType() == FieldTypes.PAGE) {
432                 if (curFieldBean.getValue() != null) {
433                     int pageID = -1;
434                     try {
435                         pageID = Integer.parseInt(curFieldBean.getRawValue());
436                     } catch (NumberFormatException JavaDoc nfe) {
437                         pageID = -1;
438                     }
439                     if (pageID > 0) {
440                         if (pageID == paramBean.getPageID()) {
441                             return true;
442                         }
443                     }
444                 }
445             }
446         }
447         return false;
448     }
449     
450     private class FieldsWrapper extends HashMap JavaDoc {
451         private Map JavaDoc cachedFieldsAndDefsFromContainers = new HashMap JavaDoc();
452
453         public FieldsWrapper() {
454         }
455
456         public Object JavaDoc get(Object JavaDoc key) {
457             if (!containsKey(key)) {
458                 try {
459                     JahiaFieldDefinition fieldDef = getJahiaContainer()
460                             .getDefinition().findFieldInStructure(
461                                     (String JavaDoc) key,
462                                     ContentPage.getPage(
463                                             getJahiaContainer().getPageID())
464                                             .getPageTemplateID(getParamBean()));
465                     if (fieldDef != null) {
466                         EntryLoadRequest loadRequest = getParamBean()
467                                 .getEntryLoadRequest();
468                         if (getJahiaContainer().getVersionID() == -1
469                                 && !loadRequest.isWithMarkedForDeletion()) {
470                             loadRequest = (EntryLoadRequest) loadRequest
471                                     .clone();
472                             loadRequest.setWithMarkedForDeletion(true);
473                         }
474                         JahiaField theField = ContainerFactory.getInstance()
475                                 .loadContainerField(getJahiaContainer(),
476                                         fieldDef.getID(),
477                                         ContainerFactoryProxy.LOAD_FIELDS,
478                                         getParamBean(), loadRequest,
479                                         cachedFieldsAndDefsFromContainers);
480                         if (theField != null) {
481                             super.put(key, new FieldBean(theField,
482                                     getParamBean()));
483                         }
484                     }
485                 } catch (JahiaException e) {
486                     getLogger().error("cannot load container field", e);
487                 }
488             }
489
490             return super.get(key);
491         }
492     }
493
494     /**
495      * @return Returns the logger.
496      */

497     protected static org.apache.log4j.Logger getLogger() {
498         return logger;
499     }
500
501     /**
502      * @return Returns the paramBean.
503      */

504     protected ParamBean getParamBean() {
505         return paramBean;
506     }
507     
508 }
Popular Tags