1 17 package org.alfresco.web.bean; 18 19 import java.io.Serializable ; 20 import java.text.MessageFormat ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.faces.context.FacesContext; 28 import javax.faces.event.ActionEvent; 29 import javax.faces.model.SelectItem; 30 import javax.transaction.UserTransaction ; 31 32 import org.alfresco.config.Config; 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.DataTypeDefinition; 37 import org.alfresco.service.cmr.dictionary.DictionaryService; 38 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 39 import org.alfresco.service.cmr.model.FileExistsException; 40 import org.alfresco.service.cmr.model.FileFolderService; 41 import org.alfresco.service.cmr.repository.AssociationRef; 42 import org.alfresco.service.cmr.repository.ChildAssociationRef; 43 import org.alfresco.service.cmr.repository.ContentData; 44 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 45 import org.alfresco.service.cmr.repository.MimetypeService; 46 import org.alfresco.service.cmr.repository.NodeRef; 47 import org.alfresco.service.cmr.repository.NodeService; 48 import org.alfresco.service.namespace.QName; 49 import org.alfresco.web.app.Application; 50 import org.alfresco.web.bean.repository.Node; 51 import org.alfresco.web.bean.repository.Repository; 52 import org.alfresco.web.config.PropertySheetConfigElement; 53 import org.alfresco.web.data.IDataContainer; 54 import org.alfresco.web.data.QuickSort; 55 import org.alfresco.web.ui.common.Utils; 56 57 62 public class DocumentPropertiesBean 63 { 64 private static final String TEMP_PROP_MIMETYPE = "mimetype"; 65 66 protected NodeService nodeService; 67 protected FileFolderService fileFolderService; 68 protected DictionaryService dictionaryService; 69 protected BrowseBean browseBean; 70 private List <SelectItem> contentTypes; 71 private Node editableNode; 72 private Boolean hasOtherProperties; 73 74 79 public Node getEditableNode() 80 { 81 return this.editableNode; 82 } 83 84 89 public void setupDocumentForAction(ActionEvent event) 90 { 91 this.editableNode = new Node(this.browseBean.getDocument().getNodeRef()); 92 93 ContentData content = (ContentData)this.editableNode.getProperties().get(ContentModel.PROP_CONTENT); 97 if (content != null) 98 { 99 this.editableNode.getProperties().put(TEMP_PROP_MIMETYPE, content.getMimetype()); 100 } 101 102 this.hasOtherProperties = null; 103 } 104 105 110 public String save() 111 { 112 String outcome = "cancel"; 113 114 UserTransaction tx = null; 115 116 try 117 { 118 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance()); 119 tx.begin(); 120 121 NodeRef nodeRef = this.browseBean.getDocument().getNodeRef(); 122 Map <String , Object > props = this.editableNode.getProperties(); 123 124 String name = (String ) props.get(ContentModel.PROP_NAME); 126 if (name != null) 127 { 128 fileFolderService.rename(nodeRef, name); 129 } 130 131 Map <QName, Serializable > properties = this.nodeService.getProperties(nodeRef); 132 135 String mimetype = (String )props.get(TEMP_PROP_MIMETYPE); 137 if (mimetype != null) 138 { 139 props.remove(TEMP_PROP_MIMETYPE); 141 ContentData contentData = (ContentData)props.get(ContentModel.PROP_CONTENT); 142 if (contentData != null) 143 { 144 contentData = ContentData.setMimetype(contentData, mimetype); 145 props.put(ContentModel.PROP_CONTENT.toString(), contentData); 146 } 147 } 148 149 String author = (String )props.get(ContentModel.PROP_AUTHOR); 151 if (author != null && author.length() != 0) 152 { 153 if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_AUTHOR) == false) 155 { 156 Map <QName, Serializable > authorProps = new HashMap <QName, Serializable >(1, 1.0f); 157 authorProps.put(ContentModel.PROP_AUTHOR, author); 158 this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_AUTHOR, authorProps); 159 } 160 } 162 163 String title = (String )props.get(ContentModel.PROP_TITLE); 165 String description = (String )props.get(ContentModel.PROP_DESCRIPTION); 166 if (title != null || description != null) 167 { 168 nodeService.addAspect(nodeRef, ContentModel.ASPECT_TITLED, null); 170 } 172 173 Iterator <String > iterProps = props.keySet().iterator(); 175 while (iterProps.hasNext()) 176 { 177 String propName = iterProps.next(); 178 QName qname = QName.createQName(propName); 179 180 Serializable propValue = (Serializable )props.get(propName); 182 183 if ((propValue != null) && (propValue instanceof String ) && 185 (propValue.toString().length() == 0)) 186 { 187 PropertyDefinition propDef = this.dictionaryService.getProperty(qname); 188 if (propDef != null) 189 { 190 if (propDef.getDataType().getName().equals(DataTypeDefinition.DOUBLE) || 191 propDef.getDataType().getName().equals(DataTypeDefinition.FLOAT) || 192 propDef.getDataType().getName().equals(DataTypeDefinition.INT) || 193 propDef.getDataType().getName().equals(DataTypeDefinition.LONG)) 194 { 195 propValue = null; 196 } 197 } 198 } 199 200 properties.put(qname, propValue); 201 } 202 203 this.nodeService.setProperties(this.browseBean.getDocument().getNodeRef(), properties); 205 206 208 Map <String , Map <String , AssociationRef>> addedAssocs = this.editableNode.getAddedAssociations(); 210 for (Map <String , AssociationRef> typedAssoc : addedAssocs.values()) 211 { 212 for (AssociationRef assoc : typedAssoc.values()) 213 { 214 this.nodeService.createAssociation(assoc.getSourceRef(), assoc.getTargetRef(), assoc.getTypeQName()); 215 } 216 } 217 218 Map <String , Map <String , AssociationRef>> removedAssocs = this.editableNode.getRemovedAssociations(); 220 for (Map <String , AssociationRef> typedAssoc : removedAssocs.values()) 221 { 222 for (AssociationRef assoc : typedAssoc.values()) 223 { 224 this.nodeService.removeAssociation(assoc.getSourceRef(), assoc.getTargetRef(), assoc.getTypeQName()); 225 } 226 } 227 228 Map <String , Map <String , ChildAssociationRef>> addedChildAssocs = this.editableNode.getAddedChildAssociations(); 230 for (Map <String , ChildAssociationRef> typedAssoc : addedChildAssocs.values()) 231 { 232 for (ChildAssociationRef assoc : typedAssoc.values()) 233 { 234 this.nodeService.addChild(assoc.getParentRef(), assoc.getChildRef(), assoc.getTypeQName(), assoc.getTypeQName()); 235 } 236 } 237 238 Map <String , Map <String , ChildAssociationRef>> removedChildAssocs = this.editableNode.getRemovedChildAssociations(); 240 for (Map <String , ChildAssociationRef> typedAssoc : removedChildAssocs.values()) 241 { 242 for (ChildAssociationRef assoc : typedAssoc.values()) 243 { 244 this.nodeService.removeChild(assoc.getParentRef(), assoc.getChildRef()); 245 } 246 } 247 248 tx.commit(); 250 251 outcome = "finish"; 253 254 this.browseBean.getDocument().reset(); 256 } 257 catch (FileExistsException e) 258 { 259 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 261 String statusMsg = MessageFormat.format( 263 Application.getMessage( 264 FacesContext.getCurrentInstance(), "error_exists"), 265 e.getExisting().getName()); 266 Utils.addErrorMessage(statusMsg); 267 outcome = null; 269 } 270 catch (InvalidNodeRefException err) 271 { 272 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 274 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 275 FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object [] {this.browseBean.getDocument().getId()}) ); 276 outcome = "browse"; 278 } 279 catch (Throwable e) 280 { 281 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 283 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 284 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e); 285 } 286 287 return outcome; 288 } 289 290 public Map <String , Object > getProperties() 291 { 292 return this.editableNode.getProperties(); 293 } 294 295 298 public List <SelectItem> getContentTypes() 299 { 300 if (this.contentTypes == null) 301 { 302 this.contentTypes = new ArrayList <SelectItem>(80); 303 ServiceRegistry registry = Repository.getServiceRegistry(FacesContext.getCurrentInstance()); 304 MimetypeService mimetypeService = registry.getMimetypeService(); 305 306 Map <String , String > mimeTypes = mimetypeService.getDisplaysByMimetype(); 308 for (String mimeType : mimeTypes.keySet()) 309 { 310 this.contentTypes.add(new SelectItem(mimeType, mimeTypes.get(mimeType))); 311 } 312 313 QuickSort sorter = new QuickSort(this.contentTypes, "label", true, IDataContainer.SORT_CASEINSENSITIVE); 315 sorter.sort(); 316 } 317 318 return this.contentTypes; 319 } 320 321 327 public boolean getOtherPropertiesPresent() 328 { 329 if (this.hasOtherProperties == null) 330 { 331 ConfigService configSvc = Application.getConfigService(FacesContext.getCurrentInstance()); 334 Config configProps = configSvc.getConfig(this.editableNode); 335 PropertySheetConfigElement propsToDisplay = (PropertySheetConfigElement)configProps. 336 getConfigElement("property-sheet"); 337 338 if (propsToDisplay != null && propsToDisplay.getEditableItemNamesToShow().size() > 0) 339 { 340 this.hasOtherProperties = Boolean.TRUE; 341 } 342 else 343 { 344 this.hasOtherProperties = Boolean.FALSE; 345 } 346 } 347 348 return this.hasOtherProperties.booleanValue(); 349 } 350 351 354 public NodeService getNodeService() 355 { 356 return this.nodeService; 357 } 358 359 362 public void setNodeService(NodeService nodeService) 363 { 364 this.nodeService = nodeService; 365 } 366 367 370 public void setFileFolderService(FileFolderService fileFolderService) 371 { 372 this.fileFolderService = fileFolderService; 373 } 374 375 380 public void setDictionaryService(DictionaryService dictionaryService) 381 { 382 this.dictionaryService = dictionaryService; 383 } 384 385 388 public BrowseBean getBrowseBean() 389 { 390 return this.browseBean; 391 } 392 393 396 public void setBrowseBean(BrowseBean browseBean) 397 { 398 this.browseBean = browseBean; 399 } 400 } 401 | Popular Tags |