KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > wizard > BaseContentWizard


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.wizard;
18
19 import java.io.File JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.text.MessageFormat JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.faces.context.FacesContext;
28 import javax.faces.model.SelectItem;
29 import javax.transaction.UserTransaction JavaDoc;
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 /**
56  * Base Handler class used by the Content Wizards
57  *
58  * @author gavinc kevinr
59  */

60 public abstract class BaseContentWizard extends AbstractWizardBean
61 {
62    private static Log logger = LogFactory.getLog(BaseContentWizard.class);
63
64    protected static final String JavaDoc FINISH_INSTRUCTION_ID = "content_finish_instruction";
65    
66    // content wizard specific attributes
67
protected String JavaDoc fileName;
68    protected String JavaDoc author;
69    protected String JavaDoc title;
70    protected String JavaDoc description;
71    protected String JavaDoc contentType;
72    protected String JavaDoc objectType;
73    protected boolean inlineEdit;
74    protected List JavaDoc<SelectItem> contentTypes;
75    protected List JavaDoc<SelectItem> objectTypes;
76    protected ContentService contentService;
77    protected DictionaryService dictionaryService;
78    
79    // the NodeRef of the node created during finish
80
protected NodeRef createdNode;
81    
82    /**
83     * Save the specified content using the currently set wizard attributes
84     *
85     * @param fileContent File content to save
86     * @param strContent String content to save
87     */

88    protected String JavaDoc saveContent(File JavaDoc fileContent, String JavaDoc strContent)
89    {
90       String JavaDoc outcome = FINISH_OUTCOME;
91       
92       UserTransaction JavaDoc 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             // update the existing node in the repository
103
Node currentDocument = this.browseBean.getDocument();
104             NodeRef nodeRef = currentDocument.getNodeRef();
105             
106             // move the file - location and name checks will be performed
107
this.fileFolderService.move(nodeRef, null, this.fileName);
108             // set up the content data
109
// update the modified timestamp and other content props
110
Map JavaDoc<QName, Serializable JavaDoc> contentProps = this.nodeService.getProperties(nodeRef);
111             contentProps.put(ContentModel.PROP_TITLE, this.title);
112             contentProps.put(ContentModel.PROP_DESCRIPTION, this.description);
113             
114             // add author property
115
if (this.author != null && this.author.length() != 0)
116             {
117                if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_AUTHOR) == false)
118                {
119                   Map JavaDoc<QName, Serializable JavaDoc> authorProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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             // set up content properties - copy or create the compound property
130
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 JavaDoc<QName, Serializable JavaDoc> editProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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             // get the node ref of the node that will contain the content
160
NodeRef containerNodeRef;
161             String JavaDoc 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             // set the author aspect (if we have one)
178
if (this.author != null && this.author.length() > 0)
179             {
180                Map JavaDoc<QName, Serializable JavaDoc> authorProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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             // apply the titled aspect - title and description
189
Map JavaDoc<QName, Serializable JavaDoc> titledProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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             // apply the inlineeditable aspect
198
if (this.inlineEdit == true)
199             {
200                Map JavaDoc<QName, Serializable JavaDoc> editProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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             // get a writer for the content and put the file
209
ContentWriter writer = contentService.getWriter(fileNodeRef, ContentModel.PROP_CONTENT, true);
210             // set the mimetype and encoding
211
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             // remember the created node now
223
this.createdNode = fileNodeRef;
224          }
225          
226          // give subclasses a chance to perform custom processing before committing
227
performCustomProcessing();
228          
229          // commit the transaction
230
tx.commit();
231       }
232       catch (FileExistsException e)
233       {
234          // rollback the transaction
235
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc ex) {}
236          // print status message
237
String JavaDoc statusMsg = MessageFormat.format(
238                Application.getMessage(
239                      FacesContext.getCurrentInstance(), "error_exists"),
240                      e.getExisting().getName());
241          Utils.addErrorMessage(statusMsg);
242          // no outcome
243
outcome = null;
244       }
245       catch (Throwable JavaDoc e)
246       {
247          // rollback the transaction
248
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc 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    /**
258     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getStepInstructions()
259     */

260    public String JavaDoc getStepInstructions()
261    {
262       String JavaDoc 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    /**
281     * Initialises the wizard
282     */

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    /**
300     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#populate()
301     */

302    public void populate()
303    {
304       // get hold of the current document and populate the appropriate values
305
Node currentDocument = this.browseBean.getDocument();
306       Map JavaDoc<String JavaDoc, Object JavaDoc> props = currentDocument.getProperties();
307       
308       Boolean JavaDoc inline = (Boolean JavaDoc)props.get("editInline");
309       this.inlineEdit = inline != null ? inline.booleanValue() : false;
310       this.author = (String JavaDoc)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 JavaDoc)props.get("description");
318       this.fileName = currentDocument.getName();
319       this.title = (String JavaDoc)props.get("title");
320    }
321    
322    /**
323     * @return Returns the contentService.
324     */

325    public ContentService getContentService()
326    {
327       return contentService;
328    }
329
330    /**
331     * @param contentService The contentService to set.
332     */

333    public void setContentService(ContentService contentService)
334    {
335       this.contentService = contentService;
336    }
337    
338    /**
339     * Sets the dictionary service
340     *
341     * @param dictionaryService the dictionary service
342     */

343    public void setDictionaryService(DictionaryService dictionaryService)
344    {
345       this.dictionaryService = dictionaryService;
346    }
347
348    /**
349     * @return Returns the name of the file
350     */

351    public String JavaDoc getFileName()
352    {
353       return this.fileName;
354    }
355
356    /**
357     * @param fileName The name of the file
358     */

359    public void setFileName(String JavaDoc fileName)
360    {
361       this.fileName = fileName;
362    }
363    
364    /**
365     * @return Returns the author
366     */

367    public String JavaDoc getAuthor()
368    {
369       return this.author;
370    }
371
372    /**
373     * @param author Sets the author
374     */

375    public void setAuthor(String JavaDoc author)
376    {
377       this.author = author;
378    }
379
380    /**
381     * @return Returns the content type currenty selected
382     */

383    public String JavaDoc getContentType()
384    {
385       return this.contentType;
386    }
387
388    /**
389     * @param contentType Sets the currently selected content type
390     */

391    public void setContentType(String JavaDoc contentType)
392    {
393       this.contentType = contentType;
394    }
395    
396    /**
397     * @return Returns the object type currenty selected
398     */

399    public String JavaDoc getObjectType()
400    {
401       return this.objectType;
402    }
403
404    /**
405     * @param objectType Sets the currently selected object type
406     */

407    public void setObjectType(String JavaDoc objectType)
408    {
409       this.objectType = objectType;
410    }
411
412    /**
413     * @return Returns the description
414     */

415    public String JavaDoc getDescription()
416    {
417       return this.description;
418    }
419
420    /**
421     * @param description Sets the description
422     */

423    public void setDescription(String JavaDoc description)
424    {
425       this.description = description;
426    }
427
428    /**
429     * @return Returns the title
430     */

431    public String JavaDoc getTitle()
432    {
433       return this.title;
434    }
435
436    /**
437     * @param title Sets the title
438     */

439    public void setTitle(String JavaDoc title)
440    {
441       this.title = title;
442    }
443    
444    /**
445     * @return Returns the inline edit flag.
446     */

447    public boolean isInlineEdit()
448    {
449       return this.inlineEdit;
450    }
451
452    /**
453     * @param inlineEdit The inline edit flag to set.
454     */

455    public void setInlineEdit(boolean inlineEdit)
456    {
457       this.inlineEdit = inlineEdit;
458    }
459
460    /**
461     * @return Returns a list of content types to allow the user to select from
462     */

463    public List JavaDoc<SelectItem> getContentTypes()
464    {
465       if (this.contentTypes == null)
466       {
467          this.contentTypes = new ArrayList JavaDoc<SelectItem>(80);
468          ServiceRegistry registry = Repository.getServiceRegistry(FacesContext.getCurrentInstance());
469          MimetypeService mimetypeService = registry.getMimetypeService();
470          
471          // get the mime type display names
472
Map JavaDoc<String JavaDoc, String JavaDoc> mimeTypes = mimetypeService.getDisplaysByMimetype();
473          for (String JavaDoc mimeType : mimeTypes.keySet())
474          {
475             this.contentTypes.add(new SelectItem(mimeType, mimeTypes.get(mimeType)));
476          }
477          
478          // make sure the list is sorted by the values
479
QuickSort sorter = new QuickSort(this.contentTypes, "label", true, IDataContainer.SORT_CASEINSENSITIVE);
480          sorter.sort();
481       }
482       
483       return this.contentTypes;
484    }
485    
486    /**
487     * @return Returns a list of object types to allow the user to select from
488     */

489    public List JavaDoc<SelectItem> getObjectTypes()
490    {
491       if (this.objectTypes == null)
492       {
493          FacesContext context = FacesContext.getCurrentInstance();
494          
495          // add the well known object type to start with
496
this.objectTypes = new ArrayList JavaDoc<SelectItem>(5);
497          this.objectTypes.add(new SelectItem(ContentModel.TYPE_CONTENT.toString(),
498                Application.getMessage(context, "content")));
499          
500          // add any configured content sub-types to the list
501
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                      // look for a client localized string
517
String JavaDoc label = null;
518                      String JavaDoc msgId = child.getAttribute("displayLabelId");
519                      if (msgId != null)
520                      {
521                         label = Application.getMessage(context, msgId);
522                      }
523                      
524                      // if there wasn't an externalized string look for one in the config
525
if (label == null)
526                      {
527                         label = child.getAttribute("displayLabel");
528                      }
529    
530                      // if there wasn't a client based label try and get it from the dictionary
531
if (label == null)
532                      {
533                         label = typeDef.getTitle();
534                      }
535                      
536                      // finally, just use the localname
537
if (label == null)
538                      {
539                         label = idQName.getLocalName();
540                      }
541                      
542                      this.objectTypes.add(new SelectItem(idQName.toString(), label));
543                   }
544                }
545                
546                // make sure the list is sorted by the label
547
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    /**
566     * @return Determines whether the next and finish button should be enabled
567     */

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    /**
583     * Returns the display label for the content type currently chosen
584     *
585     * @return The human readable version of the content type
586     */

587    protected String JavaDoc getSummaryContentType()
588    {
589       ServiceRegistry registry = Repository.getServiceRegistry(FacesContext.getCurrentInstance());
590       MimetypeService mimetypeService = registry.getMimetypeService();
591          
592       // get the mime type display name
593
Map JavaDoc<String JavaDoc, String JavaDoc> mimeTypes = mimetypeService.getDisplaysByMimetype();
594       return mimeTypes.get(this.contentType);
595    }
596    
597    /**
598     * Returns the display label for the currently selected object type
599     *
600     * @return The objevt type label
601     */

602    protected String JavaDoc getSummaryObjectType()
603    {
604       String JavaDoc 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    /**
619     * Performs any processing sub classes may wish to do before commit is called
620     */

621    protected void performCustomProcessing()
622    {
623       // used by subclasses if necessary
624
}
625 }
626
Popular Tags