1 23 24 28 29 package org.infoglue.cms.applications.contenttool.actions; 30 31 import java.io.IOException ; 32 import java.io.PrintWriter ; 33 import java.io.StringWriter ; 34 import java.io.UnsupportedEncodingException ; 35 import java.net.URLEncoder ; 36 import java.util.Collection ; 37 import java.util.Date ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 41 import org.apache.log4j.Logger; 42 import org.dom4j.Document; 43 import org.dom4j.DocumentHelper; 44 import org.dom4j.Element; 45 import org.dom4j.io.OutputFormat; 46 import org.dom4j.io.XMLWriter; 47 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction; 48 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController; 49 import org.infoglue.cms.controllers.kernel.impl.simple.ContentControllerProxy; 50 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController; 51 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController; 52 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController; 53 import org.infoglue.cms.controllers.kernel.impl.simple.RepositoryController; 54 import org.infoglue.cms.controllers.kernel.impl.simple.TransactionHistoryController; 55 import org.infoglue.cms.entities.content.ContentVO; 56 import org.infoglue.cms.entities.content.ContentVersionVO; 57 import org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl; 58 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO; 59 import org.infoglue.cms.entities.management.LanguageVO; 60 import org.infoglue.cms.entities.management.RepositoryVO; 61 import org.infoglue.cms.entities.management.TransactionHistoryVO; 62 import org.infoglue.cms.entities.management.impl.simple.ContentTypeDefinitionImpl; 63 import org.infoglue.cms.exception.Bug; 64 import org.infoglue.cms.exception.SystemException; 65 import org.infoglue.cms.treeservice.ss.ContentNodeSupplier; 66 67 import com.frovi.ss.Tree.BaseNode; 68 import com.frovi.ss.Tree.INodeSupplier; 69 70 public class ContentTreeXMLAction extends InfoGlueAbstractAction 71 { 72 private final static Logger logger = Logger.getLogger(ContentTreeXMLAction.class.getName()); 73 74 private static final long serialVersionUID = 3321168219082615063L; 75 76 private static String TYPE_FOLDER = "ContentFolder"; 77 private static String TYPE_ITEM = "ContentItem"; 78 private static String TYPE_REPOSITORY = "Repository"; 79 private String showLeafs = "yes"; 80 private Integer parent = null; 81 private Integer repositoryId = null; 82 private String urlArgSeparator = "&"; 83 private String action = ""; 84 private boolean createAction = false; 85 private boolean useTemplate = false; 86 private String [] allowedContentTypeIds = null; 87 88 public INodeSupplier getNodeSupplier() throws SystemException 89 { 90 return new ContentNodeSupplier(getRepositoryId(), this.getInfoGluePrincipal()); 91 } 92 93 public List getContentTypeDefinitions() throws Exception 94 { 95 return ContentTypeDefinitionController.getController().getContentTypeDefinitionVOList(); 96 } 97 98 private String makeAction(BaseNode node) throws UnsupportedEncodingException 99 { 100 String action = "javascript:onTreeItemClick(this,"; 101 action+="'" + node.getId() + "','" + repositoryId + "','" + URLEncoder.encode(node.getTitle(),"UTF-8") + "');"; 102 return action; 103 } 104 105 private String getFormattedDocument(Document doc) 106 { 107 return getFormattedDocument(doc, true); 108 } 109 110 private String getFormattedDocument(Document doc, boolean compact) 111 { 112 OutputFormat format = compact ? OutputFormat.createCompactFormat() : OutputFormat.createPrettyPrint(); 113 format.setEncoding("UTF-8"); 115 format.setExpandEmptyElements(false); 116 StringWriter stringWriter = new StringWriter (); 117 XMLWriter writer = new XMLWriter(stringWriter, format); 118 try 119 { 120 writer.write(doc); 121 } 122 catch (IOException e) 123 { 124 e.printStackTrace(); 125 } 126 return stringWriter.toString(); 127 } 128 129 public String doDigitalAssets() throws Exception 130 { 131 152 return null; 153 } 154 155 156 159 public String doLanguage() throws Exception 160 { 161 return null; 162 } 163 164 public ContentVersionVO getLatestContentVersionVO(Integer contentId, Integer languageId) 165 { 166 ContentVersionVO contentVersionVO = null; 167 try 168 { 169 contentVersionVO = ContentVersionController.getContentVersionController().getLatestActiveContentVersionVO(contentId, languageId); 170 } 171 catch(Exception e) 172 { 173 logger.error("An error occurred when we tried to get the latest version for the content:" + e.getMessage(), e); 174 } 175 176 return contentVersionVO; 177 } 178 179 public Element getContentVersionElement(Integer contentVersionId) throws SystemException, Bug, UnsupportedEncodingException 180 { 181 ContentVersionController contentVersionController = ContentVersionController.getContentVersionController(); 182 ContentVersionVO vo = contentVersionController.getContentVersionVOWithId(contentVersionId); 183 return getContentVersionElement(vo); 184 } 185 186 public Element getContentVersionElement(ContentVersionVO vo) throws SystemException, Bug, UnsupportedEncodingException 187 { 188 Element element = DocumentHelper.createElement("contentVersion"); 189 Element head = DocumentHelper.createElement("head"); 190 Element value = DocumentHelper.createElement("value"); 191 192 head.addAttribute("id", "" + vo.getContentVersionId()); 193 head.addAttribute("languageId", "" + vo.getLanguageId()); 194 head.addAttribute("languageName", vo.getLanguageName()); 195 head.addAttribute("isActive", "" + vo.getIsActive()); 196 197 TransactionHistoryController transactionHistoryController = TransactionHistoryController.getController(); 198 TransactionHistoryVO transactionHistoryVO = transactionHistoryController.getLatestTransactionHistoryVOForEntity(ContentVersionImpl.class, vo.getContentVersionId()); 199 if(transactionHistoryVO!=null) 200 head.addAttribute("mod", formatDate(transactionHistoryVO.getTransactionDateTime())); 201 value.addCDATA(URLEncoder.encode(vo.getVersionValue(),"UTF-8")); 203 element.add(head); 204 element.add(value); 205 return element; 206 } 207 208 211 public String doContentVersion() throws Exception 212 { 213 Document doc = DocumentHelper.createDocument(); 214 doc.add(getContentVersionElement(parent)); 215 return out(getFormattedDocument(doc)); 216 } 217 218 221 public String doContentVersions() throws Exception 222 { 223 Document doc = DocumentHelper.createDocument(); 224 Element root = doc.addElement("contentVersions"); 225 Collection availableLanguages = ContentController.getContentController().getRepositoryLanguages(parent); 226 for(Iterator i=availableLanguages.iterator();i.hasNext();) 227 { 228 LanguageVO lvo = (LanguageVO) i.next(); 229 ContentVersionVO vo = getLatestContentVersionVO(parent, lvo.getLanguageId()); 230 if(vo!=null) 231 root.add(getContentVersionElement(vo)); 232 } 233 234 ContentVersionController contentVersionController = ContentVersionController.getContentVersionController(); 235 return out(getFormattedDocument(doc)); 236 } 237 238 241 public String doContentTypeDefinitions() throws Exception 242 { 243 List contentTypeDefinitions = getContentTypeDefinitions(); 244 Document doc = DocumentHelper.createDocument(); 245 Element root = doc.addElement("definitions"); 246 TransactionHistoryController transactionHistoryController = TransactionHistoryController.getController(); 247 248 for(Iterator i=contentTypeDefinitions.iterator();i.hasNext();) 249 { 250 ContentTypeDefinitionVO vo = (ContentTypeDefinitionVO) i.next(); 251 if(vo.getType().compareTo(ContentTypeDefinitionVO.CONTENT)==0) 252 { 253 TransactionHistoryVO transactionHistoryVO = transactionHistoryController.getLatestTransactionHistoryVOForEntity(ContentTypeDefinitionImpl.class, vo.getContentTypeDefinitionId()); 254 255 Element definition = DocumentHelper.createElement("definition"); 256 definition 257 .addAttribute("id", "" + vo.getContentTypeDefinitionId()) 258 .addAttribute("type", "" + vo.getType()) 259 .addAttribute("name", vo.getName()) 260 ; 261 262 if(transactionHistoryVO!=null) 263 definition.addAttribute("mod", formatDate(transactionHistoryVO.getTransactionDateTime())); 264 265 Element schemaValue = definition.addElement("schemaValue"); 266 schemaValue.addCDATA(vo.getSchemaValue()); 267 root.add(definition); 268 } 269 } 270 return out(getFormattedDocument(doc)); 271 272 } 273 274 private String formatDate(Date date) 275 { 276 return "" + date; 277 } 278 279 282 public String doExecute() throws Exception 283 { 284 if (useTemplate) return "success"; 285 286 Document doc = DocumentHelper.createDocument(); 287 Element root = doc.addElement("tree"); 288 289 INodeSupplier sup; 290 293 if(repositoryId == null) 294 { 295 List repositories = RepositoryController.getController().getAuthorizedRepositoryVOList(this.getInfoGluePrincipal(), false); 296 for(Iterator i=repositories.iterator();i.hasNext();) 297 { 298 RepositoryVO r = (RepositoryVO) i.next(); 299 ContentVO contentVO = ContentControllerProxy.getController().getRootContentVO(r.getId(), this.getInfoGluePrincipal().getName()); 300 301 String SRC= action + "?repositoryId=" + r.getId() + urlArgSeparator + "parent=" + contentVO.getId(); 302 if(createAction && src.length() >0) src += urlArgSeparator + "createAction=true"; 303 if(action.length()>0 && src.length() >0) src += urlArgSeparator + "action=" + action; 304 String allowedContentTypeIdsUrlEncodedString = getAllowedContentTypeIdsAsUrlEncodedString(); 305 if(allowedContentTypeIdsUrlEncodedString.length()>0 && src.length() >0) src += urlArgSeparator + allowedContentTypeIdsUrlEncodedString; 306 String text=r.getName(); 307 Element element = root.addElement("tree"); 308 element 309 .addAttribute("id", "" + r.getId()) 310 .addAttribute("repositoryId", "" + r.getId()) 311 .addAttribute("text", text) 312 .addAttribute("src", src) 313 .addAttribute("type", TYPE_REPOSITORY); 314 } 315 out(getFormattedDocument(doc)); 316 return null; 317 } 318 319 sup = getNodeSupplier(); 320 ((ContentNodeSupplier) sup).setShowLeafs(showLeafs.compareTo("yes")==0); 321 ((ContentNodeSupplier) sup).setAllowedContentTypeIds(allowedContentTypeIds); 322 323 if(parent == null) 324 { 325 BaseNode node = sup.getRootNode(); 326 String text = node.getTitle(); 327 String type = TYPE_FOLDER; 328 String src = action + "?repositoryId=" + repositoryId + urlArgSeparator + "parent=" + node.getId(); 329 if(createAction && src.length() >0) src += urlArgSeparator + "createAction=true"; 330 if(action.length()>0 && src.length() >0) src += urlArgSeparator + "action=" + action; 331 String allowedContentTypeIdsUrlEncodedString = getAllowedContentTypeIdsAsUrlEncodedString(); 332 if(allowedContentTypeIdsUrlEncodedString.length()>0 && src.length() >0) src += urlArgSeparator + allowedContentTypeIdsUrlEncodedString; 333 334 335 Element elm = root.addElement("tree"); 336 elm 337 .addAttribute("id", "" + node.getId()) 338 .addAttribute("repositoryId", "" + repositoryId) 339 .addAttribute("text", text) 340 .addAttribute("src", src) 341 .addAttribute("type", type); 342 343 out(getFormattedDocument(doc)); 344 return null; 345 } 346 347 if(parent.intValue() > -1) 348 { 349 Collection containerNodes = sup.getChildContainerNodes(parent); 350 Collection childNodes = sup.getChildLeafNodes(parent); 351 352 ContentController contentController = ContentController.getContentController(); 353 ContentVersionController contentVersionController = ContentVersionController.getContentVersionController(); 354 355 Iterator it = containerNodes.iterator(); 356 while (it.hasNext()) 357 { 358 BaseNode theNode = (BaseNode) it.next(); 359 if (theNode.isContainer() && sup.hasChildren()) 360 { 361 theNode.setChildren(sup.hasChildren(theNode.getId())); 362 } 363 364 String src = theNode.hasChildren() ? action + "?repositoryId=" + repositoryId + urlArgSeparator + "parent=" + theNode.getId(): ""; 365 if(createAction && src.length() >0) src += urlArgSeparator + "createAction=true"; 366 if(createAction && src.length() >0) src += urlArgSeparator + "showLeafs=" + showLeafs; 367 if(action.length()>0 && src.length() >0) src += urlArgSeparator + "action=" + action; 368 String allowedContentTypeIdsUrlEncodedString = getAllowedContentTypeIdsAsUrlEncodedString(); 369 if(allowedContentTypeIdsUrlEncodedString.length()>0 && src.length() >0) src += urlArgSeparator + allowedContentTypeIdsUrlEncodedString; 370 371 Element elm = root.addElement("tree"); 372 elm 373 .addAttribute("id", "" + theNode.getId()) 374 .addAttribute("parent", "" + parent) 375 .addAttribute("repositoryId", "" + repositoryId) 376 .addAttribute("text", theNode.getTitle()) 377 .addAttribute("src", src) 378 .addAttribute("type", TYPE_FOLDER); 379 380 381 if(createAction) elm.addAttribute("action", makeAction(theNode)); 382 } 383 384 it = childNodes.iterator(); 385 while (it.hasNext()) 386 { 387 BaseNode theNode = (BaseNode) it.next(); 388 String text = theNode.getTitle(); 389 String action = makeAction(theNode); 390 String type = TYPE_ITEM; 391 Element elm = root.addElement("tree"); 392 elm 393 .addAttribute("id", "" + theNode.getId()) 394 .addAttribute("parent", "" + parent) 395 .addAttribute("repositoryId", "" + repositoryId) 396 .addAttribute("text", text) 397 .addAttribute("type", type) 398 ; 399 if(createAction) 400 elm.addAttribute("action", action); 401 else 402 { 403 ContentVersionVO activeVersion = contentVersionController.getLatestActiveContentVersionVO(theNode.getId(), LanguageController.getController().getMasterLanguage(repositoryId).getLanguageId()); 404 if(activeVersion!=null && !useTemplate) 405 elm.addAttribute("activeVersion", "" + activeVersion.getContentVersionId()); 406 } 407 408 if(!useTemplate) 409 elm.addAttribute("contentTypeId","" + contentController.getContentTypeDefinition(theNode.getId()).getContentTypeDefinitionId()); 410 } 411 412 out(getFormattedDocument(doc)); 413 return null; 414 } 415 416 return null; 417 } 418 419 private String out(String string) throws IOException 420 { 421 getResponse().setContentType("text/xml"); 422 getResponse().setContentLength(string.length()); 423 PrintWriter out = getResponse().getWriter(); 424 out.println(string); 425 return null; 426 } 427 428 public Integer getParent() 429 { 430 return parent; 431 } 432 433 public void setParent(Integer integer) 434 { 435 parent = integer; 436 } 437 438 public Integer getRepositoryId() 439 { 440 return repositoryId; 441 } 442 443 public void setRepositoryId(Integer integer) 444 { 445 repositoryId = integer; 446 } 447 448 public boolean isCreateAction() 449 { 450 return createAction; 451 } 452 453 public void setCreateAction(boolean createAction) 454 { 455 this.createAction = createAction; 456 } 457 458 public boolean isUseTemplate() 459 { 460 return useTemplate; 461 } 462 463 public void setUseTemplate(boolean useTemplate) 464 { 465 this.useTemplate = useTemplate; 466 } 467 468 public String getAction() 469 { 470 return action; 471 } 472 473 public void setAction(String action) 474 { 475 this.action = action; 476 } 477 478 public String getShowLeafs() 479 { 480 return showLeafs; 481 } 482 483 public void setShowLeafs(String showLeafs) 484 { 485 this.showLeafs = showLeafs; 486 } 487 488 public String [] getAllowedContentTypeIds() 489 { 490 return allowedContentTypeIds; 491 } 492 493 public void setAllowedContentTypeIds(String [] allowedContentTypeIds) 494 { 495 this.allowedContentTypeIds = allowedContentTypeIds; 496 } 497 498 public String getAllowedContentTypeIdsAsUrlEncodedString() throws Exception 499 { 500 StringBuffer sb = new StringBuffer (); 501 502 for(int i=0; i<allowedContentTypeIds.length; i++) 503 { 504 if(i > 0) 505 sb.append("&"); 506 507 sb.append("allowedContentTypeIds=" + URLEncoder.encode(allowedContentTypeIds[i], "UTF-8")); 508 } 509 510 return sb.toString(); 511 } 512 513 } 514 | Popular Tags |