KickJava   Java API By Example, From Geeks To Geeks.

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


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
package org.jahia.layout;
13
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.transform.OutputKeys JavaDoc;
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerConfigurationException JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.dom.DOMSource JavaDoc;
31 import javax.xml.transform.stream.StreamResult JavaDoc;
32
33 import org.jahia.exceptions.JahiaException;
34 import org.jahia.registries.ServicesRegistry;
35 import org.jahia.utils.JahiaConsole;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.NamedNodeMap JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41 import org.w3c.dom.Text JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  * This class is responsible for loading up data from an XML file that contains
46  * the portlet configuration of a specific user.
47  *
48  * Format of XML document is as follows:
49  * <?xml version="1.0" encoding="ISO-8859-1"?>
50  * <plml>
51  * <portletgroup id="">
52  * <group_parameter name="page_number">1</group_parameter>
53  * <portlet id="">
54  * <portlet_parameter name="source_id">12</portlet_parameter>
55  * <portlet_parameter name="column_position">1</portlet_parameter>
56  * <portlet_parameter name="row_position">1</portlet_parameter>
57  * other parameters...
58  * </portlet>
59  * other portlets
60  * </portletgroup>
61  * other portlet lists (also known as pages)
62  * </plml>
63  *
64  * @author Serge Huber
65  *
66  */

67 public class PortletsXMLSerializer {
68
69     private static final String JavaDoc CANT_READ_PORTLET_XML_MSG = "Can't read portlet XML file";
70     private static final String JavaDoc ERROR_READING_PLML_FILE_MSG = "Error reading PLML file";
71     private static final String JavaDoc PLML_TAG = "PLML";
72     private static final String JavaDoc GROUP_PARAMETER_TAG = "GROUP_PARAMETER";
73     private static final String JavaDoc PORTLET_PARAMETER_TAG = "PORTLET_PARAMETER";
74     private static final String JavaDoc PARAMETER_TAG_NAME_ATTRIBUTE = "name";
75     private static final String JavaDoc PARAMETER_TAG_ID_ATTRIBUTE = "id";
76     private static final String JavaDoc PORTLETLIST_TAG = "PORTLETGROUP";
77     private static final String JavaDoc PORTLET_TAG = "PORTLET";
78     private static final String JavaDoc XML_ENCODING = "ISO-8859-1";
79
80     private Document JavaDoc rootDocument = null;
81     private boolean addPortletGroup = false;
82     private File JavaDoc theFile;
83     private String JavaDoc theXMLfile;
84     private String JavaDoc theDefaultXMLfile;
85     private String JavaDoc paramAttrNode;
86     private Text JavaDoc theTextNode;
87     private Element JavaDoc portletGroupElement;
88     private Element JavaDoc plmlElement;
89
90     /**
91      * @associates PortletBean
92      */

93     private Vector JavaDoc thePortletList;
94     private int thePageID;
95     private int maxColumn = 0;
96
97     /**
98      * Constructor is initialized with the path to the Portlets XML file Descriptor
99      */

100     PortletsXMLSerializer(String JavaDoc theTemplatesPathDirectory, String JavaDoc thefilename,
101                           String JavaDoc theDefaultfilename, int thePageID) throws JahiaException {
102         initXMLSerializer(theTemplatesPathDirectory, thefilename,
103                           theDefaultfilename, thePageID, "PortletList");
104     }
105
106
107     /**
108      * Constructor is initialized with the path to the Portlets XML file Descriptor
109      */

110     PortletsXMLSerializer(String JavaDoc theTemplatesPathDirectory, String JavaDoc thefilename,
111                           String JavaDoc theDefaultfilename, int thePageID,
112                           String JavaDoc portletGroupName) throws JahiaException {
113
114         initXMLSerializer(theTemplatesPathDirectory, thefilename,
115                           theDefaultfilename, thePageID, portletGroupName);
116     } // end Constructor
117

118     private void initXMLSerializer(String JavaDoc theTemplatesPathDirectory,
119                                    String JavaDoc thefilename,
120                                    String JavaDoc theDefaultfilename, int thePageID,
121                                    String JavaDoc portletGroupName)
122     throws JahiaException {
123         File JavaDoc thePortletsAPIDirectory = new File JavaDoc( theTemplatesPathDirectory +
124                                                   File.separator +
125                                                   "portlets_api" +
126                                                   File.separator );
127         if(!thePortletsAPIDirectory.exists()) {
128             thePortletsAPIDirectory.mkdirs();
129         }
130         String JavaDoc thePath = theTemplatesPathDirectory + File.separator +
131                          "portlets_api" + File.separator;
132         this.addPortletGroup = false;
133         this.theFile = new File JavaDoc( thePath + thefilename);
134         this.theXMLfile = thePath + thefilename;
135         this.theDefaultXMLfile = thePath + theDefaultfilename;
136         this.thePageID = thePageID;
137
138         if (!theFile.exists()) {
139
140             // file does not exist, we must initialize it
141

142             JahiaConsole.println("PortletsXMLSerializer constructor",
143                                  "Creating file " + theXMLfile + "...");
144
145             try {
146                 DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
147                 //dfactory.setValidating(true); // create only parsers that are validating
148
DocumentBuilder JavaDoc docBuilder = dfactory.newDocumentBuilder();
149                 rootDocument = docBuilder.newDocument();
150                 // let's now create the PLML tag and save the file
151
Element JavaDoc plmlElement = rootDocument.createElement(PLML_TAG);
152                 rootDocument.appendChild(plmlElement);
153
154                 addPortletGroup(thePageID);
155
156                 saveFile(theFile.toString());
157                 loadFile(theFile.toString());
158
159                 addPortletGroup = true;
160
161             } catch (Throwable JavaDoc t) {
162                 t.printStackTrace();
163                 throw new JahiaException("PortletXMLSerializer",
164                                          "Error while creating new XML file",
165                                          JahiaException.CRITICAL_SEVERITY,
166                                          JahiaException.DATA_ERROR,
167                                          t);
168             }
169
170
171         } else {
172             // file exists
173

174             JahiaConsole.println("PortletsXMLSerializer constructor", "Loading file " + theXMLfile + "...");
175             try {
176                 loadFile( theXMLfile );
177             } catch (Throwable JavaDoc t) {
178                 t.printStackTrace();
179                 throw new JahiaException ( "PortletsXMLSerializer",
180                                            "Error loading XML file",
181                                            JahiaException.CRITICAL_SEVERITY,
182                                            JahiaException.DATA_ERROR,
183                                            t );
184             }
185
186         }
187
188         plmlElement = findPlml(rootDocument.getDocumentElement());
189         portletGroupElement = findPortletGroup(plmlElement, thePageID);
190     }
191
192     private void createFile(String JavaDoc newFileName)
193     throws JahiaException {
194         /** @todo code to create minimal DOM, or maybe even do this directly
195          * in the constructor code */

196         saveFile(newFileName);
197     }
198
199     private void loadFile(String JavaDoc sourceFileName)
200     throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
201         DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
202         // dfactory.setValidating(true); // create only parsers that are validating
203
DocumentBuilder JavaDoc docBuilder = dfactory.newDocumentBuilder();
204         FileInputStream JavaDoc sourceStream = new FileInputStream JavaDoc(sourceFileName);
205         rootDocument = docBuilder.parse(sourceStream);
206         rootDocument.normalize(); // clean up DOM tree a little
207

208     }
209
210     private void saveFile(String JavaDoc destinationFileName)
211     throws JahiaException
212     {
213
214         try {
215
216             rootDocument.normalize(); // cleanup DOM tree a little
217

218             TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
219
220             // This creates a transformer that does a simple identity transform,
221
// and thus can be used for all intents and purposes as a serializer.
222
Transformer JavaDoc serializer = tfactory.newTransformer();
223
224             serializer.setOutputProperty(OutputKeys.METHOD, "xml");
225             serializer.setOutputProperty(OutputKeys.INDENT, "yes");
226             FileOutputStream JavaDoc fileStream = new FileOutputStream JavaDoc(destinationFileName);
227             serializer.transform(new DOMSource JavaDoc(rootDocument),
228                                  new StreamResult JavaDoc(fileStream));
229
230         } catch (TransformerConfigurationException JavaDoc tce) {
231             throw new JahiaException ("PortletXMLSerializer.saveFile",
232                                       "Error in XSL transformer configuration",
233                                       JahiaException.WARNING_SEVERITY,
234                                       JahiaException.CONFIG_ERROR, tce);
235         } catch (TransformerException JavaDoc te) {
236             throw new JahiaException ("PortletXMLSerializer.saveFile",
237                                       "Error in XSL transformer exception",
238                                       JahiaException.WARNING_SEVERITY,
239                                       JahiaException.CONFIG_ERROR, te);
240         } catch (FileNotFoundException JavaDoc fnfe) {
241             throw new JahiaException ("PortletXMLSerializer.saveFile",
242                                       "File not found exception",
243                                       JahiaException.WARNING_SEVERITY,
244                                       JahiaException.CONFIG_ERROR, fnfe);
245         }
246
247     }
248
249     /**
250      * Finds the PLML tag in the tree specified root element.
251      */

252     private Element JavaDoc findPlml(Element JavaDoc plmlElement) throws JahiaException {
253
254         if (plmlElement == null) {
255             throw new JahiaException ("PortletXMLSerializer.findPlml",
256                                       ERROR_READING_PLML_FILE_MSG,
257                                       JahiaException.WARNING_SEVERITY,
258                                       JahiaException.CONFIG_ERROR);
259         }
260
261         if (plmlElement.getTagName().equals(PLML_TAG)) {
262         } else {
263             throw new JahiaException ("PortletXMLSerializer.findPlml",
264                                       ERROR_READING_PLML_FILE_MSG,
265                                       JahiaException.WARNING_SEVERITY,
266                                       JahiaException.CONFIG_ERROR);
267         }
268
269         return (Element JavaDoc) plmlElement;
270     }
271
272     /**
273      * Finds the portlet group Element tag corresponding to the specified
274      * group ID
275      * @returns Element on success, null on failure to find group corresponding
276      * to id.
277      */

278     private Element JavaDoc findPortletGroup(Element JavaDoc groupParent, int groupID)
279     throws JahiaException {
280         //JahiaConsole.println("PortletXMLSerializer.findPortletGroup", "Looking for group with id = " + Integer.toString(groupID) );
281
NodeList JavaDoc groupList = groupParent.getElementsByTagName(PORTLETLIST_TAG);
282         for (int i = 0; i < groupList.getLength(); i++) {
283             Element JavaDoc groupElement = (Element JavaDoc) groupList.item(i);
284             String JavaDoc pageIDStr = getGroupParameterValue(groupElement, "page_id");
285             if (pageIDStr != null) {
286                 //JahiaConsole.println("PortletXMLSerializer.findPortletGroup", "Found pageID string = [" + pageIDStr + "]");
287
if (groupID == Integer.parseInt(pageIDStr))
288                     return groupElement;
289             }
290         }
291         throw new JahiaException ("PortletXMLSerializer.findPortletGroup",
292                                   ERROR_READING_PLML_FILE_MSG,
293                                   JahiaException.WARNING_SEVERITY,
294                                   JahiaException.CONFIG_ERROR);
295     }
296
297     /**
298      * Finds the portlet Element tag corresponding to the specified
299      * portlet ID
300      * @returns Element on success, null on failure to find portlet corresponding
301      * to id.
302      */

303     private Element JavaDoc findPortlet(Element JavaDoc portletParent, int portletID)
304     throws JahiaException {
305         NodeList JavaDoc portletList = portletParent.getElementsByTagName(PORTLET_TAG);
306         for (int i = 0; i < portletList.getLength(); i++) {
307             Element JavaDoc portletElement = (Element JavaDoc) portletList.item(i);
308             String JavaDoc idStr = portletElement.getAttribute("id");
309             if (idStr != null) {
310                 if (portletID == Integer.parseInt(idStr))
311                     return portletElement;
312             }
313         }
314         throw new JahiaException ("PortletXMLSerializer.findPortlet",
315                                   ERROR_READING_PLML_FILE_MSG,
316                                   JahiaException.WARNING_SEVERITY,
317                                   JahiaException.CONFIG_ERROR);
318     }
319
320     /**
321      * Retrieves the value of a specified portlet parameter
322      */

323     private String JavaDoc getPortletParameterValue(Element JavaDoc paramParent, String JavaDoc parameterName)
324     throws JahiaException {
325         return getParameterValue(paramParent, PORTLET_PARAMETER_TAG, parameterName);
326     }
327
328     /**
329      * Retrieves the value of a specified portlet group parameter
330      */

331     private String JavaDoc getGroupParameterValue(Element JavaDoc paramParent, String JavaDoc parameterName)
332     throws JahiaException {
333         return getParameterValue(paramParent, GROUP_PARAMETER_TAG, parameterName);
334     }
335
336     /**
337      * Processes through all the parameter tags of a given node to find the
338      * value of a certain named parameter
339      *
340      */

341     private String JavaDoc getParameterValue(Element JavaDoc paramParent,
342                                      String JavaDoc parameterTagName,
343                                      String JavaDoc parameterName)
344     throws JahiaException {
345
346         //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Searching for parameter " + parameterName +
347
// " in tag " + parameterTagName);
348

349         NodeList JavaDoc paramList = paramParent.getElementsByTagName(parameterTagName);
350         //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Found " + Integer.toString(paramList.getLength()) + " tags");
351
for (int i = 0; i < paramList.getLength(); i++) {
352             Element JavaDoc paramElement = (Element JavaDoc) paramList.item(i);
353             //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Examining parameter " + Integer.toString(i) + " named " + paramElement.getTagName());
354
String JavaDoc paramName = paramElement.getAttribute("name");
355             if (paramName != null) {
356                 if (paramName.equalsIgnoreCase(parameterName)) {
357                     //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Found parameter name, looking for value... ");
358
// we have found the parameter value
359
Node JavaDoc textNode = paramElement.getFirstChild();
360                     if (textNode.getNodeType() == Node.TEXT_NODE) {
361                         //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Found parameter value = [" + textNode.getNodeValue() + "]");
362
return textNode.getNodeValue();
363                     } else {
364                         //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Not a text node ?!");
365
throw new JahiaException(ERROR_READING_PLML_FILE_MSG,
366                                                  "Value of parameter is not in correct format, should only be text",
367                                                  JahiaException.CRITICAL_SEVERITY,
368                                                  JahiaException.CONFIG_ERROR);
369                     }
370                 } else {
371                     //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Attribute case is not correct [" + paramName + "]");
372
}
373             } else {
374                 //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "Parameter not found");
375
throw new JahiaException(ERROR_READING_PLML_FILE_MSG,
376                                          "Parameter not found !",
377                                          JahiaException.CRITICAL_SEVERITY,
378                                          JahiaException.CONFIG_ERROR);
379             }
380         }
381         //JahiaConsole.println("PortletsXMLSerializer.getParameterValue", "No parameter found, returning null... ");
382
return null;
383     }
384
385
386     /**
387      * Find the value of a certain named attribute
388      *
389      */

390     private String JavaDoc getAttributeValue(Node JavaDoc paramParent, String JavaDoc attributeName) throws JahiaException {
391
392         NamedNodeMap JavaDoc attrib = paramParent.getAttributes();
393         Node JavaDoc paramAttrib = attrib.getNamedItem(attributeName);
394         return paramAttrib.getNodeValue();
395
396     } // end getAttributeValue()
397

398
399     /**
400      * Retrieves a column count for the specified page
401      */

402     public int getColumnCount() throws JahiaException {
403
404         maxColumn = 0;
405
406         Element JavaDoc plmlElement = findPlml(rootDocument.getDocumentElement());
407         Element JavaDoc portletGroupElement = findPortletGroup(plmlElement, thePageID);
408
409         NodeList JavaDoc portletList = portletGroupElement.getElementsByTagName(PORTLET_TAG);
410         for (int i = 0; i < portletList.getLength(); i++) {
411             Element JavaDoc portletElement = (Element JavaDoc) portletList.item(i);
412             String JavaDoc columnPosStr = getPortletParameterValue(portletElement, "column_position");
413             int columnPos = Integer.parseInt(columnPosStr);
414             if (columnPos > maxColumn) {
415                 maxColumn = columnPos;
416             }
417         }
418
419         return maxColumn;
420     } // end getColumnCount()
421

422     /**
423      * Builds a PortletBean object from a specified portletElement node in the
424      * DOM tree.
425      * Warning : no verification for the positioning is done here !
426      * @param portletElement DOM Element of the portlet node
427      * @param
428      */

429     private PortletBean xmlPortletToPortletBean(Element JavaDoc portletElement)
430     throws JahiaException {
431         String JavaDoc idStr = getAttributeValue(portletElement, PARAMETER_TAG_ID_ATTRIBUTE);
432         PortletBean thePortlet = new PortletBean(Integer.parseInt(idStr),
433                                                  Integer.parseInt(getPortletParameterValue(portletElement, "source_id")),
434                                                  Integer.parseInt(getPortletParameterValue(portletElement, "w")),
435                                                  Integer.parseInt(getPortletParameterValue(portletElement, "h")),
436                                                  Integer.parseInt(getPortletParameterValue(portletElement, "column_position")),
437                                                  Integer.parseInt(getPortletParameterValue(portletElement, "row_position")));
438         return thePortlet;
439     }
440
441     /**
442      * Retrieves a portle bean from the DOM tree
443      * @param thePortletID id of the portlet
444      */

445     public PortletBean getPortlet(int thePortletID) throws JahiaException {
446
447         PortletBean thePortlet = null;
448         Element JavaDoc plmlElement = findPlml(rootDocument.getDocumentElement());
449         Element JavaDoc portletGroupElement = findPortletGroup(plmlElement, thePageID);
450         Element JavaDoc portletElement = findPortlet(portletGroupElement, thePortletID);
451         thePortlet = xmlPortletToPortletBean(portletElement);
452         return thePortlet;
453
454     } // end getPortlet()
455

456
457     /**
458      * Retrieves a list of portlet settings
459      *
460      * @author J�r�me B�dat
461      *
462      */

463     public Enumeration JavaDoc getPortlets() throws JahiaException {
464
465         thePortletList = new Vector JavaDoc();
466
467         Element JavaDoc plmlElement = findPlml(rootDocument.getDocumentElement());
468         Element JavaDoc portletGroupElement = findPortletGroup(plmlElement, thePageID);
469         NodeList JavaDoc portletNodeList = portletGroupElement.getElementsByTagName(PORTLET_TAG);
470         for (int i = 0; i < portletNodeList.getLength(); i++) {
471             Element JavaDoc portletElement = (Element JavaDoc) portletNodeList.item(i);
472             PortletBean portletBean = xmlPortletToPortletBean(portletElement);
473             thePortletList.add(portletBean);
474         }
475
476         return thePortletList.elements();
477     } // end getPortletList()
478

479
480
481     /**
482      * Retrieves a list of portlets for the specified column
483      *
484      * @todo this should probably be moved to the portlets manager
485      *
486      */

487     public Enumeration JavaDoc getPortletsFromColumn(int theColumn) throws JahiaException {
488
489         Enumeration JavaDoc allPortlets = getPortlets();
490         Vector JavaDoc portletList = new Vector JavaDoc();
491         while (allPortlets.hasMoreElements()) {
492             PortletBean portlet = (PortletBean) allPortlets.nextElement();
493             if (portlet.getPortletColumn() == theColumn) {
494                 portletList.add(portlet);
495             }
496         }
497
498         return thePortletList.elements();
499     } // end getPortletsFromColumn()
500

501     /**
502      * Stores a portlet parameter. If the parameter does not exist it is created
503      * and used for storage. This allows for dynamic creation of new objects,
504      * with just a small performance overhead.
505      *
506      * @param parameterName name of the parameter to set
507      * @param parameterValue value to set for the parameter
508      * @param portletElement the parent portlet element in which to store the
509      * parameter value
510      */

511     private void storePortletParameter(String JavaDoc parameterName,
512                                        String JavaDoc parameterValue,
513                                        Element JavaDoc portletElement)
514     throws JahiaException{
515         NodeList JavaDoc parameterList = portletElement.getElementsByTagName(PORTLET_PARAMETER_TAG);
516         Element JavaDoc paramElement = null;
517         boolean foundParam = false;
518         for (int i = 0; i < parameterList.getLength(); i++) {
519             paramElement = (Element JavaDoc) parameterList.item(i);
520             String JavaDoc paramNameValue = paramElement.getAttribute(PARAMETER_TAG_NAME_ATTRIBUTE);
521             if (paramNameValue != null) {
522                 if (paramNameValue.equalsIgnoreCase(parameterName)) {
523                     foundParam = true;
524                     break;
525                 }
526             }
527         }
528         if (!foundParam) {
529             // parameter tag was not found, let's create it.
530
Element JavaDoc parameterElement = rootDocument.createElement(PORTLET_PARAMETER_TAG);
531             parameterElement.setAttribute(PARAMETER_TAG_NAME_ATTRIBUTE, parameterName);
532             portletElement.appendChild(parameterElement);
533             Text JavaDoc parameterValueElement = rootDocument.createTextNode("");
534             parameterElement.appendChild(parameterValueElement);
535             paramElement = parameterElement;
536         }
537         Node JavaDoc textNode = paramElement.getFirstChild();
538         if (textNode.getNodeType() == Node.TEXT_NODE) {
539             Text JavaDoc paramValueText = (Text JavaDoc) textNode;
540             paramValueText.setData(parameterValue);
541         } else {
542             throw new JahiaException("PortletsXMLSerializer",
543                                      "Invalid parameter value in DOM tree !",
544                                      JahiaException.CRITICAL_SEVERITY,
545                                      JahiaException.DATA_ERROR);
546         }
547     }
548
549     /**
550      * Serializes a PortletBean object to a DOM tree. The necessary update or
551      * creation calls are made to make sure that the necessary ressources are
552      * created if this is a new portlet.
553      */

554     public void storePortlet(PortletBean portlet,
555                              String JavaDoc currentUserName)
556     throws JahiaException {
557         Element JavaDoc plmlElement = findPlml(rootDocument.getDocumentElement());
558         Element JavaDoc portletGroupElement = findPortletGroup(plmlElement, thePageID);
559         Element JavaDoc portletElement = null;
560         try {
561             portletElement = findPortlet(portletGroupElement, portlet.getPortletID());
562         } catch (JahiaException je) {
563             // the portlet does not yet exist in the DOM tree, let's create it.
564
Element JavaDoc newPortletElement = rootDocument.createElement(PORTLET_TAG);
565             newPortletElement.setAttribute("id", Integer.toString(update_count("portlet")));
566             portletGroupElement.appendChild(newPortletElement);
567             portletElement = newPortletElement; // we will use this new element from now on
568
}
569
570         storePortletParameter("source_id", Integer.toString(portlet.getPortletSourceID()), portletElement);
571         storePortletParameter("w", Integer.toString(portlet.getPortletW()), portletElement);
572         storePortletParameter("h", Integer.toString(portlet.getPortletH()), portletElement);
573         storePortletParameter("column_position", Integer.toString(portlet.getPortletColumn()), portletElement);
574         storePortletParameter("row_position", Integer.toString(portlet.getPortletRow()), portletElement);
575
576         // commit the changes to the XML file
577
saveFile(theFile.toString());
578
579     }
580
581     /**
582      * delete a portlet by specifying it's source ID parameter value
583      */

584     public void deletePortlet(int theSourceID)
585     throws JahiaException {
586         Element JavaDoc plmlElement = findPlml (rootDocument.getDocumentElement());
587         Element JavaDoc portletGroupElement = findPortletGroup(plmlElement, thePageID);
588         NodeList JavaDoc portletList = portletGroupElement.getElementsByTagName(PORTLET_TAG);
589         for (int i = 0; i < portletList.getLength(); i++) {
590             Element JavaDoc portletElement = (Element JavaDoc) portletList.item(i);
591             String JavaDoc sourceIDStr = getPortletParameterValue(portletElement, "source_id");
592             int sourceID = Integer.parseInt(sourceIDStr);
593             if (sourceID == theSourceID) {
594                 portletGroupElement.removeChild(portletElement);
595                 return;
596             }
597         }
598
599         // commit the changes to the XML file
600
saveFile(theFile.toString());
601
602     } // end deletePortlet
603

604     /**
605      * Add new Portlet Group to the Portlets XML file
606      * @param groupID the new group ID to insert
607      */

608     public void addPortletGroup(int groupID)
609     throws JahiaException {
610
611         JahiaConsole.println("PortletsXMLSerializer.addPortletGroup", "groupID = " + Integer.toString(groupID) );
612
613         if (addPortletGroup) { return; } // small optimization
614

615         JahiaConsole.println("PortletsXMLSerializer.addPortletGroup", "Finding PLML group " + Integer.toString(groupID) );
616
617         try {
618
619             Element JavaDoc plmlElement = findPlml(rootDocument.getDocumentElement());
620
621             Element JavaDoc newPortletGroupElement = rootDocument.createElement(PORTLETLIST_TAG);
622             newPortletGroupElement.setAttribute( PARAMETER_TAG_ID_ATTRIBUTE,
623                                                  Integer.toString(update_count("portletgroup")));
624             plmlElement.appendChild(newPortletGroupElement);
625
626             Element JavaDoc groupParamElement = rootDocument.createElement(GROUP_PARAMETER_TAG);
627             groupParamElement.setAttribute(PARAMETER_TAG_NAME_ATTRIBUTE, "page_id");
628             newPortletGroupElement.appendChild(groupParamElement);
629
630             Text JavaDoc groupParamText = rootDocument.createTextNode(Integer.toString(groupID));
631             groupParamElement.appendChild(groupParamText);
632
633         } catch (Throwable JavaDoc t) {
634             t.printStackTrace();
635             throw new JahiaException("PortletsXMLSerializer.addPortletGroup",
636                                      "Error while adding portlet group !",
637                                      JahiaException.CRITICAL_SEVERITY,
638                                      JahiaException.DATA_ERROR);
639
640         }
641
642         // commit the changes to the XML file
643
saveFile(theFile.toString());
644
645     } // end addPortletGroup
646

647     /**
648      * Update portlet autoid or portletgroup autoid
649      *
650      * @author J�r�me B�dat
651      *
652      */

653     public int update_count(String JavaDoc theType)
654     throws JahiaException
655     {
656         int thePortletsCount = 0;
657         int theDesktopsCount = 0;
658         try {
659
660             if (theType.equals("portlet")) {
661                 thePortletsCount = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahiatemplates_portlet" );
662             } else {
663                 theDesktopsCount = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement( "jahiatemplates_portletgroup" );
664             }
665
666         } catch ( Exception JavaDoc se ) {
667             String JavaDoc errorMsg = "Error in update_count : " + se.getMessage();
668             JahiaConsole.println( "JahiaDBManager", errorMsg + " -> BAILING OUT" );
669             throw new JahiaException( "Cannot update count data to the database",
670                                         errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY );
671         }
672         if (theType.equals("portlet")) {
673             return thePortletsCount;
674         } else {
675             return theDesktopsCount;
676         }
677
678     } // end update_count
679

680 }
681
Popular Tags