KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > ContentTypeDefinitionController


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.controllers.kernel.impl.simple;
25
26 import java.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.xml.transform.TransformerException JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.apache.xerces.parsers.DOMParser;
37 import org.apache.xpath.XPathAPI;
38 import org.exolab.castor.jdo.Database;
39 import org.exolab.castor.jdo.OQLQuery;
40 import org.exolab.castor.jdo.QueryResults;
41 import org.infoglue.cms.applications.databeans.AssetKeyDefinition;
42 import org.infoglue.cms.entities.kernel.BaseEntityVO;
43 import org.infoglue.cms.entities.management.CategoryAttribute;
44 import org.infoglue.cms.entities.management.ContentTypeAttribute;
45 import org.infoglue.cms.entities.management.ContentTypeAttributeParameter;
46 import org.infoglue.cms.entities.management.ContentTypeAttributeParameterValue;
47 import org.infoglue.cms.entities.management.ContentTypeAttributeValidator;
48 import org.infoglue.cms.entities.management.ContentTypeDefinition;
49 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
50 import org.infoglue.cms.entities.management.impl.simple.ContentTypeDefinitionImpl;
51 import org.infoglue.cms.exception.Bug;
52 import org.infoglue.cms.exception.ConstraintException;
53 import org.infoglue.cms.exception.SystemException;
54 import org.infoglue.cms.security.InfoGluePrincipal;
55 import org.infoglue.deliver.util.NullObject;
56 import org.infoglue.deliver.util.CacheController;
57 import org.w3c.dom.Document JavaDoc;
58 import org.w3c.dom.Element JavaDoc;
59 import org.w3c.dom.NamedNodeMap JavaDoc;
60 import org.w3c.dom.Node JavaDoc;
61 import org.w3c.dom.NodeList JavaDoc;
62 import org.xml.sax.InputSource JavaDoc;
63
64 /**
65  * @author ss
66  *
67  * To change this generated comment edit the template variable "typecomment":
68  * Window>Preferences>Java>Templates.
69  * To enable and disable the creation of type comments go to
70  * Window>Preferences>Java>Code Generation.
71  */

72 public class ContentTypeDefinitionController extends BaseController
73 {
74     private final static Logger logger = Logger.getLogger(ContentTypeDefinitionController.class.getName());
75
76     public static final String JavaDoc ASSET_KEYS = "assetKeys";
77     public static final String JavaDoc CATEGORY_KEYS = "categoryKeys";
78
79     private static final NodeList JavaDoc EMPTY_NODELIST = new NodeList JavaDoc()
80     {
81         public int getLength() { return 0; }
82         public Node JavaDoc item(int i) { return null; }
83     };
84
85     /**
86      * Factory method
87      */

88
89     public static ContentTypeDefinitionController getController()
90     {
91         return new ContentTypeDefinitionController();
92     }
93
94     public ContentTypeDefinitionVO getContentTypeDefinitionVOWithId(Integer JavaDoc contentTypeDefinitionId) throws SystemException, Bug
95     {
96         return (ContentTypeDefinitionVO) getVOWithId(ContentTypeDefinitionImpl.class, contentTypeDefinitionId);
97     }
98
99     /*
100     public static ContentTypeDefinition getContentTypeDefinitionWithId(Integer contentTypeDefinitionId) throws SystemException, Bug
101     {
102         return (ContentTypeDefinition) getObjectWithId(ContentTypeDefinitionImpl.class, contentTypeDefinitionId);
103     }
104     */

105
106     public ContentTypeDefinition getContentTypeDefinitionWithId(Integer JavaDoc contentTypeDefinitionId, Database db) throws SystemException, Bug
107     {
108         return (ContentTypeDefinition) getObjectWithId(ContentTypeDefinitionImpl.class, contentTypeDefinitionId, db);
109     }
110
111     public List JavaDoc getContentTypeDefinitionVOList() throws SystemException, Bug
112     {
113         String JavaDoc key = "contentTypeDefinitionVOList";
114         logger.info("key:" + key);
115         List JavaDoc cachedContentTypeDefinitionVOList = (List JavaDoc)CacheController.getCachedObject("contentTypeDefinitionCache", key);
116         if(cachedContentTypeDefinitionVOList != null)
117         {
118             logger.info("There was an cached contentTypeDefinitionVOList:" + cachedContentTypeDefinitionVOList.size());
119             return cachedContentTypeDefinitionVOList;
120         }
121
122         List JavaDoc contentTypeDefinitionVOList = getAllVOObjects(ContentTypeDefinitionImpl.class, "contentTypeDefinitionId");
123
124         CacheController.cacheObject("contentTypeDefinitionCache", key, contentTypeDefinitionVOList);
125
126         return contentTypeDefinitionVOList;
127     }
128
129     public List JavaDoc getContentTypeDefinitionVOList(Database db) throws SystemException, Bug
130     {
131         return getAllVOObjects(ContentTypeDefinitionImpl.class, "contentTypeDefinitionId", db);
132     }
133
134     public List JavaDoc getContentTypeDefinitionList(Database db) throws SystemException, Bug
135     {
136         return getAllObjects(ContentTypeDefinitionImpl.class, "contentTypeDefinitionId", db);
137     }
138
139     /**
140      * This method can be used by actions and use-case-controllers that only need to have simple access to the
141      * functionality. They don't get the transaction-safety but probably just wants to show the info.
142      */

143     
144     public List JavaDoc getAuthorizedContentTypeDefinitionVOList(InfoGluePrincipal infoGluePrincipal) throws ConstraintException, SystemException, Bug
145     {
146         List JavaDoc accessableContentTypeDefinitionVOList = new ArrayList JavaDoc();
147         
148         List JavaDoc allContentTypeDefinitionVOList = this.getContentTypeDefinitionVOList();
149         Iterator JavaDoc i = allContentTypeDefinitionVOList.iterator();
150         while(i.hasNext())
151         {
152             ContentTypeDefinitionVO contentTypeDefinitionVO = (ContentTypeDefinitionVO)i.next();
153             if(getIsAccessApproved(contentTypeDefinitionVO.getId(), infoGluePrincipal))
154                 accessableContentTypeDefinitionVOList.add(contentTypeDefinitionVO);
155         }
156         
157         return accessableContentTypeDefinitionVOList;
158     }
159     
160     /**
161      * This method returns true if the user should have access to the contentTypeDefinition sent in.
162      */

163     
164     public boolean getIsAccessApproved(Integer JavaDoc contentTypeDefinitionId, InfoGluePrincipal infoGluePrincipal) throws SystemException
165     {
166         logger.info("getIsAccessApproved for " + contentTypeDefinitionId + " AND " + infoGluePrincipal);
167         boolean hasAccess = false;
168         
169         Database db = CastorDatabaseService.getDatabase();
170        
171         beginTransaction(db);
172
173         try
174         {
175             hasAccess = AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "ContentTypeDefinition.Read", contentTypeDefinitionId.toString());
176         
177             commitTransaction(db);
178         }
179         catch(Exception JavaDoc e)
180         {
181             logger.error("An error occurred so we should not complete the transaction:" + e, e);
182             rollbackTransaction(db);
183             throw new SystemException(e.getMessage());
184         }
185     
186         return hasAccess;
187     }
188     
189     /**
190      * Returns the Content Type Definition with the given name.
191      *
192      * @param name
193      * @return
194      * @throws SystemException
195      * @throws Bug
196      */

197
198     public ContentTypeDefinitionVO getContentTypeDefinitionVOWithName(String JavaDoc name) throws SystemException, Bug
199     {
200         ContentTypeDefinitionVO contentTypeDefinitionVO = null;
201
202         Database db = CastorDatabaseService.getDatabase();
203
204         try
205         {
206             beginTransaction(db);
207
208             ContentTypeDefinition contentTypeDefinition = getContentTypeDefinitionWithName(name, db);
209             if(contentTypeDefinition != null)
210                 contentTypeDefinitionVO = contentTypeDefinition.getValueObject();
211
212             commitTransaction(db);
213         }
214         catch (Exception JavaDoc e)
215         {
216             logger.info("An error occurred so we should not complete the transaction:" + e);
217             rollbackTransaction(db);
218             throw new SystemException(e.getMessage());
219         }
220
221         return contentTypeDefinitionVO;
222     }
223
224     /**
225      * Returns the Content Type Definition with the given name fetched within a given transaction.
226      *
227      * @param name
228      * @param db
229      * @return
230      * @throws SystemException
231      * @throws Bug
232      */

233
234     public ContentTypeDefinition getContentTypeDefinitionWithName(String JavaDoc name, Database db) throws SystemException, Bug
235     {
236         ContentTypeDefinition contentTypeDefinition = null;
237
238         try
239         {
240             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.ContentTypeDefinitionImpl f WHERE f.name = $1");
241             oql.bind(name);
242
243             this.logger.info("Fetching entity in read/write mode" + name);
244             
245             QueryResults results = oql.execute();
246             if (results.hasMore())
247             {
248                 contentTypeDefinition = (ContentTypeDefinition)results.next();
249             }
250             
251             results.close();
252             oql.close();
253         }
254         catch(Exception JavaDoc e)
255         {
256             throw new SystemException("An error occurred when we tried to fetch a named ContentTypeDefinition. Reason:" + e.getMessage(), e);
257         }
258
259         return contentTypeDefinition;
260     }
261
262     
263     /**
264      * Returns the Content Type Definition with the given name fetched within a given transaction.
265      *
266      * @param name
267      * @param db
268      * @return
269      * @throws SystemException
270      * @throws Bug
271      */

272
273     public ContentTypeDefinitionVO getContentTypeDefinitionVOWithName(String JavaDoc name, Database db) throws SystemException, Bug
274     {
275         String JavaDoc key = "" + name;
276         logger.info("key:" + key);
277         ContentTypeDefinitionVO contentTypeDefinitionVO = (ContentTypeDefinitionVO)CacheController.getCachedObject("contentTypeDefinitionCache", key);
278         if(contentTypeDefinitionVO != null)
279         {
280             logger.info("There was an cached contentTypeDefinitionVO:" + contentTypeDefinitionVO);
281         }
282         else
283         {
284             logger.info("Refetching contentTypeDefinitionVO:" + contentTypeDefinitionVO);
285
286             try
287             {
288                 OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.ContentTypeDefinitionImpl f WHERE f.name = $1");
289                 oql.bind(name);
290     
291                 QueryResults results = oql.execute(Database.ReadOnly);
292                 if (results.hasMore())
293                 {
294                     ContentTypeDefinition contentTypeDefinition = (ContentTypeDefinition)results.next();
295                     contentTypeDefinitionVO = contentTypeDefinition.getValueObject();
296
297                     CacheController.cacheObject("contentTypeDefinitionCache", key, contentTypeDefinitionVO);
298                 }
299                 
300                 results.close();
301                 oql.close();
302             }
303             catch(Exception JavaDoc e)
304             {
305                 throw new SystemException("An error occurred when we tried to fetch a named ContentTypeDefinition. Reason:" + e.getMessage(), e);
306             }
307         }
308         
309         return contentTypeDefinitionVO;
310     }
311
312     
313     public List JavaDoc getContentTypeDefinitionVOList(Integer JavaDoc type) throws SystemException, Bug
314     {
315         List JavaDoc contentTypeDefinitionVOList = null;
316         Database db = CastorDatabaseService.getDatabase();
317
318         try
319         {
320             beginTransaction(db);
321
322             contentTypeDefinitionVOList = getContentTypeDefinitionVOList(type, db);
323
324             commitTransaction(db);
325         }
326         catch (Exception JavaDoc e)
327         {
328             logger.info("An error occurred so we should not complete the transaction:" + e);
329             rollbackTransaction(db);
330             throw new SystemException(e.getMessage());
331         }
332         return contentTypeDefinitionVOList;
333     }
334
335     public List JavaDoc getContentTypeDefinitionVOList(Integer JavaDoc type, Database db) throws SystemException, Bug
336     {
337         ArrayList JavaDoc contentTypeDefinitionVOList = new ArrayList JavaDoc();
338         try
339         {
340             OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.ContentTypeDefinitionImpl f WHERE f.type = $1");
341             oql.bind(type);
342
343             QueryResults results = oql.execute(Database.ReadOnly);
344             while (results.hasMore())
345             {
346                 ContentTypeDefinition contentTypeDefinition = (ContentTypeDefinition)results.next();
347                 contentTypeDefinitionVOList.add(contentTypeDefinition.getValueObject());
348             }
349             
350             results.close();
351             oql.close();
352         }
353         catch(Exception JavaDoc e)
354         {
355             throw new SystemException("An error occurred when we tried to fetch a list of Function. Reason:" + e.getMessage(), e);
356         }
357
358         return contentTypeDefinitionVOList;
359     }
360
361     public ContentTypeDefinitionVO create(ContentTypeDefinitionVO contentTypeDefinitionVO) throws ConstraintException, SystemException
362     {
363         ContentTypeDefinition contentTypeDefinition = new ContentTypeDefinitionImpl();
364         contentTypeDefinition.setValueObject(contentTypeDefinitionVO);
365         contentTypeDefinition = (ContentTypeDefinition) createEntity(contentTypeDefinition);
366         return contentTypeDefinition.getValueObject();
367     }
368
369     public void delete(ContentTypeDefinitionVO contentTypeDefinitionVO) throws ConstraintException, SystemException
370     {
371         deleteEntity(ContentTypeDefinitionImpl.class, contentTypeDefinitionVO.getContentTypeDefinitionId());
372     }
373
374     public ContentTypeDefinitionVO update(ContentTypeDefinitionVO contentTypeDefinitionVO) throws ConstraintException, SystemException
375     {
376         return (ContentTypeDefinitionVO) updateEntity(ContentTypeDefinitionImpl.class, contentTypeDefinitionVO);
377     }
378
379     /**
380      * This method fetches a predefined assetKeys from a xml-string representing a contentTypeDefinition.
381      */

382     public AssetKeyDefinition getDefinedAssetKey(String JavaDoc contentTypeDefinitionString, String JavaDoc assetKey)
383     {
384         AssetKeyDefinition assetKeyDefinition = null;
385         
386         List JavaDoc definedAssetKeys = getDefinedAssetKeys(contentTypeDefinitionString);
387         Iterator JavaDoc i = definedAssetKeys.iterator();
388         while(i.hasNext())
389         {
390             AssetKeyDefinition currentAssetKeyDefinition = (AssetKeyDefinition)i.next();
391             if(currentAssetKeyDefinition.getAssetKey().equals(assetKey))
392             {
393                 assetKeyDefinition = currentAssetKeyDefinition;
394                 break;
395             }
396         }
397         
398         return assetKeyDefinition;
399     }
400
401     /**
402      * This method fetches any predefined assetKeys from a xml-string representing a contentTypeDefinition.
403      */

404     public List JavaDoc getDefinedAssetKeys(String JavaDoc contentTypeDefinitionString)
405     {
406         NodeList JavaDoc nodes = getEnumerationNodeList(contentTypeDefinitionString, ASSET_KEYS);
407
408         return getEnumValues(nodes);
409     }
410
411
412     /**
413      * This method fetches any predefined categoryKeys from a xml-string representing a contentTypeDefinition.
414      */

415     public List JavaDoc getDefinedCategoryKeys(String JavaDoc contentTypeDefinitionString)
416     {
417         NodeList JavaDoc nodes = getEnumerationNodeList(contentTypeDefinitionString, CATEGORY_KEYS);
418         return getCategoryInfo(nodes);
419     }
420
421     /**
422      * Returns a List of values fro the "value" atribute of the provided NodeList
423      */

424     protected List JavaDoc getEnumValues(NodeList JavaDoc nodes)
425     {
426        List JavaDoc keys = new ArrayList JavaDoc();
427         for(int i = 0; i < nodes.getLength(); i++)
428         {
429             Node JavaDoc ichild = nodes.item(i);
430             
431             logger.info("ichild:" + ichild.getNodeName() + ":" + ichild.getNodeValue());
432             
433             try
434             {
435                 Node JavaDoc assetKeyValue = ichild.getAttributes().getNamedItem("value");
436
437                 Element JavaDoc params = (Element JavaDoc)XPathAPI.selectSingleNode(ichild, "xs:annotation/xs:appinfo/params");
438
439                 String JavaDoc descriptionValue = "";
440                 String JavaDoc maximumSizeValue = "1000000";
441                 String JavaDoc allowedContentTypesValue = "*";
442                 String JavaDoc imageWidthValue = "*";
443                 String JavaDoc imageHeightValue = "*";
444             
445                 if(params != null)
446                 {
447                     descriptionValue = getElementValue(params, "description");
448                     maximumSizeValue = getElementValue(params, "maximumSize");
449                     allowedContentTypesValue = getElementValue(params, "allowedContentTypes");
450                     imageWidthValue = getElementValue(params, "imageWidth");
451                     imageHeightValue = getElementValue(params, "imageHeight");
452                 }
453                 
454                 AssetKeyDefinition assetKeyDefinition = new AssetKeyDefinition();
455                 
456                 assetKeyDefinition.setAssetKey(assetKeyValue.getNodeValue());
457                 assetKeyDefinition.setDescription(descriptionValue);
458                 assetKeyDefinition.setMaximumSize(new Integer JavaDoc(maximumSizeValue));
459                 assetKeyDefinition.setAllowedContentTypes(allowedContentTypesValue);
460                 assetKeyDefinition.setImageWidth(imageWidthValue);
461                 assetKeyDefinition.setImageHeight(imageHeightValue);
462                 
463                 logger.info("Adding assetKeyDefinition " + assetKeyDefinition.getAssetKey());
464                 keys.add(assetKeyDefinition);
465             }
466             catch(Exception JavaDoc e)
467             {
468                 e.printStackTrace();
469             }
470         }
471         
472         logger.info("keys:" + keys.size());
473         
474         return keys;
475     }
476
477     /**
478      * Returns a List of CategoryInfos for the category atributes of the NodeList
479      */

480     protected List JavaDoc getCategoryInfo(NodeList JavaDoc nodes)
481     {
482         String JavaDoc attributesXPath = "xs:annotation/xs:appinfo/params";
483
484         List JavaDoc keys = new ArrayList JavaDoc();
485         for(int i = 0; i < nodes.getLength(); i++)
486         {
487             Node JavaDoc enumeration = nodes.item(i);
488             String JavaDoc value = enumeration.getAttributes().getNamedItem("value").getNodeValue();
489             try
490             {
491                 CategoryAttribute category = new CategoryAttribute(value);
492                 keys.add(category);
493
494                 Element JavaDoc params = (Element JavaDoc)XPathAPI.selectSingleNode(enumeration, attributesXPath);
495                 if(params != null)
496                 {
497                     category.setTitle(getElementValue(params, "title"));
498                     category.setDescription(getElementValue(params, "description"));
499                     category.setCategoryId(getElementValue(params, "categoryId"));
500                 }
501             }
502             catch (TransformerException JavaDoc e)
503             {
504                 keys.add(new CategoryAttribute(value));
505             }
506         }
507         return keys;
508     }
509
510     /**
511      * Returns a list of xs:enumeration nodes base on the provided key.
512      * @param keyType The key to find enumerations for
513      */

514     protected NodeList JavaDoc getEnumerationNodeList(String JavaDoc contentTypeDefinitionString, String JavaDoc keyType)
515     {
516         try
517         {
518             if(contentTypeDefinitionString != null)
519             {
520                 InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(contentTypeDefinitionString));
521
522                 DOMParser parser = new DOMParser();
523                 parser.parse(xmlSource);
524                 Document JavaDoc document = parser.getDocument();
525
526                 String JavaDoc attributesXPath = "/xs:schema/xs:simpleType[@name = '" + keyType + "']/xs:restriction/xs:enumeration";
527                 return XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
528             }
529         }
530         catch(Exception JavaDoc e)
531         {
532             logger.warn("An error occurred when trying to fetch the asset keys:" + e.getMessage(), e);
533         }
534
535         return EMPTY_NODELIST;
536     }
537
538     /**
539      * Get the CDATA value from the provided elements child tag
540      * @param root The root element to find the child tag
541      * @param tagName The tag name of the child to get the CDATA value
542      * @return The String CDATA or null if the tag does not exist or no value is set.
543      */

544     protected String JavaDoc getElementValue(Element JavaDoc root, String JavaDoc tagName)
545     {
546         NodeList JavaDoc nodes = root.getElementsByTagName(tagName);
547         if(nodes.getLength() > 0)
548         {
549             Node JavaDoc cdata = nodes.item(0).getFirstChild();
550             return (cdata != null)? cdata.getNodeValue() : null;
551         }
552
553         return null;
554     }
555
556     /**
557      * This method returns the attributes in the content type definition for generation.
558      */

559
560     public List JavaDoc getContentTypeAttributes(String JavaDoc schemaValue)
561     {
562         //List attributes = new ArrayList();
563

564         String JavaDoc key = "schemaValue_" + schemaValue.hashCode();
565         //System.out.println("key:" + key);
566
Object JavaDoc attributesCandidate = CacheController.getCachedObject("contentTypeDefinitionCache", key);
567         //System.out.println("propertyCandidate for key " + key + "=" + propertyCandidate);
568
List JavaDoc attributes = new ArrayList JavaDoc();
569             
570         if(attributesCandidate != null)
571         {
572             if(attributesCandidate instanceof NullObject)
573                 attributes = new ArrayList JavaDoc();
574             else
575                 attributes = (List JavaDoc)attributesCandidate;
576                 
577             //logger.info("Returning cached attributes for key " + key + "-" + attributes);
578
}
579         else
580         {
581             try
582             {
583                 InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(schemaValue));
584     
585                 DOMParser parser = new DOMParser();
586                 parser.parse(xmlSource);
587                 Document JavaDoc document = parser.getDocument();
588     
589                 String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element";
590                 NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
591                 for(int i=0; i < anl.getLength(); i++)
592                 {
593                     Element JavaDoc child = (Element JavaDoc)anl.item(i);
594                     String JavaDoc attributeName = child.getAttribute("name");
595                     String JavaDoc attributeType = child.getAttribute("type");
596     
597                     ContentTypeAttribute contentTypeAttribute = new ContentTypeAttribute();
598                     contentTypeAttribute.setPosition(i);
599                     contentTypeAttribute.setName(attributeName);
600                     contentTypeAttribute.setInputType(attributeType);
601     
602                     String JavaDoc validatorsXPath = "/xs:schema/xs:complexType[@name = 'Validation']/xs:annotation/xs:appinfo/form-validation/formset/form/field[@property = '"+ attributeName +"']";
603     
604                     // Get validators
605
NodeList JavaDoc validatorNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), validatorsXPath);
606                     for(int j=0; j < validatorNodeList.getLength(); j++)
607                     {
608                         Element JavaDoc validatorNode = (Element JavaDoc)validatorNodeList.item(j);
609                         if (validatorNode != null)
610                         {
611                             Map JavaDoc arguments = new HashMap JavaDoc();
612                             
613                             NodeList JavaDoc varNodeList = validatorNode.getElementsByTagName("var");
614                             for(int k=0; k < varNodeList.getLength(); k++)
615                             {
616                                 Element JavaDoc varNode = (Element JavaDoc)varNodeList.item(k);
617                                 
618                                 String JavaDoc varName = getElementValue(varNode, "var-name");
619                                 String JavaDoc varValue = getElementValue(varNode, "var-value");
620     
621                                 arguments.put(varName, varValue);
622                             }
623                             
624                             String JavaDoc attribute = ((Element JavaDoc)validatorNode).getAttribute("depends");
625                             String JavaDoc[] depends = attribute.split(",");
626                             for(int dependsIndex=0; dependsIndex < depends.length; dependsIndex++)
627                             {
628                                 String JavaDoc name = depends[dependsIndex];
629     
630                                 ContentTypeAttributeValidator contentTypeAttributeValidator = new ContentTypeAttributeValidator();
631                                 contentTypeAttributeValidator.setName(name);
632                                 contentTypeAttributeValidator.setArguments(arguments);
633                                 contentTypeAttribute.getValidators().add(contentTypeAttributeValidator);
634                             }
635                             
636                             
637                         }
638                     }
639                     
640                     // Get extra parameters
641
Node JavaDoc paramsNode = org.apache.xpath.XPathAPI.selectSingleNode(child, "xs:annotation/xs:appinfo/params");
642                     if (paramsNode != null)
643                     {
644                         NodeList JavaDoc childnl = ((Element JavaDoc)paramsNode).getElementsByTagName("param");
645                         for(int ci=0; ci < childnl.getLength(); ci++)
646                         {
647                             Element JavaDoc param = (Element JavaDoc)childnl.item(ci);
648                             String JavaDoc paramId = param.getAttribute("id");
649                             String JavaDoc paramInputTypeId = param.getAttribute("inputTypeId");
650     
651                             ContentTypeAttributeParameter contentTypeAttributeParameter = new ContentTypeAttributeParameter();
652                             contentTypeAttributeParameter.setId(paramId);
653                             if(paramInputTypeId != null && paramInputTypeId.length() > 0)
654                                 contentTypeAttributeParameter.setType(Integer.parseInt(paramInputTypeId));
655     
656                             contentTypeAttribute.putContentTypeAttributeParameter(paramId, contentTypeAttributeParameter);
657     
658                             NodeList JavaDoc valuesNodeList = param.getElementsByTagName("values");
659                             for(int vsnli=0; vsnli < valuesNodeList.getLength(); vsnli++)
660                             {
661                                 NodeList JavaDoc valueNodeList = param.getElementsByTagName("value");
662                                 for(int vnli=0; vnli < valueNodeList.getLength(); vnli++)
663                                 {
664                                     Element JavaDoc value = (Element JavaDoc)valueNodeList.item(vnli);
665                                     String JavaDoc valueId = value.getAttribute("id");
666     
667                                     ContentTypeAttributeParameterValue contentTypeAttributeParameterValue = new ContentTypeAttributeParameterValue();
668                                     contentTypeAttributeParameterValue.setId(valueId);
669     
670                                     NamedNodeMap JavaDoc nodeMap = value.getAttributes();
671                                     for(int nmi =0; nmi < nodeMap.getLength(); nmi++)
672                                     {
673                                         Node JavaDoc attribute = (Node JavaDoc)nodeMap.item(nmi);
674                                         String JavaDoc valueAttributeName = attribute.getNodeName();
675                                         String JavaDoc valueAttributeValue = attribute.getNodeValue();
676                                         contentTypeAttributeParameterValue.addAttribute(valueAttributeName, valueAttributeValue);
677                                     }
678     
679                                     contentTypeAttributeParameter.addContentTypeAttributeParameterValue(valueId, contentTypeAttributeParameterValue);
680                                 }
681                             }
682                         }
683                     }
684                     // End extra parameters
685

686                     attributes.add(contentTypeAttribute);
687                 }
688     
689             }
690             catch(Exception JavaDoc e)
691             {
692                 logger.error("An error occurred when we tried to get the attributes of the content type: " + e.getMessage(), e);
693             }
694         }
695
696         if(attributes != null)
697             CacheController.cacheObject("contentTypeDefinitionCache", key, attributes);
698         else
699             CacheController.cacheObject("contentTypeDefinitionCache", key, new NullObject());
700                 
701         return attributes;
702     }
703
704     /**
705      * This method adds a new content type attribute to the contentTypeDefinition. It sets some default values.
706      */

707
708     public String JavaDoc insertContentTypeAttribute(String JavaDoc schemaValue, String JavaDoc inputTypeId, List JavaDoc activatedName)
709     {
710         String JavaDoc newSchemaValue = schemaValue;
711
712         try
713         {
714             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(schemaValue));
715
716             DOMParser parser = new DOMParser();
717             parser.parse(xmlSource);
718             Document JavaDoc document = parser.getDocument();
719
720             //Build the entire structure for a contenttype...
721

722             String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all";
723             NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
724             for(int i=0; i < anl.getLength(); i++)
725             {
726                 Node JavaDoc child = anl.item(i);
727                 Element JavaDoc childElement = (Element JavaDoc)child;
728                 Element JavaDoc newAttribute = document.createElement("xs:element");
729                 String JavaDoc name = "newAttributeName" + (int)(Math.random() * 100);
730                 activatedName.add(name);
731                 newAttribute.setAttribute("name", name);
732                 newAttribute.setAttribute("type", inputTypeId);
733                 childElement.appendChild(newAttribute);
734
735                 Element JavaDoc annotation = document.createElement("xs:annotation");
736                 Element JavaDoc appInfo = document.createElement("xs:appinfo");
737                 Element JavaDoc params = document.createElement("params");
738
739                 addParameterElement(params, "title", "0");
740                 addParameterElement(params, "description", "0");
741                 addParameterElement(params, "initialData", "");
742                 addParameterElement(params, "class", "0");
743
744                 newAttribute.appendChild(annotation);
745                 annotation.appendChild(appInfo);
746                 appInfo.appendChild(params);
747
748                 if(inputTypeId.equalsIgnoreCase("checkbox") || inputTypeId.equalsIgnoreCase("select") || inputTypeId.equalsIgnoreCase("radiobutton"))
749                 {
750                     addParameterElement(params, "values", "1");
751                 }
752
753                 if(inputTypeId.equalsIgnoreCase("textarea"))
754                 {
755                     addParameterElement(params, "width", "0", "700");
756                     addParameterElement(params, "height", "0", "150");
757                     addParameterElement(params, "enableWYSIWYG", "0", "false");
758                     addParameterElement(params, "enableTemplateEditor", "0", "false");
759                     addParameterElement(params, "enableFormEditor", "0", "false");
760                     addParameterElement(params, "enableContentRelationEditor", "0", "false");
761                     addParameterElement(params, "enableStructureRelationEditor", "0", "false");
762                     addParameterElement(params, "enableComponentPropertiesEditor", "0", "false");
763                     addParameterElement(params, "activateExtendedEditorOnLoad", "0", "false");
764                 }
765             }
766
767             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
768             org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
769             newSchemaValue = sb.toString();
770         }
771         catch(Exception JavaDoc e)
772         {
773             e.printStackTrace();
774         }
775
776         return newSchemaValue;
777     }
778
779
780     /**
781      * This method creates a parameter for the given input type.
782      * This is to support form steering information later.
783      */

784
785     private void addParameterElement(Element JavaDoc parent, String JavaDoc id, String JavaDoc inputTypeId)
786     {
787         Element JavaDoc parameter = parent.getOwnerDocument().createElement("param");
788         parameter.setAttribute("id", id);
789         parameter.setAttribute("inputTypeId", inputTypeId); //Multiple values
790
Element JavaDoc parameterValuesValues = parent.getOwnerDocument().createElement("values");
791         Element JavaDoc parameterValuesValue = parent.getOwnerDocument().createElement("value");
792         parameterValuesValue.setAttribute("id", "undefined" + (int)(Math.random() * 100));
793         parameterValuesValue.setAttribute("label", "undefined" + (int)(Math.random() * 100));
794         parameterValuesValues.appendChild(parameterValuesValue);
795         parameter.appendChild(parameterValuesValues);
796         parent.appendChild(parameter);
797     }
798
799
800     /**
801      * This method creates a parameter for the given input type and the default value.
802      * This is to support form steering information later.
803      */

804
805     private void addParameterElement(Element JavaDoc parent, String JavaDoc id, String JavaDoc inputTypeId, String JavaDoc defaultValue)
806     {
807         Element JavaDoc parameter = parent.getOwnerDocument().createElement("param");
808         parameter.setAttribute("id", id);
809         parameter.setAttribute("inputTypeId", inputTypeId); //Multiple values
810
Element JavaDoc parameterValuesValues = parent.getOwnerDocument().createElement("values");
811         Element JavaDoc parameterValuesValue = parent.getOwnerDocument().createElement("value");
812         parameterValuesValue.setAttribute("id", id);
813         parameterValuesValue.setAttribute("label", defaultValue);
814         parameterValuesValues.appendChild(parameterValuesValue);
815         parameter.appendChild(parameterValuesValues);
816         parent.appendChild(parameter);
817     }
818
819
820     /**
821      * This method creates a parameter for the given input type and the default value.
822      * This is to support form steering information later.
823      */

824
825     private void addParameterElementIfNotExists(Element JavaDoc parent, String JavaDoc id, String JavaDoc inputTypeId, String JavaDoc defaultValue) throws Exception JavaDoc
826     {
827         String JavaDoc parameterXPath = "param[@id='" + id + "']";
828         NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(parent, parameterXPath);
829         if(anl.getLength() == 0)
830         {
831             Element JavaDoc parameter = parent.getOwnerDocument().createElement("param");
832             parameter.setAttribute("id", id);
833             parameter.setAttribute("inputTypeId", inputTypeId); //Multiple values
834
Element JavaDoc parameterValuesValues = parent.getOwnerDocument().createElement("values");
835             Element JavaDoc parameterValuesValue = parent.getOwnerDocument().createElement("value");
836             parameterValuesValue.setAttribute("id", id);
837             parameterValuesValue.setAttribute("label", defaultValue);
838             parameterValuesValues.appendChild(parameterValuesValue);
839             parameter.appendChild(parameterValuesValues);
840             parent.appendChild(parameter);
841
842         }
843     }
844
845     /**
846      * This method validates the current content type and updates it to be valid in the future.
847      */

848
849     public ContentTypeDefinitionVO validateAndUpdateContentType(ContentTypeDefinitionVO contentTypeDefinitionVO)
850     {
851         try
852         {
853             boolean isModified = false;
854
855             InputSource JavaDoc xmlSource = new InputSource JavaDoc(new StringReader JavaDoc(contentTypeDefinitionVO.getSchemaValue()));
856             DOMParser parser = new DOMParser();
857             parser.parse(xmlSource);
858             Document JavaDoc document = parser.getDocument();
859
860             //Set the new versionId
861
String JavaDoc rootXPath = "/xs:schema";
862             NodeList JavaDoc schemaList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), rootXPath);
863             for(int i=0; i < schemaList.getLength(); i++)
864             {
865                 Element JavaDoc schemaElement = (Element JavaDoc)schemaList.item(i);
866                 if(schemaElement.getAttribute("version") == null || schemaElement.getAttribute("version").equalsIgnoreCase(""))
867                 {
868                     isModified = true;
869                     schemaElement.setAttribute("version", "2.0");
870
871                     //First check out if the old/wrong definitions are there and delete them
872
String JavaDoc definitionsXPath = "/xs:schema/xs:simpleType";
873                     NodeList JavaDoc definitionList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), definitionsXPath);
874                     for(int j=0; j < definitionList.getLength(); j++)
875                     {
876                         Element JavaDoc childElement = (Element JavaDoc)definitionList.item(j);
877                         if(!childElement.getAttribute("name").equalsIgnoreCase("assetKeys"))
878                             childElement.getParentNode().removeChild(childElement);
879                     }
880
881                     //Now we create the new definitions
882
Element JavaDoc textFieldDefinition = document.createElement("xs:simpleType");
883                     textFieldDefinition.setAttribute("name", "textfield");
884                     Element JavaDoc restriction = document.createElement("xs:restriction");
885                     restriction.setAttribute("base", "xs:string");
886                     Element JavaDoc maxLength = document.createElement("xs:maxLength");
887                     maxLength.setAttribute("value", "100");
888                     restriction.appendChild(maxLength);
889                     textFieldDefinition.appendChild(restriction);
890                     schemaElement.insertBefore(textFieldDefinition, schemaElement.getFirstChild());
891
892                     Element JavaDoc selectDefinition = document.createElement("xs:simpleType");
893                     selectDefinition.setAttribute("name", "select");
894                     restriction = document.createElement("xs:restriction");
895                     restriction.setAttribute("base", "xs:string");
896                     maxLength = document.createElement("xs:maxLength");
897                     maxLength.setAttribute("value", "100");
898                     restriction.appendChild(maxLength);
899                     selectDefinition.appendChild(restriction);
900                     schemaElement.insertBefore(selectDefinition, schemaElement.getFirstChild());
901
902                     Element JavaDoc checkboxDefinition = document.createElement("xs:simpleType");
903                     checkboxDefinition.setAttribute("name", "checkbox");
904                     restriction = document.createElement("xs:restriction");
905                     restriction.setAttribute("base", "xs:string");
906                     maxLength = document.createElement("xs:maxLength");
907                     maxLength.setAttribute("value", "100");
908                     restriction.appendChild(maxLength);
909                     checkboxDefinition.appendChild(restriction);
910                     schemaElement.insertBefore(checkboxDefinition, schemaElement.getFirstChild());
911
912                     Element JavaDoc radiobuttonDefinition = document.createElement("xs:simpleType");
913                     radiobuttonDefinition.setAttribute("name", "radiobutton");
914                     restriction = document.createElement("xs:restriction");
915                     restriction.setAttribute("base", "xs:string");
916                     maxLength = document.createElement("xs:maxLength");
917                     maxLength.setAttribute("value", "100");
918                     restriction.appendChild(maxLength);
919                     radiobuttonDefinition.appendChild(restriction);
920                     schemaElement.insertBefore(radiobuttonDefinition, schemaElement.getFirstChild());
921
922                     Element JavaDoc textareaDefinition = document.createElement("xs:simpleType");
923                     textareaDefinition.setAttribute("name", "textarea");
924                     restriction = document.createElement("xs:restriction");
925                     restriction.setAttribute("base", "xs:string");
926                     maxLength = document.createElement("xs:maxLength");
927                     maxLength.setAttribute("value", "100");
928                     restriction.appendChild(maxLength);
929                     textareaDefinition.appendChild(restriction);
930                     schemaElement.insertBefore(textareaDefinition, schemaElement.getFirstChild());
931
932
933                     //Now we deal with the individual attributes and parameters
934
String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element";
935                     NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
936                     for(int k=0; k < anl.getLength(); k++)
937                     {
938                         Element JavaDoc childElement = (Element JavaDoc)anl.item(k);
939
940                         if(childElement.getAttribute("type").equals("shortString"))
941                         {
942                             childElement.setAttribute("type", "textfield");
943                             isModified = true;
944                         }
945                         else if(childElement.getAttribute("type").equals("shortText"))
946                         {
947                             childElement.setAttribute("type", "textarea");
948                             isModified = true;
949                         }
950                         else if(childElement.getAttribute("type").equals("fullText"))
951                         {
952                             childElement.setAttribute("type", "textarea");
953                             isModified = true;
954                         }
955                         else if(childElement.getAttribute("type").equals("hugeText"))
956                         {
957                             childElement.setAttribute("type", "textarea");
958                             isModified = true;
959                         }
960
961                         String JavaDoc inputTypeId = childElement.getAttribute("type");
962
963                         NodeList JavaDoc annotationNodeList = childElement.getElementsByTagName("xs:annotation");
964                         if(annotationNodeList != null && annotationNodeList.getLength() > 0)
965                         {
966                             Element JavaDoc annotationElement = (Element JavaDoc)annotationNodeList.item(0);
967                             NodeList JavaDoc appinfoNodeList = childElement.getElementsByTagName("xs:appinfo");
968                             if(appinfoNodeList != null && appinfoNodeList.getLength() > 0)
969                             {
970                                 Element JavaDoc appinfoElement = (Element JavaDoc)appinfoNodeList.item(0);
971                                 NodeList JavaDoc paramsNodeList = childElement.getElementsByTagName("params");
972                                 if(paramsNodeList != null && paramsNodeList.getLength() > 0)
973                                 {
974                                     Element JavaDoc paramsElement = (Element JavaDoc)paramsNodeList.item(0);
975                                     addParameterElement(paramsElement, "title", "0");
976                                     addParameterElement(paramsElement, "description", "0");
977                                     addParameterElement(paramsElement, "class", "0");
978
979                                     if(inputTypeId.equalsIgnoreCase("checkbox") || inputTypeId.equalsIgnoreCase("select") || inputTypeId.equalsIgnoreCase("radiobutton"))
980                                     {
981                                         addParameterElement(paramsElement, "values", "1");
982                                     }
983
984                                     if(inputTypeId.equalsIgnoreCase("textarea"))
985                                     {
986                                         addParameterElement(paramsElement, "width", "0", "700");
987                                         addParameterElement(paramsElement, "height", "0", "150");
988                                         addParameterElement(paramsElement, "enableWYSIWYG", "0", "false");
989                                         addParameterElement(paramsElement, "enableTemplateEditor", "0", "false");
990                                         addParameterElement(paramsElement, "enableFormEditor", "0", "false");
991                                         addParameterElement(paramsElement, "enableContentRelationEditor", "0", "false");
992                                         addParameterElement(paramsElement, "enableStructureRelationEditor", "0", "false");
993                                         addParameterElement(paramsElement, "activateExtendedEditorOnLoad", "0", "false");
994                                     }
995                                 }
996                                 else
997                                 {
998                                     Element JavaDoc paramsElement = document.createElement("params");
999
1000                                    addParameterElement(paramsElement, "title", "0");
1001                                    addParameterElement(paramsElement, "description", "0");
1002                                    addParameterElement(paramsElement, "class", "0");
1003
1004                                    if(inputTypeId.equalsIgnoreCase("checkbox") || inputTypeId.equalsIgnoreCase("select") || inputTypeId.equalsIgnoreCase("radiobutton"))
1005                                    {
1006                                        addParameterElement(paramsElement, "values", "1");
1007                                    }
1008
1009                                    if(inputTypeId.equalsIgnoreCase("textarea"))
1010                                    {
1011                                        addParameterElement(paramsElement, "width", "0", "700");
1012                                        addParameterElement(paramsElement, "height", "0", "150");
1013                                        addParameterElement(paramsElement, "enableWYSIWYG", "0", "false");
1014                                        addParameterElement(paramsElement, "enableTemplateEditor", "0", "false");
1015                                        addParameterElement(paramsElement, "enableFormEditor", "0", "false");
1016                                        addParameterElement(paramsElement, "enableContentRelationEditor", "0", "false");
1017                                        addParameterElement(paramsElement, "enableStructureRelationEditor", "0", "false");
1018                                        addParameterElement(paramsElement, "activateExtendedEditorOnLoad", "0", "false");
1019                                    }
1020
1021                                    appinfoElement.appendChild(paramsElement);
1022                                    isModified = true;
1023                                }
1024                            }
1025                            else
1026                            {
1027                                Element JavaDoc appInfo = document.createElement("xs:appinfo");
1028                                Element JavaDoc paramsElement = document.createElement("params");
1029
1030                                addParameterElement(paramsElement, "title", "0");
1031                                addParameterElement(paramsElement, "description", "0");
1032                                addParameterElement(paramsElement, "class", "0");
1033
1034                                if(inputTypeId.equalsIgnoreCase("checkbox") || inputTypeId.equalsIgnoreCase("select") || inputTypeId.equalsIgnoreCase("radiobutton"))
1035                                {
1036                                    addParameterElement(paramsElement, "values", "1");
1037                                }
1038
1039                                if(inputTypeId.equalsIgnoreCase("textarea"))
1040                                {
1041                                    addParameterElement(paramsElement, "width", "0", "700");
1042                                    addParameterElement(paramsElement, "height", "0", "150");
1043                                    addParameterElement(paramsElement, "enableWYSIWYG", "0", "false");
1044                                    addParameterElement(paramsElement, "enableTemplateEditor", "0", "false");
1045                                    addParameterElement(paramsElement, "enableFormEditor", "0", "false");
1046                                    addParameterElement(paramsElement, "enableContentRelationEditor", "0", "false");
1047                                    addParameterElement(paramsElement, "enableStructureRelationEditor", "0", "false");
1048                                    addParameterElement(paramsElement, "activateExtendedEditorOnLoad", "0", "false");
1049                                }
1050
1051                                annotationElement.appendChild(appInfo);
1052                                appInfo.appendChild(paramsElement);
1053                                isModified = true;
1054                            }
1055                        }
1056                        else
1057                        {
1058                            Element JavaDoc annotation = document.createElement("xs:annotation");
1059                            Element JavaDoc appInfo = document.createElement("xs:appinfo");
1060                            Element JavaDoc paramsElement = document.createElement("params");
1061
1062                            addParameterElement(paramsElement, "title", "0");
1063                            addParameterElement(paramsElement, "description", "0");
1064                            addParameterElement(paramsElement, "class", "0");
1065
1066                            if(inputTypeId.equalsIgnoreCase("checkbox") || inputTypeId.equalsIgnoreCase("select") || inputTypeId.equalsIgnoreCase("radiobutton"))
1067                            {
1068                                addParameterElement(paramsElement, "values", "1");
1069                            }
1070
1071                            if(inputTypeId.equalsIgnoreCase("textarea"))
1072                            {
1073                                addParameterElement(paramsElement, "width", "0", "700");
1074                                addParameterElement(paramsElement, "height", "0", "150");
1075                                addParameterElement(paramsElement, "enableWYSIWYG", "0", "false");
1076                                addParameterElement(paramsElement, "enableTemplateEditor", "0", "false");
1077                                addParameterElement(paramsElement, "enableFormEditor", "0", "false");
1078                                addParameterElement(paramsElement, "enableContentRelationEditor", "0", "false");
1079                                addParameterElement(paramsElement, "enableStructureRelationEditor", "0", "false");
1080                                addParameterElement(paramsElement, "activateExtendedEditorOnLoad", "0", "false");
1081                            }
1082
1083                            childElement.appendChild(annotation);
1084                            annotation.appendChild(appInfo);
1085                            appInfo.appendChild(paramsElement);
1086                            isModified = true;
1087                        }
1088
1089                    }
1090                }
1091                else if(schemaElement.getAttribute("version") != null && schemaElement.getAttribute("version").equalsIgnoreCase("2.0"))
1092                {
1093                    isModified = true;
1094                    schemaElement.setAttribute("version", "2.1");
1095
1096                    //Now we deal with the individual attributes and parameters
1097
String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element";
1098                    NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
1099                    for(int k=0; k < anl.getLength(); k++)
1100                    {
1101                        Element JavaDoc childElement = (Element JavaDoc)anl.item(k);
1102
1103                        String JavaDoc inputTypeId = childElement.getAttribute("type");
1104
1105                        NodeList JavaDoc annotationNodeList = childElement.getElementsByTagName("xs:annotation");
1106                        if(annotationNodeList != null && annotationNodeList.getLength() > 0)
1107                        {
1108                            NodeList JavaDoc appinfoNodeList = childElement.getElementsByTagName("xs:appinfo");
1109                            if(appinfoNodeList != null && appinfoNodeList.getLength() > 0)
1110                            {
1111                                NodeList JavaDoc paramsNodeList = childElement.getElementsByTagName("params");
1112                                if(paramsNodeList != null && paramsNodeList.getLength() > 0)
1113                                {
1114                                    Element JavaDoc paramsElement = (Element JavaDoc)paramsNodeList.item(0);
1115
1116                                    if(inputTypeId.equalsIgnoreCase("textarea"))
1117                                    {
1118                                        addParameterElementIfNotExists(paramsElement, "width", "0", "700");
1119                                        addParameterElementIfNotExists(paramsElement, "height", "0", "150");
1120                                        addParameterElementIfNotExists(paramsElement, "enableWYSIWYG", "0", "false");
1121                                        addParameterElementIfNotExists(paramsElement, "enableTemplateEditor", "0", "false");
1122                                        addParameterElementIfNotExists(paramsElement, "enableFormEditor", "0", "false");
1123                                        addParameterElementIfNotExists(paramsElement, "enableContentRelationEditor", "0", "false");
1124                                        addParameterElementIfNotExists(paramsElement, "enableStructureRelationEditor", "0", "false");
1125                                        addParameterElementIfNotExists(paramsElement, "activateExtendedEditorOnLoad", "0", "false");
1126                                        
1127                                        isModified = true;
1128                                    }
1129                                }
1130                            }
1131                        }
1132                    }
1133                }
1134                else if(schemaElement.getAttribute("version") != null && schemaElement.getAttribute("version").equalsIgnoreCase("2.1"))
1135                {
1136                    isModified = true;
1137                    schemaElement.setAttribute("version", "2.2");
1138
1139                    //Now we deal with adding the validation part if not existent
1140
String JavaDoc validatorsXPath = "/xs:schema/xs:complexType[@name = 'Validation']";
1141                    Node JavaDoc formNode = org.apache.xpath.XPathAPI.selectSingleNode(document.getDocumentElement(), validatorsXPath);
1142                    if(formNode == null)
1143                    {
1144                        String JavaDoc schemaXPath = "/xs:schema";
1145                        Node JavaDoc schemaNode = org.apache.xpath.XPathAPI.selectSingleNode(document.getDocumentElement(), schemaXPath);
1146                        
1147                        Element JavaDoc element = (Element JavaDoc)schemaNode;
1148                        
1149                        String JavaDoc validationXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:complexType name=\"Validation\" xmlns:xi=\"http://www.w3.org/2001/XInclude\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"><xs:annotation><xs:appinfo><form-validation><global><validator name=\"required\" classname=\"org.infoglue.cms.util.validators.CommonsValidator\" method=\"validateRequired\" methodParams=\"java.lang.Object,org.apache.commons.validator.Field\" msg=\"300\"/><validator name=\"requiredif\" classname=\"org.infoglue.cms.util.validators.CommonsValidator\" method=\"validateRequiredIf\" methodParams=\"java.lang.Object,org.apache.commons.validator.Field,org.apache.commons.validator.Validator\" msg=\"315\"/><validator name=\"matchRegexp\" classname=\"org.infoglue.cms.util.validators.CommonsValidator\" method=\"validateRegexp\" methodParams=\"java.lang.Object,org.apache.commons.validator.Field\" msg=\"300\"/></global><formset><form name=\"requiredForm\"></form></formset></form-validation></xs:appinfo></xs:annotation></xs:complexType>";
1150                        
1151                        InputSource JavaDoc validationXMLSource = new InputSource JavaDoc(new StringReader JavaDoc(validationXML));
1152                        DOMParser parser2 = new DOMParser();
1153                        parser2.parse(validationXMLSource);
1154                        Document JavaDoc document2 = parser2.getDocument();
1155
1156                        Node JavaDoc node = document.importNode(document2.getDocumentElement(), true);
1157                        element.appendChild(node);
1158                    }
1159                }
1160                else if(schemaElement.getAttribute("version") != null && schemaElement.getAttribute("version").equalsIgnoreCase("2.2"))
1161                {
1162                    isModified = true;
1163                    schemaElement.setAttribute("version", "2.3");
1164
1165                    //Now we deal with the individual attributes and parameters
1166
String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element";
1167                    NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
1168                    for(int k=0; k < anl.getLength(); k++)
1169                    {
1170                        Element JavaDoc childElement = (Element JavaDoc)anl.item(k);
1171
1172                        String JavaDoc inputTypeId = childElement.getAttribute("type");
1173
1174                        NodeList JavaDoc annotationNodeList = childElement.getElementsByTagName("xs:annotation");
1175                        if(annotationNodeList != null && annotationNodeList.getLength() > 0)
1176                        {
1177                            NodeList JavaDoc appinfoNodeList = childElement.getElementsByTagName("xs:appinfo");
1178                            if(appinfoNodeList != null && appinfoNodeList.getLength() > 0)
1179                            {
1180                                NodeList JavaDoc paramsNodeList = childElement.getElementsByTagName("params");
1181                                if(paramsNodeList != null && paramsNodeList.getLength() > 0)
1182                                {
1183                                    Element JavaDoc paramsElement = (Element JavaDoc)paramsNodeList.item(0);
1184
1185                                    addParameterElementIfNotExists(paramsElement, "initialData", "0", "");
1186                                    isModified = true;
1187                                }
1188                            }
1189                        }
1190                    }
1191
1192                }
1193                else if(schemaElement.getAttribute("version") != null && schemaElement.getAttribute("version").equalsIgnoreCase("2.3"))
1194                {
1195                    isModified = true;
1196                    schemaElement.setAttribute("version", "2.4");
1197
1198                    //Now we deal with the individual attributes and parameters
1199
String JavaDoc attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element";
1200                    NodeList JavaDoc anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath);
1201                    for(int k=0; k < anl.getLength(); k++)
1202                    {
1203                        Element JavaDoc childElement = (Element JavaDoc)anl.item(k);
1204
1205                        String JavaDoc inputTypeId = childElement.getAttribute("type");
1206
1207                        NodeList JavaDoc annotationNodeList = childElement.getElementsByTagName("xs:annotation");
1208                        if(annotationNodeList != null && annotationNodeList.getLength() > 0)
1209                        {
1210                            NodeList JavaDoc appinfoNodeList = childElement.getElementsByTagName("xs:appinfo");
1211                            if(appinfoNodeList != null && appinfoNodeList.getLength() > 0)
1212                            {
1213                                NodeList JavaDoc paramsNodeList = childElement.getElementsByTagName("params");
1214                                if(paramsNodeList != null && paramsNodeList.getLength() > 0)
1215                                {
1216                                    Element JavaDoc paramsElement = (Element JavaDoc)paramsNodeList.item(0);
1217
1218                                    addParameterElementIfNotExists(paramsElement, "enableComponentPropertiesEditor", "0", "false");
1219
1220                                    isModified = true;
1221                                }
1222                            }
1223                        }
1224                    }
1225
1226                }
1227
1228                
1229            }
1230
1231            if(isModified)
1232            {
1233                StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1234                org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
1235                contentTypeDefinitionVO.setSchemaValue(sb.toString());
1236
1237                update(contentTypeDefinitionVO);
1238            }
1239        }
1240        catch(Exception JavaDoc e)
1241        {
1242            e.printStackTrace();
1243        }
1244
1245        return contentTypeDefinitionVO;
1246    }
1247
1248
1249    /**
1250     * This method adds a parameter node with some default values if not allready existing.
1251     */

1252
1253    private boolean addParameterElement(Element JavaDoc parent, String JavaDoc name, String JavaDoc inputTypeId, String JavaDoc value, boolean isAllreadyModified)
1254    {
1255        boolean isModified = isAllreadyModified;
1256
1257        NodeList JavaDoc titleNodeList = parent.getElementsByTagName(name);
1258        if(titleNodeList != null && titleNodeList.getLength() > 0)
1259        {
1260            Element JavaDoc titleElement = (Element JavaDoc)titleNodeList.item(0);
1261            if(!titleElement.hasChildNodes())
1262            {
1263                titleElement.appendChild(parent.getOwnerDocument().createTextNode(value));
1264                isModified = true;
1265            }
1266        }
1267        else
1268        {
1269            Element JavaDoc title = parent.getOwnerDocument().createElement(name);
1270            title.appendChild(parent.getOwnerDocument().createTextNode(value));
1271            parent.appendChild(title);
1272            isModified = true;
1273        }
1274
1275        return isModified;
1276    }
1277
1278    /**
1279     * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
1280     * is handling.
1281     */

1282
1283    public BaseEntityVO getNewVO()
1284    {
1285        return new ContentTypeDefinitionVO();
1286    }
1287}
1288
Popular Tags