1 17 package org.alfresco.web.bean; 18 19 import java.io.Serializable ; 20 import java.text.MessageFormat ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.faces.context.FacesContext; 26 import javax.faces.event.ActionEvent; 27 import javax.transaction.UserTransaction ; 28 29 import org.alfresco.model.ContentModel; 30 import org.alfresco.repo.security.permissions.AccessDeniedException; 31 import org.alfresco.service.cmr.dictionary.DictionaryService; 32 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 33 import org.alfresco.service.cmr.repository.NodeRef; 34 import org.alfresco.service.cmr.repository.NodeService; 35 import org.alfresco.service.cmr.security.AccessStatus; 36 import org.alfresco.service.cmr.security.PermissionService; 37 import org.alfresco.service.namespace.NamespaceService; 38 import org.alfresco.service.namespace.QName; 39 import org.alfresco.web.app.Application; 40 import org.alfresco.web.bean.repository.Node; 41 import org.alfresco.web.bean.repository.Repository; 42 import org.alfresco.web.ui.common.Utils; 43 import org.alfresco.web.ui.common.component.UIActionLink; 44 import org.alfresco.web.ui.repo.component.shelf.UIShortcutsShelfItem; 45 import org.apache.log4j.Logger; 46 47 52 public class UserShortcutsBean 53 { 54 private static Logger logger = Logger.getLogger(UserShortcutsBean.class); 55 56 57 protected NodeService nodeService; 58 59 60 protected BrowseBean browseBean; 61 62 63 protected PermissionService permissionService; 64 65 66 private List <Node> shortcuts = null; 67 68 private QName QNAME_SHORTCUTS = QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "shortcuts"); 69 70 71 74 77 public void setNodeService(NodeService nodeService) 78 { 79 this.nodeService = nodeService; 80 } 81 82 85 public void setBrowseBean(BrowseBean browseBean) 86 { 87 this.browseBean = browseBean; 88 } 89 90 93 public void setPermissionService(PermissionService permissionService) 94 { 95 this.permissionService = permissionService; 96 } 97 98 101 public List <Node> getShortcuts() 102 { 103 if (this.shortcuts == null) 104 { 105 List <String > shortcuts = null; 106 NodeRef prefRef = null; 107 UserTransaction tx = null; 108 boolean rollback = false; 109 try 110 { 111 FacesContext context = FacesContext.getCurrentInstance(); 112 tx = Repository.getUserTransaction(context); 113 tx.begin(); 114 115 prefRef = getShortcutsNodeRef(); 117 shortcuts = (List <String >)this.nodeService.getProperty(prefRef, QNAME_SHORTCUTS); 118 if (shortcuts != null) 119 { 120 this.shortcuts = new ArrayList <Node>(shortcuts.size()); 122 for (int i=0; i<shortcuts.size(); i++) 123 { 124 NodeRef ref = new NodeRef(Repository.getStoreRef(), shortcuts.get(i)); 125 try 126 { 127 if (this.nodeService.exists(ref) == true) 128 { 129 Node node = new Node(ref); 130 131 node.getProperties(); 133 134 this.shortcuts.add(node); 136 } 137 else 138 { 139 if (logger.isDebugEnabled()) 142 logger.debug("Found invalid shortcut node Id: " + ref.getId()); 143 } 144 } 145 catch (AccessDeniedException accessErr) 146 { 147 if (logger.isDebugEnabled()) 150 logger.debug("Found invalid shortcut node Id: " + ref.getId()); 151 rollback = true; 152 } 153 } 154 } 155 else 156 { 157 this.shortcuts = new ArrayList <Node>(5); 158 } 159 160 if (rollback == false) 161 { 162 tx.commit(); 163 } 164 else 165 { 166 tx.rollback(); 167 } 168 } 169 catch (Throwable err) 170 { 171 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 172 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 173 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 174 } 175 176 if (shortcuts != null && shortcuts.size() != this.shortcuts.size()) 179 { 180 try 181 { 182 shortcuts = new ArrayList <String >(this.shortcuts.size()); 183 for (int i=0; i<this.shortcuts.size(); i++) 184 { 185 shortcuts.add(this.shortcuts.get(i).getId()); 186 } 187 this.nodeService.setProperty(prefRef, QNAME_SHORTCUTS, (Serializable )shortcuts); 188 } 189 catch (Exception err) 190 { 191 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 192 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 193 } 194 } 195 } 196 197 return this.shortcuts; 198 } 199 200 203 public void setShortcuts(List <Node> nodes) 204 { 205 this.shortcuts = nodes; 206 } 207 208 209 212 215 public void createShortcut(ActionEvent event) 216 { 217 UIActionLink link = (UIActionLink)event.getComponent(); 219 Map <String , String > params = link.getParameterMap(); 220 String id = params.get("id"); 221 if (id != null && id.length() != 0) 222 { 223 try 224 { 225 NodeRef ref = new NodeRef(Repository.getStoreRef(), id); 226 Node node = new Node(ref); 227 228 boolean foundShortcut = false; 229 for (int i=0; i<getShortcuts().size(); i++) 230 { 231 if (node.getId().equals(getShortcuts().get(i).getId())) 232 { 233 foundShortcut = true; 235 break; 236 } 237 } 238 239 if (foundShortcut == false) 240 { 241 UserTransaction tx = null; 243 try 244 { 245 FacesContext context = FacesContext.getCurrentInstance(); 246 tx = Repository.getUserTransaction(context); 247 tx.begin(); 248 249 NodeRef prefRef = getShortcutsNodeRef(); 250 List <String > shortcuts = (List <String >)this.nodeService.getProperty(prefRef, QNAME_SHORTCUTS); 251 if (shortcuts == null) 252 { 253 shortcuts = new ArrayList <String >(1); 254 } 255 shortcuts.add(node.getNodeRef().getId()); 256 this.nodeService.setProperty(prefRef, QNAME_SHORTCUTS, (Serializable )shortcuts); 257 258 tx.commit(); 260 261 getShortcuts().add(node); 263 264 if (logger.isDebugEnabled()) 265 logger.debug("Added node: " + node.getName() + " to the user shortcuts list."); 266 } 267 catch (Throwable err) 268 { 269 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 270 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 271 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 272 } 273 } 274 } 275 catch (InvalidNodeRefException refErr) 276 { 277 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 278 FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object [] {id}) ); 279 } 280 } 281 } 282 283 286 private NodeRef getShortcutsNodeRef() 287 { 288 return Application.getCurrentUser(FacesContext.getCurrentInstance()).getUserPreferencesRef(); 289 } 290 291 294 public void removeShortcut(ActionEvent event) 295 { 296 UIShortcutsShelfItem.ShortcutEvent shortcutEvent = (UIShortcutsShelfItem.ShortcutEvent)event; 297 298 UserTransaction tx = null; 300 try 301 { 302 FacesContext context = FacesContext.getCurrentInstance(); 303 tx = Repository.getUserTransaction(context); 304 tx.begin(); 305 306 NodeRef prefRef = getShortcutsNodeRef(); 307 List <String > shortcuts = (List <String >)this.nodeService.getProperty(prefRef, QNAME_SHORTCUTS); 308 if (shortcuts != null && shortcuts.size() > shortcutEvent.Index) 309 { 310 shortcuts.remove(shortcutEvent.Index); 312 this.nodeService.setProperty(prefRef, QNAME_SHORTCUTS, (Serializable )shortcuts); 313 314 tx.commit(); 316 317 Node node = getShortcuts().remove(shortcutEvent.Index); 319 320 if (logger.isDebugEnabled()) 321 logger.debug("Removed node: " + node.getName() + " from the user shortcuts list."); 322 } 323 } 324 catch (Throwable err) 325 { 326 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 327 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 328 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 329 } 330 } 331 332 335 public void click(ActionEvent event) 336 { 337 UIShortcutsShelfItem.ShortcutEvent shortcutEvent = (UIShortcutsShelfItem.ShortcutEvent)event; 339 Node selectedNode = getShortcuts().get(shortcutEvent.Index); 340 341 try 342 { 343 if (permissionService.hasPermission(selectedNode.getNodeRef(), PermissionService.READ) == AccessStatus.ALLOWED) 344 { 345 if (nodeService.exists(selectedNode.getNodeRef()) == false) 346 { 347 throw new InvalidNodeRefException(selectedNode.getNodeRef()); 348 } 349 350 DictionaryService dd = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getDictionaryService(); 351 if (dd.isSubClass(selectedNode.getType(), ContentModel.TYPE_FOLDER)) 352 { 353 this.browseBean.updateUILocation(selectedNode.getNodeRef()); 356 } 357 else if (dd.isSubClass(selectedNode.getType(), ContentModel.TYPE_CONTENT)) 358 { 359 this.browseBean.setupContentAction(selectedNode.getId(), true); 361 FacesContext fc = FacesContext.getCurrentInstance(); 362 fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "dialog:showDocDetails"); 363 } 364 } 365 else 366 { 367 Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), "error_shortcut_permissions")); 368 } 369 } 370 catch (InvalidNodeRefException refErr) 371 { 372 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 373 FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object [] {selectedNode.getId()}) ); 374 375 UserTransaction tx = null; 377 try 378 { 379 FacesContext context = FacesContext.getCurrentInstance(); 380 tx = Repository.getUserTransaction(context); 381 tx.begin(); 382 383 NodeRef prefRef = getShortcutsNodeRef(); 384 List <String > shortcuts = (List <String >)this.nodeService.getProperty(prefRef, QNAME_SHORTCUTS); 385 if (shortcuts != null && shortcuts.size() > shortcutEvent.Index) 386 { 387 shortcuts.remove(shortcutEvent.Index); 389 this.nodeService.setProperty(prefRef, QNAME_SHORTCUTS, (Serializable )shortcuts); 390 391 tx.commit(); 393 394 Node node = getShortcuts().remove(shortcutEvent.Index); 396 397 if (logger.isDebugEnabled()) 398 logger.debug("Removed deleted node: " + node.getName() + " from the user shortcuts list."); 399 } 400 } 401 catch (Throwable err) 402 { 403 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 404 } 405 } 406 } 407 } 408 | Popular Tags |