1 17 package org.alfresco.repo.webservice; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Calendar ; 22 import java.util.Date ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.transaction.UserTransaction ; 29 30 import org.alfresco.repo.webservice.axis.QueryConfigHandler; 31 import org.alfresco.repo.webservice.types.AssociationDefinition; 32 import org.alfresco.repo.webservice.types.Cardinality; 33 import org.alfresco.repo.webservice.types.ClassDefinition; 34 import org.alfresco.repo.webservice.types.NamedValue; 35 import org.alfresco.repo.webservice.types.ParentReference; 36 import org.alfresco.repo.webservice.types.Predicate; 37 import org.alfresco.repo.webservice.types.PropertyDefinition; 38 import org.alfresco.repo.webservice.types.Query; 39 import org.alfresco.repo.webservice.types.QueryLanguageEnum; 40 import org.alfresco.repo.webservice.types.Reference; 41 import org.alfresco.repo.webservice.types.RoleDefinition; 42 import org.alfresco.repo.webservice.types.Store; 43 import org.alfresco.repo.webservice.types.StoreEnum; 44 import org.alfresco.repo.webservice.types.Version; 45 import org.alfresco.service.ServiceRegistry; 46 import org.alfresco.service.cmr.repository.NodeRef; 47 import org.alfresco.service.cmr.repository.NodeService; 48 import org.alfresco.service.cmr.repository.StoreRef; 49 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 50 import org.alfresco.service.cmr.search.ResultSet; 51 import org.alfresco.service.cmr.search.SearchService; 52 import org.alfresco.service.cmr.version.VersionType; 53 import org.alfresco.service.namespace.NamespaceService; 54 import org.alfresco.service.namespace.QName; 55 import org.alfresco.service.transaction.TransactionService; 56 import org.apache.axis.MessageContext; 57 import org.apache.axis.transport.http.HTTPConstants; 58 import org.apache.commons.logging.Log; 59 import org.apache.commons.logging.LogFactory; 60 import org.springframework.web.context.WebApplicationContext; 61 import org.springframework.web.context.support.WebApplicationContextUtils; 62 63 68 public class Utils 69 { 70 public static final String REPOSITORY_SERVICE_NAMESPACE = "http://www.alfresco.org/ws/service/repository/1.0"; 71 72 73 private static Log logger = LogFactory.getLog(Utils.class); 74 75 private Utils() 76 { 77 } 79 80 87 public static StoreRef convertToStoreRef(Store store) 88 { 89 return new StoreRef(store.getScheme().getValue(), store.getAddress()); 90 } 91 92 99 public static Store convertToStore(StoreRef ref) 100 { 101 return new Store(StoreEnum.fromValue(ref.getProtocol()), ref 102 .getIdentifier()); 103 } 104 105 112 public static NodeRef convertToNodeRef(Reference ref, 113 NodeService nodeService, SearchService searchService, 114 NamespaceService namespaceService) 115 { 116 return resolveToNodeRef(ref.getStore(), ref.getUuid(), ref.getPath(), 117 nodeService, searchService, namespaceService); 118 } 119 120 128 public static NodeRef convertToNodeRef(ParentReference parentRef, 129 NodeService nodeService, SearchService searchService, 130 NamespaceService namespaceService) 131 { 132 135 return resolveToNodeRef(parentRef.getStore(), parentRef.getUuid(), 136 parentRef.getPath(), nodeService, searchService, 137 namespaceService); 138 } 139 140 148 public static Reference convertToReference(NodeRef node) 149 { 150 Reference ref = new Reference(); 151 Store store = new Store(StoreEnum.fromValue(node.getStoreRef() 152 .getProtocol()), node.getStoreRef().getIdentifier()); 153 ref.setStore(store); 154 ref.setUuid(node.getId()); 155 return ref; 156 } 157 158 178 public static NodeRef resolveToNodeRef(Store store, String uuid, 179 String path, NodeService nodeService, SearchService searchService, 180 NamespaceService namespaceService) 181 { 182 if (store == null) 183 { 184 throw new IllegalArgumentException ( 185 "A Store must be supplied to resolve to a NodeRef"); 186 } 187 188 NodeRef nodeRef = null; 189 190 NodeRef rootNodeRef = null; 193 if (uuid == null || uuid.length() == 0) 194 { 195 rootNodeRef = nodeService.getRootNode(convertToStoreRef(store)); 196 } 197 else 198 { 199 rootNodeRef = new NodeRef(convertToStoreRef(store), uuid); 200 } 201 202 if (path != null && path.length() != 0) 204 { 205 if (logger.isDebugEnabled() == true) 206 { 207 logger.debug("Resolving path: " + path); 208 } 209 210 List <NodeRef> nodes = searchService.selectNodes(rootNodeRef, path, 211 null, namespaceService, false); 212 213 if (nodes.size() != 1) 215 { 216 StringBuilder builder = new StringBuilder ( 217 "Failed to resolve to a single NodeRef with parameters (store="); 218 builder.append(store.getScheme().getValue()).append(":") 219 .append(store.getAddress()); 220 builder.append(" uuid=").append(uuid); 221 builder.append(" path=").append(path).append("), found "); 222 builder.append(nodes.size()).append(" nodes."); 223 throw new IllegalStateException (builder.toString()); 224 } 225 226 nodeRef = nodes.get(0); 227 } 228 else 229 { 230 if (logger.isDebugEnabled() == true) 231 { 232 logger.debug("There was no path to resolve so using root or specified node"); 233 } 234 235 nodeRef = rootNodeRef; 238 } 239 240 return nodeRef; 241 } 242 243 257 public static List <NodeRef> resolvePredicate(Predicate predicate, 258 NodeService nodeService, SearchService searchService, 259 NamespaceService namespaceService) 260 { 261 List <NodeRef> nodeRefs = null; 262 263 if (predicate.getNodes() != null) 264 { 265 Reference[] nodes = predicate.getNodes(); 266 nodeRefs = new ArrayList <NodeRef>(nodes.length); 267 268 for (int x = 0; x < nodes.length; x++) 269 { 270 nodeRefs.add(convertToNodeRef(nodes[x], nodeService, 271 searchService, namespaceService)); 272 } 273 } 274 else if (predicate.getQuery() != null) 275 { 276 Query query = predicate.getQuery(); 278 279 if (query == null) 280 { 281 throw new IllegalStateException ( 282 "Either a set of nodes or a query must be supplied in a Predicate."); 283 } 284 285 if (predicate.getStore() == null) 287 { 288 throw new IllegalStateException ( 289 "A Store has to be supplied to in order to execute a query."); 290 } 291 292 QueryLanguageEnum langEnum = query.getLanguage(); 293 294 if (langEnum.equals(QueryLanguageEnum.cql) 295 || langEnum.equals(QueryLanguageEnum.xpath)) 296 { 297 throw new IllegalArgumentException ("Only '" 298 + QueryLanguageEnum.lucene.getValue() 299 + "' queries are currently supported!"); 300 } 301 302 ResultSet searchResults = null; 304 try 305 { 306 searchResults = searchService.query(Utils 307 .convertToStoreRef(predicate.getStore()), langEnum 308 .getValue(), query.getStatement()); 309 nodeRefs = searchResults.getNodeRefs(); 311 } 312 finally 313 { 314 if (searchResults != null) 315 { 316 searchResults.close(); 317 } 318 } 319 } 320 else if (predicate.getStore() != null) 321 { 322 Store store = predicate.getStore(); 325 NodeRef rootNode = nodeService.getRootNode(Utils.convertToStoreRef(store)); 326 327 nodeRefs = new ArrayList <NodeRef>(); 328 nodeRefs.add(rootNode); 329 } 330 331 return nodeRefs; 332 } 333 334 341 public static WebApplicationContext getSpringContext( 342 MessageContext msgContext) 343 { 344 HttpServletRequest req = (HttpServletRequest ) msgContext 346 .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); 347 ServletContext servletCtx = req.getSession().getServletContext(); 348 return WebApplicationContextUtils 349 .getRequiredWebApplicationContext(servletCtx); 350 } 351 352 359 public static UserTransaction getUserTransaction(MessageContext msgContext) 360 { 361 ServiceRegistry svcReg = (ServiceRegistry) getSpringContext(msgContext) 363 .getBean(ServiceRegistry.SERVICE_REGISTRY); 364 365 TransactionService transactionService = svcReg.getTransactionService(); 366 return transactionService.getUserTransaction(); 367 } 368 369 377 public static int getBatchSize(MessageContext msgContext) 378 { 379 int batchSize = -1; 380 381 Integer batchConfigSize = (Integer ) MessageContext.getCurrentContext() 382 .getProperty(QueryConfigHandler.ALF_FETCH_SIZE); 383 if (batchConfigSize != null) 384 { 385 batchSize = batchConfigSize.intValue(); 386 } 387 388 return batchSize; 389 } 390 391 398 public static Version convertToVersion( 399 org.alfresco.service.cmr.version.Version version) 400 { 401 Version webServiceVersion = new Version(); 402 403 webServiceVersion.setId(Utils.convertToReference(version 405 .getFrozenStateNodeRef())); 406 webServiceVersion.setCreator(version.getCreator()); 407 webServiceVersion.setLabel(version.getVersionLabel()); 408 409 Date createdDate = version.getCreatedDate(); 411 Calendar calendar = Calendar.getInstance(); 412 calendar.setTime(createdDate); 413 webServiceVersion.setCreated(calendar); 414 415 boolean isMajor = false; 417 VersionType versionType = version.getVersionType(); 418 if (versionType != null 419 && versionType.equals(VersionType.MAJOR) == true) 420 { 421 isMajor = true; 422 } 423 webServiceVersion.setMajor(isMajor); 424 425 Map <String , Serializable > versionProps = version.getVersionProperties(); 427 NamedValue[] namedValues = new NamedValue[versionProps.size()]; 428 int iIndex = 0; 429 for (Map.Entry <String , Serializable > entry : versionProps.entrySet()) 430 { 431 String value = null; 432 try 433 { 434 value = DefaultTypeConverter.INSTANCE.convert(String .class, entry.getValue()); 435 } 436 catch (Throwable exception) 437 { 438 value = entry.getValue().toString(); 439 } 440 namedValues[iIndex] = new NamedValue(entry.getKey(), value); 441 iIndex++; 442 } 443 webServiceVersion.setCommentaries(namedValues); 444 445 return webServiceVersion; 446 } 447 448 455 public static ClassDefinition setupClassDefObject(org.alfresco.service.cmr.dictionary.ClassDefinition ddClassDef) 456 { 457 ClassDefinition classDef = new ClassDefinition(); 458 classDef.setName(ddClassDef.getName().toString()); 459 classDef.setIsAspect(ddClassDef.isAspect()); 460 461 if (ddClassDef.getTitle() != null) 462 { 463 classDef.setTitle(ddClassDef.getTitle()); 464 } 465 if (ddClassDef.getDescription() != null) 466 { 467 classDef.setDescription(ddClassDef.getDescription()); 468 } 469 if (ddClassDef.getParentName() != null) 470 { 471 classDef.setSuperClass(ddClassDef.getParentName().toString()); 472 } 473 474 Map <QName, org.alfresco.service.cmr.dictionary.PropertyDefinition> props = ddClassDef.getProperties(); 476 if (props != null) 477 { 478 PropertyDefinition[] propDefs = new PropertyDefinition[props.size()]; 479 int pos = 0; 480 for (org.alfresco.service.cmr.dictionary.PropertyDefinition ddPropDef : props.values()) 481 { 482 PropertyDefinition propDef = new PropertyDefinition(); 483 propDef.setName(ddPropDef.getName().toString()); 484 propDef.setDataType(ddPropDef.getDataType().getName().toString()); 485 propDef.setMandatory(ddPropDef.isMandatory()); 486 propDef.setReadOnly(ddPropDef.isProtected()); 487 if (ddPropDef.getDefaultValue() != null) 488 { 489 propDef.setDefaultValue(ddPropDef.getDefaultValue()); 490 } 491 if (ddPropDef.getTitle() != null) 492 { 493 propDef.setTitle(ddPropDef.getTitle()); 494 } 495 if (ddPropDef.getDescription() != null) 496 { 497 propDef.setDescription(ddPropDef.getDescription()); 498 } 499 500 propDefs[pos] = propDef; 502 pos++; 503 } 504 505 classDef.setProperties(propDefs); 507 } 508 509 511 Map <QName, org.alfresco.service.cmr.dictionary.AssociationDefinition> assocs = ddClassDef.getAssociations(); 513 if (assocs != null) 514 { 515 AssociationDefinition[] assocDefs = new AssociationDefinition[assocs.size()]; 516 int pos = 0; 517 for (org.alfresco.service.cmr.dictionary.AssociationDefinition ddAssocDef : assocs.values()) 518 { 519 AssociationDefinition assocDef = new AssociationDefinition(); 520 assocDef.setName(ddAssocDef.getName().toString()); 521 assocDef.setIsChild(ddAssocDef.isChild()); 522 if (ddAssocDef.getTitle() != null) 523 { 524 assocDef.setTitle(ddAssocDef.getTitle()); 525 } 526 if (ddAssocDef.getDescription() != null) 527 { 528 assocDef.setDescription(ddAssocDef.getDescription()); 529 } 530 531 RoleDefinition sourceRole = new RoleDefinition(); 532 if (ddAssocDef.getSourceRoleName() != null) 533 { 534 sourceRole.setName(ddAssocDef.getSourceRoleName().toString()); 535 } 536 sourceRole.setCardinality(setupSourceCardinalityObject(ddAssocDef)); 537 assocDef.setSourceRole(sourceRole); 538 539 RoleDefinition targetRole = new RoleDefinition(); 540 if (ddAssocDef.getTargetRoleName() != null) 541 { 542 targetRole.setName(ddAssocDef.getTargetRoleName().toString()); 543 } 544 targetRole.setCardinality(setupTargetCardinalityObject(ddAssocDef));; 545 assocDef.setTargetRole(targetRole); 546 assocDef.setTargetClass(ddAssocDef.getTargetClass().getName().toString()); 547 548 assocDefs[pos] = assocDef; 549 pos++; 550 } 551 552 classDef.setAssociations(assocDefs); 553 } 554 555 return classDef; 556 } 557 558 564 private static Cardinality setupSourceCardinalityObject(org.alfresco.service.cmr.dictionary.AssociationDefinition ddAssocDef) 565 { 566 if (ddAssocDef.isSourceMandatory() == false && ddAssocDef.isSourceMany() == false) 567 { 568 return Cardinality.value1; 570 } 571 else if (ddAssocDef.isSourceMandatory() && ddAssocDef.isSourceMany() == false) 572 { 573 return Cardinality.value2; 575 } 576 else if (ddAssocDef.isSourceMandatory() && ddAssocDef.isSourceMany()) 577 { 578 return Cardinality.value4; 580 } 581 else 582 { 583 return Cardinality.value3; 585 } 586 } 587 588 594 private static Cardinality setupTargetCardinalityObject(org.alfresco.service.cmr.dictionary.AssociationDefinition ddAssocDef) 595 { 596 if (ddAssocDef.isTargetMandatory() == false && ddAssocDef.isTargetMany() == false) 597 { 598 return Cardinality.value1; 600 } 601 else if (ddAssocDef.isTargetMandatory() && ddAssocDef.isTargetMany() == false) 602 { 603 return Cardinality.value2; 605 } 606 else if (ddAssocDef.isTargetMandatory() && ddAssocDef.isTargetMany()) 607 { 608 return Cardinality.value4; 610 } 611 else 612 { 613 return Cardinality.value3; 615 } 616 } 617 } 618 | Popular Tags |