KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > DocumentPropertiesBean


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean;
18
19 import java.io.Serializable JavaDoc;
20 import java.text.MessageFormat JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.faces.context.FacesContext;
28 import javax.faces.event.ActionEvent;
29 import javax.faces.model.SelectItem;
30 import javax.transaction.UserTransaction JavaDoc;
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 /**
58  * Backing bean for the edit document properties dialog
59  *
60  * @author gavinc
61  */

62 public class DocumentPropertiesBean
63 {
64    private static final String JavaDoc TEMP_PROP_MIMETYPE = "mimetype";
65    
66    protected NodeService nodeService;
67    protected FileFolderService fileFolderService;
68    protected DictionaryService dictionaryService;
69    protected BrowseBean browseBean;
70    private List JavaDoc<SelectItem> contentTypes;
71    private Node editableNode;
72    private Boolean JavaDoc hasOtherProperties;
73    
74    /**
75     * Returns the node being edited
76     *
77     * @return The node being edited
78     */

79    public Node getEditableNode()
80    {
81       return this.editableNode;
82    }
83    
84    /**
85     * Event handler called to setup the document for property editing
86     *
87     * @param event The event
88     */

89    public void setupDocumentForAction(ActionEvent event)
90    {
91       this.editableNode = new Node(this.browseBean.getDocument().getNodeRef());
92       
93       // special case for Mimetype - since this is a sub-property of the ContentData object
94
// we must extract it so it can be edited in the client, then we check for it later
95
// and create a new ContentData object to wrap it and it's associated URL
96
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    /**
106     * Event handler used to save the edited properties back to the repository
107     *
108     * @return The outcome
109     */

110    public String JavaDoc save()
111    {
112       String JavaDoc outcome = "cancel";
113       
114       UserTransaction JavaDoc 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 JavaDoc<String JavaDoc, Object JavaDoc> props = this.editableNode.getProperties();
123          
124          // get the name and move the node as necessary
125
String JavaDoc name = (String JavaDoc) props.get(ContentModel.PROP_NAME);
126          if (name != null)
127          {
128             fileFolderService.rename(nodeRef, name);
129          }
130          
131          Map JavaDoc<QName, Serializable JavaDoc> properties = this.nodeService.getProperties(nodeRef);
132          // we need to put all the properties from the editable bag back into
133
// the format expected by the repository
134

135          // but first extract and deal with the special mimetype property for ContentData
136
String JavaDoc mimetype = (String JavaDoc)props.get(TEMP_PROP_MIMETYPE);
137          if (mimetype != null)
138          {
139             // remove temporary prop from list so it isn't saved with the others
140
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          // extra and deal with the Author prop if the aspect has not been applied yet
150
String JavaDoc author = (String JavaDoc)props.get(ContentModel.PROP_AUTHOR);
151          if (author != null && author.length() != 0)
152          {
153             // add aspect if required
154
if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_AUTHOR) == false)
155             {
156                Map JavaDoc<QName, Serializable JavaDoc> authorProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(1, 1.0f);
157                authorProps.put(ContentModel.PROP_AUTHOR, author);
158                this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_AUTHOR, authorProps);
159             }
160             // else it will get updated in the later setProperties() call
161
}
162          
163          // deal with adding the "titled" aspect if required
164
String JavaDoc title = (String JavaDoc)props.get(ContentModel.PROP_TITLE);
165          String JavaDoc description = (String JavaDoc)props.get(ContentModel.PROP_DESCRIPTION);
166          if (title != null || description != null)
167          {
168             // add the aspect to be sure it's present
169
nodeService.addAspect(nodeRef, ContentModel.ASPECT_TITLED, null);
170             // props will get added later in setProperties()
171
}
172          
173          // add the remaining properties
174
Iterator JavaDoc<String JavaDoc> iterProps = props.keySet().iterator();
175          while (iterProps.hasNext())
176          {
177             String JavaDoc propName = iterProps.next();
178             QName qname = QName.createQName(propName);
179             
180             // make sure the property is represented correctly
181
Serializable JavaDoc propValue = (Serializable JavaDoc)props.get(propName);
182             
183             // check for empty strings when using number types, set to null in this case
184
if ((propValue != null) && (propValue instanceof String JavaDoc) &&
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          // send the properties back to the repository
204
this.nodeService.setProperties(this.browseBean.getDocument().getNodeRef(), properties);
205          
206          // we also need to persist any association changes that may have been made
207

208          // add any associations added in the UI
209
Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AssociationRef>> addedAssocs = this.editableNode.getAddedAssociations();
210          for (Map JavaDoc<String JavaDoc, 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          // remove any association removed in the UI
219
Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AssociationRef>> removedAssocs = this.editableNode.getRemovedAssociations();
220          for (Map JavaDoc<String JavaDoc, 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          // add any child associations added in the UI
229
Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, ChildAssociationRef>> addedChildAssocs = this.editableNode.getAddedChildAssociations();
230          for (Map JavaDoc<String JavaDoc, 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          // remove any child association removed in the UI
239
Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, ChildAssociationRef>> removedChildAssocs = this.editableNode.getRemovedChildAssociations();
240          for (Map JavaDoc<String JavaDoc, ChildAssociationRef> typedAssoc : removedChildAssocs.values())
241          {
242             for (ChildAssociationRef assoc : typedAssoc.values())
243             {
244                this.nodeService.removeChild(assoc.getParentRef(), assoc.getChildRef());
245             }
246          }
247          
248          // commit the transaction
249
tx.commit();
250          
251          // set the outcome to refresh
252
outcome = "finish";
253          
254          // reset the document held by the browse bean as it's just been updated
255
this.browseBean.getDocument().reset();
256       }
257       catch (FileExistsException e)
258       {
259          // rollback the transaction
260
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc ex) {}
261          // print status message
262
String JavaDoc statusMsg = MessageFormat.format(
263                Application.getMessage(
264                      FacesContext.getCurrentInstance(), "error_exists"),
265                      e.getExisting().getName());
266          Utils.addErrorMessage(statusMsg);
267          // no outcome
268
outcome = null;
269       }
270       catch (InvalidNodeRefException err)
271       {
272          // rollback the transaction
273
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc ex) {}
274          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
275                FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object JavaDoc[] {this.browseBean.getDocument().getId()}) );
276          // this failure means the node no longer exists - we cannot show the doc properties screen
277
outcome = "browse";
278       }
279       catch (Throwable JavaDoc e)
280       {
281          // rollback the transaction
282
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc 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 JavaDoc<String JavaDoc, Object JavaDoc> getProperties()
291    {
292       return this.editableNode.getProperties();
293    }
294    
295    /**
296     * @return Returns a list of content types to allow the user to select from
297     */

298    public List JavaDoc<SelectItem> getContentTypes()
299    {
300       if (this.contentTypes == null)
301       {
302          this.contentTypes = new ArrayList JavaDoc<SelectItem>(80);
303          ServiceRegistry registry = Repository.getServiceRegistry(FacesContext.getCurrentInstance());
304          MimetypeService mimetypeService = registry.getMimetypeService();
305          
306          // get the mime type display names
307
Map JavaDoc<String JavaDoc, String JavaDoc> mimeTypes = mimetypeService.getDisplaysByMimetype();
308          for (String JavaDoc mimeType : mimeTypes.keySet())
309          {
310             this.contentTypes.add(new SelectItem(mimeType, mimeTypes.get(mimeType)));
311          }
312          
313          // make sure the list is sorted by the values
314
QuickSort sorter = new QuickSort(this.contentTypes, "label", true, IDataContainer.SORT_CASEINSENSITIVE);
315          sorter.sort();
316       }
317       
318       return this.contentTypes;
319    }
320    
321    /**
322     * Determines whether this document has any other properties other than the
323     * default set to display to the user.
324     *
325     * @return true of there are properties to show, false otherwise
326     */

327    public boolean getOtherPropertiesPresent()
328    {
329       if (this.hasOtherProperties == null)
330       {
331          // we need to use the config service to see whether there are any
332
// editable properties configured for this document.
333
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    /**
352     * @return Returns the nodeService.
353     */

354    public NodeService getNodeService()
355    {
356       return this.nodeService;
357    }
358
359    /**
360     * @param nodeService The nodeService to set.
361     */

362    public void setNodeService(NodeService nodeService)
363    {
364       this.nodeService = nodeService;
365    }
366    
367    /**
368     * @param fileFolderService the file and folder model-specific functions
369     */

370    public void setFileFolderService(FileFolderService fileFolderService)
371    {
372       this.fileFolderService = fileFolderService;
373    }
374
375    /**
376     * Sets the DictionaryService to use when persisting metadata
377     *
378     * @param dictionaryService The DictionaryService
379     */

380    public void setDictionaryService(DictionaryService dictionaryService)
381    {
382       this.dictionaryService = dictionaryService;
383    }
384
385    /**
386     * @return The BrowseBean
387     */

388    public BrowseBean getBrowseBean()
389    {
390       return this.browseBean;
391    }
392
393    /**
394     * @param browseBean The BrowseBean to set.
395     */

396    public void setBrowseBean(BrowseBean browseBean)
397    {
398       this.browseBean = browseBean;
399    }
400 }
401
Popular Tags