1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.io.StringReader ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.xml.transform.TransformerException ; 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 ; 58 import org.w3c.dom.Element ; 59 import org.w3c.dom.NamedNodeMap ; 60 import org.w3c.dom.Node ; 61 import org.w3c.dom.NodeList ; 62 import org.xml.sax.InputSource ; 63 64 72 public class ContentTypeDefinitionController extends BaseController 73 { 74 private final static Logger logger = Logger.getLogger(ContentTypeDefinitionController.class.getName()); 75 76 public static final String ASSET_KEYS = "assetKeys"; 77 public static final String CATEGORY_KEYS = "categoryKeys"; 78 79 private static final NodeList EMPTY_NODELIST = new NodeList () 80 { 81 public int getLength() { return 0; } 82 public Node item(int i) { return null; } 83 }; 84 85 88 89 public static ContentTypeDefinitionController getController() 90 { 91 return new ContentTypeDefinitionController(); 92 } 93 94 public ContentTypeDefinitionVO getContentTypeDefinitionVOWithId(Integer contentTypeDefinitionId) throws SystemException, Bug 95 { 96 return (ContentTypeDefinitionVO) getVOWithId(ContentTypeDefinitionImpl.class, contentTypeDefinitionId); 97 } 98 99 105 106 public ContentTypeDefinition getContentTypeDefinitionWithId(Integer contentTypeDefinitionId, Database db) throws SystemException, Bug 107 { 108 return (ContentTypeDefinition) getObjectWithId(ContentTypeDefinitionImpl.class, contentTypeDefinitionId, db); 109 } 110 111 public List getContentTypeDefinitionVOList() throws SystemException, Bug 112 { 113 String key = "contentTypeDefinitionVOList"; 114 logger.info("key:" + key); 115 List cachedContentTypeDefinitionVOList = (List )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 contentTypeDefinitionVOList = getAllVOObjects(ContentTypeDefinitionImpl.class, "contentTypeDefinitionId"); 123 124 CacheController.cacheObject("contentTypeDefinitionCache", key, contentTypeDefinitionVOList); 125 126 return contentTypeDefinitionVOList; 127 } 128 129 public List getContentTypeDefinitionVOList(Database db) throws SystemException, Bug 130 { 131 return getAllVOObjects(ContentTypeDefinitionImpl.class, "contentTypeDefinitionId", db); 132 } 133 134 public List getContentTypeDefinitionList(Database db) throws SystemException, Bug 135 { 136 return getAllObjects(ContentTypeDefinitionImpl.class, "contentTypeDefinitionId", db); 137 } 138 139 143 144 public List getAuthorizedContentTypeDefinitionVOList(InfoGluePrincipal infoGluePrincipal) throws ConstraintException, SystemException, Bug 145 { 146 List accessableContentTypeDefinitionVOList = new ArrayList (); 147 148 List allContentTypeDefinitionVOList = this.getContentTypeDefinitionVOList(); 149 Iterator 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 163 164 public boolean getIsAccessApproved(Integer 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 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 197 198 public ContentTypeDefinitionVO getContentTypeDefinitionVOWithName(String 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 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 233 234 public ContentTypeDefinition getContentTypeDefinitionWithName(String 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 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 272 273 public ContentTypeDefinitionVO getContentTypeDefinitionVOWithName(String name, Database db) throws SystemException, Bug 274 { 275 String 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 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 getContentTypeDefinitionVOList(Integer type) throws SystemException, Bug 314 { 315 List 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 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 getContentTypeDefinitionVOList(Integer type, Database db) throws SystemException, Bug 336 { 337 ArrayList contentTypeDefinitionVOList = new ArrayList (); 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 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 382 public AssetKeyDefinition getDefinedAssetKey(String contentTypeDefinitionString, String assetKey) 383 { 384 AssetKeyDefinition assetKeyDefinition = null; 385 386 List definedAssetKeys = getDefinedAssetKeys(contentTypeDefinitionString); 387 Iterator 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 404 public List getDefinedAssetKeys(String contentTypeDefinitionString) 405 { 406 NodeList nodes = getEnumerationNodeList(contentTypeDefinitionString, ASSET_KEYS); 407 408 return getEnumValues(nodes); 409 } 410 411 412 415 public List getDefinedCategoryKeys(String contentTypeDefinitionString) 416 { 417 NodeList nodes = getEnumerationNodeList(contentTypeDefinitionString, CATEGORY_KEYS); 418 return getCategoryInfo(nodes); 419 } 420 421 424 protected List getEnumValues(NodeList nodes) 425 { 426 List keys = new ArrayList (); 427 for(int i = 0; i < nodes.getLength(); i++) 428 { 429 Node ichild = nodes.item(i); 430 431 logger.info("ichild:" + ichild.getNodeName() + ":" + ichild.getNodeValue()); 432 433 try 434 { 435 Node assetKeyValue = ichild.getAttributes().getNamedItem("value"); 436 437 Element params = (Element )XPathAPI.selectSingleNode(ichild, "xs:annotation/xs:appinfo/params"); 438 439 String descriptionValue = ""; 440 String maximumSizeValue = "1000000"; 441 String allowedContentTypesValue = "*"; 442 String imageWidthValue = "*"; 443 String 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 (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 e) 467 { 468 e.printStackTrace(); 469 } 470 } 471 472 logger.info("keys:" + keys.size()); 473 474 return keys; 475 } 476 477 480 protected List getCategoryInfo(NodeList nodes) 481 { 482 String attributesXPath = "xs:annotation/xs:appinfo/params"; 483 484 List keys = new ArrayList (); 485 for(int i = 0; i < nodes.getLength(); i++) 486 { 487 Node enumeration = nodes.item(i); 488 String value = enumeration.getAttributes().getNamedItem("value").getNodeValue(); 489 try 490 { 491 CategoryAttribute category = new CategoryAttribute(value); 492 keys.add(category); 493 494 Element params = (Element )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 e) 503 { 504 keys.add(new CategoryAttribute(value)); 505 } 506 } 507 return keys; 508 } 509 510 514 protected NodeList getEnumerationNodeList(String contentTypeDefinitionString, String keyType) 515 { 516 try 517 { 518 if(contentTypeDefinitionString != null) 519 { 520 InputSource xmlSource = new InputSource (new StringReader (contentTypeDefinitionString)); 521 522 DOMParser parser = new DOMParser(); 523 parser.parse(xmlSource); 524 Document document = parser.getDocument(); 525 526 String attributesXPath = "/xs:schema/xs:simpleType[@name = '" + keyType + "']/xs:restriction/xs:enumeration"; 527 return XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 528 } 529 } 530 catch(Exception 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 544 protected String getElementValue(Element root, String tagName) 545 { 546 NodeList nodes = root.getElementsByTagName(tagName); 547 if(nodes.getLength() > 0) 548 { 549 Node cdata = nodes.item(0).getFirstChild(); 550 return (cdata != null)? cdata.getNodeValue() : null; 551 } 552 553 return null; 554 } 555 556 559 560 public List getContentTypeAttributes(String schemaValue) 561 { 562 564 String key = "schemaValue_" + schemaValue.hashCode(); 565 Object attributesCandidate = CacheController.getCachedObject("contentTypeDefinitionCache", key); 567 List attributes = new ArrayList (); 569 570 if(attributesCandidate != null) 571 { 572 if(attributesCandidate instanceof NullObject) 573 attributes = new ArrayList (); 574 else 575 attributes = (List )attributesCandidate; 576 577 } 579 else 580 { 581 try 582 { 583 InputSource xmlSource = new InputSource (new StringReader (schemaValue)); 584 585 DOMParser parser = new DOMParser(); 586 parser.parse(xmlSource); 587 Document document = parser.getDocument(); 588 589 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 590 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 591 for(int i=0; i < anl.getLength(); i++) 592 { 593 Element child = (Element )anl.item(i); 594 String attributeName = child.getAttribute("name"); 595 String 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 validatorsXPath = "/xs:schema/xs:complexType[@name = 'Validation']/xs:annotation/xs:appinfo/form-validation/formset/form/field[@property = '"+ attributeName +"']"; 603 604 NodeList validatorNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), validatorsXPath); 606 for(int j=0; j < validatorNodeList.getLength(); j++) 607 { 608 Element validatorNode = (Element )validatorNodeList.item(j); 609 if (validatorNode != null) 610 { 611 Map arguments = new HashMap (); 612 613 NodeList varNodeList = validatorNode.getElementsByTagName("var"); 614 for(int k=0; k < varNodeList.getLength(); k++) 615 { 616 Element varNode = (Element )varNodeList.item(k); 617 618 String varName = getElementValue(varNode, "var-name"); 619 String varValue = getElementValue(varNode, "var-value"); 620 621 arguments.put(varName, varValue); 622 } 623 624 String attribute = ((Element )validatorNode).getAttribute("depends"); 625 String [] depends = attribute.split(","); 626 for(int dependsIndex=0; dependsIndex < depends.length; dependsIndex++) 627 { 628 String 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 Node paramsNode = org.apache.xpath.XPathAPI.selectSingleNode(child, "xs:annotation/xs:appinfo/params"); 642 if (paramsNode != null) 643 { 644 NodeList childnl = ((Element )paramsNode).getElementsByTagName("param"); 645 for(int ci=0; ci < childnl.getLength(); ci++) 646 { 647 Element param = (Element )childnl.item(ci); 648 String paramId = param.getAttribute("id"); 649 String 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 valuesNodeList = param.getElementsByTagName("values"); 659 for(int vsnli=0; vsnli < valuesNodeList.getLength(); vsnli++) 660 { 661 NodeList valueNodeList = param.getElementsByTagName("value"); 662 for(int vnli=0; vnli < valueNodeList.getLength(); vnli++) 663 { 664 Element value = (Element )valueNodeList.item(vnli); 665 String valueId = value.getAttribute("id"); 666 667 ContentTypeAttributeParameterValue contentTypeAttributeParameterValue = new ContentTypeAttributeParameterValue(); 668 contentTypeAttributeParameterValue.setId(valueId); 669 670 NamedNodeMap nodeMap = value.getAttributes(); 671 for(int nmi =0; nmi < nodeMap.getLength(); nmi++) 672 { 673 Node attribute = (Node )nodeMap.item(nmi); 674 String valueAttributeName = attribute.getNodeName(); 675 String valueAttributeValue = attribute.getNodeValue(); 676 contentTypeAttributeParameterValue.addAttribute(valueAttributeName, valueAttributeValue); 677 } 678 679 contentTypeAttributeParameter.addContentTypeAttributeParameterValue(valueId, contentTypeAttributeParameterValue); 680 } 681 } 682 } 683 } 684 686 attributes.add(contentTypeAttribute); 687 } 688 689 } 690 catch(Exception 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 707 708 public String insertContentTypeAttribute(String schemaValue, String inputTypeId, List activatedName) 709 { 710 String newSchemaValue = schemaValue; 711 712 try 713 { 714 InputSource xmlSource = new InputSource (new StringReader (schemaValue)); 715 716 DOMParser parser = new DOMParser(); 717 parser.parse(xmlSource); 718 Document document = parser.getDocument(); 719 720 722 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all"; 723 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 724 for(int i=0; i < anl.getLength(); i++) 725 { 726 Node child = anl.item(i); 727 Element childElement = (Element )child; 728 Element newAttribute = document.createElement("xs:element"); 729 String 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 annotation = document.createElement("xs:annotation"); 736 Element appInfo = document.createElement("xs:appinfo"); 737 Element 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 sb = new StringBuffer (); 768 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 769 newSchemaValue = sb.toString(); 770 } 771 catch(Exception e) 772 { 773 e.printStackTrace(); 774 } 775 776 return newSchemaValue; 777 } 778 779 780 784 785 private void addParameterElement(Element parent, String id, String inputTypeId) 786 { 787 Element parameter = parent.getOwnerDocument().createElement("param"); 788 parameter.setAttribute("id", id); 789 parameter.setAttribute("inputTypeId", inputTypeId); Element parameterValuesValues = parent.getOwnerDocument().createElement("values"); 791 Element 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 804 805 private void addParameterElement(Element parent, String id, String inputTypeId, String defaultValue) 806 { 807 Element parameter = parent.getOwnerDocument().createElement("param"); 808 parameter.setAttribute("id", id); 809 parameter.setAttribute("inputTypeId", inputTypeId); Element parameterValuesValues = parent.getOwnerDocument().createElement("values"); 811 Element 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 824 825 private void addParameterElementIfNotExists(Element parent, String id, String inputTypeId, String defaultValue) throws Exception 826 { 827 String parameterXPath = "param[@id='" + id + "']"; 828 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(parent, parameterXPath); 829 if(anl.getLength() == 0) 830 { 831 Element parameter = parent.getOwnerDocument().createElement("param"); 832 parameter.setAttribute("id", id); 833 parameter.setAttribute("inputTypeId", inputTypeId); Element parameterValuesValues = parent.getOwnerDocument().createElement("values"); 835 Element 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 848 849 public ContentTypeDefinitionVO validateAndUpdateContentType(ContentTypeDefinitionVO contentTypeDefinitionVO) 850 { 851 try 852 { 853 boolean isModified = false; 854 855 InputSource xmlSource = new InputSource (new StringReader (contentTypeDefinitionVO.getSchemaValue())); 856 DOMParser parser = new DOMParser(); 857 parser.parse(xmlSource); 858 Document document = parser.getDocument(); 859 860 String rootXPath = "/xs:schema"; 862 NodeList schemaList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), rootXPath); 863 for(int i=0; i < schemaList.getLength(); i++) 864 { 865 Element schemaElement = (Element )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 String definitionsXPath = "/xs:schema/xs:simpleType"; 873 NodeList definitionList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), definitionsXPath); 874 for(int j=0; j < definitionList.getLength(); j++) 875 { 876 Element childElement = (Element )definitionList.item(j); 877 if(!childElement.getAttribute("name").equalsIgnoreCase("assetKeys")) 878 childElement.getParentNode().removeChild(childElement); 879 } 880 881 Element textFieldDefinition = document.createElement("xs:simpleType"); 883 textFieldDefinition.setAttribute("name", "textfield"); 884 Element restriction = document.createElement("xs:restriction"); 885 restriction.setAttribute("base", "xs:string"); 886 Element 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 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 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 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 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 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 935 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 936 for(int k=0; k < anl.getLength(); k++) 937 { 938 Element childElement = (Element )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 inputTypeId = childElement.getAttribute("type"); 962 963 NodeList annotationNodeList = childElement.getElementsByTagName("xs:annotation"); 964 if(annotationNodeList != null && annotationNodeList.getLength() > 0) 965 { 966 Element annotationElement = (Element )annotationNodeList.item(0); 967 NodeList appinfoNodeList = childElement.getElementsByTagName("xs:appinfo"); 968 if(appinfoNodeList != null && appinfoNodeList.getLength() > 0) 969 { 970 Element appinfoElement = (Element )appinfoNodeList.item(0); 971 NodeList paramsNodeList = childElement.getElementsByTagName("params"); 972 if(paramsNodeList != null && paramsNodeList.getLength() > 0) 973 { 974 Element paramsElement = (Element )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 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 appInfo = document.createElement("xs:appinfo"); 1028 Element 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 annotation = document.createElement("xs:annotation"); 1059 Element appInfo = document.createElement("xs:appinfo"); 1060 Element 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 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 1098 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 1099 for(int k=0; k < anl.getLength(); k++) 1100 { 1101 Element childElement = (Element )anl.item(k); 1102 1103 String inputTypeId = childElement.getAttribute("type"); 1104 1105 NodeList annotationNodeList = childElement.getElementsByTagName("xs:annotation"); 1106 if(annotationNodeList != null && annotationNodeList.getLength() > 0) 1107 { 1108 NodeList appinfoNodeList = childElement.getElementsByTagName("xs:appinfo"); 1109 if(appinfoNodeList != null && appinfoNodeList.getLength() > 0) 1110 { 1111 NodeList paramsNodeList = childElement.getElementsByTagName("params"); 1112 if(paramsNodeList != null && paramsNodeList.getLength() > 0) 1113 { 1114 Element paramsElement = (Element )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 String validatorsXPath = "/xs:schema/xs:complexType[@name = 'Validation']"; 1141 Node formNode = org.apache.xpath.XPathAPI.selectSingleNode(document.getDocumentElement(), validatorsXPath); 1142 if(formNode == null) 1143 { 1144 String schemaXPath = "/xs:schema"; 1145 Node schemaNode = org.apache.xpath.XPathAPI.selectSingleNode(document.getDocumentElement(), schemaXPath); 1146 1147 Element element = (Element )schemaNode; 1148 1149 String 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 validationXMLSource = new InputSource (new StringReader (validationXML)); 1152 DOMParser parser2 = new DOMParser(); 1153 parser2.parse(validationXMLSource); 1154 Document document2 = parser2.getDocument(); 1155 1156 Node 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 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 1167 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 1168 for(int k=0; k < anl.getLength(); k++) 1169 { 1170 Element childElement = (Element )anl.item(k); 1171 1172 String inputTypeId = childElement.getAttribute("type"); 1173 1174 NodeList annotationNodeList = childElement.getElementsByTagName("xs:annotation"); 1175 if(annotationNodeList != null && annotationNodeList.getLength() > 0) 1176 { 1177 NodeList appinfoNodeList = childElement.getElementsByTagName("xs:appinfo"); 1178 if(appinfoNodeList != null && appinfoNodeList.getLength() > 0) 1179 { 1180 NodeList paramsNodeList = childElement.getElementsByTagName("params"); 1181 if(paramsNodeList != null && paramsNodeList.getLength() > 0) 1182 { 1183 Element paramsElement = (Element )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 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 1200 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 1201 for(int k=0; k < anl.getLength(); k++) 1202 { 1203 Element childElement = (Element )anl.item(k); 1204 1205 String inputTypeId = childElement.getAttribute("type"); 1206 1207 NodeList annotationNodeList = childElement.getElementsByTagName("xs:annotation"); 1208 if(annotationNodeList != null && annotationNodeList.getLength() > 0) 1209 { 1210 NodeList appinfoNodeList = childElement.getElementsByTagName("xs:appinfo"); 1211 if(appinfoNodeList != null && appinfoNodeList.getLength() > 0) 1212 { 1213 NodeList paramsNodeList = childElement.getElementsByTagName("params"); 1214 if(paramsNodeList != null && paramsNodeList.getLength() > 0) 1215 { 1216 Element paramsElement = (Element )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 sb = new StringBuffer (); 1234 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 1235 contentTypeDefinitionVO.setSchemaValue(sb.toString()); 1236 1237 update(contentTypeDefinitionVO); 1238 } 1239 } 1240 catch(Exception e) 1241 { 1242 e.printStackTrace(); 1243 } 1244 1245 return contentTypeDefinitionVO; 1246 } 1247 1248 1249 1252 1253 private boolean addParameterElement(Element parent, String name, String inputTypeId, String value, boolean isAllreadyModified) 1254 { 1255 boolean isModified = isAllreadyModified; 1256 1257 NodeList titleNodeList = parent.getElementsByTagName(name); 1258 if(titleNodeList != null && titleNodeList.getLength() > 0) 1259 { 1260 Element titleElement = (Element )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 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 1282 1283 public BaseEntityVO getNewVO() 1284 { 1285 return new ContentTypeDefinitionVO(); 1286 } 1287} 1288 | Popular Tags |