KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.MessageFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.faces.application.FacesMessage;
26 import javax.faces.context.FacesContext;
27 import javax.faces.event.ActionEvent;
28 import javax.faces.model.SelectItem;
29 import javax.transaction.UserTransaction JavaDoc;
30
31 import org.alfresco.model.ContentModel;
32 import org.alfresco.service.cmr.dictionary.DictionaryService;
33 import org.alfresco.service.cmr.repository.NodeRef;
34 import org.alfresco.service.cmr.repository.NodeService;
35 import org.alfresco.service.cmr.repository.TemplateImageResolver;
36 import org.alfresco.service.cmr.repository.TemplateNode;
37 import org.alfresco.service.cmr.security.OwnableService;
38 import org.alfresco.service.cmr.security.PermissionService;
39 import org.alfresco.service.namespace.NamespaceService;
40 import org.alfresco.web.app.AlfrescoNavigationHandler;
41 import org.alfresco.web.app.Application;
42 import org.alfresco.web.bean.repository.Node;
43 import org.alfresco.web.bean.repository.Repository;
44 import org.alfresco.web.data.IDataContainer;
45 import org.alfresco.web.data.QuickSort;
46 import org.alfresco.web.ui.common.Utils;
47 import org.alfresco.web.ui.common.Utils.URLMode;
48 import org.alfresco.web.ui.common.component.UIActionLink;
49
50 /**
51  * Back bean provided access to the details of a Space
52  *
53  * @author Kevin Roast
54  */

55 public class SpaceDetailsBean
56 {
57    private static final String JavaDoc MSG_SUCCESS_OWNERSHIP = "success_ownership";
58    
59    private static final String JavaDoc OUTCOME_RETURN = "showSpaceDetails";
60
61    /** BrowseBean instance */
62    protected BrowseBean browseBean;
63    
64    /** The NavigationBean bean reference */
65    protected NavigationBean navigator;
66    
67    /** PermissionService bean reference */
68    protected PermissionService permissionService;
69    
70    /** OwnableService bean reference */
71    protected OwnableService ownableService;
72    
73    /** NodeServuce bean reference */
74    protected NodeService nodeService;
75    
76    /** Selected template Id */
77    private String JavaDoc template;
78    
79    
80    // ------------------------------------------------------------------------------
81
// Bean property getters and setters
82

83    /**
84     * Sets the BrowseBean instance to use to retrieve the current Space
85     *
86     * @param browseBean BrowseBean instance
87     */

88    public void setBrowseBean(BrowseBean browseBean)
89    {
90       this.browseBean = browseBean;
91    }
92    
93    /**
94     * @param navigator The NavigationBean to set.
95     */

96    public void setNavigator(NavigationBean navigator)
97    {
98       this.navigator = navigator;
99    }
100    
101    /**
102     * @param nodeService The NodeService to set
103     */

104    public void setNodeService(NodeService nodeService)
105    {
106       this.nodeService = nodeService;
107    }
108    
109    /**
110     * @param permissionService The PermissionService to set.
111     */

112    public void setPermissionService(PermissionService permissionService)
113    {
114       this.permissionService = permissionService;
115    }
116    
117    /**
118     * Sets the ownable service instance the bean should use
119     *
120     * @param ownableService The OwnableService
121     */

122    public void setOwnableService(OwnableService ownableService)
123    {
124       this.ownableService = ownableService;
125    }
126    
127    /**
128     * Returns the Space this bean is currently representing
129     *
130     * @return The Space Node
131     */

132    public Node getSpace()
133    {
134       return this.browseBean.getActionSpace();
135    }
136    
137    /**
138     * Returns the id of the current space
139     *
140     * @return The id
141     */

142    public String JavaDoc getId()
143    {
144       return getSpace().getId();
145    }
146    
147    /**
148     * Returns the name of the current space
149     *
150     * @return Name of the current space
151     */

152    public String JavaDoc getName()
153    {
154       return getSpace().getName();
155    }
156    
157    /**
158     * Returns the WebDAV URL for the current space
159     *
160     * @return The WebDAV url
161     */

162    public String JavaDoc getWebdavUrl()
163    {
164       return Utils.generateURL(FacesContext.getCurrentInstance(), getSpace(), URLMode.WEBDAV);
165    }
166
167    /**
168     * Returns the URL to access the details page for the current space
169     *
170     * @return The bookmark URL
171     */

172    public String JavaDoc getBookmarkUrl()
173    {
174       return Utils.generateURL(FacesContext.getCurrentInstance(), getSpace(), URLMode.SHOW_DETAILS);
175    }
176    
177    /**
178     * Returns the CIFS path for the current space
179     *
180     * @return The CIFS path
181     */

182    public String JavaDoc getCifsPath()
183    {
184       return Utils.generateURL(FacesContext.getCurrentInstance(), getSpace(), URLMode.CIFS);
185    }
186    
187    /**
188     * Return the Alfresco NodeRef URL for the current space
189     *
190     * @return the Alfresco NodeRef URL
191     */

192    public String JavaDoc getNodeRefUrl()
193    {
194       return getSpace().getNodeRef().toString();
195    }
196
197    /**
198     * @return Returns the template Id.
199     */

200    public String JavaDoc getTemplate()
201    {
202       // return current template if it exists
203
NodeRef ref = (NodeRef)getSpace().getProperties().get(ContentModel.PROP_TEMPLATE);
204       return ref != null ? ref.getId() : this.template;
205    }
206
207    /**
208     * @param template The template Id to set.
209     */

210    public void setTemplate(String JavaDoc template)
211    {
212       this.template = template;
213    }
214    
215    /**
216     * @return true if the current document has the 'templatable' aspect applied and
217     * references a template that currently exists in the system.
218     */

219    public boolean isTemplatable()
220    {
221       NodeRef templateRef = (NodeRef)getSpace().getProperties().get(ContentModel.PROP_TEMPLATE);
222       return (getSpace().hasAspect(ContentModel.ASPECT_TEMPLATABLE) &&
223               templateRef != null && nodeService.exists(templateRef));
224    }
225    
226    /**
227     * @return String of the NodeRef for the dashboard template used by the space if any
228     */

229    public String JavaDoc getTemplateRef()
230    {
231       NodeRef ref = (NodeRef)getSpace().getProperties().get(ContentModel.PROP_TEMPLATE);
232       return ref != null ? ref.toString() : null;
233    }
234    
235    /**
236     * Returns a model for use by a template on the Space Details page.
237     *
238     * @return model containing current current space info.
239     */

240    @SuppressWarnings JavaDoc("unchecked")
241    public Map JavaDoc getTemplateModel()
242    {
243       HashMap JavaDoc model = new HashMap JavaDoc(1, 1.0f);
244       
245       FacesContext fc = FacesContext.getCurrentInstance();
246       TemplateNode spaceNode = new TemplateNode(getSpace().getNodeRef(), Repository.getServiceRegistry(fc),
247             new TemplateImageResolver() {
248                public String JavaDoc resolveImagePathForName(String JavaDoc filename, boolean small) {
249                   return Utils.getFileTypeImage(filename, small);
250                }
251             });
252       model.put("space", spaceNode);
253       
254       return model;
255    }
256    
257    /**
258     * @return the list of available Content Templates that can be applied to the current document.
259     */

260    public SelectItem[] getTemplates()
261    {
262       // get the template from the special Content Templates folder
263
FacesContext context = FacesContext.getCurrentInstance();
264       String JavaDoc xpath = Application.getRootPath(context) + "/" +
265             Application.getGlossaryFolderName(context) + "/" +
266             Application.getContentTemplatesFolderName(context) + "//*";
267       NodeRef rootNodeRef = this.nodeService.getRootNode(Repository.getStoreRef());
268       NamespaceService resolver = Repository.getServiceRegistry(context).getNamespaceService();
269       List JavaDoc<NodeRef> results = Repository.getServiceRegistry(context).getSearchService().selectNodes(
270             rootNodeRef, xpath, null, resolver, false);
271       
272       List JavaDoc<SelectItem> templates = new ArrayList JavaDoc<SelectItem>(results.size());
273       if (results.size() != 0)
274       {
275          DictionaryService dd = Repository.getServiceRegistry(context).getDictionaryService();
276          for (NodeRef ref : results)
277          {
278             Node childNode = new Node(ref);
279             if (dd.isSubClass(childNode.getType(), ContentModel.TYPE_CONTENT))
280             {
281                templates.add(new SelectItem(childNode.getId(), childNode.getName()));
282             }
283          }
284          
285          // make sure the list is sorted by the label
286
QuickSort sorter = new QuickSort(templates, "label", true, IDataContainer.SORT_CASEINSENSITIVE);
287          sorter.sort();
288       }
289       
290       return templates.toArray(new SelectItem[templates.size()]);
291    }
292    
293    
294    // ------------------------------------------------------------------------------
295
// Action event handlers
296

297    /**
298     * Action handler to apply the selected Template and Templatable aspect to the current Space
299     */

300    public String JavaDoc applyTemplate()
301    {
302       try
303       {
304          // apply the templatable aspect if required
305
if (getSpace().hasAspect(ContentModel.ASPECT_TEMPLATABLE) == false)
306          {
307             this.nodeService.addAspect(getSpace().getNodeRef(), ContentModel.ASPECT_TEMPLATABLE, null);
308          }
309          
310          // get the selected template from the Template Picker
311
NodeRef templateRef = new NodeRef(Repository.getStoreRef(), this.template);
312          
313          // set the template NodeRef into the templatable aspect property
314
this.nodeService.setProperty(getSpace().getNodeRef(), ContentModel.PROP_TEMPLATE, templateRef);
315          
316          // reset space details for next refresh of details page
317
getSpace().reset();
318       }
319       catch (Exception JavaDoc e)
320       {
321          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
322                FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e);
323       }
324       return OUTCOME_RETURN;
325    }
326    
327    /**
328     * Action handler to remove a dashboard template from the current Space
329     */

330    public String JavaDoc removeTemplate()
331    {
332       try
333       {
334          // clear template property
335
this.nodeService.setProperty(getSpace().getNodeRef(), ContentModel.PROP_TEMPLATE, null);
336          this.nodeService.removeAspect(getSpace().getNodeRef(), ContentModel.ASPECT_TEMPLATABLE);
337          
338          // reset space details for next refresh of details page
339
getSpace().reset();
340       }
341       catch (Exception JavaDoc e)
342       {
343          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
344                FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e);
345       }
346       return OUTCOME_RETURN;
347    }
348    
349    /**
350     * Action Handler to take Ownership of the current Space
351     */

352    public void takeOwnership(ActionEvent event)
353    {
354       UserTransaction JavaDoc tx = null;
355       
356       try
357       {
358          tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
359          tx.begin();
360          
361          this.ownableService.takeOwnership(getSpace().getNodeRef());
362          
363          FacesContext context = FacesContext.getCurrentInstance();
364          String JavaDoc msg = Application.getMessage(context, MSG_SUCCESS_OWNERSHIP);
365          FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
366          context.addMessage(event.getComponent().getClientId(context), facesMsg);
367          
368          // commit the transaction
369
tx.commit();
370       }
371       catch (Throwable JavaDoc e)
372       {
373          // rollback the transaction
374
try { if (tx != null) {tx.rollback();} } catch (Exception JavaDoc ex) {}
375          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
376                FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e);
377       }
378    }
379    
380    /**
381     * Navigates to next item in the list of Spaces
382     */

383    public void nextItem(ActionEvent event)
384    {
385       UIActionLink link = (UIActionLink)event.getComponent();
386       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
387       String JavaDoc id = params.get("id");
388       if (id != null && id.length() != 0)
389       {
390          List JavaDoc<Node> nodes = this.browseBean.getNodes();
391          if (nodes.size() > 1)
392          {
393             // perform a linear search - this is slow but stateless
394
// otherwise we would have to manage state of last selected node
395
// this gets very tricky as this bean is instantiated once and never
396
// reset - it does not know when the document has changed etc.
397
for (int i=0; i<nodes.size(); i++)
398             {
399                if (id.equals(nodes.get(i).getId()) == true)
400                {
401                   Node next;
402                   // found our item - navigate to next
403
if (i != nodes.size() - 1)
404                   {
405                      next = nodes.get(i + 1);
406                   }
407                   else
408                   {
409                      // handle wrapping case
410
next = nodes.get(0);
411                   }
412                   
413                   // prepare for showing details for this node
414
this.browseBean.setupSpaceAction(next.getId(), false);
415                }
416             }
417         }
418       }
419    }
420    
421    /**
422     * Navigates to the previous item in the list Spaces
423     */

424    public void previousItem(ActionEvent event)
425    {
426       UIActionLink link = (UIActionLink)event.getComponent();
427       Map JavaDoc<String JavaDoc, String JavaDoc> params = link.getParameterMap();
428       String JavaDoc id = params.get("id");
429       if (id != null && id.length() != 0)
430       {
431          List JavaDoc<Node> nodes = this.browseBean.getNodes();
432          if (nodes.size() > 1)
433          {
434             // see above
435
for (int i=0; i<nodes.size(); i++)
436             {
437                if (id.equals(nodes.get(i).getId()) == true)
438                {
439                   Node previous;
440                   // found our item - navigate to previous
441
if (i != 0)
442                   {
443                      previous = nodes.get(i - 1);
444                   }
445                   else
446                   {
447                      // handle wrapping case
448
previous = nodes.get(nodes.size() - 1);
449                   }
450                   
451                   // show details for this node
452
this.browseBean.setupSpaceAction(previous.getId(), false);
453                }
454             }
455          }
456       }
457    }
458    
459    /**
460     * Action handler to clear the current Space properties before returning to the browse screen,
461     * as the user may have modified the properties!
462     */

463    public String JavaDoc closeDialog()
464    {
465       this.navigator.resetCurrentNodeProperties();
466       return AlfrescoNavigationHandler.CLOSE_DIALOG_OUTCOME;
467    }
468 }
469
Popular Tags