KickJava   Java API By Example, From Geeks To Geeks.

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


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.context.FacesContext;
26 import javax.faces.event.ActionEvent;
27
28 import org.alfresco.error.AlfrescoRuntimeException;
29 import org.alfresco.filesys.CIFSServer;
30 import org.alfresco.filesys.server.filesys.DiskSharedDevice;
31 import org.alfresco.filesys.smb.server.repo.ContentContext;
32 import org.alfresco.filesys.smb.server.repo.ContentDiskInterface;
33 import org.alfresco.model.ContentModel;
34 import org.alfresco.repo.security.permissions.AccessDeniedException;
35 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
36 import org.alfresco.service.cmr.repository.NodeRef;
37 import org.alfresco.service.cmr.repository.NodeService;
38 import org.alfresco.service.cmr.repository.Path;
39 import org.alfresco.service.cmr.repository.TemplateImageResolver;
40 import org.alfresco.service.cmr.repository.TemplateNode;
41 import org.alfresco.service.cmr.rule.RuleService;
42 import org.alfresco.service.cmr.search.SearchService;
43 import org.alfresco.service.cmr.security.PermissionService;
44 import org.alfresco.service.namespace.NamespaceService;
45 import org.alfresco.web.app.Application;
46 import org.alfresco.web.app.context.UIContextService;
47 import org.alfresco.web.bean.repository.Node;
48 import org.alfresco.web.bean.repository.Repository;
49 import org.alfresco.web.bean.repository.User;
50 import org.alfresco.web.bean.wizard.NewSpaceWizard;
51 import org.alfresco.web.config.ClientConfigElement;
52 import org.alfresco.web.ui.common.Utils;
53 import org.alfresco.web.ui.common.component.IBreadcrumbHandler;
54 import org.alfresco.web.ui.common.component.UIBreadcrumb;
55 import org.alfresco.web.ui.common.component.UIModeList;
56 import org.alfresco.web.ui.repo.component.IRepoBreadcrumbHandler;
57 import org.alfresco.web.ui.repo.component.shelf.UIShelf;
58 import org.apache.log4j.Logger;
59
60 /**
61  * @author Kevin Roast
62  */

63 public class NavigationBean
64 {
65    /**
66     * Default constructor
67     */

68    public NavigationBean()
69    {
70       initFromClientConfig();
71    }
72    
73    
74    // ------------------------------------------------------------------------------
75
// Bean property getters and setters
76

77    /**
78     * @param nodeService The nodeService to set.
79     */

80    public void setNodeService(NodeService nodeService)
81    {
82       this.nodeService = nodeService;
83    }
84
85    /**
86     * @param searchService The searchService to set.
87     */

88    public void setSearchService(SearchService searchService)
89    {
90       this.searchService = searchService;
91    }
92    
93    /**
94     * @param namespaceService The namespaceService to set.
95     */

96    public void setNamespaceService(NamespaceService namespaceService)
97    {
98       this.namespaceService = namespaceService;
99    }
100    
101    /**
102     * @param ruleService The ruleService to use
103     */

104    public void setRuleService(RuleService ruleService)
105    {
106       this.ruleService = ruleService;
107    }
108    
109    /**
110     * @param cifsServer The cifsServer to set.
111     */

112    public void setCifsServer(CIFSServer cifsServer)
113    {
114       this.cifsServer = cifsServer;
115    }
116
117    /**
118     * @param contentDiskDriver The contentDiskDriver to set.
119     */

120    public void setContentDiskDriver(ContentDiskInterface contentDiskDriver)
121    {
122       this.contentDiskDriver = contentDiskDriver;
123    }
124    
125    /**
126     * @return the User object representing the current instance for this user
127     */

128    public User getCurrentUser()
129    {
130       return Application.getCurrentUser(FacesContext.getCurrentInstance());
131    }
132
133    /**
134     * Return the expanded state of the Shelf panel wrapper component
135     *
136     * @return the expanded state of the Shelf panel wrapper component
137     */

138    public boolean isShelfExpanded()
139    {
140       return this.shelfExpanded;
141    }
142    
143    /**
144     * Set the expanded state of the Shelf panel wrapper component
145     *
146     * @param expanded true to expanded the Shelf panel area, false to hide it
147     */

148    public void setShelfExpanded(boolean expanded)
149    {
150       this.shelfExpanded = expanded;
151    }
152    
153    /**
154     * @return Returns the array containing the expanded state of the shelf items
155     */

156    public boolean[] getShelfItemExpanded()
157    {
158       return this.shelfItemExpanded;
159    }
160
161    /**
162     * @param shelfItemExpanded The array containing the expanded state of the shelf items
163     */

164    public void setShelfItemExpanded(boolean[] shelfItemExpanded)
165    {
166       this.shelfItemExpanded = shelfItemExpanded;
167    }
168    
169    /**
170     * @return Returns the toolbar Location.
171     */

172    public String JavaDoc getToolbarLocation()
173    {
174       return this.toolbarLocation;
175    }
176    
177    /**
178     * @param toolbarLocation The toolbar Location to set.
179     */

180    public void setToolbarLocation(String JavaDoc toolbarLocation)
181    {
182       this.toolbarLocation = toolbarLocation;
183    }
184    
185    /**
186     * @return Returns the helpUrl.
187     */

188    public String JavaDoc getHelpUrl()
189    {
190       return this.helpUrl;
191    }
192
193    /**
194     * @param helpUrl The helpUrl to set.
195     */

196    public void setHelpUrl(String JavaDoc helpUrl)
197    {
198       this.helpUrl = helpUrl;
199    }
200    
201    /**
202     * @return the number of rules associated with the current space
203     */

204    public int getRuleCount()
205    {
206        Node node = getCurrentNode();
207        return (node != null ? this.ruleService.countRules(node.getNodeRef()) : 0);
208    }
209    
210    /**
211     * @return Returns the search context object if any.
212     */

213    public SearchContext getSearchContext()
214    {
215       return this.searchContext;
216    }
217    
218    /**
219     * @param searchContext The search context object to set or null to clear search.
220     */

221    public void setSearchContext(SearchContext searchContext)
222    {
223       this.searchContext = searchContext;
224       
225       UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
226    }
227    
228    /**
229     * @return Returns the currently browsing node Id.
230     */

231    public String JavaDoc getCurrentNodeId()
232    {
233       return this.currentNodeId;
234    }
235
236    /**
237     * Set the node Id of the current folder/space container node.
238     * <p>
239     * Setting this value causes the UI to update and display the specified node as current.
240     *
241     * @param currentNodeId The currently browsing node Id.
242     */

243    public void setCurrentNodeId(String JavaDoc currentNodeId)
244    {
245       if (s_logger.isDebugEnabled())
246          s_logger.debug("Setting current node id to: " + currentNodeId);
247       
248       if (currentNodeId == null)
249       {
250          throw new AlfrescoRuntimeException("Can not set the current node id to null");
251       }
252       
253       // set the current Node Id for our UI context operations
254
this.currentNodeId = currentNodeId;
255       
256       // clear other context that is based on or relevant to the Node id
257
this.currentNode = null;
258       this.searchContext = null;
259       
260       // inform any interested beans that the UI needs updating after this change
261
UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
262       
263       // clear current node context after the notify - this is to ensure that if any delegates
264
// performed operations on the current node, that we have fresh data for the next View
265
this.currentNode = null;
266    }
267    
268    /**
269     * @return true if the current node has a template view available
270     */

271    public boolean getCurrentNodeHasTemplate()
272    {
273       boolean templateView = false;
274       Node node = getCurrentNode();
275       if (node.hasAspect(ContentModel.ASPECT_TEMPLATABLE))
276       {
277          NodeRef templateRef = (NodeRef)node.getProperties().get(ContentModel.PROP_TEMPLATE);
278          try
279          {
280             templateView = (templateRef != null && this.nodeService.exists(templateRef));
281          }
282          catch (AccessDeniedException err)
283          {
284             // default to false if no access to template
285
}
286       }
287       return templateView;
288    }
289    
290    /**
291     * @return the NodeRef.toString() for the current node template view if it has one set
292     */

293    public String JavaDoc getCurrentNodeTemplate()
294    {
295       String JavaDoc strRef = null;
296       if (getCurrentNodeHasTemplate() == true)
297       {
298          strRef = getCurrentNode().getProperties().get(ContentModel.PROP_TEMPLATE).toString();
299       }
300       return strRef;
301    }
302    
303    /**
304     * Returns a model for use by a template on a space Dashboard page.
305     *
306     * @return model containing current current space info.
307     */

308    public Map JavaDoc getTemplateModel()
309    {
310       HashMap JavaDoc model = new HashMap JavaDoc(1, 1.0f);
311       
312       FacesContext fc = FacesContext.getCurrentInstance();
313       TemplateNode spaceNode = new TemplateNode(getCurrentNode().getNodeRef(), Repository.getServiceRegistry(fc),
314             new TemplateImageResolver() {
315                public String JavaDoc resolveImagePathForName(String JavaDoc filename, boolean small) {
316                   return Utils.getFileTypeImage(filename, small);
317                }
318             });
319       model.put("space", spaceNode);
320       
321       return model;
322    }
323    
324    /**
325     * Clear state so that the current node properties cache for the next time they are requested
326     */

327    public void resetCurrentNodeProperties()
328    {
329       this.currentNode = null;
330    }
331    
332    /**
333     * @return The Map of properties for the current Node.
334     */

335    public Map JavaDoc<String JavaDoc, Object JavaDoc> getNodeProperties()
336    {
337       return getCurrentNode().getProperties();
338    }
339    
340    /**
341     * @return The current Node object for UI context operations
342     */

343    public Node getCurrentNode()
344    {
345       if (this.currentNode == null)
346       {
347          if (this.currentNodeId == null)
348          {
349             throw new AlfrescoRuntimeException("Cannot retrieve current Node if NodeId is null!");
350          }
351          
352          if (s_logger.isDebugEnabled())
353             s_logger.debug("Caching properties for node id: " + this.currentNodeId);
354          
355          NodeRef nodeRef;
356          Node node;
357          Map JavaDoc<String JavaDoc, Object JavaDoc> props;
358          try
359          {
360             // build a node which components on the JSP page can bind too
361
nodeRef = new NodeRef(Repository.getStoreRef(), this.currentNodeId);
362             node = new Node(nodeRef);
363             
364             // early init properties for this node (by getProperties() call)
365
// resolve icon in-case one has not been set
366
props = node.getProperties();
367          }
368          catch (InvalidNodeRefException refErr)
369          {
370             Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
371                   FacesContext.getCurrentInstance(), ERROR_DELETED_FOLDER), new Object JavaDoc[] {this.currentNodeId}) );
372             
373             nodeRef = new NodeRef(Repository.getStoreRef(), Application.getCompanyRootId());
374             node = new Node(nodeRef);
375             props = node.getProperties();
376          }
377          String JavaDoc icon = (String JavaDoc)props.get("app:icon");
378          props.put("icon", icon != null ? icon : NewSpaceWizard.SPACE_ICON_DEFAULT);
379          Path path = this.nodeService.getPath(nodeRef);
380          
381          // resolve CIFS network folder location for this node
382
DiskSharedDevice diskShare = cifsServer.getConfiguration().getPrimaryFilesystem();
383          
384          if (diskShare != null)
385          {
386              ContentContext contentCtx = (ContentContext) diskShare.getContext();
387              NodeRef rootNode = contentCtx.getRootNode();
388              String JavaDoc cifsPath = Repository.getNamePath(this.nodeService, path, rootNode, "\\", "file:///" + getCIFSServerPath(diskShare));
389               
390              node.getProperties().put("cifsPath", cifsPath);
391              node.getProperties().put("cifsPathLabel", cifsPath.substring(8)); // strip file:/// part
392
}
393          
394          this.currentNode = node;
395       }
396       
397       return this.currentNode;
398    }
399
400    /**
401     * @return Returns the breadcrumb handler elements representing the location path of the UI.
402     */

403    public List JavaDoc<IBreadcrumbHandler> getLocation()
404    {
405       if (this.location == null)
406       {
407          // set the current node to the users Home Space Id if one has not already been set
408
NodeRef homeSpaceRef;
409          List JavaDoc<IBreadcrumbHandler> elements = new ArrayList JavaDoc<IBreadcrumbHandler>(1);
410          if (getCurrentNodeId() == null)
411          {
412             User user = Application.getCurrentUser(FacesContext.getCurrentInstance());
413             homeSpaceRef = new NodeRef(Repository.getStoreRef(), user.getHomeSpaceId());
414          }
415          else
416          {
417             homeSpaceRef = new NodeRef(Repository.getStoreRef(), getCurrentNodeId());
418          }
419          
420          // set initial node ID
421
setCurrentNodeId(homeSpaceRef.getId());
422          
423          // setup the breadcrumb with the same initial location
424
String JavaDoc homeSpaceName = Repository.getNameForNode(this.nodeService, homeSpaceRef);
425          elements.add(new NavigationBreadcrumbHandler(homeSpaceRef, homeSpaceName));
426          setLocation(elements);
427       }
428       
429       return this.location;
430    }
431    
432    /**
433     * @param location The UI location representation to set.
434     */

435    public void setLocation(List JavaDoc<IBreadcrumbHandler> location)
436    {
437       this.location = location;
438    }
439    
440    /**
441     * @return true if we are currently the special Guest user
442     */

443    public boolean getIsGuest()
444    {
445       return Application.getCurrentUser(FacesContext.getCurrentInstance()).getUserName().equals(PermissionService.GUEST_AUTHORITY);
446    }
447    
448    /**
449     * Sets up the dispatch context so that the navigation handler knows
450     * what object is being acted upon
451     *
452     * @param node The node to be added to the dispatch context
453     */

454    public void setupDispatchContext(Node node)
455    {
456       this.dispatchContext = node;
457    }
458    
459    /**
460     * Resets the dispatch context
461     */

462    public void resetDispatchContext()
463    {
464       this.dispatchContext = null;
465    }
466    
467    /**
468     * Returns the node currently set in the dispatch context
469     *
470     * @return The node being dispatched or null if there is no
471     * dispatch context
472     */

473    public Node getDispatchContextNode()
474    {
475       return this.dispatchContext;
476    }
477    
478    
479    // ------------------------------------------------------------------------------
480
// Navigation action event handlers
481

482    /**
483     * Action handler to toggle the expanded state of the shelf.
484     * The panel component wrapping the shelf area of the UI is value bound to the shelfExpanded property.
485     */

486    public void toggleShelf(ActionEvent event)
487    {
488       this.shelfExpanded = !this.shelfExpanded;
489    }
490    
491    /**
492     * Action handler called after a Shelf Group has had its expanded state toggled by the user
493     */

494    public void shelfGroupToggled(ActionEvent event)
495    {
496       UIShelf.ShelfEvent shelfEvent = (UIShelf.ShelfEvent)event;
497       this.shelfItemExpanded[shelfEvent.Index] = shelfEvent.Expanded;
498    }
499    
500    /**
501     * Action to change the toolbar location
502     * Currently this will changed the location from Company to the users Home space
503     */

504    public void toolbarLocationChanged(ActionEvent event)
505    {
506       FacesContext context = FacesContext.getCurrentInstance();
507       try
508       {
509          UIModeList locationList = (UIModeList)event.getComponent();
510          String JavaDoc location = locationList.getValue().toString();
511          setToolbarLocation(location);
512          
513          if (LOCATION_COMPANY.equals(location))
514          {
515             List JavaDoc<IBreadcrumbHandler> elements = new ArrayList JavaDoc<IBreadcrumbHandler>(1);
516             NodeRef companyRootRef = new NodeRef(Repository.getStoreRef(), Application.getCompanyRootId());
517             String JavaDoc companySpaceName = Repository.getNameForNode(this.nodeService, companyRootRef);
518             elements.add(new NavigationBreadcrumbHandler(companyRootRef, companySpaceName));
519             setLocation(elements);
520             setCurrentNodeId(companyRootRef.getId());
521          }
522          else if (LOCATION_HOME.equals(location))
523          {
524             List JavaDoc<IBreadcrumbHandler> elements = new ArrayList JavaDoc<IBreadcrumbHandler>(1);
525             String JavaDoc homeSpaceId = Application.getCurrentUser(context).getHomeSpaceId();
526             NodeRef homeSpaceRef = new NodeRef(Repository.getStoreRef(), homeSpaceId);
527             String JavaDoc homeSpaceName = Repository.getNameForNode(this.nodeService, homeSpaceRef);
528             elements.add(new NavigationBreadcrumbHandler(homeSpaceRef, homeSpaceName));
529             setLocation(elements);
530             setCurrentNodeId(homeSpaceRef.getId());
531          }
532          
533          // we need to force a navigation to refresh the browse screen breadcrumb
534
context.getApplication().getNavigationHandler().handleNavigation(context, null, "browse");
535       }
536       catch (InvalidNodeRefException refErr)
537       {
538          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
539                FacesContext.getCurrentInstance(), Repository.ERROR_NOHOME), Application.getCurrentUser(context).getHomeSpaceId()), refErr );
540       }
541       catch (Exception JavaDoc err)
542       {
543          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
544                FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
545       }
546    }
547    
548    /**
549     * @param diskShare Filesystem shared device
550     * @return CIFS server path as network style string label
551     */

552    public String JavaDoc getCIFSServerPath(DiskSharedDevice diskShare)
553    {
554       if (this.cifsServerPath == null)
555       {
556          StringBuilder JavaDoc buf = new StringBuilder JavaDoc(24);
557          
558          String JavaDoc serverName = this.cifsServer.getConfiguration().getServerName();
559          if (serverName != null && serverName.length() != 0)
560          {
561             buf.append("\\\\")
562                .append(serverName)
563                .append("\\");
564             buf.append(diskShare.getName());
565          }
566          
567          this.cifsServerPath = buf.toString();
568       }
569       
570       return this.cifsServerPath;
571    }
572    
573    
574    // ------------------------------------------------------------------------------
575
// Private helpers
576

577    /**
578     * Initialise default values from client configuration
579     */

580    private void initFromClientConfig()
581    {
582       this.clientConfig = Application.getClientConfig(FacesContext.getCurrentInstance());
583       this.helpUrl = clientConfig.getHelpUrl();
584       this.shelfExpanded = clientConfig.isShelfVisible();
585    }
586    
587    
588    // ------------------------------------------------------------------------------
589
// Inner classes
590

591    /**
592     * Class to handle breadcrumb interaction for top-level navigation pages
593     */

594    public class NavigationBreadcrumbHandler implements IRepoBreadcrumbHandler
595    {
596       private static final long serialVersionUID = 4833194653193016638L;
597       
598       /**
599        * Constructor
600        *
601        * @param label Element label
602        */

603       public NavigationBreadcrumbHandler(NodeRef ref, String JavaDoc label)
604       {
605          this.label = label;
606          this.ref = ref;
607       }
608       
609       /**
610        * @see java.lang.Object#toString()
611        */

612       public String JavaDoc toString()
613       {
614          return this.label;
615       }
616
617       /**
618        * @see org.alfresco.web.ui.common.component.IBreadcrumbHandler#navigationOutcome(org.alfresco.web.ui.common.component.UIBreadcrumb)
619        */

620       public String JavaDoc navigationOutcome(UIBreadcrumb breadcrumb)
621       {
622          // set the current node to the specified top level node ID
623
FacesContext fc = FacesContext.getCurrentInstance();
624          setCurrentNodeId(ref.getId());
625          setLocation( (List JavaDoc)breadcrumb.getValue() );
626          
627          // setup the dispatch context
628
setupDispatchContext(new Node(ref));
629          
630          if (fc.getViewRoot().getViewId().equals(BrowseBean.BROWSE_VIEW_ID))
631          {
632             return null;
633          }
634          else
635          {
636             return "browse";
637          }
638       }
639       
640       public NodeRef getNodeRef()
641       {
642          return this.ref;
643       }
644       
645       private String JavaDoc label;
646       private NodeRef ref;
647    }
648
649    
650    // ------------------------------------------------------------------------------
651
// Private data
652

653    private static Logger s_logger = Logger.getLogger(NavigationBean.class);
654    
655    /** constant values used by the toolbar location modelist control */
656    private static final String JavaDoc LOCATION_COMPANY = "company";
657    private static final String JavaDoc LOCATION_HOME = "home";
658    
659    private static final String JavaDoc ERROR_DELETED_FOLDER = "error_deleted_folder";
660    
661    /** The NodeService to be used by the bean */
662    protected NodeService nodeService;
663    
664    /** The SearchService to be used by the bean */
665    protected SearchService searchService;
666    
667    /** NamespaceService bean reference */
668    protected NamespaceService namespaceService;
669    
670    /** RuleService bean reference*/
671    protected RuleService ruleService;
672    
673    /** CIFSServer bean reference */
674    protected CIFSServer cifsServer;
675    
676    /** CIFS content disk driver bean reference */
677    protected ContentDiskInterface contentDiskDriver;
678    
679    /** Client configuration object */
680    protected ClientConfigElement clientConfig = null;
681    
682    /** Cached path to our CIFS server and top level node DIR */
683    private String JavaDoc cifsServerPath;
684    
685    /** Node Id we are using for UI context operations */
686    private String JavaDoc currentNodeId;
687    
688    /** Node we are using for UI context operations */
689    private Node currentNode = null;
690    
691    /** Node we are using for dispatching */
692    private Node dispatchContext = null;
693    
694    /** Current toolbar location */
695    private String JavaDoc toolbarLocation = LOCATION_HOME;
696    
697    /** Search context object we are currently using or null for no search */
698    private SearchContext searchContext;
699    
700    /** expanded state of the Shelf panel wrapper component */
701    private boolean shelfExpanded = true;
702    
703    /** expanded state of the Shelf item components */
704    private boolean[] shelfItemExpanded = new boolean[] {true, true, true, false, false};
705    
706    /** list of the breadcrumb handler elements representing the location path of the UI */
707    private List JavaDoc<IBreadcrumbHandler> location = null;
708    
709    /** The client Help file url */
710    private String JavaDoc helpUrl;
711 }
712
Popular Tags