1 17 package org.alfresco.web.bean.preview; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Map ; 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 49 public abstract class BasePreviewBean 50 { 51 private static final String NO_SELECTION = "none"; 52 53 54 protected BrowseBean browseBean; 55 56 57 protected NodeService nodeService; 58 59 60 protected SearchService searchService; 61 62 63 protected NavigationBean navigator; 64 65 protected NodeRef template; 66 67 68 private ExpiringValueCache<List <SelectItem>> cachedTemplates = new ExpiringValueCache<List <SelectItem>>(1000*10); 69 70 71 74 public void setNodeService(NodeService nodeService) 75 { 76 this.nodeService = nodeService; 77 } 78 79 82 public void setBrowseBean(BrowseBean browseBean) 83 { 84 this.browseBean = browseBean; 85 } 86 87 90 public void setSearchService(SearchService searchService) 91 { 92 this.searchService = searchService; 93 } 94 95 98 public void setNavigator(NavigationBean navigator) 99 { 100 this.navigator = navigator; 101 } 102 103 108 public abstract Node getNode(); 109 110 115 public String getId() 116 { 117 return getNode().getId(); 118 } 119 120 125 public String getName() 126 { 127 return getNode().getName(); 128 } 129 130 133 public List <SelectItem> getTemplates() 134 { 135 List <SelectItem> templates = cachedTemplates.get(); 136 if (templates == null) 137 { 138 FacesContext context = FacesContext.getCurrentInstance(); 140 String 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 <NodeRef> results = this.searchService.selectNodes(rootNodeRef, xpath, null, resolver, false); 148 149 templates = new ArrayList <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 QuickSort sorter = new QuickSort(templates, "label", true, IDataContainer.SORT_CASEINSENSITIVE); 167 sorter.sort(); 168 } 169 } 170 catch (AccessDeniedException accessErr) 171 { 172 } 174 175 if (templates == null) 177 { 178 templates = new ArrayList <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 193 public abstract Map getTemplateModel(); 194 195 196 protected TemplateImageResolver imageResolver = new TemplateImageResolver() 197 { 198 public String resolveImagePathForName(String filename, boolean small) 199 { 200 return Utils.getFileTypeImage(filename, small); 201 } 202 }; 203 204 207 public NodeRef getTemplateRef() 208 { 209 return this.template; 210 } 211 212 215 public String getTemplate() 216 { 217 return (this.template != null ? this.template.getId() : null); 218 } 219 220 223 public void setTemplate(String 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 <Node> nodes, int start) 232 { 233 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 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 <Node> nodes, int start) 255 { 256 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 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 |