KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > contenttool > actions > ViewFormEditorAction


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.contenttool.actions;
25
26 import java.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.apache.xerces.parsers.DOMParser;
32 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
33 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController;
34 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController;
35 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
36 import org.infoglue.cms.io.FileHelper;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41
42
43 /**
44  * This class implements the action class for the new Form Editor.
45  *
46  * @author Mattias Bogeblad
47  */

48
49 public class ViewFormEditorAction extends InfoGlueAbstractAction //extends ViewContentTypeDefinitionAction
50
{
51     private final static Logger logger = Logger.getLogger(ViewFormEditorAction.class.getName());
52
53     private static final long serialVersionUID = 1L;
54     
55     private Integer JavaDoc contentVersionId;
56     private String JavaDoc contentVersionAttributeName;
57     private String JavaDoc attributeName;
58     private String JavaDoc formDefinition;
59     private List JavaDoc attributes;
60     
61     private String JavaDoc inputTypeId;
62     private String JavaDoc newAttributeParameterValueId;
63     private String JavaDoc currentContentTypeEditorViewLanguageCode;
64     private String JavaDoc attributeParameterValueLabel;
65     private String JavaDoc attributeParameterId;
66     private String JavaDoc attributeParameterValueId;
67     private String JavaDoc newAttributeName;
68     private String JavaDoc attributeParameterValueLocale;
69     private String JavaDoc attributeToExpand;
70
71     private List JavaDoc availableLanguages = null;
72
73     
74     public ViewFormEditorAction()
75     {
76     }
77         
78     protected void initialize() throws Exception JavaDoc
79     {
80         this.formDefinition = ContentVersionController.getContentVersionController().getAttributeValue(getContentVersionId(), getContentVersionAttributeName(), false);
81         logger.info("this.formDefinition:" + this.formDefinition);
82         
83         boolean isFormDefinitionValid = true;
84         try
85         {
86             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(this.formDefinition));
87             
88             DOMParser parser = new DOMParser();
89             parser.parse(xmlSource);
90             Document JavaDoc document = parser.getDocument();
91         }
92         catch(Exception JavaDoc e)
93         {
94             isFormDefinitionValid = false;
95         }
96         
97         if(this.formDefinition == null || this.formDefinition.equals("") || isFormDefinitionValid == false)
98         {
99             logger.info("Trying to get the default definition...");
100             String JavaDoc schemaValue = "";
101             try
102             {
103                 String JavaDoc newFormDefinition = FileHelper.getStreamAsString(this.getClass().getResourceAsStream("/org/infoglue/cms/applications/defaultContentTypeDefinition.xml"));
104                 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), newFormDefinition, this.getInfoGluePrincipal());
105                 this.formDefinition = ContentVersionController.getContentVersionController().getAttributeValue(getContentVersionId(), getContentVersionAttributeName(), false);
106             }
107             catch(Exception JavaDoc e)
108             {
109                 logger.error("The system could not find the default content type definition:" + e.getMessage(), e);
110             }
111         }
112         
113         this.attributes = ContentTypeDefinitionController.getController().getContentTypeAttributes(formDefinition);
114         this.availableLanguages = LanguageController.getController().getLanguageVOList();
115     }
116
117     /**
118      * The main method that just initializes the editor with the correct contentVersion and attribute.
119      */

120     
121     public String JavaDoc doExecute() throws Exception JavaDoc
122     {
123         this.initialize();
124         return "success";
125     }
126             
127     public String JavaDoc doInsertAttribute() throws Exception JavaDoc
128     {
129         this.initialize();
130         String JavaDoc newFormDefinition = ContentTypeDefinitionController.getController().insertContentTypeAttribute(this.formDefinition, this.inputTypeId, new ArrayList JavaDoc());
131         ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), newFormDefinition, this.getInfoGluePrincipal());
132                 
133         this.initialize();
134         
135         return "success";
136     }
137     
138     
139     public String JavaDoc doDeleteAttribute() throws Exception JavaDoc
140     {
141         this.initialize();
142         
143         try
144         {
145             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(this.formDefinition));
146             
147             DOMParser parser = new DOMParser();
148             parser.parse(xmlSource);
149             Document JavaDoc document = parser.getDocument();
150             
151             String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']";
152             NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
153             if(anl != null && anl.getLength() > 0)
154             {
155                 Element JavaDoc element = (Element JavaDoc)anl.item(0);
156                 element.getParentNode().removeChild(element);
157             }
158             
159             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
160             org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
161             
162             ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal());
163         }
164         catch(Exception JavaDoc e)
165         {
166             e.printStackTrace();
167         }
168         
169         this.initialize();
170         
171         return "success";
172     }
173
174     /**
175      * This method moves an content type attribute up one step.
176      */

177     
178     public String JavaDoc doMoveAttributeUp() throws Exception JavaDoc
179     {
180         this.initialize();
181         
182         try
183         {
184             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(this.formDefinition));
185             
186             DOMParser parser = new DOMParser();
187             parser.parse(xmlSource);
188             Document JavaDoc document = parser.getDocument();
189             
190             String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element";
191             NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
192             Element JavaDoc previousElement = null;
193             for(int i=0; i < anl.getLength(); i++)
194             {
195                 Element JavaDoc element = (Element JavaDoc)anl.item(i);
196                 if(element.getAttribute("name").equalsIgnoreCase(this.attributeName) && previousElement != null)
197                 {
198                     Element JavaDoc parent = (Element JavaDoc)element.getParentNode();
199                     parent.removeChild(element);
200                     parent.insertBefore(element, previousElement);
201                 }
202                 previousElement = element;
203             }
204             
205             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
206             org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
207
208             ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal());
209         }
210         catch(Exception JavaDoc e)
211         {
212             e.printStackTrace();
213         }
214         
215         this.initialize();
216         return "success";
217     }
218
219
220     /**
221      * This method moves an content type attribute down one step.
222      */

223     
224     public String JavaDoc doMoveAttributeDown() throws Exception JavaDoc
225     {
226         this.initialize();
227         
228         try
229         {
230             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(this.formDefinition));
231             
232             DOMParser parser = new DOMParser();
233             parser.parse(xmlSource);
234             Document JavaDoc document = parser.getDocument();
235                                     
236             String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element";
237             NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
238             Element JavaDoc parent = null;
239             Element JavaDoc elementToMove = null;
240             boolean isInserted = false;
241             int position = 0;
242             for(int i=0; i < anl.getLength(); i++)
243             {
244                 Element JavaDoc element = (Element JavaDoc)anl.item(i);
245                 parent = (Element JavaDoc)element.getParentNode();
246                     
247                 if(elementToMove != null)
248                 {
249                     if(position == 2)
250                     {
251                         parent.insertBefore(elementToMove, element);
252                         isInserted = true;
253                         break;
254                     }
255                     else
256                         position++;
257                 }
258                 
259                 if(element.getAttribute("name").equalsIgnoreCase(this.attributeName))
260                 {
261                     elementToMove = element;
262                     parent.removeChild(elementToMove);
263                     position++;
264                 }
265             }
266             
267             if(!isInserted && elementToMove != null)
268                 parent.appendChild(elementToMove);
269                 
270             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
271             org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
272
273             ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal());
274         }
275         catch(Exception JavaDoc e)
276         {
277             e.printStackTrace();
278         }
279         
280         this.initialize();
281
282         return "success";
283     }
284
285
286     public String JavaDoc doUpdateAttribute() throws Exception JavaDoc
287     {
288         this.initialize();
289         
290         try
291         {
292             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(this.formDefinition));
293             
294             DOMParser parser = new DOMParser();
295             parser.parse(xmlSource);
296             Document JavaDoc document = parser.getDocument();
297             
298             //Updating the content attribute
299
String JavaDoc[] extraParameterNames = getRequest().getParameterValues("parameterNames");
300             if(extraParameterNames != null)
301             {
302                 for(int i=0; i < extraParameterNames.length; i++)
303                 {
304                     String JavaDoc extraParameterName = extraParameterNames[i];
305                     String JavaDoc value = getRequest().getParameter(extraParameterName);
306             
307                     String JavaDoc extraParametersXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']/xs:annotation/xs:appinfo/params/param[@id='" + extraParameterName +"']/values/value";
308                     NodeList JavaDoc extraParamsNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), extraParametersXPath);
309                     if(extraParamsNodeList != null && extraParamsNodeList.getLength() > 0)
310                     {
311                         Element JavaDoc element = (Element JavaDoc)extraParamsNodeList.item(0);
312                         
313                         if(extraParameterName.equalsIgnoreCase("values") && (this.inputTypeId.equalsIgnoreCase("select") || this.inputTypeId.equalsIgnoreCase("checkbox") || this.inputTypeId.equalsIgnoreCase("radiobutton")))
314                         {
315                             ((Element JavaDoc)element.getParentNode().getParentNode()).setAttribute("inputTypeId", "1");
316                         }
317                         else
318                         {
319                             ((Element JavaDoc)element.getParentNode().getParentNode()).setAttribute("inputTypeId", "0");
320                         }
321                         
322                         if(((Element JavaDoc)element.getParentNode().getParentNode()).getAttribute("inputTypeId").equals("0"))
323                         {
324                             if(this.currentContentTypeEditorViewLanguageCode != null && this.currentContentTypeEditorViewLanguageCode.length() > 0)
325                             {
326                                 element.setAttribute("label_" + this.currentContentTypeEditorViewLanguageCode, value);
327                             }
328                             else
329                             {
330                                 element.setAttribute("label", value);
331                             }
332                         }
333                     }
334                 }
335             }
336
337             //Updating the name and type
338
String JavaDoc attributeXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']";
339             NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributeXPath);
340             if(anl != null && anl.getLength() > 0)
341             {
342                 Element JavaDoc element = (Element JavaDoc)anl.item(0);
343                 element.setAttribute("name", this.newAttributeName);
344                 element.setAttribute("type", this.inputTypeId);
345             }
346         
347             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
348             org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
349
350             ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal());
351         }
352         catch(Exception JavaDoc e)
353         {
354             e.printStackTrace();
355         }
356         
357         this.initialize();
358         
359         return "success";
360     }
361
362
363     public String JavaDoc doInsertAttributeParameterValue() throws Exception JavaDoc
364     {
365         this.initialize();
366         
367         try
368         {
369             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(this.formDefinition));
370             
371             DOMParser parser = new DOMParser();
372             parser.parse(xmlSource);
373             Document JavaDoc document = parser.getDocument();
374             
375             String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']/xs:annotation/xs:appinfo/params/param[@id='" + this.attributeParameterId +"']/values";
376             NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
377             if(anl != null && anl.getLength() > 0)
378             {
379                 Element JavaDoc element = (Element JavaDoc)anl.item(0);
380                 Element JavaDoc newValue = document.createElement("value");
381                 newValue.setAttribute("id", "undefined" + (int)(Math.random() * 100));
382                 newValue.setAttribute("label", "undefined" + (int)(Math.random() * 100));
383                 element.appendChild(newValue);
384             }
385             
386             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
387             org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
388
389             ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal());
390         }
391         catch(Exception JavaDoc e)
392         {
393             e.printStackTrace();
394         }
395         
396         this.initialize();
397         
398         return "success";
399     }
400
401
402     public String JavaDoc doDeleteAttributeParameterValue() throws Exception JavaDoc
403     {
404         this.initialize();
405         
406         try
407         {
408             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(this.formDefinition));
409             
410             DOMParser parser = new DOMParser();
411             parser.parse(xmlSource);
412             Document JavaDoc document = parser.getDocument();
413             
414             String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']/xs:annotation/xs:appinfo/params/param[@id='" + this.attributeParameterId +"']/values/value[@id='" + this.attributeParameterValueId + "']";
415             NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
416             if(anl != null && anl.getLength() > 0)
417             {
418                 Element JavaDoc element = (Element JavaDoc)anl.item(0);
419                 element.getParentNode().removeChild(element);
420             }
421             
422             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
423             org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
424
425             ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal());
426         }
427         catch(Exception JavaDoc e)
428         {
429             e.printStackTrace();
430         }
431         
432         this.initialize();
433         
434         return "success";
435     }
436
437
438
439     public String JavaDoc doUpdateAttributeParameterValue() throws Exception JavaDoc
440     {
441         this.initialize();
442         
443         try
444         {
445             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(this.formDefinition));
446             
447             DOMParser parser = new DOMParser();
448             parser.parse(xmlSource);
449             Document JavaDoc document = parser.getDocument();
450             
451             String JavaDoc parameterValueXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']/xs:annotation/xs:appinfo/params/param[@id='" + this.attributeParameterId +"']/values/value[@id='" + this.attributeParameterValueId + "']";
452             NodeList JavaDoc parameterValuesNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), parameterValueXPath);
453             if(parameterValuesNodeList != null && parameterValuesNodeList.getLength() > 0)
454             {
455                 Element JavaDoc element = (Element JavaDoc)parameterValuesNodeList.item(0);
456                 element.setAttribute("id", this.newAttributeParameterValueId);
457                 
458                 if(this.currentContentTypeEditorViewLanguageCode != null && this.currentContentTypeEditorViewLanguageCode.length() > 0)
459                     element.setAttribute("label_" + this.currentContentTypeEditorViewLanguageCode, this.attributeParameterValueLabel);
460                 else
461                     element.setAttribute("label", this.attributeParameterValueLabel);
462             }
463         
464             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
465             org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
466
467             ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal());
468         }
469         catch(Exception JavaDoc e)
470         {
471             e.printStackTrace();
472         }
473         
474         this.initialize();
475
476         return "success";
477     }
478
479     public List JavaDoc getAvailableLanguages()
480     {
481         return this.availableLanguages;
482     }
483     
484     public String JavaDoc getAttributeName()
485     {
486         return this.attributeName;
487     }
488
489     public Integer JavaDoc getContentVersionId()
490     {
491         return this.contentVersionId;
492     }
493
494     public String JavaDoc getFormDefinition()
495     {
496         return this.formDefinition;
497     }
498
499     public void setAttributeName(String JavaDoc attributeName)
500     {
501         this.attributeName = attributeName;
502     }
503
504     public void setContentVersionId(Integer JavaDoc contentVersionId)
505     {
506         this.contentVersionId = contentVersionId;
507     }
508
509     public void setFormDefinition(String JavaDoc formDefinition)
510     {
511         this.formDefinition = formDefinition;
512     }
513
514     public List JavaDoc getAttributes()
515     {
516         return attributes;
517     }
518
519     public void setAttributes(List JavaDoc attributes)
520     {
521         this.attributes = attributes;
522     }
523
524     public String JavaDoc getInputTypeId()
525     {
526         return this.inputTypeId;
527     }
528
529     public void setInputTypeId(String JavaDoc inputTypeId)
530     {
531         this.inputTypeId = inputTypeId;
532     }
533
534     public String JavaDoc getContentVersionAttributeName()
535     {
536         return this.contentVersionAttributeName;
537     }
538
539     public void setContentVersionAttributeName(String JavaDoc contentVersionAttributeName)
540     {
541         this.contentVersionAttributeName = contentVersionAttributeName;
542     }
543
544     public String JavaDoc getAttributeParameterId()
545     {
546         return this.attributeParameterId;
547     }
548
549     public String JavaDoc getAttributeParameterValueId()
550     {
551         return this.attributeParameterValueId;
552     }
553
554     public String JavaDoc getAttributeParameterValueLabel()
555     {
556         return this.attributeParameterValueLabel;
557     }
558
559     public String JavaDoc getAttributeParameterValueLocale()
560     {
561         return this.attributeParameterValueLocale;
562     }
563
564     public String JavaDoc getAttributeToExpand()
565     {
566         return this.attributeToExpand;
567     }
568
569     public String JavaDoc getCurrentContentTypeEditorViewLanguageCode()
570     {
571         return this.currentContentTypeEditorViewLanguageCode;
572     }
573
574     public String JavaDoc getNewAttributeName()
575     {
576         return this.newAttributeName;
577     }
578
579     public String JavaDoc getNewAttributeParameterValueId()
580     {
581         return this.newAttributeParameterValueId;
582     }
583
584     public void setAttributeParameterId(String JavaDoc attributeParameterId)
585     {
586         this.attributeParameterId = attributeParameterId;
587     }
588
589     public void setAttributeParameterValueId(String JavaDoc attributeParameterValueId)
590     {
591         this.attributeParameterValueId = attributeParameterValueId;
592     }
593
594     public void setAttributeParameterValueLabel(String JavaDoc attributeParameterValueLabel)
595     {
596         this.attributeParameterValueLabel = attributeParameterValueLabel;
597     }
598
599     public void setAttributeParameterValueLocale(String JavaDoc attributeParameterValueLocale)
600     {
601         this.attributeParameterValueLocale = attributeParameterValueLocale;
602     }
603
604     public void setAttributeToExpand(String JavaDoc attributeToExpand)
605     {
606         this.attributeToExpand = attributeToExpand;
607     }
608
609     public void setCurrentContentTypeEditorViewLanguageCode(String JavaDoc currentContentTypeEditorViewLanguageCode)
610     {
611         this.currentContentTypeEditorViewLanguageCode = currentContentTypeEditorViewLanguageCode;
612     }
613
614     public void setNewAttributeName(String JavaDoc newAttributeName)
615     {
616         this.newAttributeName = newAttributeName;
617     }
618
619     public void setNewAttributeParameterValueId(String JavaDoc newAttributeParameterValueId)
620     {
621         this.newAttributeParameterValueId = newAttributeParameterValueId;
622     }
623
624 }
625
Popular Tags