KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License as
5  * published by the Free Software Foundation; either version
6  * 2.1 of the License, or (at your option) any later version.
7  * You may obtain a copy of the License at
8  *
9  * http://www.gnu.org/licenses/lgpl.txt
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the
16  * License.
17  */

18 package org.alfresco.web.bean;
19
20 import java.io.File JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.faces.context.FacesContext;
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import org.alfresco.model.ContentModel;
30 import org.alfresco.repo.action.executer.ImporterActionExecuter;
31 import org.alfresco.repo.content.MimetypeMap;
32 import org.alfresco.service.cmr.action.Action;
33 import org.alfresco.service.cmr.action.ActionService;
34 import org.alfresco.service.cmr.repository.ChildAssociationRef;
35 import org.alfresco.service.cmr.repository.ContentService;
36 import org.alfresco.service.cmr.repository.ContentWriter;
37 import org.alfresco.service.cmr.repository.NodeRef;
38 import org.alfresco.service.cmr.repository.NodeService;
39 import org.alfresco.service.namespace.NamespaceService;
40 import org.alfresco.service.namespace.QName;
41 import org.alfresco.web.app.Application;
42 import org.alfresco.web.bean.repository.Repository;
43 import org.alfresco.web.ui.common.Utils;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47 /**
48  * Backing bean implementation for the Import dialog.
49  *
50  * @author gavinc
51  */

52 public class ImportBean
53 {
54    private static final Log logger = LogFactory.getLog(ImportBean.class);
55
56    private static final String JavaDoc DEFAULT_OUTCOME = "dialog:close";
57    
58    private static final String JavaDoc MSG_ERROR = "error_import";
59    private static final String JavaDoc MSG_ERROR_NO_FILE = "error_import_no_file";
60    private static final String JavaDoc MSG_ERROR_EMPTY_FILE = "error_import_empty_file";
61    
62    protected BrowseBean browseBean;
63    protected NodeService nodeService;
64    protected ActionService actionService;
65    protected ContentService contentService;
66    
67    private File JavaDoc file;
68    private String JavaDoc fileName;
69    private String JavaDoc encoding = "UTF-8";
70    private boolean runInBackground = true;
71    
72    /**
73     * Performs the import operation using the current state of the bean
74     *
75     * @return The outcome
76     */

77    public String JavaDoc performImport()
78    {
79       String JavaDoc outcome = DEFAULT_OUTCOME;
80       
81       if (logger.isDebugEnabled())
82          logger.debug("Called import for file: " + this.file);
83       
84       if (this.file != null && this.file.exists())
85       {
86          // check the file actually has contents
87
if (this.file.length() > 0)
88          {
89             UserTransaction JavaDoc tx = null;
90             
91             try
92             {
93                FacesContext context = FacesContext.getCurrentInstance();
94                tx = Repository.getUserTransaction(context);
95                tx.begin();
96                
97                // first of all we need to add the uploaded ACP file to the repository
98
NodeRef acpNodeRef = addACPToRepository(context);
99                
100                // build the action params map based on the bean's current state
101
Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(3);
102                params.put(ImporterActionExecuter.PARAM_DESTINATION_FOLDER, this.browseBean.getActionSpace().getNodeRef());
103                params.put(ImporterActionExecuter.PARAM_ENCODING, this.encoding);
104                 
105                // build the action to execute
106
Action action = this.actionService.createAction(ImporterActionExecuter.NAME, params);
107                action.setExecuteAsynchronously(this.runInBackground);
108                
109                // execute the action on the ACP file
110
this.actionService.executeAction(action, acpNodeRef);
111                
112                if (logger.isDebugEnabled())
113                {
114                   logger.debug("Executed import action with action params of " + params);
115                }
116                
117                // commit the transaction
118
tx.commit();
119                
120                // reset the bean
121
reset();
122             }
123             catch (Throwable JavaDoc e)
124             {
125                // rollback the transaction
126
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc ex) {}
127                Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
128                      FacesContext.getCurrentInstance(), MSG_ERROR), e.toString()), e);
129                outcome = null;
130             }
131          }
132          else
133          {
134             Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_EMPTY_FILE));
135             outcome = null;
136          }
137       }
138       else
139       {
140          Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_NO_FILE));
141          outcome = null;
142       }
143       
144       return outcome;
145    }
146    
147    /**
148     * Action called when the dialog is cancelled, just resets the bean's state
149     *
150     * @return The outcome
151     */

152    public String JavaDoc cancel()
153    {
154       reset();
155       
156       return DEFAULT_OUTCOME;
157    }
158    
159    /**
160     * Resets the dialog state back to the default
161     */

162    public void reset()
163    {
164       this.file = null;
165       this.fileName = null;
166       this.runInBackground = true;
167       
168       // delete the temporary file we uploaded earlier
169
if (this.file != null)
170       {
171          this.file.delete();
172       }
173       
174       // remove the file upload bean from the session
175
FacesContext ctx = FacesContext.getCurrentInstance();
176       ctx.getExternalContext().getSessionMap().remove(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
177    }
178    
179    /**
180     * @return Returns the message to display when a file has been uploaded
181     */

182    public String JavaDoc getFileUploadSuccessMsg()
183    {
184       String JavaDoc msg = Application.getMessage(FacesContext.getCurrentInstance(), "file_upload_success");
185       return MessageFormat.format(msg, new Object JavaDoc[] {getFileName()});
186    }
187    
188    /**
189     * @return Returns the name of the file
190     */

191    public String JavaDoc getFileName()
192    {
193       // try and retrieve the file and filename from the file upload bean
194
// representing the file we previously uploaded.
195
FacesContext ctx = FacesContext.getCurrentInstance();
196       FileUploadBean fileBean = (FileUploadBean)ctx.getExternalContext().getSessionMap().
197          get(FileUploadBean.FILE_UPLOAD_BEAN_NAME);
198       if (fileBean != null)
199       {
200          this.fileName = fileBean.getFileName();
201          this.file = fileBean.getFile();
202       }
203       
204       return this.fileName;
205    }
206    
207    /**
208     * Returns the encoding to use for the export
209     *
210     * @return The encoding
211     */

212    public String JavaDoc getEncoding()
213    {
214       return this.encoding;
215    }
216
217    /**
218     * Sets the encoding to use for the export package
219     *
220     * @param encoding The encoding
221     */

222    public void setEncoding(String JavaDoc encoding)
223    {
224       this.encoding = encoding;
225    }
226
227    /**
228     * Determines whether the import should run in the background
229     *
230     * @return true means the import will run in the background
231     */

232    public boolean getRunInBackground()
233    {
234       return this.runInBackground;
235    }
236
237    /**
238     * Determines whether the import will run in the background
239     *
240     * @param runInBackground true to run the import in the background
241     */

242    public void setRunInBackground(boolean runInBackground)
243    {
244       this.runInBackground = runInBackground;
245    }
246
247    /**
248     * Sets the BrowseBean instance to use to retrieve the current document
249     *
250     * @param browseBean BrowseBean instance
251     */

252    public void setBrowseBean(BrowseBean browseBean)
253    {
254       this.browseBean = browseBean;
255    }
256    
257    /**
258     * Sets the action service
259     *
260     * @param actionService the action service
261     */

262    public void setActionService(ActionService actionService)
263    {
264       this.actionService = actionService;
265    }
266    
267    /**
268     * Sets the node service
269     *
270     * @param nodeService the node service
271     */

272    public void setNodeService(NodeService nodeService)
273    {
274       this.nodeService = nodeService;
275    }
276    
277    /**
278     * Sets the content service
279     *
280     * @param contentService the content service
281     */

282    public void setContentService(ContentService contentService)
283    {
284       this.contentService = contentService;
285    }
286    
287    /**
288     * Adds the uploaded ACP file to the repository
289     *
290     * @param context Faces context
291     * @return NodeRef representing the ACP file in the repository
292     */

293    private NodeRef addACPToRepository(FacesContext context)
294    {
295       // set the name for the new node
296
Map JavaDoc<QName, Serializable JavaDoc> contentProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(1);
297       contentProps.put(ContentModel.PROP_NAME, this.fileName);
298       
299       // create the node to represent the zip file
300
String JavaDoc assocName = QName.createValidLocalName(this.fileName);
301       ChildAssociationRef assocRef = this.nodeService.createNode(
302            this.browseBean.getActionSpace().getNodeRef(), ContentModel.ASSOC_CONTAINS,
303            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, assocName),
304            ContentModel.TYPE_CONTENT, contentProps);
305       
306       NodeRef acpNodeRef = assocRef.getChildRef();
307      
308       // apply the titled aspect to behave in the web client
309
Map JavaDoc<QName, Serializable JavaDoc> titledProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(3, 1.0f);
310       titledProps.put(ContentModel.PROP_TITLE, this.fileName);
311       titledProps.put(ContentModel.PROP_DESCRIPTION, Application.getMessage(context, "import_package_description"));
312       this.nodeService.addAspect(acpNodeRef, ContentModel.ASPECT_TITLED, titledProps);
313      
314       // add the content to the node
315
ContentWriter writer = this.contentService.getWriter(acpNodeRef, ContentModel.PROP_CONTENT, true);
316       writer.setEncoding(this.encoding);
317       writer.setMimetype(MimetypeMap.MIMETYPE_ACP);
318       writer.putContent(this.file);
319             
320       return acpNodeRef;
321    }
322 }
323
Popular Tags