KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > layout > PortletsJahiaContentSerializer


1 package org.jahia.layout;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Vector JavaDoc;
5
6 import org.jahia.data.JahiaData;
7 import org.jahia.data.containers.JahiaContainer;
8 import org.jahia.data.containers.JahiaContainerDefinition;
9 import org.jahia.data.containers.JahiaContainerList;
10 import org.jahia.data.fields.FieldTypes;
11 import org.jahia.data.fields.JahiaField;
12 import org.jahia.data.fields.JahiaFieldDefinition;
13 import org.jahia.exceptions.JahiaException;
14 import org.jahia.registries.ServicesRegistry;
15 import org.jahia.services.fields.ContentField;
16 import org.jahia.services.fields.ContentSmallTextSharedLangField;
17 import org.jahia.services.version.EntryLoadRequest;
18 import org.jahia.services.version.EntrySaveRequest;
19 import org.jahia.services.containers.ContentContainer;
20 import java.util.Set JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import org.jahia.content.ContentObject;
23 import org.jahia.services.version.StateModificationContext;
24 import org.jahia.content.ContentContainerKey;
25
26 /**
27  * Portlet serialization class for shared portlets. Storage is done using Jahia's
28  * content definition system, effectively using containers and fields to store
29  * information relative to portlets.
30  *
31  * The serializer must be created with a JahiaData object in order for the
32  * declaration of the field to be done properly. The portlet group name must be
33  * also specified. The portlet group name is the name of the instance of a
34  * portlet layout manager. This allows a page to have multiple portlet layouts
35  *
36  * @author Serge Huber
37  * @version 1.0
38  */

39
40 public class PortletsJahiaContentSerializer {
41
42     private static org.apache.log4j.Logger logger =
43             org.apache.log4j.Logger.getLogger(PortletsJahiaContentSerializer.class);
44
45     private static final String JavaDoc METADATA_CONTAINER_SUFFIT = "MetaData";
46
47     // PortletsAPI Default Parameters
48
private static final int defaultPortletID = 0;
49     private static final int defaultPortletSourceID = 0;
50     private static final int defaultPortletW = 100;
51     private static final int defaultPortletH = 100;
52     private static final int defaultPortletColumn = 0;
53     private static final int defaultPortletRow = 0;
54
55     private JahiaData jData;
56     private String JavaDoc portletGroupName;
57
58     /**
59      * The serializer constructor enforces the relationship between a JahiaData
60      * object, a portletGroupName string and the serializer itself. For a single
61      * page we may use multiple serializer if we are processing multiple portlet
62      * groups
63      */

64     public PortletsJahiaContentSerializer(JahiaData jData, String JavaDoc portletGroupName)
65     throws JahiaException {
66         this.jData = jData;
67         this.portletGroupName = portletGroupName + METADATA_CONTAINER_SUFFIT;
68             // this concatenation is done to keep a relationship between the
69
// content container and the metadata container
70
declareJahiaTemplatePortlets();
71     }
72
73     /**
74      * Since we are using Jahia's content storage feature, we need to declare
75      * the storage containers and fields. This function does just that, plus
76      * it defines default values for the field.
77      */

78     private void declareJahiaTemplatePortlets()
79     throws JahiaException {
80         PortletBean defaultPortlet = new PortletBean(defaultPortletID,
81                                                      defaultPortletSourceID,
82                                                      defaultPortletW,
83                                                      defaultPortletH,
84                                                      defaultPortletColumn,
85                                                      defaultPortletRow);
86         declareJahiaTemplatePortlets(defaultPortlet);
87     }
88
89     /**
90      * Since we are using Jahia's content storage feature, we need to declare
91      * the storage containers and fields. This function also allows us to define
92      * a set of default values to customize the default values of the fields
93      */

94     private void declareJahiaTemplatePortlets(PortletBean defaultValues)
95     throws JahiaException {
96
97         try {
98
99             jData.containers().declareField( this.portletGroupName, "PortletID", "ID", FieldTypes.SMALLTEXT_SHARED_LANG,
100                                              Integer.toString(defaultValues.getPortletID()) );
101             jData.containers().declareField( this.portletGroupName, "PortletSourceID", "Source ID", FieldTypes.SMALLTEXT_SHARED_LANG,
102                                              Integer.toString(defaultValues.getPortletSourceID()) );
103             jData.containers().declareField( this.portletGroupName, "PortletW", "Width", FieldTypes.SMALLTEXT_SHARED_LANG,
104                                              Integer.toString(defaultValues.getPortletW()) );
105             jData.containers().declareField( this.portletGroupName, "PortletH", "Height", FieldTypes.SMALLTEXT_SHARED_LANG,
106                                              Integer.toString(defaultValues.getPortletH()) );
107             jData.containers().declareField( this.portletGroupName, "PortletColumn", "Column", FieldTypes.SMALLTEXT_SHARED_LANG,
108                                              Integer.toString(defaultValues.getPortletColumn()) );
109             jData.containers().declareField( this.portletGroupName, "PortletRow", "Row", FieldTypes.SMALLTEXT_SHARED_LANG,
110                                              Integer.toString(defaultValues.getPortletRow()) );
111
112             Vector JavaDoc theFieldList = new Vector JavaDoc();
113             theFieldList.add( "PortletID");
114             theFieldList.add( "PortletSourceID" );
115             theFieldList.add( "PortletW" );
116             theFieldList.add( "PortletH" );
117             theFieldList.add( "PortletColumn" );
118             theFieldList.add( "PortletRow" );
119             jData.containers().declareContainer( portletGroupName, "Portlet List Meta Data", theFieldList );
120             logger.debug("Container for portlet list meta data [" +
121                          portletGroupName + "] declared");
122         } catch (JahiaException je) {
123             // this probably means we have already declared the portlet
124
// definition.
125
/** @todo find a cleaner way to handle multiple definitions */
126             logger.debug("Portlets already defined...");
127         }
128
129     }
130
131     /**
132      * Returns an enumeration of PortletBean objects that are contained in the
133      * page
134      */

135     public Enumeration JavaDoc getPortlets(String JavaDoc currentUserName) throws JahiaException {
136         // this first call must be done before any access. It will only declare
137
// the portlets once for each page
138
JahiaContainerList portletContainerList = jData.containers().getContainerList(portletGroupName);
139         Enumeration JavaDoc portletContainerEnumeration = portletContainerList.getContainers();
140         Vector JavaDoc resultPortletList = new Vector JavaDoc();
141         while (portletContainerEnumeration.hasMoreElements()) {
142             JahiaContainer portletContainer = (JahiaContainer) portletContainerEnumeration.nextElement();
143             resultPortletList.add(containerToPortlet(portletContainer));
144         }
145         return resultPortletList.elements();
146     }
147
148     private PortletBean containerToPortlet(JahiaContainer container) throws JahiaException {
149         PortletBean resultPortlet = null;
150         resultPortlet = new PortletBean(container.getFieldValue("PortletID"),
151                                         container.getFieldValue("PortletSourceID"),
152                                         container.getFieldValue("PortletW"),
153                                         container.getFieldValue("PortletH"),
154                                         container.getFieldValue("PortletColumn"),
155                                         container.getFieldValue("PortletRow"),
156                                         container.getID()
157                                         );
158
159         return resultPortlet;
160     }
161
162     private void setSharedFieldValue(JahiaContainer container, String JavaDoc fieldName, String JavaDoc fieldValue) throws JahiaException {
163         if (container.getField(fieldName) == null) {
164             JahiaContainerDefinition ctnDef = container.getDefinition();
165             JahiaFieldDefinition fieldDef = ctnDef.findFieldInStructure(fieldName, jData.params().getPage().getPageTemplateID());
166             if (fieldDef != null) {
167
168                 ContentSmallTextSharedLangField theField =
169                         ContentSmallTextSharedLangField.createSmallText(
170                         jData.params().getSiteID(),
171                         jData.params().getPageID(),
172                         container.getID(),
173                         fieldDef.getID(),
174                         container.getAclID(),
175                         0,
176                         fieldValue,
177                         jData.params());
178             }
179         } else {
180             JahiaField theField = container.getField(fieldName);
181             ContentSmallTextSharedLangField contentField = (ContentSmallTextSharedLangField) ContentField.getField(theField.getID());
182
183             contentField.setText(fieldValue, new EntrySaveRequest(jData.params().getUser()));
184
185         }
186     }
187
188     private void portletToContainer(PortletBean pb, JahiaContainer container) throws JahiaException {
189         setSharedFieldValue(container, "PortletID", Integer.toString(pb.getPortletID()));
190         setSharedFieldValue(container, "PortletSourceID", Integer.toString(pb.getPortletSourceID()));
191         setSharedFieldValue(container, "PortletW", Integer.toString(pb.getPortletW()));
192         setSharedFieldValue(container, "PortletH", Integer.toString(pb.getPortletH()));
193         setSharedFieldValue(container, "PortletColumn", Integer.toString(pb.getPortletColumn()));
194         setSharedFieldValue(container, "PortletRow", Integer.toString(pb.getPortletRow()));
195     }
196
197     public PortletBean getPortlet(String JavaDoc currentUserName,
198                                   int portletID) throws JahiaException {
199         JahiaContainerList portletContainerList = jData.containers().getContainerList(portletGroupName);
200         Enumeration JavaDoc portletContainerEnumeration = portletContainerList.getContainers();
201         while (portletContainerEnumeration.hasMoreElements()) {
202             JahiaContainer portletContainer = (JahiaContainer) portletContainerEnumeration.nextElement();
203             PortletBean portlet = containerToPortlet(portletContainer);
204             if (portlet.getPortletID() == portletID)
205                 return portlet;
206         }
207         return null;
208     }
209
210    /**
211      * Retrieves a list of portlets for the specified column
212      *
213      * @todo this should probably return a sorted list
214      *
215      */

216     public Enumeration JavaDoc getPortletsFromColumn(int theColumn, String JavaDoc currentUserName) throws JahiaException {
217
218         Enumeration JavaDoc allPortlets = getPortlets(currentUserName);
219         Vector JavaDoc portletList = new Vector JavaDoc();
220         while (allPortlets.hasMoreElements()) {
221             PortletBean portlet = (PortletBean) allPortlets.nextElement();
222             if (portlet.getPortletColumn() == theColumn) {
223                 portletList.add(portlet);
224             }
225         }
226
227         return portletList.elements();
228     } // end getPortletsFromColumn()
229

230
231     public boolean isPortletInStore(PortletBean portlet,
232                                     String JavaDoc currentUserName) throws JahiaException {
233         return (getPortlet(currentUserName, portlet.getPortletID()) != null);
234     }
235
236     private JahiaContainer lookupPortletContainer(PortletBean portlet,
237                                                   String JavaDoc currentUserName)
238     throws JahiaException
239     {
240         logger.debug("Looking up portlet " + portlet.getPortletSourceID() + " for user " + currentUserName);
241         JahiaContainerList portletContainerList = jData.containers().getContainerList(portletGroupName);
242         Enumeration JavaDoc portletContainerEnumeration = portletContainerList.getContainers();
243         while (portletContainerEnumeration.hasMoreElements()) {
244             JahiaContainer portletContainer = (JahiaContainer) portletContainerEnumeration.nextElement();
245             int containerPortletID = Integer.parseInt(portletContainer.getField("PortletSourceID").getValue());
246             logger.debug("Source ID = " + Integer.toString(containerPortletID));
247             if (containerPortletID == portlet.getPortletSourceID()) {
248                 return portletContainer;
249             }
250         }
251         return null;
252     }
253
254     public void storePortlet(PortletBean portlet,
255                              String JavaDoc currentUserName)
256     throws JahiaException {
257         logger.debug("Storing portlet " + portlet.getPortletSourceID() + " meta data for user " + currentUserName);
258         JahiaContainer portletContainer = lookupPortletContainer(portlet, currentUserName);
259         if (portletContainer != null) {
260             logger.debug("Updating portlet container...");
261             portletToContainer(portlet, portletContainer);
262             ContentContainer.invalidateContainerCache(portletContainer.getID());
263         } else {
264             // container not found, let's try to create it then...
265
logger.debug("Creating portlet " + portlet.getPortletSourceID() + " meta data for user " +currentUserName);
266             JahiaContainerList portletList = jData.containers().getContainerList(this.portletGroupName);
267             ServicesRegistry.getInstance().getJahiaContainersService().saveContainerListInfo(portletList, jData.page().getAclID());
268             portletList = jData.containers().getContainerList(this.portletGroupName);
269             logger.debug("ACL ID of container list=" + Integer.toString(portletList.getAclID()));
270             portletContainer = new JahiaContainer(0, jData.params().getJahiaID(),
271                                                   jData.params().getPageID(),
272                                                   portletList.getID(),
273                                                   0, /* rank */
274                                                   portletList.getAclID(),
275                                                   portletList.getctndefid(),
276                                                   0,
277                                                   EntryLoadRequest.STAGING_WORKFLOW_STATE);
278
279             ServicesRegistry.getInstance().getJahiaContainersService().saveContainer(portletContainer, 0, jData.params());
280             logger.debug("Setting field values in container");
281             portletToContainer(portlet, portletContainer);
282         }
283     }
284
285     public void removePortlet(PortletBean portlet,
286                               String JavaDoc currentUserName) throws JahiaException {
287         JahiaContainer portletContainer = lookupPortletContainer(portlet, currentUserName);
288         if (portletContainer != null) {
289             // ServicesRegistry.getInstance().getJahiaContainersService().deleteContainer( portletContainer.getID(), jData.params() );
290
ContentContainer contentContainer = ContentContainer.
291                     getContainer (portletContainer.getID ());
292
293             // we only need to remove the shared language since this will
294
// automatically mark all sub languages for deletion too...
295
Set JavaDoc curLanguageCodes = new HashSet JavaDoc ();
296             curLanguageCodes.add (ContentObject.SHARED_LANGUAGE);
297
298             StateModificationContext stateModifContext =
299                     new StateModificationContext (new
300                             ContentContainerKey (
301                                     portletContainer.getID ()), curLanguageCodes, true);
302             stateModifContext.pushAllLanguages (true);
303
304             contentContainer.markLanguageForDeletion (
305                     jData.params().getUser (),
306                     ContentObject.SHARED_LANGUAGE,
307                     stateModifContext);
308         }
309     }
310
311 }
312
Popular Tags