1 23 24 package org.infoglue.cms.applications.contenttool.actions; 25 26 import java.io.StringReader ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 30 import org.apache.log4j.Logger; 31 import org.apache.xerces.parsers.DOMParser; 32 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction; 33 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController; 34 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController; 35 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController; 36 import org.infoglue.cms.io.FileHelper; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NodeList ; 40 import org.xml.sax.InputSource ; 41 42 43 48 49 public class ViewFormEditorAction extends InfoGlueAbstractAction { 51 private final static Logger logger = Logger.getLogger(ViewFormEditorAction.class.getName()); 52 53 private static final long serialVersionUID = 1L; 54 55 private Integer contentVersionId; 56 private String contentVersionAttributeName; 57 private String attributeName; 58 private String formDefinition; 59 private List attributes; 60 61 private String inputTypeId; 62 private String newAttributeParameterValueId; 63 private String currentContentTypeEditorViewLanguageCode; 64 private String attributeParameterValueLabel; 65 private String attributeParameterId; 66 private String attributeParameterValueId; 67 private String newAttributeName; 68 private String attributeParameterValueLocale; 69 private String attributeToExpand; 70 71 private List availableLanguages = null; 72 73 74 public ViewFormEditorAction() 75 { 76 } 77 78 protected void initialize() throws Exception 79 { 80 this.formDefinition = ContentVersionController.getContentVersionController().getAttributeValue(getContentVersionId(), getContentVersionAttributeName(), false); 81 logger.info("this.formDefinition:" + this.formDefinition); 82 83 boolean isFormDefinitionValid = true; 84 try 85 { 86 InputSource xmlSource = new InputSource (new StringReader (this.formDefinition)); 87 88 DOMParser parser = new DOMParser(); 89 parser.parse(xmlSource); 90 Document document = parser.getDocument(); 91 } 92 catch(Exception e) 93 { 94 isFormDefinitionValid = false; 95 } 96 97 if(this.formDefinition == null || this.formDefinition.equals("") || isFormDefinitionValid == false) 98 { 99 logger.info("Trying to get the default definition..."); 100 String schemaValue = ""; 101 try 102 { 103 String newFormDefinition = FileHelper.getStreamAsString(this.getClass().getResourceAsStream("/org/infoglue/cms/applications/defaultContentTypeDefinition.xml")); 104 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), newFormDefinition, this.getInfoGluePrincipal()); 105 this.formDefinition = ContentVersionController.getContentVersionController().getAttributeValue(getContentVersionId(), getContentVersionAttributeName(), false); 106 } 107 catch(Exception e) 108 { 109 logger.error("The system could not find the default content type definition:" + e.getMessage(), e); 110 } 111 } 112 113 this.attributes = ContentTypeDefinitionController.getController().getContentTypeAttributes(formDefinition); 114 this.availableLanguages = LanguageController.getController().getLanguageVOList(); 115 } 116 117 120 121 public String doExecute() throws Exception 122 { 123 this.initialize(); 124 return "success"; 125 } 126 127 public String doInsertAttribute() throws Exception 128 { 129 this.initialize(); 130 String newFormDefinition = ContentTypeDefinitionController.getController().insertContentTypeAttribute(this.formDefinition, this.inputTypeId, new ArrayList ()); 131 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), newFormDefinition, this.getInfoGluePrincipal()); 132 133 this.initialize(); 134 135 return "success"; 136 } 137 138 139 public String doDeleteAttribute() throws Exception 140 { 141 this.initialize(); 142 143 try 144 { 145 InputSource xmlSource = new InputSource (new StringReader (this.formDefinition)); 146 147 DOMParser parser = new DOMParser(); 148 parser.parse(xmlSource); 149 Document document = parser.getDocument(); 150 151 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']"; 152 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 153 if(anl != null && anl.getLength() > 0) 154 { 155 Element element = (Element )anl.item(0); 156 element.getParentNode().removeChild(element); 157 } 158 159 StringBuffer sb = new StringBuffer (); 160 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 161 162 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal()); 163 } 164 catch(Exception e) 165 { 166 e.printStackTrace(); 167 } 168 169 this.initialize(); 170 171 return "success"; 172 } 173 174 177 178 public String doMoveAttributeUp() throws Exception 179 { 180 this.initialize(); 181 182 try 183 { 184 InputSource xmlSource = new InputSource (new StringReader (this.formDefinition)); 185 186 DOMParser parser = new DOMParser(); 187 parser.parse(xmlSource); 188 Document document = parser.getDocument(); 189 190 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 191 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 192 Element previousElement = null; 193 for(int i=0; i < anl.getLength(); i++) 194 { 195 Element element = (Element )anl.item(i); 196 if(element.getAttribute("name").equalsIgnoreCase(this.attributeName) && previousElement != null) 197 { 198 Element parent = (Element )element.getParentNode(); 199 parent.removeChild(element); 200 parent.insertBefore(element, previousElement); 201 } 202 previousElement = element; 203 } 204 205 StringBuffer sb = new StringBuffer (); 206 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 207 208 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal()); 209 } 210 catch(Exception e) 211 { 212 e.printStackTrace(); 213 } 214 215 this.initialize(); 216 return "success"; 217 } 218 219 220 223 224 public String doMoveAttributeDown() throws Exception 225 { 226 this.initialize(); 227 228 try 229 { 230 InputSource xmlSource = new InputSource (new StringReader (this.formDefinition)); 231 232 DOMParser parser = new DOMParser(); 233 parser.parse(xmlSource); 234 Document document = parser.getDocument(); 235 236 String attributesXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element"; 237 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 238 Element parent = null; 239 Element elementToMove = null; 240 boolean isInserted = false; 241 int position = 0; 242 for(int i=0; i < anl.getLength(); i++) 243 { 244 Element element = (Element )anl.item(i); 245 parent = (Element )element.getParentNode(); 246 247 if(elementToMove != null) 248 { 249 if(position == 2) 250 { 251 parent.insertBefore(elementToMove, element); 252 isInserted = true; 253 break; 254 } 255 else 256 position++; 257 } 258 259 if(element.getAttribute("name").equalsIgnoreCase(this.attributeName)) 260 { 261 elementToMove = element; 262 parent.removeChild(elementToMove); 263 position++; 264 } 265 } 266 267 if(!isInserted && elementToMove != null) 268 parent.appendChild(elementToMove); 269 270 StringBuffer sb = new StringBuffer (); 271 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 272 273 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal()); 274 } 275 catch(Exception e) 276 { 277 e.printStackTrace(); 278 } 279 280 this.initialize(); 281 282 return "success"; 283 } 284 285 286 public String doUpdateAttribute() throws Exception 287 { 288 this.initialize(); 289 290 try 291 { 292 InputSource xmlSource = new InputSource (new StringReader (this.formDefinition)); 293 294 DOMParser parser = new DOMParser(); 295 parser.parse(xmlSource); 296 Document document = parser.getDocument(); 297 298 String [] extraParameterNames = getRequest().getParameterValues("parameterNames"); 300 if(extraParameterNames != null) 301 { 302 for(int i=0; i < extraParameterNames.length; i++) 303 { 304 String extraParameterName = extraParameterNames[i]; 305 String value = getRequest().getParameter(extraParameterName); 306 307 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"; 308 NodeList extraParamsNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), extraParametersXPath); 309 if(extraParamsNodeList != null && extraParamsNodeList.getLength() > 0) 310 { 311 Element element = (Element )extraParamsNodeList.item(0); 312 313 if(extraParameterName.equalsIgnoreCase("values") && (this.inputTypeId.equalsIgnoreCase("select") || this.inputTypeId.equalsIgnoreCase("checkbox") || this.inputTypeId.equalsIgnoreCase("radiobutton"))) 314 { 315 ((Element )element.getParentNode().getParentNode()).setAttribute("inputTypeId", "1"); 316 } 317 else 318 { 319 ((Element )element.getParentNode().getParentNode()).setAttribute("inputTypeId", "0"); 320 } 321 322 if(((Element )element.getParentNode().getParentNode()).getAttribute("inputTypeId").equals("0")) 323 { 324 if(this.currentContentTypeEditorViewLanguageCode != null && this.currentContentTypeEditorViewLanguageCode.length() > 0) 325 { 326 element.setAttribute("label_" + this.currentContentTypeEditorViewLanguageCode, value); 327 } 328 else 329 { 330 element.setAttribute("label", value); 331 } 332 } 333 } 334 } 335 } 336 337 String attributeXPath = "/xs:schema/xs:complexType/xs:all/xs:element/xs:complexType/xs:all/xs:element[@name='" + this.attributeName + "']"; 339 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributeXPath); 340 if(anl != null && anl.getLength() > 0) 341 { 342 Element element = (Element )anl.item(0); 343 element.setAttribute("name", this.newAttributeName); 344 element.setAttribute("type", this.inputTypeId); 345 } 346 347 StringBuffer sb = new StringBuffer (); 348 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 349 350 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal()); 351 } 352 catch(Exception e) 353 { 354 e.printStackTrace(); 355 } 356 357 this.initialize(); 358 359 return "success"; 360 } 361 362 363 public String doInsertAttributeParameterValue() throws Exception 364 { 365 this.initialize(); 366 367 try 368 { 369 InputSource xmlSource = new InputSource (new StringReader (this.formDefinition)); 370 371 DOMParser parser = new DOMParser(); 372 parser.parse(xmlSource); 373 Document document = parser.getDocument(); 374 375 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"; 376 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 377 if(anl != null && anl.getLength() > 0) 378 { 379 Element element = (Element )anl.item(0); 380 Element newValue = document.createElement("value"); 381 newValue.setAttribute("id", "undefined" + (int)(Math.random() * 100)); 382 newValue.setAttribute("label", "undefined" + (int)(Math.random() * 100)); 383 element.appendChild(newValue); 384 } 385 386 StringBuffer sb = new StringBuffer (); 387 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 388 389 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal()); 390 } 391 catch(Exception e) 392 { 393 e.printStackTrace(); 394 } 395 396 this.initialize(); 397 398 return "success"; 399 } 400 401 402 public String doDeleteAttributeParameterValue() throws Exception 403 { 404 this.initialize(); 405 406 try 407 { 408 InputSource xmlSource = new InputSource (new StringReader (this.formDefinition)); 409 410 DOMParser parser = new DOMParser(); 411 parser.parse(xmlSource); 412 Document document = parser.getDocument(); 413 414 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 + "']"; 415 NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), attributesXPath); 416 if(anl != null && anl.getLength() > 0) 417 { 418 Element element = (Element )anl.item(0); 419 element.getParentNode().removeChild(element); 420 } 421 422 StringBuffer sb = new StringBuffer (); 423 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 424 425 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal()); 426 } 427 catch(Exception e) 428 { 429 e.printStackTrace(); 430 } 431 432 this.initialize(); 433 434 return "success"; 435 } 436 437 438 439 public String doUpdateAttributeParameterValue() throws Exception 440 { 441 this.initialize(); 442 443 try 444 { 445 InputSource xmlSource = new InputSource (new StringReader (this.formDefinition)); 446 447 DOMParser parser = new DOMParser(); 448 parser.parse(xmlSource); 449 Document document = parser.getDocument(); 450 451 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 + "']"; 452 NodeList parameterValuesNodeList = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), parameterValueXPath); 453 if(parameterValuesNodeList != null && parameterValuesNodeList.getLength() > 0) 454 { 455 Element element = (Element )parameterValuesNodeList.item(0); 456 element.setAttribute("id", this.newAttributeParameterValueId); 457 458 if(this.currentContentTypeEditorViewLanguageCode != null && this.currentContentTypeEditorViewLanguageCode.length() > 0) 459 element.setAttribute("label_" + this.currentContentTypeEditorViewLanguageCode, this.attributeParameterValueLabel); 460 else 461 element.setAttribute("label", this.attributeParameterValueLabel); 462 } 463 464 StringBuffer sb = new StringBuffer (); 465 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb); 466 467 ContentVersionController.getContentVersionController().updateAttributeValue(getContentVersionId(), getContentVersionAttributeName(), sb.toString(), this.getInfoGluePrincipal()); 468 } 469 catch(Exception e) 470 { 471 e.printStackTrace(); 472 } 473 474 this.initialize(); 475 476 return "success"; 477 } 478 479 public List getAvailableLanguages() 480 { 481 return this.availableLanguages; 482 } 483 484 public String getAttributeName() 485 { 486 return this.attributeName; 487 } 488 489 public Integer getContentVersionId() 490 { 491 return this.contentVersionId; 492 } 493 494 public String getFormDefinition() 495 { 496 return this.formDefinition; 497 } 498 499 public void setAttributeName(String attributeName) 500 { 501 this.attributeName = attributeName; 502 } 503 504 public void setContentVersionId(Integer contentVersionId) 505 { 506 this.contentVersionId = contentVersionId; 507 } 508 509 public void setFormDefinition(String formDefinition) 510 { 511 this.formDefinition = formDefinition; 512 } 513 514 public List getAttributes() 515 { 516 return attributes; 517 } 518 519 public void setAttributes(List attributes) 520 { 521 this.attributes = attributes; 522 } 523 524 public String getInputTypeId() 525 { 526 return this.inputTypeId; 527 } 528 529 public void setInputTypeId(String inputTypeId) 530 { 531 this.inputTypeId = inputTypeId; 532 } 533 534 public String getContentVersionAttributeName() 535 { 536 return this.contentVersionAttributeName; 537 } 538 539 public void setContentVersionAttributeName(String contentVersionAttributeName) 540 { 541 this.contentVersionAttributeName = contentVersionAttributeName; 542 } 543 544 public String getAttributeParameterId() 545 { 546 return this.attributeParameterId; 547 } 548 549 public String getAttributeParameterValueId() 550 { 551 return this.attributeParameterValueId; 552 } 553 554 public String getAttributeParameterValueLabel() 555 { 556 return this.attributeParameterValueLabel; 557 } 558 559 public String getAttributeParameterValueLocale() 560 { 561 return this.attributeParameterValueLocale; 562 } 563 564 public String getAttributeToExpand() 565 { 566 return this.attributeToExpand; 567 } 568 569 public String getCurrentContentTypeEditorViewLanguageCode() 570 { 571 return this.currentContentTypeEditorViewLanguageCode; 572 } 573 574 public String getNewAttributeName() 575 { 576 return this.newAttributeName; 577 } 578 579 public String getNewAttributeParameterValueId() 580 { 581 return this.newAttributeParameterValueId; 582 } 583 584 public void setAttributeParameterId(String attributeParameterId) 585 { 586 this.attributeParameterId = attributeParameterId; 587 } 588 589 public void setAttributeParameterValueId(String attributeParameterValueId) 590 { 591 this.attributeParameterValueId = attributeParameterValueId; 592 } 593 594 public void setAttributeParameterValueLabel(String attributeParameterValueLabel) 595 { 596 this.attributeParameterValueLabel = attributeParameterValueLabel; 597 } 598 599 public void setAttributeParameterValueLocale(String attributeParameterValueLocale) 600 { 601 this.attributeParameterValueLocale = attributeParameterValueLocale; 602 } 603 604 public void setAttributeToExpand(String attributeToExpand) 605 { 606 this.attributeToExpand = attributeToExpand; 607 } 608 609 public void setCurrentContentTypeEditorViewLanguageCode(String currentContentTypeEditorViewLanguageCode) 610 { 611 this.currentContentTypeEditorViewLanguageCode = currentContentTypeEditorViewLanguageCode; 612 } 613 614 public void setNewAttributeName(String newAttributeName) 615 { 616 this.newAttributeName = newAttributeName; 617 } 618 619 public void setNewAttributeParameterValueId(String newAttributeParameterValueId) 620 { 621 this.newAttributeParameterValueId = newAttributeParameterValueId; 622 } 623 624 } 625 | Popular Tags |