KickJava   Java API By Example, From Geeks To Geeks.

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


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.Serializable JavaDoc;
21 import java.text.MessageFormat JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.faces.context.FacesContext;
26 import javax.transaction.UserTransaction JavaDoc;
27
28 import org.alfresco.repo.action.executer.ExporterActionExecuter;
29 import org.alfresco.service.cmr.action.Action;
30 import org.alfresco.service.cmr.action.ActionService;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.repository.NodeService;
33 import org.alfresco.web.app.Application;
34 import org.alfresco.web.bean.repository.Repository;
35 import org.alfresco.web.ui.common.Utils;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * Backing bean implementation for the Export dialog.
41  *
42  * @author gavinc
43  */

44 public class ExportBean
45 {
46    private static final Log logger = LogFactory.getLog(ExportBean.class);
47    
48    private static final String JavaDoc ALL_SPACES = "all";
49    private static final String JavaDoc CURRENT_SPACE = "current";
50
51    private static final String JavaDoc DEFAULT_OUTCOME = "dialog:close";
52    
53    private static final String JavaDoc MSG_ERROR = "error_export";
54    
55    protected BrowseBean browseBean;
56    protected NodeService nodeService;
57    protected ActionService actionService;
58    
59    private String JavaDoc packageName;
60    private String JavaDoc encoding = "UTF-8";
61    private String JavaDoc mode = CURRENT_SPACE;
62    private NodeRef destination;
63    private boolean includeChildren = true;
64    private boolean runInBackground = true;
65    private boolean includeSelf;
66    
67    /**
68     * Performs the export operation using the current state of the bean
69     *
70     * @return The outcome
71     */

72    public String JavaDoc export()
73    {
74       if (logger.isDebugEnabled())
75          logger.debug("Called export for " + this.mode + " with package name: " + this.packageName);
76       
77       String JavaDoc outcome = DEFAULT_OUTCOME;
78       
79       UserTransaction JavaDoc tx = null;
80       
81       try
82       {
83          tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
84          tx.begin();
85          
86          // build the action params map based on the bean's current state
87
Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(5);
88          params.put(ExporterActionExecuter.PARAM_STORE, Repository.getStoreRef().toString());
89          params.put(ExporterActionExecuter.PARAM_PACKAGE_NAME, this.packageName);
90          params.put(ExporterActionExecuter.PARAM_ENCODING, this.encoding);
91          params.put(ExporterActionExecuter.PARAM_DESTINATION_FOLDER, this.destination);
92          params.put(ExporterActionExecuter.PARAM_INCLUDE_CHILDREN, new Boolean JavaDoc(includeChildren));
93          params.put(ExporterActionExecuter.PARAM_INCLUDE_SELF, new Boolean JavaDoc(includeSelf));
94           
95          // build the action to execute
96
Action action = this.actionService.createAction(ExporterActionExecuter.NAME, params);
97          action.setExecuteAsynchronously(this.runInBackground);
98          
99          // get the appropriate node
100
NodeRef startNode = null;
101          if (this.mode.equals(ALL_SPACES))
102          {
103             startNode = this.nodeService.getRootNode(Repository.getStoreRef());
104          }
105          else
106          {
107             startNode = this.browseBean.getActionSpace().getNodeRef();
108          }
109          
110          // execute the action on the relevant node
111
this.actionService.executeAction(action, startNode);
112          
113          if (logger.isDebugEnabled())
114          {
115             logger.debug("Executed export action with action params of " + params);
116          }
117          
118          // commit the transaction
119
tx.commit();
120          
121          // reset the bean
122
reset();
123       }
124       catch (Throwable JavaDoc e)
125       {
126          // rollback the transaction
127
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc ex) {}
128          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
129                FacesContext.getCurrentInstance(), MSG_ERROR), e.toString()), e);
130          outcome = null;
131       }
132       
133       return outcome;
134    }
135    
136    /**
137     * Action called when the dialog is cancelled, just resets the bean's state
138     *
139     * @return The outcome
140     */

141    public String JavaDoc cancel()
142    {
143       reset();
144       
145       return DEFAULT_OUTCOME;
146    }
147    
148    /**
149     * Resets the dialog state back to the default
150     */

151    public void reset()
152    {
153       this.packageName = null;
154       this.mode = CURRENT_SPACE;
155       this.destination = null;
156       this.includeChildren = true;
157       this.includeSelf = false;
158       this.runInBackground = true;
159    }
160    
161    /**
162     * Returns the package name for the export
163     *
164     * @return The export package name
165     */

166    public String JavaDoc getPackageName()
167    {
168       return this.packageName;
169    }
170    
171    /**
172     * Sets the package name for the export
173     *
174     * @param packageName The export package name
175     */

176    public void setPackageName(String JavaDoc packageName)
177    {
178       this.packageName = packageName;
179    }
180    
181    /**
182     * The destination for the export as a NodeRef
183     *
184     * @return The destination
185     */

186    public NodeRef getDestination()
187    {
188       return this.destination;
189    }
190    
191    /**
192     * Sets the destination for the export
193     *
194     * @param destination The destination for the export
195     */

196    public void setDestination(NodeRef destination)
197    {
198       this.destination = destination;
199    }
200    
201    /**
202     * Determines whether the export will include child spaces
203     *
204     * @return true includes children
205     */

206    public boolean getIncludeChildren()
207    {
208       return this.includeChildren;
209    }
210    
211    /**
212     * Sets whether child spaces are included in the export
213     *
214     * @param includeChildren true to include the child spaces
215     */

216    public void setIncludeChildren(boolean includeChildren)
217    {
218       this.includeChildren = includeChildren;
219    }
220    
221    /**
222     * Determines whether the export will include the space itself
223     *
224     * @return true includes the space being exported from
225     */

226    public boolean getIncludeSelf()
227    {
228       return this.includeSelf;
229    }
230    
231    /**
232     * Sets whether the space itself is included in the export
233     *
234     * @param includeSelf true to include the space itself
235     */

236    public void setIncludeSelf(boolean includeSelf)
237    {
238       this.includeSelf = includeSelf;
239    }
240    
241    /**
242     * Determines whether to export only the current space or all spaces
243     *
244     * @return "all" to export all space and "current" to export the current space
245     */

246    public String JavaDoc getMode()
247    {
248       return this.mode;
249    }
250    
251    /**
252     * Sets whether to export the current space or all spaces
253     *
254     * @param mode "all" to export all space and "current" to export the current space
255     */

256    public void setMode(String JavaDoc mode)
257    {
258       this.mode = mode;
259    }
260    
261    /**
262     * Returns the encoding to use for the export
263     *
264     * @return The encoding
265     */

266    public String JavaDoc getEncoding()
267    {
268       return this.encoding;
269    }
270
271    /**
272     * Sets the encoding to use for the export package
273     *
274     * @param encoding The encoding
275     */

276    public void setEncoding(String JavaDoc encoding)
277    {
278       this.encoding = encoding;
279    }
280
281    /**
282     * Determines whether the import should run in the background
283     *
284     * @return true means the import will run in the background
285     */

286    public boolean getRunInBackground()
287    {
288       return this.runInBackground;
289    }
290
291    /**
292     * Determines whether the import will run in the background
293     *
294     * @param runInBackground true to run the import in the background
295     */

296    public void setRunInBackground(boolean runInBackground)
297    {
298       this.runInBackground = runInBackground;
299    }
300    
301    /**
302     * Sets the BrowseBean instance to use to retrieve the current document
303     *
304     * @param browseBean BrowseBean instance
305     */

306    public void setBrowseBean(BrowseBean browseBean)
307    {
308       this.browseBean = browseBean;
309    }
310    
311    /**
312     * Sets the action service
313     *
314     * @param actionService the action service
315     */

316    public void setActionService(ActionService actionService)
317    {
318       this.actionService = actionService;
319    }
320    
321    /**
322     * Sets the node service
323     *
324     * @param nodeService the node service
325     */

326    public void setNodeService(NodeService nodeService)
327    {
328       this.nodeService = nodeService;
329    }
330 }
331
Popular Tags