KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 import javax.faces.context.FacesContext;
27
28 import org.alfresco.model.ContentModel;
29 import org.alfresco.repo.content.MimetypeMap;
30 import org.alfresco.repo.content.filestore.FileContentReader;
31 import org.alfresco.service.cmr.repository.ContentReader;
32 import org.alfresco.service.namespace.QName;
33 import org.alfresco.web.app.Application;
34 import org.alfresco.web.bean.FileUploadBean;
35 import org.alfresco.web.bean.repository.Repository;
36
37 /**
38  * Handler class used by the Add Content Wizard
39  *
40  * @author gavinc
41  */

42 public class AddContentWizard extends BaseContentWizard
43 {
44    // TODO: retrieve these from the config service
45
private static final String JavaDoc WIZARD_TITLE_ID = "add_content_title";
46    private static final String JavaDoc WIZARD_DESC_ID = "add_content_desc";
47    private static final String JavaDoc STEP1_TITLE_ID = "add_conent_step1_title";
48    private static final String JavaDoc STEP1_DESCRIPTION_ID = "add_conent_step1_desc";
49    private static final String JavaDoc STEP2_TITLE_ID = "add_conent_step2_title";
50    private static final String JavaDoc STEP2_DESCRIPTION_ID = "add_conent_step2_desc";
51    
52    // add content wizard specific properties
53
private File JavaDoc file;
54    
55    
56    /**
57     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#next()
58     */

59    public String JavaDoc next()
60    {
61       String JavaDoc outcome = super.next();
62       
63       // if the outcome is "properties" we pre-set the content type and other
64
// fields accordingly
65
if (outcome.equals("properties"))
66       {
67          this.contentType = Repository.getMimeTypeForFileName(
68                FacesContext.getCurrentInstance(), this.fileName);
69          
70          // set default for in-line editing flag
71
this.inlineEdit = (this.contentType.equals(MimetypeMap.MIMETYPE_HTML));
72          
73          // Try and extract metadata from the file
74
ContentReader cr = new FileContentReader(this.file);
75          cr.setMimetype(this.contentType);
76          // create properties for content type
77
Map JavaDoc<QName, Serializable JavaDoc> contentProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(5, 1.0f);
78          
79          if (Repository.extractMetadata(FacesContext.getCurrentInstance(), cr, contentProps))
80          {
81             this.author = (String JavaDoc)(contentProps.get(ContentModel.PROP_AUTHOR));
82             this.title = (String JavaDoc)(contentProps.get(ContentModel.PROP_TITLE));
83             this.description = (String JavaDoc)(contentProps.get(ContentModel.PROP_DESCRIPTION));
84          }
85          if (this.title == null)
86          {
87             this.title = this.fileName;
88          }
89       }
90       
91       return outcome;
92    }
93
94    /**
95     * Deals with the finish button being pressed
96     *
97     * @return outcome
98     */

99    public String JavaDoc finish()
100    {
101       String JavaDoc outcome = saveContent(this.file, null);
102       
103       // now we know the new details are in the repository, reset the
104
// client side node representation so the new details are retrieved
105
if (this.editMode)
106       {
107          this.browseBean.getDocument().reset();
108       }
109       
110       return outcome;
111    }
112    
113    /**
114     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getWizardDescription()
115     */

116    public String JavaDoc getWizardDescription()
117    {
118       return Application.getMessage(FacesContext.getCurrentInstance(), WIZARD_DESC_ID);
119    }
120
121    /**
122     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getWizardTitle()
123     */

124    public String JavaDoc getWizardTitle()
125    {
126       return Application.getMessage(FacesContext.getCurrentInstance(), WIZARD_TITLE_ID);
127    }
128    
129    /**
130     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getStepDescription()
131     */

132    public String JavaDoc getStepDescription()
133    {
134       String JavaDoc stepDesc = null;
135       
136       switch (this.currentStep)
137       {
138          case 1:
139          {
140             stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), STEP1_DESCRIPTION_ID);
141             break;
142          }
143          case 2:
144          {
145             stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), STEP2_DESCRIPTION_ID);
146             break;
147          }
148          case 3:
149          {
150             stepDesc = Application.getMessage(FacesContext.getCurrentInstance(), SUMMARY_DESCRIPTION_ID);
151             break;
152          }
153          default:
154          {
155             stepDesc = "";
156          }
157       }
158       
159       return stepDesc;
160    }
161
162    /**
163     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#getStepTitle()
164     */

165    public String JavaDoc getStepTitle()
166    {
167       String JavaDoc stepTitle = null;
168       
169       switch (this.currentStep)
170       {
171          case 1:
172          {
173             stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP1_TITLE_ID);
174             break;
175          }
176          case 2:
177          {
178             stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), STEP2_TITLE_ID);
179             break;
180          }
181          case 3:
182          {
183             stepTitle = Application.getMessage(FacesContext.getCurrentInstance(), SUMMARY_TITLE_ID);
184             break;
185          }
186          default:
187          {
188             stepTitle = "";
189          }
190       }
191       
192       return stepTitle;
193    }
194    
195    /**
196     * Initialises the wizard
197     */

198    public void init()
199    {
200       super.init();
201       
202       clearUpload();
203       
204       this.file = null;
205    }
206
207    /**
208     * @return Returns the message to display when a file has been uploaded
209     */

210    public String JavaDoc getFileUploadSuccessMsg()
211    {
212       String JavaDoc msg = Application.getMessage(FacesContext.getCurrentInstance(), "file_upload_success");
213       return MessageFormat.format(msg, new Object JavaDoc[] {getFileName()});
214    }
215    
216    /**
217     * @return Returns the name of the file
218     */

219    public String JavaDoc getFileName()
220    {
221       // try and retrieve the file and filename from the file upload bean
222
// representing the file we previously uploaded.
223
FacesContext ctx = FacesContext.getCurrentInstance();
224       FileUploadBean fileBean = (FileUploadBean)ctx.getExternalContext().getSessionMap().
225          get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
226       if (fileBean != null)
227       {
228          this.file = fileBean.getFile();
229          this.fileName = fileBean.getFileName();
230       }
231       
232       return this.fileName;
233    }
234
235    /**
236     * @param fileName The name of the file
237     */

238    public void setFileName(String JavaDoc fileName)
239    {
240       this.fileName = fileName;
241       
242       // we also need to keep the file upload bean in sync
243
FacesContext ctx = FacesContext.getCurrentInstance();
244       FileUploadBean fileBean = (FileUploadBean)ctx.getExternalContext().getSessionMap().
245          get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
246       if (fileBean != null)
247       {
248          fileBean.setFileName(this.fileName);
249       }
250    }
251    
252    /**
253     * @return Returns the summary data for the wizard.
254     */

255    public String JavaDoc getSummary()
256    {
257       ResourceBundle JavaDoc bundle = Application.getBundle(FacesContext.getCurrentInstance());
258       
259       return buildSummary(
260             new String JavaDoc[] {bundle.getString("file_name"), bundle.getString("type"),
261                           bundle.getString("content_type"), bundle.getString("title"),
262                           bundle.getString("description"), bundle.getString("author"),
263                           bundle.getString("inline_editable")},
264             new String JavaDoc[] {this.fileName, getSummaryObjectType(), getSummaryContentType(), this.title,
265                           this.description, this.author, Boolean.toString(this.inlineEdit)});
266    }
267    
268    /**
269     * @see org.alfresco.web.bean.wizard.AbstractWizardBean#determineOutcomeForStep(int)
270     */

271    protected String JavaDoc determineOutcomeForStep(int step)
272    {
273       String JavaDoc outcome = null;
274       
275       switch(step)
276       {
277          case 1:
278          {
279             outcome = "upload";
280             break;
281          }
282          case 2:
283          {
284             outcome = "properties";
285             break;
286          }
287          case 3:
288          {
289             outcome = "summary";
290             break;
291          }
292          default:
293          {
294             outcome = CANCEL_OUTCOME;
295          }
296       }
297       
298       return outcome;
299    }
300    
301    /**
302     * Deletes the uploaded file and removes the FileUploadBean from the session
303     */

304    private void clearUpload()
305    {
306       // delete the temporary file we uploaded earlier
307
if (this.file != null)
308       {
309          this.file.delete();
310       }
311       
312       // remove the file upload bean from the session
313
FacesContext ctx = FacesContext.getCurrentInstance();
314       ctx.getExternalContext().getSessionMap().remove(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
315    }
316 }
317
Popular Tags