KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > preview > BasePreviewBean


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.preview;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.faces.context.FacesContext;
24 import javax.faces.model.SelectItem;
25
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.repo.cache.ExpiringValueCache;
28 import org.alfresco.repo.security.permissions.AccessDeniedException;
29 import org.alfresco.service.cmr.dictionary.DictionaryService;
30 import org.alfresco.service.cmr.repository.NodeRef;
31 import org.alfresco.service.cmr.repository.NodeService;
32 import org.alfresco.service.cmr.repository.TemplateImageResolver;
33 import org.alfresco.service.cmr.search.SearchService;
34 import org.alfresco.service.namespace.NamespaceService;
35 import org.alfresco.web.app.Application;
36 import org.alfresco.web.bean.BrowseBean;
37 import org.alfresco.web.bean.NavigationBean;
38 import org.alfresco.web.bean.repository.Node;
39 import org.alfresco.web.bean.repository.Repository;
40 import org.alfresco.web.data.IDataContainer;
41 import org.alfresco.web.data.QuickSort;
42 import org.alfresco.web.ui.common.Utils;
43
44 /**
45  * Backing bean for the Preview Document in Template action page
46  *
47  * @author Kevin Roast
48  */

49 public abstract class BasePreviewBean
50 {
51    private static final String JavaDoc NO_SELECTION = "none";
52
53    /** BrowseBean instance */
54    protected BrowseBean browseBean;
55    
56    /** NodeService instance */
57    protected NodeService nodeService;
58    
59    /** The SearchService instance */
60    protected SearchService searchService;
61    
62    /** The NavigationBean bean reference */
63    protected NavigationBean navigator;
64    
65    protected NodeRef template;
66    
67    /** cache of templates that last 10 seconds - enough for a couple of page refreshes */
68    private ExpiringValueCache<List JavaDoc<SelectItem>> cachedTemplates = new ExpiringValueCache<List JavaDoc<SelectItem>>(1000*10);
69    
70    
71    /**
72     * @param nodeService The nodeService to set.
73     */

74    public void setNodeService(NodeService nodeService)
75    {
76       this.nodeService = nodeService;
77    }
78
79    /**
80     * @param browseBean The BrowseBean to set.
81     */

82    public void setBrowseBean(BrowseBean browseBean)
83    {
84       this.browseBean = browseBean;
85    }
86    
87    /**
88     * @param searchService The searchService to set.
89     */

90    public void setSearchService(SearchService searchService)
91    {
92       this.searchService = searchService;
93    }
94    
95    /**
96     * @param navigator The NavigationBean to set.
97     */

98    public void setNavigator(NavigationBean navigator)
99    {
100       this.navigator = navigator;
101    }
102    
103    /**
104     * Returns the node this bean is currently working with
105     *
106     * @return The current Node
107     */

108    public abstract Node getNode();
109    
110    /**
111     * Returns the id of the current node
112     *
113     * @return The id
114     */

115    public String JavaDoc getId()
116    {
117       return getNode().getId();
118    }
119    
120    /**
121     * Returns the name of the current node
122     *
123     * @return Name of the current node
124     */

125    public String JavaDoc getName()
126    {
127       return getNode().getName();
128    }
129    
130    /**
131     * @return the list of available Content Templates that can be applied to the current document.
132     */

133    public List JavaDoc<SelectItem> getTemplates()
134    {
135       List JavaDoc<SelectItem> templates = cachedTemplates.get();
136       if (templates == null)
137       {
138          // get the template from the special Content Templates folder
139
FacesContext context = FacesContext.getCurrentInstance();
140          String JavaDoc xpath = Application.getRootPath(context) + "/" +
141                Application.getGlossaryFolderName(context) + "/" +
142                Application.getContentTemplatesFolderName(context) + "//*";
143          try
144          {
145             NodeRef rootNodeRef = this.nodeService.getRootNode(Repository.getStoreRef());
146             NamespaceService resolver = Repository.getServiceRegistry(context).getNamespaceService();
147             List JavaDoc<NodeRef> results = this.searchService.selectNodes(rootNodeRef, xpath, null, resolver, false);
148             
149             templates = new ArrayList JavaDoc<SelectItem>(results.size() + 1);
150             if (results.size() != 0)
151             {
152                DictionaryService dd = Repository.getServiceRegistry(context).getDictionaryService();
153                for (NodeRef ref : results)
154                {
155                   if (nodeService.exists(ref) == true)
156                   {
157                      Node childNode = new Node(ref);
158                      if (dd.isSubClass(childNode.getType(), ContentModel.TYPE_CONTENT))
159                      {
160                         templates.add(new SelectItem(childNode.getId(), childNode.getName()));
161                      }
162                   }
163                }
164                
165                // make sure the list is sorted by the label
166
QuickSort sorter = new QuickSort(templates, "label", true, IDataContainer.SORT_CASEINSENSITIVE);
167                sorter.sort();
168             }
169          }
170          catch (AccessDeniedException accessErr)
171          {
172             // ignore the result if we cannot access the root
173
}
174          
175          // add an entry (at the start) to instruct the user to select a template
176
if (templates == null)
177          {
178             templates = new ArrayList JavaDoc<SelectItem>(1);
179          }
180          templates.add(0, new SelectItem(NO_SELECTION, Application.getMessage(FacesContext.getCurrentInstance(), "select_a_template")));
181          
182          cachedTemplates.put(templates);
183       }
184       
185       return templates;
186    }
187    
188    /**
189     * Returns a model for use by the template on the Preview page.
190     *
191     * @return model containing current document/space info.
192     */

193    public abstract Map JavaDoc getTemplateModel();
194    
195    /** Template Image resolver helper */
196    protected TemplateImageResolver imageResolver = new TemplateImageResolver()
197    {
198       public String JavaDoc resolveImagePathForName(String JavaDoc filename, boolean small)
199       {
200          return Utils.getFileTypeImage(filename, small);
201       }
202    };
203
204    /**
205     * @return the current template as a full NodeRef
206     */

207    public NodeRef getTemplateRef()
208    {
209       return this.template;
210    }
211    
212    /**
213     * @return Returns the template Id.
214     */

215    public String JavaDoc getTemplate()
216    {
217       return (this.template != null ? this.template.getId() : null);
218    }
219
220    /**
221     * @param template The template Id to set.
222     */

223    public void setTemplate(String JavaDoc template)
224    {
225       if (template != null && template.equals(NO_SELECTION) == false)
226       {
227          this.template = new NodeRef(Repository.getStoreRef(), template);
228       }
229    }
230    
231    private int findNextPreviewNode(List JavaDoc<Node> nodes, int start)
232    {
233       // search from start to end of list
234
for (int i=start; i<nodes.size(); i++)
235       {
236          Node next = nodes.get(i);
237          if (next.hasAspect(ContentModel.ASPECT_TEMPLATABLE))
238          {
239             return i;
240          }
241       }
242       // search from zero index to start - 1 (to skip original node)
243
for (int i=0; i<start - 1; i++)
244       {
245          Node next = nodes.get(i);
246          if (next.hasAspect(ContentModel.ASPECT_TEMPLATABLE))
247          {
248             return i;
249          }
250       }
251       return -1;
252    }
253    
254    private int findPrevPreviewNode(List JavaDoc<Node> nodes, int start)
255    {
256       // search from start to beginning of list
257
for (int i=start; i>=0; i--)
258       {
259          Node next = nodes.get(i);
260          if (next.hasAspect(ContentModel.ASPECT_TEMPLATABLE))
261          {
262             return i;
263          }
264       }
265       // end of list to start + 1 (to skip original node)
266
for (int i=nodes.size() - 1; i>start; i--)
267       {
268          Node next = nodes.get(i);
269          if (next.hasAspect(ContentModel.ASPECT_TEMPLATABLE))
270          {
271             return i;
272          }
273       }
274       return -1;
275    }
276 }
277
Popular Tags