1 23 24 package org.infoglue.cms.applications.managementtool.actions; 25 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import javax.xml.transform.TransformerException ; 33 34 import org.apache.log4j.Logger; 35 import org.apache.xerces.parsers.DOMParser; 36 import org.apache.xpath.XPathAPI; 37 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction; 38 import org.infoglue.cms.controllers.kernel.impl.simple.CategoryController; 39 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController; 40 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController; 41 import org.infoglue.cms.entities.management.CategoryAttribute; 42 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO; 43 import org.infoglue.cms.exception.ConstraintException; 44 import org.infoglue.cms.exception.SystemException; 45 import org.infoglue.cms.util.ConstraintExceptionBuffer; 46 import org.infoglue.cms.util.XMLHelper; 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.Element ; 49 import org.w3c.dom.Node ; 50 import org.w3c.dom.NodeList ; 51 import org.xml.sax.InputSource ; 52 import org.xml.sax.SAXException ; 53 54 60 61 public class ViewContentTypeDefinitionAction extends InfoGlueAbstractAction 62 { 63 private final static Logger logger = Logger.getLogger(ViewContentTypeDefinitionAction.class.getName()); 64 65 private static final long serialVersionUID = 1L; 66 67 public static final String USE_EDITOR = "useEditor"; 68 69 private static CategoryController categoryController = CategoryController.getController(); 70 71 private ContentTypeDefinitionVO contentTypeDefinitionVO; 72 private String currentContentTypeEditorViewLanguageCode; 73 private List attributes = null; 74 private List availableLanguages = null; 75 private List languageVOList; 76 private String title; 77 private String inputTypeId; 78 private String attributeName; 79 private String newAttributeName; 80 private String attributeParameterId; 81 private String attributeParameterValueId; 82 private String newAttributeParameterValueId; 83 private String attributeParameterValueLabel; 84 private String attributeParameterValueLocale; 85 private String attributeToExpand; 86 private String assetKey; 87 private String newAssetKey; 88 private String categoryKey; 89 private String newCategoryKey; 90 91 private String description = ""; 92 private Integer maximumSize; 93 private String allowedContentTypes = "any"; 94 private String imageWidth; 95 private String imageHeight; 96 97 private List activatedName = new ArrayList (); 98 99 public ViewContentTypeDefinitionAction() 100 { 101 this(new ContentTypeDefinitionVO()); 102 } 103 104 public ViewContentTypeDefinitionAction(ContentTypeDefinitionVO contentTypeDefinitionVO) 105 { 106 this.contentTypeDefinitionVO = contentTypeDefinitionVO; 107 } 108 109 protected void initialize(Integer contentTypeDefinitionId) throws Exception 110 { 111 this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(contentTypeDefinitionId); 112 114 this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().validateAndUpdateContentType(this.contentTypeDefinitionVO); 115 this.contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(contentTypeDefinitionId); 116 this.attributes = ContentTypeDefinitionController.getController().getContentTypeAttributes(this.contentTypeDefinitionVO.getSchemaValue()); 117 this.availableLanguages = LanguageController.getController().getLanguageVOList(); 118 } 119 120 123 124 public String doExecute() throws Exception 125 { 126 this.initialize(getContentTypeDefinitionId()); 127 return USE_EDITOR; 128 } 129 130 133 134 public String doUseEditor() throws Exception 135 { 136 this.initialize(getContentTypeDefinitionId()); 137 return USE_EDITOR; 138 } 139 140 143 144 public String doUseSimple() throws Exception 145 { 146 this.initialize(getContentTypeDefinitionId()); 147 return SUCCESS; 148 } 149 150 public String doInsertAttribute() throws Exception 151 { 152 this.initialize(getContentTypeDefinitionId()); 153 String newSchemaValue = ContentTypeDefinitionController.getController().insertContentTypeAttribute(this.contentTypeDefinitionVO.getSchemaValue(), this.inputTypeId, this.activatedName); 154 this.contentTypeDefinitionVO.setSchemaValue(newSchemaValue); 155 ContentTypeDefinitionController.getController().update(this.contentTypeDefinitionVO); 156 157 this.initialize(getContentTypeDefinitionId()); 158 return USE_EDITOR; 159 } 160 161 162 public String doDeleteAttribute() throws Exception 163 { 164 this.initialize(getContentTypeDefinitionId()); 165 166 try 167 { 168 Document document = createDocumentFromDefinition(); 169 170 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']"; 171 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 172 if(anl != null && anl.getLength() > 0) 173 { 174 Element element = (Element )anl.item(0); 175 element.getParentNode().removeChild(element); 176 } 177 178 String validatorsXPath = "/xs:schema/xs:complexType[@name = 'Validation']/xs:annotation/xs:appinfo/form-validation/formset/form/field[@property = '" + attributeName + "']"; 179 NodeList anl2 = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), validatorsXPath); 180 for(int i=0; i<anl2.getLength(); i++) 181 { 182 Element element = (Element )anl2.item(i); 183 element.getParentNode().removeChild(element); 184 } 185 186 saveUpdatedDefinition(document); 187 } 188 catch(Exception e) 189 { 190 e.printStackTrace(); 191 } 192 193 this.initialize(getContentTypeDefinitionId()); 194 return USE_EDITOR; 195 } 196 197 200 201 public String doMoveAttributeUp() throws Exception 202 { 203 this.initialize(getContentTypeDefinitionId()); 204 205 try 206 { 207 Document document = createDocumentFromDefinition(); 208 209 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 210 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 211 Element previousElement = null; 212 for(int i=0; i < anl.getLength(); i++) 213 { 214 Element element = (Element )anl.item(i); 215 if(element.getAttribute("name").equalsIgnoreCase(this.attributeName) && previousElement != null) 216 { 217 Element parent = (Element )element.getParentNode(); 218 parent.removeChild(element); 219 parent.insertBefore(element, previousElement); 220 } 221 previousElement = element; 222 } 223 224 saveUpdatedDefinition(document); 225 } 226 catch(Exception e) 227 { 228 e.printStackTrace(); 229 } 230 231 this.initialize(getContentTypeDefinitionId()); 232 return USE_EDITOR; 233 } 234 235 236 239 240 public String doMoveAttributeDown() throws Exception 241 { 242 this.initialize(getContentTypeDefinitionId()); 243 244 try 245 { 246 Document document = createDocumentFromDefinition(); 247 248 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 249 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 250 Element parent = null; 251 Element elementToMove = null; 252 boolean isInserted = false; 253 int position = 0; 254 for(int i=0; i < anl.getLength(); i++) 255 { 256 Element element = (Element )anl.item(i); 257 parent = (Element )element.getParentNode(); 258 259 if(elementToMove != null) 260 { 261 if(position == 2) 262 { 263 parent.insertBefore(elementToMove, element); 264 isInserted = true; 265 break; 266 } 267 else 268 position++; 269 } 270 271 if(element.getAttribute("name").equalsIgnoreCase(this.attributeName)) 272 { 273 elementToMove = element; 274 parent.removeChild(elementToMove); 275 position++; 276 } 277 } 278 279 if(!isInserted && elementToMove != null) 280 parent.appendChild(elementToMove); 281 282 saveUpdatedDefinition(document); 283 } 284 catch(Exception e) 285 { 286 e.printStackTrace(); 287 } 288 289 this.initialize(getContentTypeDefinitionId()); 290 return USE_EDITOR; 291 } 292 293 294 297 298 public String doMoveAssetKeyUp() throws Exception 299 { 300 this.initialize(getContentTypeDefinitionId()); 301 302 try 303 { 304 Document document = createDocumentFromDefinition(); 305 306 String attributesXPath = "/xs:schema/xs:simpleType[@name = '" + ContentTypeDefinitionController.ASSET_KEYS + "']/xs:restriction/xs:enumeration[@value='" + this.assetKey + "']"; 307 NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 308 if(anl != null && anl.getLength() > 0) 309 { 310 Element element = (Element )anl.item(0); 311 Node parentElement = element.getParentNode(); 312 Node previuosSibling = element.getPreviousSibling(); 313 if(previuosSibling != null) 314 { 315 parentElement.removeChild(element); 316 parentElement.insertBefore(element, previuosSibling); 317 } 318 } 319 320 saveUpdatedDefinition(document); 321 } 322 catch(Exception e) 323 { 324 e.printStackTrace(); 325 } 326 327 this.initialize(getContentTypeDefinitionId()); 328 329 return USE_EDITOR; 330 } 331 332 333 336 337 public String doMoveAssetKeyDown() throws Exception 338 { 339 this.initialize(getContentTypeDefinitionId()); 340 341 try 342 { 343 Document document = createDocumentFromDefinition(); 344 345 String attributesXPath = "/xs:schema/xs:simpleType[@name = '" + ContentTypeDefinitionController.ASSET_KEYS + "']/xs:restriction/xs:enumeration[@value='" + this.assetKey + "']"; 346 NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 347 if(anl != null && anl.getLength() > 0) 348 { 349 Element element = (Element )anl.item(0); 350 Node parentElement = element.getParentNode(); 351 Node nextSibling = element.getNextSibling(); 352 if(nextSibling != null) 353 { 354 parentElement.removeChild(nextSibling); 355 parentElement.insertBefore(nextSibling, element); 356 } 357 } 358 359 saveUpdatedDefinition(document); 360 } 361 catch(Exception e) 362 { 363 e.printStackTrace(); 364 } 365 366 this.initialize(getContentTypeDefinitionId()); 367 return USE_EDITOR; 368 } 369 370 371 public String doDeleteAttributeParameterValue() throws Exception 372 { 373 this.initialize(getContentTypeDefinitionId()); 374 375 try 376 { 377 Document document = createDocumentFromDefinition(); 378 379 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']/xs:annotation/xs:appinfo/params/param[@id='" + this.attributeParameterId +"']/values/value[@id='" + this.attributeParameterValueId + "']"; 380 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 381 if(anl != null && anl.getLength() > 0) 382 { 383 Element element = (Element )anl.item(0); 384 element.getParentNode().removeChild(element); 385 } 386 387 saveUpdatedDefinition(document); 388 } 389 catch(Exception e) 390 { 391 e.printStackTrace(); 392 } 393 394 this.initialize(getContentTypeDefinitionId()); 395 return USE_EDITOR; 396 } 397 398 public String doInsertAttributeParameterValue() throws Exception 399 { 400 this.initialize(getContentTypeDefinitionId()); 401 402 try 403 { 404 Document document = createDocumentFromDefinition(); 405 406 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']/xs:annotation/xs:appinfo/params/param[@id='" + this.attributeParameterId +"']/values"; 407 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 408 if(anl != null && anl.getLength() > 0) 409 { 410 Element element = (Element )anl.item(0); 411 Element newValue = document.createElement("value"); 412 newValue.setAttribute("id", getRandomName()); 413 newValue.setAttribute("label", getRandomName()); 414 element.appendChild(newValue); 415 } 416 417 saveUpdatedDefinition(document); 418 } 419 catch(Exception e) 420 { 421 e.printStackTrace(); 422 } 423 424 this.initialize(getContentTypeDefinitionId()); 425 return USE_EDITOR; 426 } 427 428 431 432 public String doUpdateAttribute() throws Exception 433 { 434 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 435 if(this.newAttributeName.indexOf(" ") > -1 || this.newAttributeName.indexOf(".") > -1 || this.newAttributeName.indexOf("'") > -1 || this.newAttributeName.indexOf("\"") > -1) 436 { 437 ceb.add(new ConstraintException("ContentTypeAttribute.updateAction", "3500")); 438 } 439 440 ceb.throwIfNotEmpty(); 441 442 443 this.initialize(getContentTypeDefinitionId()); 444 445 try 446 { 447 Document document = createDocumentFromDefinition(); 448 449 String [] extraParameterNames = getRequest().getParameterValues("parameterNames"); 451 if(extraParameterNames != null) 452 { 453 for(int i=0; i < extraParameterNames.length; i++) 454 { 455 String extraParameterName = extraParameterNames[i]; 456 String value = getRequest().getParameter(extraParameterName); 457 458 String extraParametersXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']/xs:annotation/xs:appinfo/params/param[@id='" + extraParameterName +"']/values/value"; 459 NodeList extraParamsNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), extraParametersXPath); 460 if(extraParamsNodeList != null && extraParamsNodeList.getLength() > 0) 461 { 462 Element element = (Element )extraParamsNodeList.item(0); 463 464 if(extraParameterName.equalsIgnoreCase("values") && (this.inputTypeId.equalsIgnoreCase("select") || this.inputTypeId.equalsIgnoreCase("checkbox") || this.inputTypeId.equalsIgnoreCase("radiobutton"))) 465 { 466 ((Element )element.getParentNode().getParentNode()).setAttribute("inputTypeId", "1"); 467 } 468 else 469 { 470 ((Element )element.getParentNode().getParentNode()).setAttribute("inputTypeId", "0"); 471 } 472 473 if(((Element )element.getParentNode().getParentNode()).getAttribute("inputTypeId").equals("0")) 474 { 475 if(this.currentContentTypeEditorViewLanguageCode != null && this.currentContentTypeEditorViewLanguageCode.length() > 0) 476 { 477 element.setAttribute("label_" + this.currentContentTypeEditorViewLanguageCode, value); 478 } 479 else 480 { 481 element.setAttribute("label", value); 482 } 483 } 484 } 485 } 486 } 487 488 String attributeXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']"; 490 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributeXPath); 491 if(anl != null && anl.getLength() > 0) 492 { 493 Element element = (Element )anl.item(0); 494 element.setAttribute("name", this.newAttributeName); 495 element.setAttribute("type", this.inputTypeId); 496 } 497 498 try 499 { 500 String validationXPath = "//xs:complexType[@name='Validation']/xs:annotation/xs:appinfo/form-validation/formset/form/field[@property='" + this.attributeName + "']"; 502 NodeList fieldNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), validationXPath); 503 if(fieldNodeList != null && fieldNodeList.getLength() > 0) 504 { 505 Element element = (Element )fieldNodeList.item(0); 506 element.setAttribute("property", this.newAttributeName); 507 } 508 509 String validationDependentXPath = "//xs:complexType[@name='Validation']/xs:annotation/xs:appinfo/form-validation/formset/form/field[@depends='requiredif']/var/var-value"; 511 NodeList requiredIfValueNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), validationDependentXPath); 512 if(requiredIfValueNodeList != null && requiredIfValueNodeList.getLength() > 0) 513 { 514 for(int i=0; i<requiredIfValueNodeList.getLength(); i++) 515 { 516 Element element = (Element )requiredIfValueNodeList.item(0); 517 if(element.getFirstChild() != null && element.getFirstChild().getNodeValue() != null && element.getFirstChild().getNodeValue().equals(this.attributeName)) 518 element.getFirstChild().setNodeValue(this.newAttributeName); 519 } 520 } 521 } 522 catch(Exception ve) 523 { 524 ve.printStackTrace(); 525 } 526 527 saveUpdatedDefinition(document); 528 } 529 catch(Exception e) 530 { 531 e.printStackTrace(); 532 } 533 534 this.initialize(getContentTypeDefinitionId()); 535 536 return USE_EDITOR; 537 } 538 539 540 public String doUpdateAttributeParameterValue() throws Exception 541 { 542 this.initialize(getContentTypeDefinitionId()); 543 544 try 545 { 546 Document document = createDocumentFromDefinition(); 547 548 String parameterValueXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']/xs:annotation/xs:appinfo/params/param[@id='" + this.attributeParameterId +"']/values/value[@id='" + this.attributeParameterValueId + "']"; 549 NodeList parameterValuesNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), parameterValueXPath); 550 if(parameterValuesNodeList != null && parameterValuesNodeList.getLength() > 0) 551 { 552 Element element = (Element )parameterValuesNodeList.item(0); 553 element.setAttribute("id", this.newAttributeParameterValueId); 554 555 if(this.currentContentTypeEditorViewLanguageCode != null && this.currentContentTypeEditorViewLanguageCode.length() > 0) 556 element.setAttribute("label_" + this.currentContentTypeEditorViewLanguageCode, this.attributeParameterValueLabel); 557 else 558 element.setAttribute("label", this.attributeParameterValueLabel); 559 } 560 561 saveUpdatedDefinition(document); 562 } 563 catch(Exception e) 564 { 565 e.printStackTrace(); 566 } 567 568 this.initialize(getContentTypeDefinitionId()); 569 return USE_EDITOR; 570 } 571 572 573 public String doInsertAttributeValidator() throws Exception 574 { 575 this.initialize(getContentTypeDefinitionId()); 576 577 try 578 { 579 Document document = createDocumentFromDefinition(); 580 581 String validatorName = this.getRequest().getParameter("validatorName"); 582 583 if(validatorName != null && !validatorName.equalsIgnoreCase("")) 584 { 585 String validatorsXPath = "/xs:schema/xs:complexType[@name = 'Validation']/xs:annotation/xs:appinfo/form-validation/formset/form"; 586 Node formNode = org.apache.xpath.XPathAPI.selectSingleNode(document.getDocumentElement(), validatorsXPath); 587 if(formNode != null) 588 { 589 Element element = (Element )formNode; 590 591 Element newField = document.createElement("field"); 592 newField.setAttribute("property", this.attributeName); 593 newField.setAttribute("depends", validatorName); 594 595 String errorKey = null; 596 597 if(validatorName.equals("required")) 598 { 599 errorKey = "300"; } 601 else if(validatorName.equals("requiredif")) 602 { 603 errorKey = "300"; 605 Element newVar = document.createElement("var"); 606 Element varNameElement = createTextElement(document, "var-name", "dependent"); 607 Element varValueElement = createTextElement(document, "var-value", "AttributeName"); 608 newVar.appendChild(varNameElement); 609 newVar.appendChild(varValueElement); 610 newField.appendChild(newVar); 611 } 612 else if(validatorName.equals("matchRegexp")) 613 { 614 errorKey = "307"; 616 Element newVar = document.createElement("var"); 617 Element varNameElement = createTextElement(document, "var-name", "regexp"); 618 Element varValueElement = createTextElement(document, "var-value", ".*"); 619 newVar.appendChild(varNameElement); 620 newVar.appendChild(varValueElement); 621 newField.appendChild(newVar); 622 } 623 624 Element newMessage = document.createElement("msg"); 625 newMessage.setAttribute("name", validatorName); 626 newMessage.setAttribute("key", errorKey); 627 newField.appendChild(newMessage); 628 629 element.appendChild(newField); 630 } 631 } 632 633 saveUpdatedDefinition(document); 634 } 635 catch(Exception e) 636 { 637 e.printStackTrace(); 638 } 639 640 this.initialize(getContentTypeDefinitionId()); 641 return USE_EDITOR; 642 } 643 644 public String doUpdateAttributeValidatorArguments() throws Exception 645 { 646 this.initialize(getContentTypeDefinitionId()); 647 648 try 649 { 650 Document document = createDocumentFromDefinition(); 651 652 int i = 0; 653 String attributeValidatorName = this.getRequest().getParameter("attributeValidatorName"); 654 String argumentName = this.getRequest().getParameter(i + "_argumentName"); 655 656 while(argumentName != null && !argumentName.equalsIgnoreCase("")) 657 { 658 String argumentValue = this.getRequest().getParameter(i + "_argumentValue"); 659 660 String validatorsXPath = "/xs:schema/xs:complexType[@name = 'Validation']/xs:annotation/xs:appinfo/form-validation/formset/form/field[@property = '" + attributeName + "'][@depends = '" + attributeValidatorName + "']"; 661 Node fieldNode = org.apache.xpath.XPathAPI.selectSingleNode(document.getDocumentElement(), validatorsXPath); 662 if(fieldNode != null) 663 { 664 Element element = (Element )fieldNode; 665 NodeList nl = element.getElementsByTagName("var"); 666 for(int nlIndex=0; nlIndex < nl.getLength(); nlIndex++) 667 { 668 Node node = (Node )nl.item(nlIndex); 669 element.removeChild(node); 670 } 671 672 Element newVar = document.createElement("var"); 673 674 Element varNameElement = createTextElement(document, "var-name", argumentName); 675 Element varValueElement = createTextElement(document, "var-value", argumentValue); 676 newVar.appendChild(varNameElement); 677 newVar.appendChild(varValueElement); 678 679 element.appendChild(newVar); 680 } 681 682 i++; 683 argumentName = this.getRequest().getParameter(i + "_argumentName"); 684 } 685 686 saveUpdatedDefinition(document); 687 } 688 catch(Exception e) 689 { 690 e.printStackTrace(); 691 } 692 693 this.initialize(getContentTypeDefinitionId()); 694 return USE_EDITOR; 695 } 696 697 public String doDeleteAttributeValidator() throws Exception 698 { 699 this.initialize(getContentTypeDefinitionId()); 700 701 try 702 { 703 Document document = createDocumentFromDefinition(); 704 705 int i = 0; 706 String attributeValidatorName = this.getRequest().getParameter("attributeValidatorName"); 707 if(attributeValidatorName != null && !attributeValidatorName.equalsIgnoreCase("")) 708 { 709 String validatorsXPath = "/xs:schema/xs:complexType[@name = 'Validation']/xs:annotation/xs:appinfo/form-validation/formset/form/field[@property = '" + attributeName + "'][@depends = '" + attributeValidatorName + "']"; 710 Node fieldNode = org.apache.xpath.XPathAPI.selectSingleNode(document.getDocumentElement(), validatorsXPath); 711 if(fieldNode != null) 712 { 713 Element element = (Element )fieldNode; 714 element.getParentNode().removeChild(element); 715 } 716 } 717 718 saveUpdatedDefinition(document); 719 } 720 catch(Exception e) 721 { 722 e.printStackTrace(); 723 } 724 725 this.initialize(getContentTypeDefinitionId()); 726 return USE_EDITOR; 727 } 728 729 730 740 public List getDefinedAssetKeys() 741 { 742 return ContentTypeDefinitionController.getController().getDefinedAssetKeys(contentTypeDefinitionVO.getSchemaValue()); 743 } 744 745 748 public List getDefinedCategoryKeys() throws Exception 749 { 750 List categoryKeys = ContentTypeDefinitionController.getController().getDefinedCategoryKeys(contentTypeDefinitionVO.getSchemaValue()); 751 for (Iterator iter = categoryKeys.iterator(); iter.hasNext();) 752 { 753 CategoryAttribute info = (CategoryAttribute) iter.next(); 754 if(info.getCategoryId() != null) 755 info.setCategoryName(getCategoryName(info.getCategoryId())); 756 else 757 info.setCategoryName("Undefined"); 758 } 759 return categoryKeys; 760 } 761 762 767 public String getCategoryName(Integer id) 768 { 769 try 770 { 771 return categoryController.findById(id).getName(); 772 } 773 catch(SystemException e) 774 { 775 return "Category not found"; 776 } 777 } 778 779 public String doInsertAssetKey() throws Exception 780 { 781 this.initialize(getContentTypeDefinitionId()); 782 783 try 784 { 785 Document document = createDocumentFromDefinition(); 786 createNewEnumerationKey(document, ContentTypeDefinitionController.ASSET_KEYS); 787 saveUpdatedDefinition(document); 788 } 789 catch(Exception e) 790 { 791 logger.warn("Error adding asset key: ", e); 792 } 793 794 this.initialize(getContentTypeDefinitionId()); 795 return USE_EDITOR; 796 } 797 798 public String doInsertCategoryKey() throws Exception 799 { 800 this.initialize(getContentTypeDefinitionId()); 801 802 try 803 { 804 Document document = createDocumentFromDefinition(); 805 Element enumeration = createNewEnumerationKey(document, ContentTypeDefinitionController.CATEGORY_KEYS); 806 807 Element annotation = document.createElement("xs:annotation"); 808 Element appinfo = document.createElement("xs:appinfo"); 809 Element params = document.createElement("params"); 810 811 enumeration.appendChild(annotation); 812 annotation.appendChild(appinfo); 813 appinfo.appendChild(params); 814 params.appendChild(createTextElement(document, "title", getRandomName())); 815 params.appendChild(createTextElement(document, "description", getRandomName())); 816 params.appendChild(createTextElement(document, "categoryId", "")); 817 818 saveUpdatedDefinition(document); 819 } 820 catch(Exception e) 821 { 822 logger.warn("Error adding categories key: ", e); 823 } 824 825 this.initialize(getContentTypeDefinitionId()); 826 return USE_EDITOR; 827 } 828 829 830 831 public String doUpdateAssetKey() throws Exception 832 { 833 initialize(getContentTypeDefinitionId()); 834 try 835 { 836 Document document = createDocumentFromDefinition(); 837 updateAssetEnumerationKey(document, ContentTypeDefinitionController.ASSET_KEYS, getAssetKey(), getNewAssetKey(), this.description, this.maximumSize, this.allowedContentTypes, this.imageWidth, this.imageHeight); 838 saveUpdatedDefinition(document); 839 } 840 catch(Exception e) 841 { 842 logger.warn("Error updating asset key: ", e); 843 } 844 845 initialize(getContentTypeDefinitionId()); 846 return USE_EDITOR; 847 } 848 849 public String doUpdateCategoryKey() throws Exception 850 { 851 initialize(getContentTypeDefinitionId()); 852 853 try 854 { 855 Document document = createDocumentFromDefinition(); 856 Element enumeration = updateEnumerationKey(document, ContentTypeDefinitionController.CATEGORY_KEYS, getCategoryKey(), getNewCategoryKey()); 857 858 if(enumeration != null) 859 { 860 Element title = (Element )XPathAPI.selectSingleNode(enumeration, "xs:annotation/xs:appinfo/params/title"); 861 setTextElement(title, getSingleParameter("title")); 862 863 Element description = (Element )XPathAPI.selectSingleNode(enumeration, "xs:annotation/xs:appinfo/params/description"); 864 setTextElement(description, getSingleParameter("description")); 865 866 Element categoryId = (Element )XPathAPI.selectSingleNode(enumeration, "xs:annotation/xs:appinfo/params/categoryId"); 867 setTextElement(categoryId, getSingleParameter("categoryId")); 868 } 869 870 saveUpdatedDefinition(document); 871 } 872 catch(Exception e) 873 { 874 logger.warn("Error updating category key: ", e); 875 } 876 877 initialize(getContentTypeDefinitionId()); 878 return USE_EDITOR; 879 } 880 881 public String doDeleteAssetKey() throws Exception 882 { 883 return deleteKey(ContentTypeDefinitionController.ASSET_KEYS, getAssetKey()); 884 } 885 886 public String doDeleteCategoryKey() throws Exception 887 { 888 return deleteKey(ContentTypeDefinitionController.CATEGORY_KEYS, getCategoryKey()); 889 } 890 891 private String deleteKey(String keyType, String key) throws Exception 892 { 893 this.initialize(getContentTypeDefinitionId()); 894 895 try 896 { 897 Document document = createDocumentFromDefinition(); 898 899 String attributesXPath = "/xs:schema/xs:simpleType[@name = '" + keyType + "']/xs:restriction/xs:enumeration[@value='" + key + "']"; 900 NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 901 if(anl != null && anl.getLength() > 0) 902 { 903 Element element = (Element )anl.item(0); 904 element.getParentNode().removeChild(element); 905 } 906 907 saveUpdatedDefinition(document); 908 } 909 catch(Exception e) 910 { 911 logger.warn("Error updating key: " + keyType, e); 912 } 913 914 this.initialize(getContentTypeDefinitionId()); 915 return USE_EDITOR; 916 } 917 918 921 public CategoryController getCategoryController() 922 { 923 return categoryController; 924 } 925 926 929 public List getAllCategories() throws SystemException 930 { 931 return getCategoryController().findAllActiveCategories(); 932 } 933 934 940 private Document createDocumentFromDefinition() throws SAXException , IOException 941 { 942 String contentTypeDefinitionString = this.contentTypeDefinitionVO.getSchemaValue(); 943 InputSource xmlSource = new InputSource (new StringReader (contentTypeDefinitionString)); 944 DOMParser parser = new DOMParser(); 945 parser.parse(xmlSource); 946 return parser.getDocument(); 947 } 948 949 952 private void saveUpdatedDefinition(Document document) throws ConstraintException, SystemException 953 { 954 StringBuffer sb = new StringBuffer (); 955 XMLHelper.serializeDom(document.getDocumentElement(), sb); 956 this.contentTypeDefinitionVO.setSchemaValue(sb.toString()); 957 ContentTypeDefinitionController.getController().update(contentTypeDefinitionVO); 958 } 959 960 964 private Element createNewEnumerationKey(Document document, String keyType) throws TransformerException 965 { 966 Element enumeration = null; 967 String assetKeysXPath = "/xs:schema/xs:simpleType[@name = '" + keyType + "']/xs:restriction"; 968 NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), assetKeysXPath); 969 970 Element keyRestriction = null; 971 972 if(anl != null && anl.getLength() > 0) 973 { 974 keyRestriction = (Element )anl.item(0); 975 } 976 else 977 { 978 String schemaXPath = "/xs:schema"; 980 NodeList schemaNL = XPathAPI.selectNodeList(document.getDocumentElement(), schemaXPath); 981 if(schemaNL != null && schemaNL.getLength() > 0) 982 { 983 Element schemaElement = (Element )schemaNL.item(0); 984 985 Element keySimpleType = document.createElement("xs:simpleType"); 986 keySimpleType.setAttribute("name", keyType); 987 988 keyRestriction = document.createElement("xs:restriction"); 989 keyRestriction.setAttribute("base", "xs:string"); 990 991 keySimpleType.appendChild(keyRestriction); 992 schemaElement.appendChild(keySimpleType); 993 } 994 } 995 996 enumeration = document.createElement("xs:enumeration"); 997 enumeration.setAttribute("value", getRandomName()); 998 keyRestriction.appendChild(enumeration); 999 return enumeration; 1000 } 1001 1002 1006 private Element updateEnumerationKey(Document document, String keyType, String oldKey, String newKey) throws TransformerException 1007 { 1008 String attributesXPath = "/xs:schema/xs:simpleType[@name = '" + keyType + "']/xs:restriction/xs:enumeration[@value='" + oldKey + "']"; 1009 NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 1010 if(anl != null && anl.getLength() > 0) 1011 { 1012 Element enumeration = (Element )anl.item(0); 1013 enumeration.setAttribute("value", newKey); 1014 return enumeration; 1015 } 1016 1017 return null; 1018 } 1019 1020 1024 private Element updateAssetEnumerationKey(Document document, String keyType, String oldKey, String newKey, String description, Integer maximumSize, String allowedContentTypes, String imageWidth, String imageHeight) throws TransformerException 1025 { 1026 if(description == null) 1027 description = "Undefined"; 1028 if(maximumSize == null) 1029 maximumSize = new Integer (100); 1030 if(allowedContentTypes == null) 1031 allowedContentTypes = "*"; 1032 if(imageWidth == null) 1033 imageWidth = "*"; 1034 if(imageHeight == null) 1035 imageHeight = "*"; 1036 1037 1038 String attributesXPath = "/xs:schema/xs:simpleType[@name = '" + keyType + "']/xs:restriction/xs:enumeration[@value='" + oldKey + "']"; 1039 NodeList anl = XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 1040 if(anl != null && anl.getLength() > 0) 1041 { 1042 Element enumeration = (Element )anl.item(0); 1043 enumeration.setAttribute("value", newKey); 1044 1045 Element descriptionElement = (Element )XPathAPI.selectSingleNode(enumeration, "xs:annotation/xs:appinfo/params/description"); 1046 Element maximumSizeElement = (Element )XPathAPI.selectSingleNode(enumeration, "xs:annotation/xs:appinfo/params/maximumSize"); 1047 Element allowedContentTypesElement = (Element )XPathAPI.selectSingleNode(enumeration, "xs:annotation/xs:appinfo/params/allowedContentTypes"); 1048 Element imageWidthElement = (Element )XPathAPI.selectSingleNode(enumeration, "xs:annotation/xs:appinfo/params/imageWidth"); 1049 Element imageHeightElement = (Element )XPathAPI.selectSingleNode(enumeration, "xs:annotation/xs:appinfo/params/imageHeight"); 1050 1051 if(descriptionElement == null) 1052 { 1053 Element annotation = document.createElement("xs:annotation"); 1054 Element appinfo = document.createElement("xs:appinfo"); 1055 Element params = document.createElement("params"); 1056 1057 enumeration.appendChild(annotation); 1058 annotation.appendChild(appinfo); 1059 appinfo.appendChild(params); 1060 1061 descriptionElement = createTextElement(document, "description", getRandomName()); 1062 maximumSizeElement = createTextElement(document, "maximumSize", maximumSize.toString()); 1063 allowedContentTypesElement = createTextElement(document, "allowedContentTypes", allowedContentTypes); 1064 imageWidthElement = createTextElement(document, "imageWidth", imageWidth); 1065 imageHeightElement = createTextElement(document, "imageHeight", imageHeight); 1066 1067 params.appendChild(descriptionElement); 1068 params.appendChild(maximumSizeElement); 1069 params.appendChild(allowedContentTypesElement); 1070 params.appendChild(imageWidthElement); 1071 params.appendChild(imageHeightElement); 1072 } 1073 else 1074 { 1075 setTextElement(descriptionElement, description); 1076 setTextElement(maximumSizeElement, maximumSize.toString()); 1077 setTextElement(allowedContentTypesElement, allowedContentTypes); 1078 setTextElement(imageWidthElement, imageWidth); 1079 setTextElement(imageHeightElement, imageHeight); 1080 } 1081 1082 return enumeration; 1083 } 1084 1085 return null; 1086 } 1087 1088 1091 private Element createTextElement(Document document, String tagName, String value) 1092 { 1093 Element e = document.createElement(tagName); 1094 e.appendChild(document.createTextNode(value)); 1095 return e; 1096 } 1097 1098 1101 private void setTextElement(Element e, String value) 1102 { 1103 if(e.getFirstChild() != null) 1104 e.getFirstChild().setNodeValue(value); 1105 else 1106 e.appendChild(e.getOwnerDocument().createTextNode(value)); 1107 } 1108 1109 1110 1113 private String getRandomName() 1114 { 1115 return "undefined" + (int)(Math.random() * 100); 1116 } 1117 1118 1119 public Integer getContentTypeDefinitionId() 1123 { 1124 return this.contentTypeDefinitionVO.getContentTypeDefinitionId(); 1125 } 1126 1127 public void setContentTypeDefinitionId(Integer contentTypeDefinitionId) throws Exception 1128 { 1129 this.contentTypeDefinitionVO.setContentTypeDefinitionId(contentTypeDefinitionId); 1130 } 1131 1132 public String getTitle() 1133 { 1134 return this.title; 1135 } 1136 1137 public void setTitle(String title) 1138 { 1139 this.title = title; 1140 } 1141 1142 public String getName() 1143 { 1144 return this.contentTypeDefinitionVO.getName(); 1145 } 1146 1147 public String getSchemaValue() 1148 { 1149 return this.contentTypeDefinitionVO.getSchemaValue(); 1150 } 1151 1152 public Integer getType() 1153 { 1154 return this.contentTypeDefinitionVO.getType(); 1155 } 1156 1157 1160 1161 public List getContentTypeAttributes() 1162 { 1163 return this.attributes; 1164 } 1165 1166 public String getInputTypeId() 1167 { 1168 return inputTypeId; 1169 } 1170 1171 public void setInputTypeId(String string) 1172 { 1173 inputTypeId = string; 1174 } 1175 1176 public String getAttributeName() 1177 { 1178 return this.attributeName; 1179 } 1180 1181 public void setAttributeName(String attributeName) 1182 { 1183 this.attributeName = attributeName; 1184 } 1185 1186 public void setNewAttributeName(String newAttributeName) 1187 { 1188 this.newAttributeName = newAttributeName; 1189 } 1190 1191 public String getAttributeParameterValueId() 1192 { 1193 return attributeParameterValueId; 1194 } 1195 1196 public void setAttributeParameterValueId(String string) 1197 { 1198 attributeParameterValueId = string; 1199 } 1200 1201 public String getAttributeParameterId() 1202 { 1203 return attributeParameterId; 1204 } 1205 1206 public void setAttributeParameterId(String string) 1207 { 1208 attributeParameterId = string; 1209 } 1210 1211 public String getAttributeParameterValueLabel() 1212 { 1213 return attributeParameterValueLabel; 1214 } 1215 1216 public String getNewAttributeParameterValueId() 1217 { 1218 return newAttributeParameterValueId; 1219 } 1220 1221 public void setAttributeParameterValueLabel(String string) 1222 { 1223 attributeParameterValueLabel = string; 1224 } 1225 1226 public void setNewAttributeParameterValueId(String string) 1227 { 1228 newAttributeParameterValueId = string; 1229 } 1230 1231 public String getAttributeParameterValueLocale() 1232 { 1233 return attributeParameterValueLocale; 1234 } 1235 1236 public void setAttributeParameterValueLocale(String string) 1237 { 1238 attributeParameterValueLocale = string; 1239 } 1240 1241 public String getAttributeToExpand() 1242 { 1243 return attributeToExpand; 1244 } 1245 1246 public void setAttributeToExpand(String string) 1247 { 1248 attributeToExpand = string; 1249 } 1250 1251 public String getCurrentContentTypeEditorViewLanguageCode() 1252 { 1253 return currentContentTypeEditorViewLanguageCode; 1254 } 1255 1256 public void setCurrentContentTypeEditorViewLanguageCode(String string) 1257 { 1258 currentContentTypeEditorViewLanguageCode = string; 1259 } 1260 1261 public List getAvailableLanguages() 1262 { 1263 return availableLanguages; 1264 } 1265 1266 public String getAssetKey() { return assetKey; } 1267 public void setAssetKey(String s) { assetKey = s; } 1268 1269 public String getNewAssetKey() { return newAssetKey; } 1270 public void setNewAssetKey(String s) { newAssetKey = s; } 1271 1272 public String getCategoryKey() { return categoryKey; } 1273 public void setCategoryKey(String s) { categoryKey = s; } 1274 1275 public String getNewCategoryKey() { return newCategoryKey; } 1276 public void setNewCategoryKey(String s) { newCategoryKey = s; } 1277 1278 public String getErrorKey() 1279 { 1280 return "ContentTypeAttribute.updateAction"; 1281 } 1282 1283 public String getReturnAddress() 1284 { 1285 return "ViewListContentTypeDefinition.action"; 1286 } 1287 1288 public String getAllowedContentTypes() 1289 { 1290 return allowedContentTypes; 1291 } 1292 1293 public void setAllowedContentTypes(String assetContentType) 1294 { 1295 this.allowedContentTypes = assetContentType; 1296 } 1297 1298 public String getImageHeight() 1299 { 1300 return imageHeight; 1301 } 1302 1303 public void setImageHeight(String imageHeight) 1304 { 1305 this.imageHeight = imageHeight; 1306 } 1307 1308 public String getImageWidth() 1309 { 1310 return imageWidth; 1311 } 1312 1313 public void setImageWidth(String imageWidth) 1314 { 1315 this.imageWidth = imageWidth; 1316 } 1317 1318 public Integer getMaximumSize() 1319 { 1320 return maximumSize; 1321 } 1322 1323 public void setMaximumSize(Integer maximumSize) 1324 { 1325 this.maximumSize = maximumSize; 1326 } 1327 1328 public void setDescription(String description) 1329 { 1330 this.description = description; 1331 } 1332 public List getActivatedName() 1333 { 1334 return activatedName; 1335 } 1336} 1337 | Popular Tags |