1 17 package org.alfresco.repo.webservice; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.alfresco.repo.webservice.repository.UpdateResult; 26 import org.alfresco.repo.webservice.types.CML; 27 import org.alfresco.repo.webservice.types.CMLAddAspect; 28 import org.alfresco.repo.webservice.types.CMLAddChild; 29 import org.alfresco.repo.webservice.types.CMLCopy; 30 import org.alfresco.repo.webservice.types.CMLCreate; 31 import org.alfresco.repo.webservice.types.CMLCreateAssociation; 32 import org.alfresco.repo.webservice.types.CMLDelete; 33 import org.alfresco.repo.webservice.types.CMLMove; 34 import org.alfresco.repo.webservice.types.CMLRemoveAspect; 35 import org.alfresco.repo.webservice.types.CMLRemoveAssociation; 36 import org.alfresco.repo.webservice.types.CMLRemoveChild; 37 import org.alfresco.repo.webservice.types.CMLUpdate; 38 import org.alfresco.repo.webservice.types.NamedValue; 39 import org.alfresco.repo.webservice.types.ParentReference; 40 import org.alfresco.repo.webservice.types.Predicate; 41 import org.alfresco.repo.webservice.types.Reference; 42 import org.alfresco.service.cmr.repository.CopyService; 43 import org.alfresco.service.cmr.repository.NodeRef; 44 import org.alfresco.service.cmr.repository.NodeService; 45 import org.alfresco.service.cmr.search.SearchService; 46 import org.alfresco.service.namespace.NamespaceService; 47 import org.alfresco.service.namespace.QName; 48 import org.alfresco.util.PropertyMap; 49 50 53 public class CMLUtil 54 { 55 private static final String CREATE = "create"; 56 private static final String ADD_ASPECT = "addAspect"; 57 private static final String REMOVE_ASPECT = "removeAspect"; 58 private static final String UPDATE = "update"; 59 private static final String DELETE = "delete"; 60 private static final String MOVE = "move"; 61 private static final String COPY = "copy"; 62 private static final String ADD_CHILD = "addChild"; 63 private static final String REMOVE_CHILD = "removeChild"; 64 private static final String CREATE_ASSOCIATION = "createAssociation"; 65 private static final String REMOVE_ASSOCIATION = "removeAssociation"; 66 67 private NodeService nodeService; 68 private SearchService searchService; 69 private NamespaceService namespaceService; 70 private CopyService copyService; 71 72 public void setNodeService(NodeService nodeService) 73 { 74 this.nodeService = nodeService; 75 } 76 77 public void setSearchService(SearchService searchService) 78 { 79 this.searchService = searchService; 80 } 81 82 public void setNamespaceService(NamespaceService namespaceService) 83 { 84 this.namespaceService = namespaceService; 85 } 86 87 public void setCopyService(CopyService copyService) 88 { 89 this.copyService = copyService; 90 } 91 92 98 public UpdateResult[] executeCML(CML cml) 99 { 100 ExecutionContext context = new ExecutionContext(); 101 List <UpdateResult> results = new ArrayList <UpdateResult>(); 102 103 CMLCreate[] creates = cml.getCreate(); 105 if (creates != null) 106 { 107 for (CMLCreate create : creates) 108 { 109 executeCMLCreate(create, context, results); 110 } 111 } 112 113 CMLAddAspect[] addAspects = cml.getAddAspect(); 115 if (addAspects != null) 116 { 117 for (CMLAddAspect addAspect : addAspects) 118 { 119 executeCMLAddAspect(addAspect, context, results); 120 } 121 } 122 123 CMLRemoveAspect[] removeAspects = cml.getRemoveAspect(); 125 if (removeAspects != null) 126 { 127 for (CMLRemoveAspect removeAspect : removeAspects) 128 { 129 executeCMLRemoveAspect(removeAspect, context, results); 130 } 131 } 132 133 CMLUpdate[] updates = cml.getUpdate(); 135 if (updates != null) 136 { 137 for (CMLUpdate update : updates) 138 { 139 executeCMLUpdate(update, context, results); 140 } 141 } 142 143 CMLDelete[] deletes = cml.getDelete(); 145 if (deletes != null) 146 { 147 for (CMLDelete delete : deletes) 148 { 149 executeCMLDelete(delete, context, results); 150 } 151 } 152 153 CMLMove[] moves = cml.getMove(); 155 if (moves != null) 156 { 157 for (CMLMove move : moves) 158 { 159 executeCMLMove(move, context, results); 160 } 161 } 162 163 CMLCopy[] copies = cml.getCopy(); 165 if (copies != null) 166 { 167 for (CMLCopy copy : copies) 168 { 169 executeCMLCopy(copy, context, results); 170 } 171 } 172 173 CMLAddChild[] addChildren = cml.getAddChild(); 175 if (addChildren != null) 176 { 177 for (CMLAddChild addChild : addChildren) 178 { 179 executeCMLAddChild(addChild, context, results); 180 } 181 } 182 183 CMLRemoveChild[] removeChildren = cml.getRemoveChild(); 185 if (removeChildren != null) 186 { 187 for (CMLRemoveChild removeChild : removeChildren) 188 { 189 executeCMLRemoveChild(removeChild, context, results); 190 } 191 } 192 193 CMLCreateAssociation[] createAssocs = cml.getCreateAssociation(); 195 if (createAssocs != null) 196 { 197 for (CMLCreateAssociation createAssoc : createAssocs) 198 { 199 executeCMLCreateAssociation(createAssoc, context, results); 200 } 201 } 202 203 CMLRemoveAssociation[] removeAssocs = cml.getRemoveAssociation(); 205 if (removeAssocs != null) 206 { 207 for (CMLRemoveAssociation removeAssoc : removeAssocs) 208 { 209 executeCMLRemoveAssociation(removeAssoc, context, results); 210 } 211 } 212 213 return results.toArray(new UpdateResult[results.size()]); 214 } 215 216 221 private void executeCMLCreate(CMLCreate create, ExecutionContext context, List <UpdateResult> results) 222 { 223 ParentReference parentReference = create.getParent(); 225 NodeRef parentNodeRef = Utils.convertToNodeRef( 226 parentReference, 227 this.nodeService, 228 this.searchService, 229 this.namespaceService); 230 QName assocTypeQName = QName.createQName(parentReference.getAssociationType()); 231 QName assocQName = QName.createQName(parentReference.getChildName()); 232 233 QName nodeTypeQName = QName.createQName(create.getType()); 235 236 PropertyMap properties = getPropertyMap(create.getProperty()); 238 239 NodeRef nodeRef = this.nodeService.createNode(parentNodeRef, assocTypeQName, assocQName, nodeTypeQName, properties).getChildRef(); 241 242 String id = create.getId(); 244 if (id != null && id.length() != 0) 245 { 246 context.addId(id, nodeRef); 247 } 248 249 results.add(createResult(CREATE, null, nodeRef)); 250 } 251 252 private PropertyMap getPropertyMap(NamedValue[] namedValues) 253 { 254 PropertyMap properties = new PropertyMap(); 255 if (namedValues != null) 256 { 257 for (NamedValue value : namedValues) 258 { 259 QName qname = QName.createQName(value.getName()); 260 properties.put(qname, value.getValue()); 261 } 262 } 263 return properties; 264 } 265 266 private UpdateResult createResult(String cmd, NodeRef sourceNodeRef, NodeRef destinationNodeRef) 267 { 268 UpdateResult result = new UpdateResult(); 269 result.setStatement(cmd); 270 if (sourceNodeRef != null) 271 { 272 result.setSource(Utils.convertToReference(sourceNodeRef)); 273 } 274 if (destinationNodeRef != null) 275 { 276 result.setDestination(Utils.convertToReference(destinationNodeRef)); 277 } 278 return result; 280 } 281 282 287 private void executeCMLAddAspect(CMLAddAspect addAspect, ExecutionContext context, List <UpdateResult> results) 288 { 289 List <NodeRef> nodeRefs = getNodeRefList(addAspect.getWhere_id(), addAspect.getWhere(), context); 291 292 QName aspectQName = QName.createQName(addAspect.getAspect()); 294 PropertyMap properties = getPropertyMap(addAspect.getProperty()); 295 296 for (NodeRef nodeRef : nodeRefs) 297 { 298 this.nodeService.addAspect(nodeRef, aspectQName, properties); 300 301 results.add(createResult(ADD_ASPECT, nodeRef, nodeRef)); 303 } 304 } 305 306 private void executeCMLRemoveAspect(CMLRemoveAspect removeAspect, ExecutionContext context, List <UpdateResult> results) 307 { 308 List <NodeRef> nodeRefs = getNodeRefList(removeAspect.getWhere_id(), removeAspect.getWhere(), context); 310 311 QName aspectQName = QName.createQName(removeAspect.getAspect()); 313 314 for (NodeRef nodeRef : nodeRefs) 315 { 316 this.nodeService.removeAspect(nodeRef, aspectQName); 318 319 results.add(createResult(REMOVE_ASPECT, nodeRef, nodeRef)); 321 } 322 } 323 324 private List <NodeRef> getNodeRefList(String id, Predicate predicate, ExecutionContext context) 325 { 326 List <NodeRef> nodeRefs = new ArrayList <NodeRef>(); 327 if (id != null && id.length() != 0) 328 { 329 NodeRef localNodeRef = context.getNodeRef(id); 330 if (localNodeRef != null) 331 { 332 nodeRefs.add(localNodeRef); 333 } 334 } 335 else 336 { 337 nodeRefs = Utils.resolvePredicate(predicate, this.nodeService, this.searchService, this.namespaceService); 338 } 339 return nodeRefs; 340 } 341 342 private void executeCMLUpdate(CMLUpdate update, ExecutionContext context, List <UpdateResult> results) 343 { 344 List <NodeRef> nodeRefs = getNodeRefList(update.getWhere_id(), update.getWhere(), context); 346 PropertyMap props = getPropertyMap(update.getProperty()); 347 348 for (NodeRef nodeRef : nodeRefs) 349 { 350 Map <QName, Serializable > currentProps = this.nodeService.getProperties(nodeRef); 352 currentProps.putAll(props); 353 this.nodeService.setProperties(nodeRef, currentProps); 354 355 results.add(createResult(UPDATE, nodeRef, nodeRef)); 357 } 358 } 359 360 private void executeCMLDelete(CMLDelete delete, ExecutionContext context, List <UpdateResult> results) 361 { 362 List <NodeRef> nodeRefs = Utils.resolvePredicate(delete.getWhere(), this.nodeService, this.searchService, this.namespaceService); 363 for (NodeRef nodeRef : nodeRefs) 364 { 365 this.nodeService.deleteNode(nodeRef); 367 368 results.add(createResult(DELETE, nodeRef, null)); 370 } 371 } 372 373 private void executeCMLMove(CMLMove move, ExecutionContext context, List <UpdateResult> results) 374 { 375 NodeRef destinationNodeRef = getNodeRef(move.getTo_id(), move.getTo(), context); 376 if (destinationNodeRef != null) 377 { 378 QName assocType = null; 379 QName assocName = null; 380 if (move.getTo_id() != null) 381 { 382 assocType = QName.createQName(move.getAssociationType()); 383 assocName = QName.createQName(move.getChildName()); 384 } 385 else 386 { 387 assocType = QName.createQName(move.getTo().getAssociationType()); 388 assocName = QName.createQName(move.getTo().getChildName()); 389 } 390 391 List <NodeRef> nodesToMove = getNodeRefList(move.getWhere_id(), move.getWhere(), context); 392 for (NodeRef nodeToMove : nodesToMove) 393 { 394 NodeRef newNodeRef = this.nodeService.moveNode(nodeToMove, destinationNodeRef, assocType, assocName).getChildRef(); 395 396 results.add(createResult(MOVE, nodeToMove, newNodeRef)); 398 } 399 } 400 } 401 402 private NodeRef getNodeRef(String id, ParentReference parentReference, ExecutionContext context) 403 { 404 NodeRef nodeRef = null; 405 if (id != null && id.length() != 0) 406 { 407 nodeRef = context.getNodeRef(id); 408 } 409 else 410 { 411 nodeRef = Utils.convertToNodeRef(parentReference, this.nodeService, this.searchService, this.namespaceService); 412 } 413 414 return nodeRef; 415 } 416 417 private NodeRef getNodeRef(String id, Reference reference, ExecutionContext context) 418 { 419 NodeRef nodeRef = null; 420 if (id != null && id.length() != 0) 421 { 422 nodeRef = context.getNodeRef(id); 423 } 424 else 425 { 426 nodeRef = Utils.convertToNodeRef(reference, this.nodeService, this.searchService, this.namespaceService); 427 } 428 429 return nodeRef; 430 } 431 432 private void executeCMLCopy(CMLCopy copy, ExecutionContext context, List <UpdateResult> results) 433 { 434 NodeRef destinationNodeRef = getNodeRef(copy.getTo_id(), copy.getTo(), context); 435 if (destinationNodeRef != null) 436 { 437 QName assocType = null; 438 QName assocName = null; 439 if (copy.getTo_id() != null) 440 { 441 assocType = QName.createQName(copy.getAssociationType()); 442 assocName = QName.createQName(copy.getChildName()); 443 } 444 else 445 { 446 assocType = QName.createQName(copy.getTo().getAssociationType()); 447 assocName = QName.createQName(copy.getTo().getChildName()); 448 } 449 450 boolean copyChildren = false; 451 Boolean value = copy.getChildren(); 452 if (value != null) 453 { 454 copyChildren = value.booleanValue(); 455 } 456 457 List <NodeRef> nodesToCopy = getNodeRefList(copy.getWhere_id(), copy.getWhere(), context); 458 for (NodeRef nodeToCopy : nodesToCopy) 459 { 460 NodeRef newNodeRef = this.copyService.copy(nodeToCopy, destinationNodeRef, assocType, assocName, copyChildren); 461 462 results.add(createResult(COPY, nodeToCopy, newNodeRef)); 464 } 465 } 466 467 } 468 469 private void executeCMLAddChild(CMLAddChild addChild, ExecutionContext context, List <UpdateResult> results) 470 { 471 NodeRef nodeRef = getNodeRef(addChild.getTo_id(), addChild.getTo(), context); 472 if (nodeRef != null) 473 { 474 QName assocType = null; 475 QName assocName = null; 476 if (addChild.getTo_id() != null) 477 { 478 assocType = QName.createQName(addChild.getAssociationType()); 479 assocName = QName.createQName(addChild.getChildName()); 480 } 481 else 482 { 483 assocType = QName.createQName(addChild.getTo().getAssociationType()); 484 assocName = QName.createQName(addChild.getTo().getChildName()); 485 } 486 487 List <NodeRef> whereNodeRefs = getNodeRefList(addChild.getWhere_id(), addChild.getWhere(), context); 488 for (NodeRef whereNodeRef : whereNodeRefs) 489 { 490 this.nodeService.addChild(nodeRef, whereNodeRef, assocType, assocName); 491 492 results.add(createResult(ADD_CHILD, nodeRef, whereNodeRef)); 494 } 495 } 496 } 497 498 private void executeCMLRemoveChild(CMLRemoveChild removeChild, ExecutionContext context, List <UpdateResult> results) 499 { 500 NodeRef parentNodeRef = getNodeRef(removeChild.getFrom_id(), removeChild.getFrom(), context); 501 if (parentNodeRef != null) 502 { 503 List <NodeRef> childNodeRefs = getNodeRefList(removeChild.getWhere_id(), removeChild.getWhere(), context); 504 for (NodeRef childNodeRef : childNodeRefs) 505 { 506 this.nodeService.removeChild(parentNodeRef, childNodeRef); 507 508 results.add(createResult(REMOVE_CHILD, parentNodeRef, null)); 510 } 511 } 512 513 } 514 515 private void executeCMLCreateAssociation(CMLCreateAssociation createAssoc, ExecutionContext context, List <UpdateResult> results) 516 { 517 QName assocType = QName.createQName(createAssoc.getAssociation()); 518 if (assocType != null) 519 { 520 List <NodeRef> fromNodeRefs = getNodeRefList(createAssoc.getFrom_id(), createAssoc.getFrom(), context); 521 List <NodeRef> toNodeRefs = getNodeRefList(createAssoc.getTo_id(), createAssoc.getTo(), context); 522 for (NodeRef fromNodeRef : fromNodeRefs) 523 { 524 for (NodeRef toNodeRef : toNodeRefs) 525 { 526 this.nodeService.createAssociation(fromNodeRef, toNodeRef, assocType); 527 528 results.add(createResult(CREATE_ASSOCIATION, fromNodeRef, toNodeRef)); 530 } 531 } 532 } 533 } 534 535 private void executeCMLRemoveAssociation(CMLRemoveAssociation removeAssoc, ExecutionContext context, List <UpdateResult> results) 536 { 537 QName assocType = QName.createQName(removeAssoc.getAssociation()); 538 if (assocType != null) 539 { 540 List <NodeRef> fromNodeRefs = getNodeRefList(removeAssoc.getFrom_id(), removeAssoc.getFrom(), context); 541 List <NodeRef> toNodeRefs = getNodeRefList(removeAssoc.getTo_id(), removeAssoc.getTo(), context); 542 for (NodeRef fromNodeRef : fromNodeRefs) 543 { 544 for (NodeRef toNodeRef : toNodeRefs) 545 { 546 this.nodeService.removeAssociation(fromNodeRef, toNodeRef, assocType); 547 548 results.add(createResult(REMOVE_ASSOCIATION, fromNodeRef, toNodeRef)); 550 } 551 } 552 } 553 } 554 555 private class ExecutionContext 556 { 557 private Map <String , NodeRef> idMap = new HashMap <String , NodeRef>(); 558 559 public void addId(String id, NodeRef nodeRef) 560 { 561 this.idMap.put(id, nodeRef); 562 } 563 564 public NodeRef getNodeRef(String id) 565 { 566 return this.idMap.get(id); 567 } 568 } 569 } 570 | Popular Tags |