KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > UICategorySelector


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.ui.repo.component;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21
22 import javax.faces.context.FacesContext;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.service.cmr.dictionary.DictionaryService;
26 import org.alfresco.service.cmr.repository.ChildAssociationRef;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.search.CategoryService;
29 import org.alfresco.service.cmr.search.CategoryService.Depth;
30 import org.alfresco.web.app.Application;
31 import org.alfresco.web.bean.repository.Node;
32 import org.alfresco.web.bean.repository.Repository;
33 import org.alfresco.web.ui.repo.WebResources;
34
35 /**
36  * Component to allow the selection of a category
37  *
38  * @author gavinc
39  */

40 public class UICategorySelector extends AbstractItemSelector
41 {
42    // ------------------------------------------------------------------------------
43
// Component Impl
44

45    /**
46     * @see javax.faces.component.UIComponent#getFamily()
47     */

48    public String JavaDoc getFamily()
49    {
50       return "org.alfresco.faces.CategorySelector";
51    }
52    
53    /**
54     *
55     * @see org.alfresco.web.ui.repo.component.AbstractItemSelector#getDefaultLabel()
56     */

57    public String JavaDoc getDefaultLabel()
58    {
59       return Application.getMessage(FacesContext.getCurrentInstance(), "select_category_prompt");
60    }
61
62    /**
63     * Use Spring JSF integration to return the category service bean instance
64     *
65     * @param context FacesContext
66     *
67     * @return category service bean instance or throws runtime exception if not found
68     */

69    private static CategoryService getCategoryService(FacesContext context)
70    {
71       CategoryService service = Repository.getServiceRegistry(context).getCategoryService();
72       if (service == null)
73       {
74          throw new IllegalStateException JavaDoc("Unable to obtain CategoryService bean reference.");
75       }
76       
77       return service;
78    }
79
80    /**
81     * Returns the parent id of the current category, or null if the parent has the category root type
82     *
83     * @see org.alfresco.web.ui.repo.component.AbstractItemSelector#getParentNodeId(javax.faces.context.FacesContext)
84     */

85    public String JavaDoc getParentNodeId(FacesContext context)
86    {
87       String JavaDoc id = null;
88       
89       if (this.navigationId != null)
90       {
91          ChildAssociationRef parentRef = getNodeService(context).getPrimaryParent(
92                new NodeRef(Repository.getStoreRef(), this.navigationId));
93          Node parentNode = new Node(parentRef.getParentRef());
94          
95          DictionaryService dd = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getDictionaryService();
96          
97          if (dd.isSubClass(parentNode.getType(), ContentModel.TYPE_CATEGORYROOT) == false)
98          {
99             id = parentRef.getParentRef().getId();
100          }
101       }
102       
103       return id;
104    }
105
106    /**
107     * Returns the child categories of the current navigation node
108     *
109     * @see org.alfresco.web.ui.repo.component.AbstractItemSelector#getChildrenForNode(javax.faces.context.FacesContext)
110     */

111    public Collection JavaDoc<NodeRef> getChildrenForNode(FacesContext context)
112    {
113       NodeRef nodeRef = new NodeRef(Repository.getStoreRef(), this.navigationId);
114       
115       Collection JavaDoc<ChildAssociationRef> childRefs = getCategoryService(context).getChildren(nodeRef,
116             CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.IMMEDIATE);
117       Collection JavaDoc<NodeRef> refs = new ArrayList JavaDoc<NodeRef>(childRefs.size());
118       for (ChildAssociationRef childRef : childRefs)
119       {
120          refs.add(childRef.getChildRef());
121       }
122       
123       return refs;
124    }
125
126    /**
127     * Returns the root categories
128     *
129     * @see org.alfresco.web.ui.repo.component.AbstractItemSelector#getRootChildren(javax.faces.context.FacesContext)
130     */

131    public Collection JavaDoc<NodeRef> getRootChildren(FacesContext context)
132    {
133       Collection JavaDoc<ChildAssociationRef> childRefs = getCategoryService(context).getCategories(
134             Repository.getStoreRef(), ContentModel.ASPECT_GEN_CLASSIFIABLE, Depth.IMMEDIATE);
135       Collection JavaDoc<NodeRef> refs = new ArrayList JavaDoc<NodeRef>(childRefs.size());
136       for (ChildAssociationRef childRef : childRefs)
137       {
138          refs.add(childRef.getChildRef());
139       }
140       
141       return refs;
142    }
143    
144    /**
145     * @see org.alfresco.web.ui.repo.component.AbstractItemSelector#getItemIcon()
146     */

147    public String JavaDoc getItemIcon()
148    {
149       return WebResources.IMAGE_CATEGORY;
150    }
151 }
152
Popular Tags