KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection 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.model.DataModel;
28 import javax.faces.model.ListDataModel;
29 import javax.faces.model.SelectItem;
30
31 import org.alfresco.error.AlfrescoRuntimeException;
32 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
33 import org.alfresco.service.cmr.dictionary.DictionaryService;
34 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
35 import org.alfresco.service.cmr.repository.AssociationRef;
36 import org.alfresco.service.cmr.repository.ChildAssociationRef;
37 import org.alfresco.service.cmr.repository.NodeRef;
38 import org.alfresco.service.cmr.repository.NodeService;
39 import org.alfresco.service.cmr.repository.Path;
40 import org.alfresco.service.cmr.repository.StoreRef;
41 import org.alfresco.service.cmr.search.ResultSet;
42 import org.alfresco.service.cmr.search.SearchService;
43 import org.alfresco.service.cmr.security.AccessPermission;
44 import org.alfresco.service.cmr.security.AccessStatus;
45 import org.alfresco.service.cmr.security.PermissionService;
46 import org.alfresco.service.namespace.NamespaceService;
47 import org.alfresco.service.namespace.QName;
48 import org.alfresco.service.namespace.RegexQNamePattern;
49 import org.alfresco.util.ISO9075;
50 import org.alfresco.web.app.servlet.DownloadContentServlet;
51
52
53 // TODO: DownloadServlet - use of request parameter for property name?
54
// TODO: Anyway to switch content view url link / property value text?
55

56
57 /**
58  * Backing bean to support the Admin Node Browser
59  */

60 public class AdminNodeBrowseBean
61 {
62     /** selected query language */
63     private String JavaDoc queryLanguage = null;
64
65     /** available query languages */
66     private static List JavaDoc<SelectItem> queryLanguages = new ArrayList JavaDoc<SelectItem>();
67     static
68     {
69         queryLanguages.add(new SelectItem("noderef"));
70         queryLanguages.add(new SelectItem(SearchService.LANGUAGE_XPATH));
71         queryLanguages.add(new SelectItem(SearchService.LANGUAGE_LUCENE));
72         queryLanguages.add(new SelectItem("selectnodes"));
73     }
74
75     // query and results
76
private String JavaDoc query = null;
77     private SearchResults searchResults = new SearchResults((List JavaDoc<NodeRef>)null);
78     
79     // stores and node
80
private DataModel stores = null;
81     private NodeRef nodeRef = null;
82     private QName nodeType = null;
83     private Path primaryPath = null;
84     private DataModel parents = null;
85     private DataModel aspects = null;
86     private DataModel properties = null;
87     private DataModel children = null;
88     private DataModel assocs = null;
89     private Boolean JavaDoc inheritPermissions = null;
90     private DataModel permissions = null;
91     
92     // supporting repository services
93
private NodeService nodeService;
94     private DictionaryService dictionaryService;
95     private SearchService searchService;
96     private NamespaceService namespaceService;
97     private PermissionService permissionService;
98     
99     /**
100      * @param nodeService node service
101      */

102     public void setNodeService(NodeService nodeService)
103     {
104         this.nodeService = nodeService;
105     }
106
107     /**
108      * @param searchService search service
109      */

110     public void setSearchService(SearchService searchService)
111     {
112         this.searchService = searchService;
113     }
114     
115     /**
116      * @param dictionaryService dictionary service
117      */

118     public void setDictionaryService(DictionaryService dictionaryService)
119     {
120         this.dictionaryService = dictionaryService;
121     }
122     
123     /**
124      * @param namespaceService namespace service
125      */

126     public void setNamespaceService(NamespaceService namespaceService)
127     {
128         this.namespaceService = namespaceService;
129     }
130
131     /**
132      * @param permissionService permission service
133      */

134     public void setPermissionService(PermissionService permissionService)
135     {
136         this.permissionService = permissionService;
137     }
138
139     /**
140      * Gets the list of repository stores
141      *
142      * @return stores
143      */

144     public DataModel getStores()
145     {
146         if (stores == null)
147         {
148             List JavaDoc<StoreRef> storeRefs = nodeService.getStores();
149             stores = new ListDataModel(storeRefs);
150         }
151         return stores;
152     }
153     
154     /**
155      * Gets the selected node reference
156      *
157      * @return node reference (defaults to system store root)
158      */

159     public NodeRef getNodeRef()
160     {
161         if (nodeRef == null)
162         {
163             nodeRef = nodeService.getRootNode(new StoreRef("system", "system"));
164         }
165         return nodeRef;
166     }
167
168     /**
169      * Sets the selected node reference
170      *
171      * @param nodeRef node reference
172      */

173     private void setNodeRef(NodeRef nodeRef)
174     {
175         this.nodeRef = nodeRef;
176
177         // clear cache
178
primaryPath = null;
179         nodeType = null;
180         parents = null;
181         aspects = null;
182         properties = null;
183         children = null;
184         assocs = null;
185         inheritPermissions = null;
186         permissions = null;
187     }
188     
189     /**
190      * Gets the current node type
191      *
192      * @return node type
193      */

194     public QName getNodeType()
195     {
196         if (nodeType == null)
197         {
198             nodeType = nodeService.getType(getNodeRef());
199         }
200         return nodeType;
201     }
202     
203     /**
204      * Gets the current node primary path
205      *
206      * @return primary path
207      */

208     public String JavaDoc getPrimaryPath()
209     {
210         if (primaryPath == null)
211         {
212             primaryPath = nodeService.getPath(getNodeRef());
213         }
214         return ISO9075.decode(primaryPath.toString());
215     }
216
217     /**
218      * Gets the current node primary parent reference
219      *
220      * @return primary parent ref
221      */

222     public NodeRef getPrimaryParent()
223     {
224         getPrimaryPath();
225         Path.Element element = primaryPath.last();
226         NodeRef parentRef = ((Path.ChildAssocElement)element).getRef().getParentRef();
227         return parentRef;
228     }
229
230     /**
231      * Gets the current node aspects
232      *
233      * @return node aspects
234      */

235     public DataModel getAspects()
236     {
237         if (aspects == null)
238         {
239             List JavaDoc<QName> aspectNames = new ArrayList JavaDoc<QName>(nodeService.getAspects(getNodeRef()));
240             aspects = new ListDataModel(aspectNames);
241         }
242         return aspects;
243     }
244         
245     /**
246      * Gets the current node parents
247      *
248      * @return node parents
249      */

250     public DataModel getParents()
251     {
252         if (parents == null)
253         {
254             List JavaDoc<ChildAssociationRef> parentRefs = nodeService.getParentAssocs(getNodeRef());
255             parents = new ListDataModel(parentRefs);
256         }
257         return parents;
258     }
259     
260     /**
261      * Gets the current node properties
262      *
263      * @return properties
264      */

265     public DataModel getProperties()
266     {
267         if (properties == null)
268         {
269             Map JavaDoc<QName, Serializable JavaDoc> propertyValues = nodeService.getProperties(getNodeRef());
270             List JavaDoc<Property> nodeProperties = new ArrayList JavaDoc<Property>(propertyValues.size());
271             for (Map.Entry JavaDoc<QName, Serializable JavaDoc> property : propertyValues.entrySet())
272             {
273                 nodeProperties.add(new Property(property.getKey(), property.getValue()));
274             }
275             properties = new ListDataModel(nodeProperties);
276         }
277         return properties;
278     }
279
280     /**
281      * Gets whether the current node inherits its permissions from a parent node
282      *
283      * @return true => inherits permissions
284      */

285     public boolean getInheritPermissions()
286     {
287         if (inheritPermissions == null)
288         {
289             inheritPermissions = permissionService.getInheritParentPermissions(nodeRef);
290         }
291         return inheritPermissions.booleanValue();
292     }
293     
294     /**
295      * Gets the current node permissions
296      *
297      * @return the permissions
298      */

299     public DataModel getPermissions()
300     {
301         if (permissions == null)
302         {
303             AccessStatus readPermissions = permissionService.hasPermission(nodeRef, PermissionService.READ_PERMISSIONS);
304             if (readPermissions.equals(AccessStatus.ALLOWED))
305             {
306                 List JavaDoc<AccessPermission> nodePermissions = new ArrayList JavaDoc<AccessPermission>(permissionService.getAllSetPermissions(nodeRef));
307                 permissions = new ListDataModel(nodePermissions);
308             }
309             else
310             {
311                 List JavaDoc<NoReadPermissionGranted> noReadPermissions = new ArrayList JavaDoc<NoReadPermissionGranted>(1);
312                 noReadPermissions.add(new NoReadPermissionGranted());
313                 permissions = new ListDataModel(noReadPermissions);
314             }
315         }
316         return permissions;
317     }
318     
319     /**
320      * Gets the current node children
321      *
322      * @return node children
323      */

324     public DataModel getChildren()
325     {
326         if (children == null)
327         {
328             List JavaDoc<ChildAssociationRef> assocRefs = nodeService.getChildAssocs(getNodeRef());
329             children = new ListDataModel(assocRefs);
330         }
331         return children;
332     }
333
334     /**
335      * Gets the current node associations
336      *
337      * @return associations
338      */

339     public DataModel getAssocs()
340     {
341         if (assocs == null)
342         {
343             List JavaDoc<AssociationRef> assocRefs = nodeService.getTargetAssocs(getNodeRef(), RegexQNamePattern.MATCH_ALL);
344             assocs = new ListDataModel(assocRefs);
345         }
346         return assocs;
347     }
348
349     /**
350      * Gets the current query language
351      *
352      * @return query language
353      */

354     public String JavaDoc getQueryLanguage()
355     {
356         return queryLanguage;
357     }
358
359     /**
360      * Sets the current query language
361      *
362      * @param queryLanguage query language
363      */

364     public void setQueryLanguage(String JavaDoc queryLanguage)
365     {
366         this.queryLanguage = queryLanguage;
367     }
368     
369     /**
370      * Gets the current query
371      *
372      * @return query statement
373      */

374     public String JavaDoc getQuery()
375     {
376         return query;
377     }
378     
379     /**
380      * Set the current query
381      *
382      * @param query query statement
383      */

384     public void setQuery(String JavaDoc query)
385     {
386         this.query = query;
387     }
388     
389     /**
390      * Gets the list of available query languages
391      *
392      * @return query languages
393      */

394     public List JavaDoc getQueryLanguages()
395     {
396         return queryLanguages;
397     }
398
399     /**
400      * Gets the current search results
401      *
402      * @return search results
403      */

404     public SearchResults getSearchResults()
405     {
406         return searchResults;
407     }
408     
409     /**
410      * Action to select a store
411      *
412      * @return next action
413      */

414     public String JavaDoc selectStore()
415     {
416         StoreRef storeRef = (StoreRef)stores.getRowData();
417         NodeRef rootNode = nodeService.getRootNode(storeRef);
418         setNodeRef(rootNode);
419         return "success";
420     }
421
422     /**
423      * Action to select stores list
424      *
425      * @return next action
426      */

427     public String JavaDoc selectStores()
428     {
429         stores = null;
430         return "success";
431     }
432
433     /**
434      * Action to select primary path
435      *
436      * @return next action
437      */

438     public String JavaDoc selectPrimaryPath()
439     {
440         // force refresh of self
441
setNodeRef(nodeRef);
442         return "success";
443     }
444     
445     /**
446      * Action to select primary parent
447      *
448      * @return next action
449      */

450     public String JavaDoc selectPrimaryParent()
451     {
452         setNodeRef(getPrimaryParent());
453         return "success";
454     }
455     
456     /**
457      * Action to select parent
458      *
459      * @return next action
460      */

461     public String JavaDoc selectParent()
462     {
463         ChildAssociationRef assocRef = (ChildAssociationRef)parents.getRowData();
464         NodeRef parentRef = assocRef.getParentRef();
465         setNodeRef(parentRef);
466         return "success";
467     }
468
469     /**
470      * Action to select association To node
471      *
472      * @return next action
473      */

474     public String JavaDoc selectToNode()
475     {
476         AssociationRef assocRef = (AssociationRef)assocs.getRowData();
477         NodeRef targetRef = assocRef.getTargetRef();
478         setNodeRef(targetRef);
479         return "success";
480     }
481
482     /**
483      * Action to select node property
484      *
485      * @return next action
486      */

487     public String JavaDoc selectNodeProperty()
488     {
489         Property property = (Property)properties.getRowData();
490         Property.Value value = (Property.Value)property.getValues().getRowData();
491         NodeRef nodeRef = (NodeRef)value.getValue();
492         setNodeRef(nodeRef);
493         return "success";
494     }
495     
496     /**
497      * Action to select child
498      *
499      * @return next action
500      */

501     public String JavaDoc selectChild()
502     {
503         ChildAssociationRef assocRef = (ChildAssociationRef)children.getRowData();
504         NodeRef childRef = assocRef.getChildRef();
505         setNodeRef(childRef);
506         return "success";
507     }
508     
509     /**
510      * Action to select search result node
511      *
512      * @return next action
513      */

514     public String JavaDoc selectResultNode()
515     {
516         ChildAssociationRef assocRef = (ChildAssociationRef)searchResults.getRows().getRowData();
517         NodeRef childRef = assocRef.getChildRef();
518         setNodeRef(childRef);
519         return "success";
520     }
521
522     /**
523      * Action to submit search
524      *
525      * @return next action
526      */

527     public String JavaDoc submitSearch()
528     {
529         try
530         {
531             if (queryLanguage.equals("noderef"))
532             {
533                 // ensure node exists
534
NodeRef nodeRef = new NodeRef(query);
535                 boolean exists = nodeService.exists(nodeRef);
536                 if (!exists)
537                 {
538                     throw new AlfrescoRuntimeException("Node " + nodeRef + " does not exist.");
539                 }
540                 setNodeRef(nodeRef);
541                 return "node";
542             }
543             else if (queryLanguage.equals("selectnodes"))
544             {
545                 List JavaDoc<NodeRef> nodes = searchService.selectNodes(getNodeRef(), query, null, namespaceService, false);
546                 searchResults = new SearchResults(nodes);
547                 return "search";
548             }
549             
550             // perform search
551
searchResults = new SearchResults(searchService.query(getNodeRef().getStoreRef(), queryLanguage, query));
552             return "search";
553         }
554         catch(Throwable JavaDoc e)
555         {
556             FacesContext context = FacesContext.getCurrentInstance();
557             FacesMessage message = new FacesMessage();
558             message.setSeverity(FacesMessage.SEVERITY_ERROR);
559             message.setDetail("Search failed due to: " + e.toString());
560             context.addMessage("searchForm:query", message);
561             return "error";
562         }
563     }
564     
565     /**
566      * Property wrapper class
567      */

568     public class Property
569     {
570         private QName name;
571         private boolean isCollection = false;
572         private DataModel values;
573         private String JavaDoc datatype;
574         private String JavaDoc residual;
575     
576         /**
577          * Construct
578          *
579          * @param name property name
580          * @param value property values
581          */

582         public Property(QName name, Serializable JavaDoc value)
583         {
584             this.name = name;
585             
586             PropertyDefinition propDef = dictionaryService.getProperty(name);
587             if (propDef != null)
588             {
589                 datatype = propDef.getDataType().getName().toString();
590                 residual = "false";
591             }
592             else
593             {
594                 residual = "true";
595             }
596             
597             // handle multi/single values
598
// TODO: perhaps this is not the most efficient way - lots of list creations
599
List JavaDoc<Value> values = new ArrayList JavaDoc<Value>();
600             if (value instanceof Collection JavaDoc)
601             {
602                 isCollection = true;
603                 for (Serializable JavaDoc multiValue : (Collection JavaDoc<Serializable JavaDoc>)value)
604                 {
605                     values.add(new Value(multiValue));
606                 }
607             }
608             else
609             {
610                 values.add(new Value(value));
611             }
612             this.values = new ListDataModel(values);
613         }
614         
615         /**
616          * Gets the property name
617          *
618          * @return name
619          */

620         public QName getName()
621         {
622             return name;
623         }
624         
625         /**
626          * Gets the property data type
627          *
628          * @return data type
629          */

630         public String JavaDoc getDataType()
631         {
632             return datatype;
633         }
634         
635         /**
636          * Gets the property value
637          *
638          * @return value
639          */

640         public DataModel getValues()
641         {
642             return values;
643         }
644
645         /**
646          * Determines whether the property is residual
647          *
648          * @return true => property is not defined in dictionary
649          */

650         public String JavaDoc getResidual()
651         {
652             return residual;
653         }
654         
655         /**
656          * Determines whether the property is of ANY type
657          *
658          * @return true => is any
659          */

660         public boolean isAny()
661         {
662             return (datatype == null) ? false : datatype.equals(DataTypeDefinition.ANY.toString());
663         }
664         
665         /**
666          * Determines whether the property is a collection
667          *
668          * @return true => is collection
669          */

670         public boolean isCollection()
671         {
672             return isCollection;
673         }
674         
675         
676         /**
677          * Value wrapper
678          */

679         public class Value
680         {
681             private Serializable JavaDoc value;
682
683             /**
684              * Construct
685              *
686              * @param value value
687              */

688             public Value(Serializable JavaDoc value)
689             {
690                 this.value = value;
691             }
692             
693             /**
694              * Gets the value
695              *
696              * @return the value
697              */

698             public Serializable JavaDoc getValue()
699             {
700                 return value;
701             }
702             
703             /**
704              * Gets the value datatype
705              *
706              * @return the value datatype
707              */

708             public String JavaDoc getDataType()
709             {
710                 String JavaDoc datatype = Property.this.getDataType();
711                 if (datatype == null || datatype.equals(DataTypeDefinition.ANY.toString()))
712                 {
713                     if (value != null)
714                     {
715                         datatype = dictionaryService.getDataType(value.getClass()).getName().toString();
716                     }
717                 }
718                 return datatype;
719             }
720             
721             /**
722              * Gets the download url (for content properties)
723              *
724              * @return url
725              */

726             public String JavaDoc getUrl()
727             {
728                 String JavaDoc url = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
729                 url += DownloadContentServlet.generateBrowserURL(nodeRef, "file.bin");
730                 url += "?property=" + name;
731                 return url;
732             }
733             
734             /**
735              * Determines whether the value is content
736              *
737              * @return true => is content
738              */

739             public boolean isContent()
740             {
741                 String JavaDoc datatype = getDataType();
742                 return (datatype == null) ? false : datatype.equals(DataTypeDefinition.CONTENT.toString());
743             }
744             
745             /**
746              * Determines whether the value is a node ref
747              *
748              * @return true => is node ref
749              */

750             public boolean isNodeRef()
751             {
752                 String JavaDoc datatype = getDataType();
753                 return (datatype == null) ? false : datatype.equals(DataTypeDefinition.NODE_REF.toString()) || datatype.equals(DataTypeDefinition.CATEGORY.toString());
754             }
755             
756             /**
757              * Determines whether the value is null
758              *
759              * @return true => value is null
760              */

761             public boolean isNullValue()
762             {
763                 return value == null;
764             }
765         }
766     }
767
768     /**
769      * Permission representing the fact that "Read Permissions" has not been granted
770      */

771     public class NoReadPermissionGranted
772     {
773         public String JavaDoc getPermission()
774         {
775             return PermissionService.READ_PERMISSIONS;
776         }
777         
778         public String JavaDoc getAuthority()
779         {
780             return "[Current Authority]";
781         }
782         
783         public String JavaDoc getAccessStatus()
784         {
785             return "Not Granted";
786         }
787     }
788     
789     /**
790      * Wrapper class for Search Results
791      */

792     public class SearchResults
793     {
794         private int length = 0;
795         private DataModel rows;
796
797         /**
798          * Construct
799          *
800          * @param resultSet query result set
801          */

802         public SearchResults(ResultSet resultSet)
803         {
804             rows = new ListDataModel();
805             if (resultSet != null)
806             {
807                 rows.setWrappedData(resultSet.getChildAssocRefs());
808                 length = resultSet.length();
809             }
810         }
811         
812         /**
813          * Construct
814          *
815          * @param resultSet query result set
816          */

817         public SearchResults(List JavaDoc<NodeRef> resultSet)
818         {
819             rows = new ListDataModel();
820             if (resultSet != null)
821             {
822                 List JavaDoc<ChildAssociationRef> assocRefs = new ArrayList JavaDoc<ChildAssociationRef>(resultSet.size());
823                 for (NodeRef nodeRef : resultSet)
824                 {
825                     ChildAssociationRef childAssocRef = nodeService.getPrimaryParent(nodeRef);
826                     assocRefs.add(childAssocRef);
827                 }
828                 rows.setWrappedData(assocRefs);
829                 length = resultSet.size();
830             }
831         }
832
833         /**
834          * Gets the row count
835          *
836          * @return count of rows
837          */

838         public int getLength()
839         {
840             return length;
841         }
842
843         /**
844          * Gets the rows
845          *
846          * @return the rows
847          */

848         public DataModel getRows()
849         {
850             return rows;
851         }
852     }
853     
854 }
855
Popular Tags