1 17 package org.alfresco.web.bean; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.faces.application.FacesMessage; 26 import javax.faces.context.FacesContext; 27 import javax.faces.event.ActionEvent; 28 import javax.faces.model.SelectItem; 29 import javax.transaction.UserTransaction ; 30 31 import org.alfresco.model.ContentModel; 32 import org.alfresco.service.cmr.dictionary.DictionaryService; 33 import org.alfresco.service.cmr.repository.NodeRef; 34 import org.alfresco.service.cmr.repository.NodeService; 35 import org.alfresco.service.cmr.repository.TemplateImageResolver; 36 import org.alfresco.service.cmr.repository.TemplateNode; 37 import org.alfresco.service.cmr.security.OwnableService; 38 import org.alfresco.service.cmr.security.PermissionService; 39 import org.alfresco.service.namespace.NamespaceService; 40 import org.alfresco.web.app.AlfrescoNavigationHandler; 41 import org.alfresco.web.app.Application; 42 import org.alfresco.web.bean.repository.Node; 43 import org.alfresco.web.bean.repository.Repository; 44 import org.alfresco.web.data.IDataContainer; 45 import org.alfresco.web.data.QuickSort; 46 import org.alfresco.web.ui.common.Utils; 47 import org.alfresco.web.ui.common.Utils.URLMode; 48 import org.alfresco.web.ui.common.component.UIActionLink; 49 50 55 public class SpaceDetailsBean 56 { 57 private static final String MSG_SUCCESS_OWNERSHIP = "success_ownership"; 58 59 private static final String OUTCOME_RETURN = "showSpaceDetails"; 60 61 62 protected BrowseBean browseBean; 63 64 65 protected NavigationBean navigator; 66 67 68 protected PermissionService permissionService; 69 70 71 protected OwnableService ownableService; 72 73 74 protected NodeService nodeService; 75 76 77 private String template; 78 79 80 83 88 public void setBrowseBean(BrowseBean browseBean) 89 { 90 this.browseBean = browseBean; 91 } 92 93 96 public void setNavigator(NavigationBean navigator) 97 { 98 this.navigator = navigator; 99 } 100 101 104 public void setNodeService(NodeService nodeService) 105 { 106 this.nodeService = nodeService; 107 } 108 109 112 public void setPermissionService(PermissionService permissionService) 113 { 114 this.permissionService = permissionService; 115 } 116 117 122 public void setOwnableService(OwnableService ownableService) 123 { 124 this.ownableService = ownableService; 125 } 126 127 132 public Node getSpace() 133 { 134 return this.browseBean.getActionSpace(); 135 } 136 137 142 public String getId() 143 { 144 return getSpace().getId(); 145 } 146 147 152 public String getName() 153 { 154 return getSpace().getName(); 155 } 156 157 162 public String getWebdavUrl() 163 { 164 return Utils.generateURL(FacesContext.getCurrentInstance(), getSpace(), URLMode.WEBDAV); 165 } 166 167 172 public String getBookmarkUrl() 173 { 174 return Utils.generateURL(FacesContext.getCurrentInstance(), getSpace(), URLMode.SHOW_DETAILS); 175 } 176 177 182 public String getCifsPath() 183 { 184 return Utils.generateURL(FacesContext.getCurrentInstance(), getSpace(), URLMode.CIFS); 185 } 186 187 192 public String getNodeRefUrl() 193 { 194 return getSpace().getNodeRef().toString(); 195 } 196 197 200 public String getTemplate() 201 { 202 NodeRef ref = (NodeRef)getSpace().getProperties().get(ContentModel.PROP_TEMPLATE); 204 return ref != null ? ref.getId() : this.template; 205 } 206 207 210 public void setTemplate(String template) 211 { 212 this.template = template; 213 } 214 215 219 public boolean isTemplatable() 220 { 221 NodeRef templateRef = (NodeRef)getSpace().getProperties().get(ContentModel.PROP_TEMPLATE); 222 return (getSpace().hasAspect(ContentModel.ASPECT_TEMPLATABLE) && 223 templateRef != null && nodeService.exists(templateRef)); 224 } 225 226 229 public String getTemplateRef() 230 { 231 NodeRef ref = (NodeRef)getSpace().getProperties().get(ContentModel.PROP_TEMPLATE); 232 return ref != null ? ref.toString() : null; 233 } 234 235 240 @SuppressWarnings ("unchecked") 241 public Map getTemplateModel() 242 { 243 HashMap model = new HashMap (1, 1.0f); 244 245 FacesContext fc = FacesContext.getCurrentInstance(); 246 TemplateNode spaceNode = new TemplateNode(getSpace().getNodeRef(), Repository.getServiceRegistry(fc), 247 new TemplateImageResolver() { 248 public String resolveImagePathForName(String filename, boolean small) { 249 return Utils.getFileTypeImage(filename, small); 250 } 251 }); 252 model.put("space", spaceNode); 253 254 return model; 255 } 256 257 260 public SelectItem[] getTemplates() 261 { 262 FacesContext context = FacesContext.getCurrentInstance(); 264 String xpath = Application.getRootPath(context) + "/" + 265 Application.getGlossaryFolderName(context) + "/" + 266 Application.getContentTemplatesFolderName(context) + "//*"; 267 NodeRef rootNodeRef = this.nodeService.getRootNode(Repository.getStoreRef()); 268 NamespaceService resolver = Repository.getServiceRegistry(context).getNamespaceService(); 269 List <NodeRef> results = Repository.getServiceRegistry(context).getSearchService().selectNodes( 270 rootNodeRef, xpath, null, resolver, false); 271 272 List <SelectItem> templates = new ArrayList <SelectItem>(results.size()); 273 if (results.size() != 0) 274 { 275 DictionaryService dd = Repository.getServiceRegistry(context).getDictionaryService(); 276 for (NodeRef ref : results) 277 { 278 Node childNode = new Node(ref); 279 if (dd.isSubClass(childNode.getType(), ContentModel.TYPE_CONTENT)) 280 { 281 templates.add(new SelectItem(childNode.getId(), childNode.getName())); 282 } 283 } 284 285 QuickSort sorter = new QuickSort(templates, "label", true, IDataContainer.SORT_CASEINSENSITIVE); 287 sorter.sort(); 288 } 289 290 return templates.toArray(new SelectItem[templates.size()]); 291 } 292 293 294 297 300 public String applyTemplate() 301 { 302 try 303 { 304 if (getSpace().hasAspect(ContentModel.ASPECT_TEMPLATABLE) == false) 306 { 307 this.nodeService.addAspect(getSpace().getNodeRef(), ContentModel.ASPECT_TEMPLATABLE, null); 308 } 309 310 NodeRef templateRef = new NodeRef(Repository.getStoreRef(), this.template); 312 313 this.nodeService.setProperty(getSpace().getNodeRef(), ContentModel.PROP_TEMPLATE, templateRef); 315 316 getSpace().reset(); 318 } 319 catch (Exception e) 320 { 321 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 322 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e); 323 } 324 return OUTCOME_RETURN; 325 } 326 327 330 public String removeTemplate() 331 { 332 try 333 { 334 this.nodeService.setProperty(getSpace().getNodeRef(), ContentModel.PROP_TEMPLATE, null); 336 this.nodeService.removeAspect(getSpace().getNodeRef(), ContentModel.ASPECT_TEMPLATABLE); 337 338 getSpace().reset(); 340 } 341 catch (Exception e) 342 { 343 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 344 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e); 345 } 346 return OUTCOME_RETURN; 347 } 348 349 352 public void takeOwnership(ActionEvent event) 353 { 354 UserTransaction tx = null; 355 356 try 357 { 358 tx = Repository.getUserTransaction(FacesContext.getCurrentInstance()); 359 tx.begin(); 360 361 this.ownableService.takeOwnership(getSpace().getNodeRef()); 362 363 FacesContext context = FacesContext.getCurrentInstance(); 364 String msg = Application.getMessage(context, MSG_SUCCESS_OWNERSHIP); 365 FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg); 366 context.addMessage(event.getComponent().getClientId(context), facesMsg); 367 368 tx.commit(); 370 } 371 catch (Throwable e) 372 { 373 try { if (tx != null) {tx.rollback();} } catch (Exception ex) {} 375 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 376 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e); 377 } 378 } 379 380 383 public void nextItem(ActionEvent event) 384 { 385 UIActionLink link = (UIActionLink)event.getComponent(); 386 Map <String , String > params = link.getParameterMap(); 387 String id = params.get("id"); 388 if (id != null && id.length() != 0) 389 { 390 List <Node> nodes = this.browseBean.getNodes(); 391 if (nodes.size() > 1) 392 { 393 for (int i=0; i<nodes.size(); i++) 398 { 399 if (id.equals(nodes.get(i).getId()) == true) 400 { 401 Node next; 402 if (i != nodes.size() - 1) 404 { 405 next = nodes.get(i + 1); 406 } 407 else 408 { 409 next = nodes.get(0); 411 } 412 413 this.browseBean.setupSpaceAction(next.getId(), false); 415 } 416 } 417 } 418 } 419 } 420 421 424 public void previousItem(ActionEvent event) 425 { 426 UIActionLink link = (UIActionLink)event.getComponent(); 427 Map <String , String > params = link.getParameterMap(); 428 String id = params.get("id"); 429 if (id != null && id.length() != 0) 430 { 431 List <Node> nodes = this.browseBean.getNodes(); 432 if (nodes.size() > 1) 433 { 434 for (int i=0; i<nodes.size(); i++) 436 { 437 if (id.equals(nodes.get(i).getId()) == true) 438 { 439 Node previous; 440 if (i != 0) 442 { 443 previous = nodes.get(i - 1); 444 } 445 else 446 { 447 previous = nodes.get(nodes.size() - 1); 449 } 450 451 this.browseBean.setupSpaceAction(previous.getId(), false); 453 } 454 } 455 } 456 } 457 } 458 459 463 public String closeDialog() 464 { 465 this.navigator.resetCurrentNodeProperties(); 466 return AlfrescoNavigationHandler.CLOSE_DIALOG_OUTCOME; 467 } 468 } 469 | Popular Tags |