1 17 package org.alfresco.web.bean.repository; 18 19 import java.io.Serializable ; 20 import java.text.MessageFormat ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.faces.context.FacesContext; 27 import javax.servlet.ServletContext ; 28 import javax.transaction.UserTransaction ; 29 30 import org.alfresco.error.AlfrescoRuntimeException; 31 import org.alfresco.model.ContentModel; 32 import org.alfresco.repo.configuration.ConfigurableService; 33 import org.alfresco.repo.content.MimetypeMap; 34 import org.alfresco.repo.content.metadata.MetadataExtracter; 35 import org.alfresco.repo.content.metadata.MetadataExtracterRegistry; 36 import org.alfresco.service.ServiceRegistry; 37 import org.alfresco.service.cmr.lock.LockService; 38 import org.alfresco.service.cmr.lock.LockStatus; 39 import org.alfresco.service.cmr.repository.ChildAssociationRef; 40 import org.alfresco.service.cmr.repository.ContentReader; 41 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 42 import org.alfresco.service.cmr.repository.MimetypeService; 43 import org.alfresco.service.cmr.repository.NodeRef; 44 import org.alfresco.service.cmr.repository.NodeService; 45 import org.alfresco.service.cmr.repository.Path; 46 import org.alfresco.service.cmr.repository.StoreRef; 47 import org.alfresco.service.cmr.search.SearchService; 48 import org.alfresco.service.cmr.security.PersonService; 49 import org.alfresco.service.namespace.NamespaceService; 50 import org.alfresco.service.namespace.QName; 51 import org.alfresco.service.transaction.TransactionService; 52 import org.alfresco.web.app.Application; 53 import org.alfresco.web.ui.common.Utils; 54 import org.apache.log4j.Logger; 55 import org.springframework.web.context.support.WebApplicationContextUtils; 56 import org.springframework.web.jsf.FacesContextUtils; 57 58 64 public final class Repository 65 { 66 67 public static final String ERROR_NODEREF = "error_noderef"; 68 public static final String ERROR_GENERIC = "error_generic"; 69 public static final String ERROR_NOHOME = "error_homespace"; 70 public static final String ERROR_SEARCH = "error_search"; 71 72 private static final String METADATA_EXTACTER_REGISTRY = "metadataExtracterRegistry"; 73 74 private static Logger logger = Logger.getLogger(Repository.class); 75 76 77 private static StoreRef storeRef = null; 78 79 80 private static NodeRef peopleRef = null; 81 82 83 private static NodeRef systemRef = null; 84 85 86 private static NamespaceService namespaceService = null; 87 88 89 private static ServiceRegistry serviceRegistry = null; 90 91 94 private Repository() 95 { 96 } 97 98 103 public static StoreRef getStoreRef() 104 { 105 return storeRef; 106 } 107 108 114 public static StoreRef getStoreRef(ServletContext context) 115 { 116 storeRef = Application.getRepositoryStoreRef(context); 117 118 return storeRef; 119 } 120 121 130 public static String getNameForNode(NodeService nodeService, NodeRef ref) 131 { 132 String name = null; 133 134 Object nameProp = nodeService.getProperty(ref, ContentModel.PROP_NAME); 136 if (nameProp != null) 137 { 138 name = nameProp.toString(); 139 } 140 else 141 { 142 QName qname = nodeService.getPrimaryParent(ref).getQName(); 144 if (qname != null) 145 { 146 name = qname.getLocalName(); 147 } 148 } 149 150 return name; 151 } 152 153 160 public static String escapeQName(QName qName) 161 { 162 String string = qName.toString(); 163 StringBuilder buf = new StringBuilder (string.length() + 4); 164 for (int i = 0; i < string.length(); i++) 165 { 166 char c = string.charAt(i); 167 if ((c == '{') || (c == '}') || (c == ':') || (c == '-')) 168 { 169 buf.append('\\'); 170 } 171 172 buf.append(c); 173 } 174 return buf.toString(); 175 } 176 177 185 public static Boolean isNodeLocked(Node node, LockService lockService) 186 { 187 Boolean locked = Boolean.FALSE; 188 189 if (node.hasAspect(ContentModel.ASPECT_LOCKABLE)) 190 { 191 LockStatus lockStatus = lockService.getLockStatus(node.getNodeRef()); 192 if (lockStatus == LockStatus.LOCKED || lockStatus == LockStatus.LOCK_OWNER) 193 { 194 locked = Boolean.TRUE; 195 } 196 } 197 198 return locked; 199 } 200 201 209 public static Boolean isNodeOwnerLocked(Node node, LockService lockService) 210 { 211 Boolean locked = Boolean.FALSE; 212 213 if (node.hasAspect(ContentModel.ASPECT_LOCKABLE) && 214 lockService.getLockStatus(node.getNodeRef()) == LockStatus.LOCK_OWNER) 215 { 216 locked = Boolean.TRUE; 217 } 218 219 return locked; 220 } 221 222 230 public static Boolean isNodeOwner(Node node, LockService lockService) 231 { 232 Boolean locked = Boolean.FALSE; 233 234 if (node.hasAspect(ContentModel.ASPECT_WORKING_COPY)) 235 { 236 Object obj = node.getProperties().get("workingCopyOwner"); 237 if (obj instanceof String ) 238 { 239 User user = Application.getCurrentUser(FacesContext.getCurrentInstance()); 240 if ( ((String )obj).equals(user.getUserName())) 241 { 242 locked = Boolean.TRUE; 243 } 244 } 245 } 246 247 return locked; 248 } 249 250 258 public static String getDisplayPath(Path path) 259 { 260 StringBuilder buf = new StringBuilder (64); 261 262 for (int i=0; i<path.size()-1; i++) 263 { 264 String elementString = null; 265 Path.Element element = path.get(i); 266 if (element instanceof Path.ChildAssocElement) 267 { 268 ChildAssociationRef elementRef = ((Path.ChildAssocElement)element).getRef(); 269 if (elementRef.getParentRef() != null) 270 { 271 elementString = elementRef.getQName().getLocalName(); 272 } 273 } 274 else 275 { 276 elementString = element.getElementString(); 277 } 278 279 if (elementString != null) 280 { 281 buf.append("/"); 282 buf.append(elementString); 283 } 284 } 285 286 return buf.toString(); 287 } 288 289 298 public static String getNamePath(NodeService nodeService, Path path, NodeRef rootNode, String separator, String prefix) 299 { 300 StringBuilder buf = new StringBuilder (128); 301 302 boolean foundRoot = (rootNode == null); 304 305 buf.append(prefix); 306 307 for (int i=1; i<path.size(); i++) 309 { 310 Path.Element element = path.get(i); 311 String elementString = null; 312 if (element instanceof Path.ChildAssocElement) 313 { 314 ChildAssociationRef elementRef = ((Path.ChildAssocElement)element).getRef(); 315 if (elementRef.getParentRef() != null) 316 { 317 if (foundRoot == true) 319 { 320 Object nameProp = nodeService.getProperty(elementRef.getChildRef(), ContentModel.PROP_NAME); 321 if (nameProp != null) 322 { 323 elementString = nameProp.toString(); 324 } 325 else 326 { 327 elementString = element.getElementString(); 328 } 329 } 330 331 foundRoot = (foundRoot || elementRef.getChildRef().equals(rootNode)); 334 } 335 } 336 else 337 { 338 elementString = element.getElementString(); 339 } 340 341 if (elementString != null) 342 { 343 buf.append(separator); 344 buf.append(elementString); 345 } 346 } 347 348 return buf.toString(); 349 } 350 351 361 public static String getMimeTypeForFileName(FacesContext context, String filename) 362 { 363 MimetypeService mimetypeService = (MimetypeService)getServiceRegistry(context).getMimetypeService(); 365 366 String mimetype = MimetypeMap.MIMETYPE_BINARY; 368 int extIndex = filename.lastIndexOf('.'); 369 if (extIndex != -1) 370 { 371 String ext = filename.substring(extIndex + 1).toLowerCase(); 372 String mt = mimetypeService.getMimetypesByExtension().get(ext); 373 if (mt != null) 374 { 375 mimetype = mt; 376 } 377 } 378 379 return mimetype; 380 } 381 382 389 public static UserTransaction getUserTransaction(FacesContext context) 390 { 391 TransactionService transactionService = getServiceRegistry(context).getTransactionService(); 392 return transactionService.getUserTransaction(); 393 } 394 395 403 public static UserTransaction getUserTransaction(FacesContext context, boolean readonly) 404 { 405 TransactionService transactionService = getServiceRegistry(context).getTransactionService(); 406 return transactionService.getUserTransaction(readonly); 407 } 408 409 415 public static ServiceRegistry getServiceRegistry(FacesContext context) 416 { 417 if (serviceRegistry == null) 418 { 419 serviceRegistry = (ServiceRegistry)FacesContextUtils.getRequiredWebApplicationContext( 420 context).getBean(ServiceRegistry.SERVICE_REGISTRY); 421 } 422 return serviceRegistry; 423 } 424 425 431 public static ServiceRegistry getServiceRegistry(ServletContext context) 432 { 433 if (serviceRegistry == null) 434 { 435 serviceRegistry = (ServiceRegistry)WebApplicationContextUtils.getRequiredWebApplicationContext( 436 context).getBean(ServiceRegistry.SERVICE_REGISTRY); 437 } 438 return serviceRegistry; 439 } 440 441 446 public static ConfigurableService getConfigurableService(FacesContext context) 447 { 448 return (ConfigurableService)FacesContextUtils.getRequiredWebApplicationContext(context).getBean("configurableService"); 449 } 450 451 457 public static MetadataExtracterRegistry getMetadataExtracterRegistry(FacesContext context) 458 { 459 return (MetadataExtracterRegistry)FacesContextUtils.getRequiredWebApplicationContext( 460 context).getBean(METADATA_EXTACTER_REGISTRY); 461 } 462 463 471 public static boolean extractMetadata(FacesContext context, ContentReader reader, Map <QName, Serializable > destination) 472 { 473 String mimetype = reader.getMimetype(); 475 if (mimetype == null) 476 { 477 throw new AlfrescoRuntimeException("The content reader mimetype must be set: " + reader); 478 } 479 480 MetadataExtracter extracter = getMetadataExtracterRegistry(context).getExtracter(mimetype); 482 if (extracter == null) 483 { 484 return false; 486 } 487 488 try 489 { 490 extracter.extract(reader, destination); 492 return true; 493 } 494 catch (Throwable e) 495 { 496 logger.warn("Metadata extraction failed: \n" + 498 " reader: " + reader + "\n" + 499 " extracter: " + extracter); 500 return false; 501 } 502 } 503 504 513 public static List <Node> getUsers(FacesContext context, NodeService nodeService, SearchService searchService) 514 { 515 List <Node> personNodes = null; 516 517 UserTransaction tx = null; 518 try 519 { 520 tx = Repository.getUserTransaction(context, true); 521 tx.begin(); 522 523 PersonService personService = (PersonService)FacesContextUtils.getRequiredWebApplicationContext(context).getBean("personService"); 524 NodeRef peopleRef = personService.getPeopleContainer(); 525 526 List <ChildAssociationRef> childRefs = nodeService.getChildAssocs(peopleRef); 528 personNodes = new ArrayList <Node>(childRefs.size()); 529 for (ChildAssociationRef ref: childRefs) 530 { 531 NodeRef nodeRef = ref.getChildRef(); 533 534 if (nodeService.getType(nodeRef).equals(ContentModel.TYPE_PERSON)) 535 { 536 MapNode node = new MapNode(nodeRef); 538 539 Map <String , Object > props = node.getProperties(); 543 String lastName = (String )props.get("lastName"); 544 props.put("fullName", ((String )props.get("firstName")) + ' ' + (lastName != null ? lastName : "")); 545 NodeRef homeFolderNodeRef = (NodeRef)props.get("homeFolder"); 546 if (homeFolderNodeRef != null) 547 { 548 props.put("homeSpace", homeFolderNodeRef); 549 } 550 551 personNodes.add(node); 552 } 553 } 554 555 tx.commit(); 557 } 558 catch (InvalidNodeRefException refErr) 559 { 560 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 561 context, Repository.ERROR_NODEREF), new Object [] {"root"}) ); 562 personNodes = Collections.<Node>emptyList(); 563 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 564 } 565 catch (Throwable err) 566 { 567 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 568 context, Repository.ERROR_GENERIC), err.getMessage()), err ); 569 personNodes = Collections.<Node>emptyList(); 570 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 571 } 572 573 return personNodes; 574 } 575 576 584 public static String safePropertyToString(Serializable value) 585 { 586 if (value == null) 587 { 588 return null; 589 } 590 else if (value instanceof String ) 591 { 592 return (String )value; 593 } 594 else 595 { 596 return value.toString(); 597 } 598 } 599 600 609 public static QName resolveToQName(String str) 610 { 611 return QName.resolveToQName(getNamespaceService(), str); 612 } 613 614 623 public static String resolveToQNameString(String str) 624 { 625 return QName.resolveToQNameString(getNamespaceService(), str); 626 } 627 628 633 private static NamespaceService getNamespaceService() 634 { 635 if (namespaceService == null) 636 { 637 ServiceRegistry svcReg = getServiceRegistry(FacesContext.getCurrentInstance()); 638 namespaceService = svcReg.getNamespaceService(); 639 } 640 641 return namespaceService; 642 } 643 } 644 | Popular Tags |