1 17 package org.alfresco.web.bean.wizard; 18 19 import java.io.File ; 20 import java.io.Serializable ; 21 import java.text.MessageFormat ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.faces.context.FacesContext; 28 import javax.faces.model.SelectItem; 29 import javax.transaction.UserTransaction ; 30 31 import org.alfresco.config.Config; 32 import org.alfresco.config.ConfigElement; 33 import org.alfresco.config.ConfigService; 34 import org.alfresco.model.ContentModel; 35 import org.alfresco.service.ServiceRegistry; 36 import org.alfresco.service.cmr.dictionary.DictionaryService; 37 import org.alfresco.service.cmr.dictionary.TypeDefinition; 38 import org.alfresco.service.cmr.model.FileExistsException; 39 import org.alfresco.service.cmr.model.FileInfo; 40 import org.alfresco.service.cmr.repository.ContentData; 41 import org.alfresco.service.cmr.repository.ContentService; 42 import org.alfresco.service.cmr.repository.ContentWriter; 43 import org.alfresco.service.cmr.repository.MimetypeService; 44 import org.alfresco.service.cmr.repository.NodeRef; 45 import org.alfresco.service.namespace.QName; 46 import org.alfresco.web.app.Application; 47 import org.alfresco.web.bean.repository.Node; 48 import org.alfresco.web.bean.repository.Repository; 49 import org.alfresco.web.data.IDataContainer; 50 import org.alfresco.web.data.QuickSort; 51 import org.alfresco.web.ui.common.Utils; 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 55 60 public abstract class BaseContentWizard extends AbstractWizardBean 61 { 62 private static Log logger = LogFactory.getLog(BaseContentWizard.class); 63 64 protected static final String FINISH_INSTRUCTION_ID = "content_finish_instruction"; 65 66 protected String fileName; 68 protected String author; 69 protected String title; 70 protected String description; 71 protected String contentType; 72 protected String objectType; 73 protected boolean inlineEdit; 74 protected List <SelectItem> contentTypes; 75 protected List <SelectItem> objectTypes; 76 protected ContentService contentService; 77 protected DictionaryService dictionaryService; 78 79 protected NodeRef createdNode; 81 82 88 protected String saveContent(File fileContent, String strContent) 89 { 90 String outcome = FINISH_OUTCOME; 91 92 UserTransaction tx = null; 93 94 try 95 { 96 FacesContext context = FacesContext.getCurrentInstance(); 97 tx = Repository.getUserTransaction(context); 98 tx.begin(); 99 100 if (this.editMode) 101 { 102 Node currentDocument = this.browseBean.getDocument(); 104 NodeRef nodeRef = currentDocument.getNodeRef(); 105 106 this.fileFolderService.move(nodeRef, null, this.fileName); 108 Map <QName, Serializable > contentProps = this.nodeService.getProperties(nodeRef); 111 contentProps.put(ContentModel.PROP_TITLE, this.title); 112 contentProps.put(ContentModel.PROP_DESCRIPTION, this.description); 113 114 if (this.author != null && this.author.length() != 0) 116 { 117 if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_AUTHOR) == false) 118 { 119 Map <QName, Serializable > authorProps = new HashMap <QName, Serializable >(1, 1.0f); 120 authorProps.put(ContentModel.PROP_AUTHOR, this.author); 121 this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_AUTHOR, authorProps); 122 } 123 else 124 { 125 contentProps.put(ContentModel.PROP_AUTHOR, this.author); 126 } 127 } 128 129 ContentData contentData = (ContentData)contentProps.get(ContentModel.PROP_CONTENT); 131 if (contentData == null) 132 { 133 contentData = new ContentData(null, this.contentType, 0L, "UTF-8"); 134 } 135 else 136 { 137 contentData = new ContentData( 138 contentData.getContentUrl(), 139 this.contentType, 140 contentData.getSize(), 141 contentData.getEncoding()); 142 } 143 contentProps.put(ContentModel.PROP_CONTENT, contentData); 144 145 if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_INLINEEDITABLE) == false) 146 { 147 Map <QName, Serializable > editProps = new HashMap <QName, Serializable >(1, 1.0f); 148 editProps.put(ContentModel.PROP_EDITINLINE, this.inlineEdit); 149 this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_INLINEEDITABLE, editProps); 150 } 151 else 152 { 153 contentProps.put(ContentModel.PROP_EDITINLINE, this.inlineEdit); 154 } 155 this.nodeService.setProperties(nodeRef, contentProps); 156 } 157 else 158 { 159 NodeRef containerNodeRef; 161 String nodeId = getNavigator().getCurrentNodeId(); 162 if (nodeId == null) 163 { 164 containerNodeRef = this.nodeService.getRootNode(Repository.getStoreRef()); 165 } 166 else 167 { 168 containerNodeRef = new NodeRef(Repository.getStoreRef(), nodeId); 169 } 170 171 FileInfo fileInfo = fileFolderService.create( 172 containerNodeRef, 173 this.fileName, 174 Repository.resolveToQName(this.objectType)); 175 NodeRef fileNodeRef = fileInfo.getNodeRef(); 176 177 if (this.author != null && this.author.length() > 0) 179 { 180 Map <QName, Serializable > authorProps = new HashMap <QName, Serializable >(1, 1.0f); 181 authorProps.put(ContentModel.PROP_AUTHOR, this.author); 182 this.nodeService.addAspect(fileNodeRef, ContentModel.ASPECT_AUTHOR, authorProps); 183 } 184 185 if (logger.isDebugEnabled()) 186 logger.debug("Created file node for file: " + this.fileName); 187 188 Map <QName, Serializable > titledProps = new HashMap <QName, Serializable >(3, 1.0f); 190 titledProps.put(ContentModel.PROP_TITLE, this.title); 191 titledProps.put(ContentModel.PROP_DESCRIPTION, this.description); 192 this.nodeService.addAspect(fileNodeRef, ContentModel.ASPECT_TITLED, titledProps); 193 194 if (logger.isDebugEnabled()) 195 logger.debug("Added titled aspect with properties: " + titledProps); 196 197 if (this.inlineEdit == true) 199 { 200 Map <QName, Serializable > editProps = new HashMap <QName, Serializable >(1, 1.0f); 201 editProps.put(ContentModel.PROP_EDITINLINE, this.inlineEdit); 202 this.nodeService.addAspect(fileNodeRef, ContentModel.ASPECT_INLINEEDITABLE, editProps); 203 204 if (logger.isDebugEnabled()) 205 logger.debug("Added inlineeditable aspect with properties: " + editProps); 206 } 207 208 ContentWriter writer = contentService.getWriter(fileNodeRef, ContentModel.PROP_CONTENT, true); 210 writer.setMimetype(this.contentType); 212 writer.setEncoding("UTF-8"); 213 if (fileContent != null) 214 { 215 writer.putContent(fileContent); 216 } 217 else if (strContent != null) 218 { 219 writer.putContent(strContent); 220 } 221 222 this.createdNode = fileNodeRef; 224 } 225 226 performCustomProcessing(); 228 229 tx.commit(); 231 } 232 catch (FileExistsException e) 233 { 234 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 236 String statusMsg = MessageFormat.format( 238 Application.getMessage( 239 FacesContext.getCurrentInstance(), "error_exists"), 240 e.getExisting().getName()); 241 Utils.addErrorMessage(statusMsg); 242 outcome = null; 244 } 245 catch (Throwable e) 246 { 247 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 249 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 250 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e); 251 outcome = null; 252 } 253 254 return outcome; 255 } 256 257 260 public String getStepInstructions() 261 { 262 String stepInstruction = null; 263 264 switch (this.currentStep) 265 { 266 case 3: 267 { 268 stepInstruction = Application.getMessage(FacesContext.getCurrentInstance(), FINISH_INSTRUCTION_ID); 269 break; 270 } 271 default: 272 { 273 stepInstruction = Application.getMessage(FacesContext.getCurrentInstance(), DEFAULT_INSTRUCTION_ID); 274 } 275 } 276 277 return stepInstruction; 278 } 279 280 283 public void init() 284 { 285 super.init(); 286 287 this.fileName = null; 288 this.author = null; 289 this.title = null; 290 this.description = null; 291 this.contentType = null; 292 this.inlineEdit = false; 293 this.contentTypes = null; 294 this.objectTypes = null; 295 296 this.objectType = ContentModel.TYPE_CONTENT.toString(); 297 } 298 299 302 public void populate() 303 { 304 Node currentDocument = this.browseBean.getDocument(); 306 Map <String , Object > props = currentDocument.getProperties(); 307 308 Boolean inline = (Boolean )props.get("editInline"); 309 this.inlineEdit = inline != null ? inline.booleanValue() : false; 310 this.author = (String )props.get("creator"); 311 this.contentType = null; 312 ContentData contentData = (ContentData)props.get(ContentModel.PROP_CONTENT); 313 if (contentData != null) 314 { 315 this.contentType = contentData.getMimetype(); 316 } 317 this.description = (String )props.get("description"); 318 this.fileName = currentDocument.getName(); 319 this.title = (String )props.get("title"); 320 } 321 322 325 public ContentService getContentService() 326 { 327 return contentService; 328 } 329 330 333 public void setContentService(ContentService contentService) 334 { 335 this.contentService = contentService; 336 } 337 338 343 public void setDictionaryService(DictionaryService dictionaryService) 344 { 345 this.dictionaryService = dictionaryService; 346 } 347 348 351 public String getFileName() 352 { 353 return this.fileName; 354 } 355 356 359 public void setFileName(String fileName) 360 { 361 this.fileName = fileName; 362 } 363 364 367 public String getAuthor() 368 { 369 return this.author; 370 } 371 372 375 public void setAuthor(String author) 376 { 377 this.author = author; 378 } 379 380 383 public String getContentType() 384 { 385 return this.contentType; 386 } 387 388 391 public void setContentType(String contentType) 392 { 393 this.contentType = contentType; 394 } 395 396 399 public String getObjectType() 400 { 401 return this.objectType; 402 } 403 404 407 public void setObjectType(String objectType) 408 { 409 this.objectType = objectType; 410 } 411 412 415 public String getDescription() 416 { 417 return this.description; 418 } 419 420 423 public void setDescription(String description) 424 { 425 this.description = description; 426 } 427 428 431 public String getTitle() 432 { 433 return this.title; 434 } 435 436 439 public void setTitle(String title) 440 { 441 this.title = title; 442 } 443 444 447 public boolean isInlineEdit() 448 { 449 return this.inlineEdit; 450 } 451 452 455 public void setInlineEdit(boolean inlineEdit) 456 { 457 this.inlineEdit = inlineEdit; 458 } 459 460 463 public List <SelectItem> getContentTypes() 464 { 465 if (this.contentTypes == null) 466 { 467 this.contentTypes = new ArrayList <SelectItem>(80); 468 ServiceRegistry registry = Repository.getServiceRegistry(FacesContext.getCurrentInstance()); 469 MimetypeService mimetypeService = registry.getMimetypeService(); 470 471 Map <String , String > mimeTypes = mimetypeService.getDisplaysByMimetype(); 473 for (String mimeType : mimeTypes.keySet()) 474 { 475 this.contentTypes.add(new SelectItem(mimeType, mimeTypes.get(mimeType))); 476 } 477 478 QuickSort sorter = new QuickSort(this.contentTypes, "label", true, IDataContainer.SORT_CASEINSENSITIVE); 480 sorter.sort(); 481 } 482 483 return this.contentTypes; 484 } 485 486 489 public List <SelectItem> getObjectTypes() 490 { 491 if (this.objectTypes == null) 492 { 493 FacesContext context = FacesContext.getCurrentInstance(); 494 495 this.objectTypes = new ArrayList <SelectItem>(5); 497 this.objectTypes.add(new SelectItem(ContentModel.TYPE_CONTENT.toString(), 498 Application.getMessage(context, "content"))); 499 500 ConfigService svc = Application.getConfigService(FacesContext.getCurrentInstance()); 502 Config wizardCfg = svc.getConfig("Custom Content Types"); 503 if (wizardCfg != null) 504 { 505 ConfigElement typesCfg = wizardCfg.getConfigElement("content-types"); 506 if (typesCfg != null) 507 { 508 for (ConfigElement child : typesCfg.getChildren()) 509 { 510 QName idQName = Repository.resolveToQName(child.getAttribute("name")); 511 TypeDefinition typeDef = this.dictionaryService.getType(idQName); 512 513 if (typeDef != null && 514 this.dictionaryService.isSubClass(typeDef.getName(), ContentModel.TYPE_CONTENT)) 515 { 516 String label = null; 518 String msgId = child.getAttribute("displayLabelId"); 519 if (msgId != null) 520 { 521 label = Application.getMessage(context, msgId); 522 } 523 524 if (label == null) 526 { 527 label = child.getAttribute("displayLabel"); 528 } 529 530 if (label == null) 532 { 533 label = typeDef.getTitle(); 534 } 535 536 if (label == null) 538 { 539 label = idQName.getLocalName(); 540 } 541 542 this.objectTypes.add(new SelectItem(idQName.toString(), label)); 543 } 544 } 545 546 QuickSort sorter = new QuickSort(this.objectTypes, "label", true, IDataContainer.SORT_CASEINSENSITIVE); 548 sorter.sort(); 549 } 550 else 551 { 552 logger.warn("Could not find 'content-types' configuration element"); 553 } 554 } 555 else 556 { 557 logger.warn("Could not find 'Custom Content Types' configuration section"); 558 } 559 560 } 561 562 return this.objectTypes; 563 } 564 565 568 public boolean getNextFinishDisabled() 569 { 570 boolean disabled = false; 571 572 if (this.fileName == null || this.fileName.length() == 0 || 573 this.title == null || this.title.length() == 0 || 574 this.contentType == null) 575 { 576 disabled = true; 577 } 578 579 return disabled; 580 } 581 582 587 protected String getSummaryContentType() 588 { 589 ServiceRegistry registry = Repository.getServiceRegistry(FacesContext.getCurrentInstance()); 590 MimetypeService mimetypeService = registry.getMimetypeService(); 591 592 Map <String , String > mimeTypes = mimetypeService.getDisplaysByMimetype(); 594 return mimeTypes.get(this.contentType); 595 } 596 597 602 protected String getSummaryObjectType() 603 { 604 String objType = null; 605 606 for (SelectItem item : this.getObjectTypes()) 607 { 608 if (item.getValue().equals(this.objectType)) 609 { 610 objType = item.getLabel(); 611 break; 612 } 613 } 614 615 return objType; 616 } 617 618 621 protected void performCustomProcessing() 622 { 623 } 625 } 626 | Popular Tags |