1 17 package org.alfresco.repo.webservice.repository; 18 19 import java.io.Serializable ; 20 import java.rmi.RemoteException ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import javax.transaction.UserTransaction ; 26 27 import org.alfresco.repo.cache.SimpleCache; 28 import org.alfresco.repo.webservice.AbstractWebService; 29 import org.alfresco.repo.webservice.CMLUtil; 30 import org.alfresco.repo.webservice.Utils; 31 import org.alfresco.repo.webservice.types.CML; 32 import org.alfresco.repo.webservice.types.ClassDefinition; 33 import org.alfresco.repo.webservice.types.NamedValue; 34 import org.alfresco.repo.webservice.types.Node; 35 import org.alfresco.repo.webservice.types.NodeDefinition; 36 import org.alfresco.repo.webservice.types.Predicate; 37 import org.alfresco.repo.webservice.types.Query; 38 import org.alfresco.repo.webservice.types.QueryLanguageEnum; 39 import org.alfresco.repo.webservice.types.Reference; 40 import org.alfresco.repo.webservice.types.Store; 41 import org.alfresco.repo.webservice.types.StoreEnum; 42 import org.alfresco.service.cmr.dictionary.AspectDefinition; 43 import org.alfresco.service.cmr.dictionary.DictionaryService; 44 import org.alfresco.service.cmr.dictionary.TypeDefinition; 45 import org.alfresco.service.cmr.repository.NodeRef; 46 import org.alfresco.service.cmr.repository.StoreRef; 47 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 48 import org.alfresco.service.namespace.QName; 49 import org.apache.axis.MessageContext; 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 60 public class RepositoryWebService extends AbstractWebService implements 61 RepositoryServiceSoapPort 62 { 63 private static Log logger = LogFactory.getLog(RepositoryWebService.class); 64 65 private DictionaryService dictionaryService; 66 67 private CMLUtil cmlUtil; 68 69 private SimpleCache<String , QuerySession> querySessionCache; 70 71 77 public void setDictionaryService(DictionaryService dictionaryService) 78 { 79 this.dictionaryService = dictionaryService; 80 } 81 82 87 public void setCmlUtil(CMLUtil cmlUtil) 88 { 89 this.cmlUtil = cmlUtil; 90 } 91 92 98 public void setQuerySessionCache( 99 SimpleCache<String , QuerySession> querySessionCache) 100 { 101 this.querySessionCache = querySessionCache; 102 } 103 104 107 public Store createStore(StoreEnum scheme, String address) throws RemoteException , RepositoryFault 108 { 109 String protocol = scheme.getValue(); 110 StoreRef storeRef = this.nodeService.createStore(protocol, address); 111 112 StoreEnum storeEnum = StoreEnum.fromString(storeRef 113 .getProtocol()); 114 return new Store(storeEnum, storeRef.getIdentifier()); 115 } 116 117 120 public Store[] getStores() throws RemoteException , RepositoryFault 121 { 122 UserTransaction tx = null; 123 124 try 125 { 126 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 127 tx.begin(); 128 129 List <StoreRef> stores = this.nodeService.getStores(); 130 Store[] returnStores = new Store[stores.size()]; 131 for (int x = 0; x < stores.size(); x++) 132 { 133 StoreRef storeRef = stores.get(x); 134 135 if (logger.isDebugEnabled() == true) 136 { 137 logger.debug("Store protocol :" + storeRef.getProtocol()); 138 } 139 140 StoreEnum storeEnum = StoreEnum.fromString(storeRef 141 .getProtocol()); 142 Store store = new Store(storeEnum, storeRef.getIdentifier()); 143 returnStores[x] = store; 144 } 145 146 tx.commit(); 148 149 return returnStores; 150 } catch (Throwable e) 151 { 152 try 154 { 155 if (tx != null) 156 { 157 tx.rollback(); 158 } 159 } catch (Exception ex) 160 { 161 } 162 163 if (logger.isDebugEnabled()) 164 { 165 logger.error("Unexpected error occurred", e); 166 } 167 168 throw new RepositoryFault(0, e.getMessage()); 169 } 170 } 171 172 176 public QueryResult query(Store store, Query query, boolean includeMetaData) 177 throws RemoteException , RepositoryFault 178 { 179 QueryLanguageEnum langEnum = query.getLanguage(); 180 181 if (langEnum.equals(QueryLanguageEnum.cql) 182 || langEnum.equals(QueryLanguageEnum.xpath)) 183 { 184 throw new RepositoryFault(110, "Only '" 185 + QueryLanguageEnum.lucene.getValue() 186 + "' queries are currently supported!"); 187 } 188 189 UserTransaction tx = null; 190 MessageContext msgContext = MessageContext.getCurrentContext(); 191 192 try 193 { 194 tx = Utils.getUserTransaction(msgContext); 195 tx.begin(); 196 197 QuerySession querySession = new ResultSetQuerySession(Utils 199 .getBatchSize(msgContext), store, query, includeMetaData); 200 QueryResult queryResult = querySession 201 .getNextResultsBatch(this.searchService, this.nodeService, 202 this.namespaceService); 203 204 if (queryResult.getQuerySession() != null) 206 { 207 this.querySessionCache.put(queryResult.getQuerySession(), 209 querySession); 210 } 211 212 tx.commit(); 214 215 return queryResult; 216 } catch (Throwable e) 217 { 218 try 220 { 221 if (tx != null) 222 { 223 tx.rollback(); 224 } 225 } catch (Exception ex) 226 { 227 } 228 229 if (logger.isDebugEnabled()) 230 { 231 logger.error("Unexpected error occurred", e); 232 } 233 234 e.printStackTrace(); 235 236 throw new RepositoryFault(0, e.getMessage()); 237 } 238 } 239 240 243 public QueryResult queryChildren(Reference node) throws RemoteException , 244 RepositoryFault 245 { 246 UserTransaction tx = null; 247 248 try 249 { 250 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 251 tx.begin(); 252 253 QuerySession querySession = new ChildrenQuerySession(Utils 255 .getBatchSize(MessageContext.getCurrentContext()), node); 256 QueryResult queryResult = querySession 257 .getNextResultsBatch(this.searchService, this.nodeService, 258 this.namespaceService); 259 260 if (queryResult.getQuerySession() != null) 262 { 263 this.querySessionCache.put(queryResult.getQuerySession(), 265 querySession); 266 } 267 268 if (logger.isDebugEnabled() == true) 269 { 270 logger.debug("Method end ... queryChildren"); 271 } 272 273 tx.commit(); 275 276 return queryResult; 277 } catch (Throwable e) 278 { 279 try 281 { 282 if (tx != null) 283 { 284 tx.rollback(); 285 } 286 } catch (Exception ex) 287 { 288 } 289 290 if (logger.isDebugEnabled()) 291 { 292 logger.error("Unexpected error occurred", e); 293 } 294 295 throw new RepositoryFault(0, e.getMessage()); 296 } 297 } 298 299 302 public QueryResult queryParents(Reference node) throws RemoteException , 303 RepositoryFault 304 { 305 UserTransaction tx = null; 306 307 try 308 { 309 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 310 tx.begin(); 311 312 QuerySession querySession = new ParentsQuerySession(Utils 314 .getBatchSize(MessageContext.getCurrentContext()), node); 315 QueryResult queryResult = querySession 316 .getNextResultsBatch(this.searchService, this.nodeService, 317 this.namespaceService); 318 319 if (queryResult.getQuerySession() != null) 321 { 322 this.querySessionCache.put(queryResult.getQuerySession(), 324 querySession); 325 } 326 327 tx.commit(); 329 330 return queryResult; 331 } catch (Throwable e) 332 { 333 try 335 { 336 if (tx != null) 337 { 338 tx.rollback(); 339 } 340 } catch (Exception ex) 341 { 342 } 343 344 if (logger.isDebugEnabled()) 345 { 346 logger.error("Unexpected error occurred", e); 347 } 348 349 throw new RepositoryFault(0, e.getMessage()); 350 } 351 } 352 353 357 public QueryResult queryAssociated(Reference node, Association[] association) 358 throws RemoteException , RepositoryFault 359 { 360 UserTransaction tx = null; 361 362 try 363 { 364 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 365 tx.begin(); 366 367 QuerySession querySession = new AssociatedQuerySession(Utils.getBatchSize(MessageContext.getCurrentContext()), node); 369 QueryResult queryResult = querySession 370 .getNextResultsBatch(this.searchService, this.nodeService, 371 this.namespaceService); 372 373 if (queryResult.getQuerySession() != null) 375 { 376 this.querySessionCache.put(queryResult.getQuerySession(), 378 querySession); 379 } 380 381 tx.commit(); 383 384 return queryResult; 385 } 386 catch (Throwable e) 387 { 388 try 390 { 391 if (tx != null) 392 { 393 tx.rollback(); 394 } 395 } catch (Exception ex) 396 { 397 } 398 399 if (logger.isDebugEnabled()) 400 { 401 logger.error("Unexpected error occurred", e); 402 } 403 404 throw new RepositoryFault(0, e.getMessage()); 405 } 406 } 407 408 411 public QueryResult fetchMore(String querySession) throws RemoteException , 412 RepositoryFault 413 { 414 QueryResult queryResult = null; 415 416 UserTransaction tx = null; 417 418 try 419 { 420 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 421 tx.begin(); 422 423 QuerySession session = this.querySessionCache.get(querySession); 425 426 if (session == null) 427 { 428 if (logger.isDebugEnabled()) 429 logger.debug("Invalid querySession id requested: " 430 + querySession); 431 432 throw new RepositoryFault(4, "querySession with id '" 433 + querySession + "' is invalid"); 434 } 435 436 queryResult = session.getNextResultsBatch(this.searchService, 438 this.nodeService, this.namespaceService); 439 440 if (queryResult.getQuerySession() == null) 443 { 444 this.querySessionCache.remove(querySession); 445 } 446 447 tx.commit(); 449 450 return queryResult; 451 } catch (Throwable e) 452 { 453 try 455 { 456 if (tx != null) 457 { 458 tx.rollback(); 459 } 460 } catch (Exception ex) 461 { 462 } 463 464 if (e instanceof RepositoryFault) 465 { 466 throw (RepositoryFault) e; 467 } else 468 { 469 if (logger.isDebugEnabled()) 470 { 471 logger.error("Unexpected error occurred", e); 472 } 473 474 throw new RepositoryFault(0, e.getMessage()); 475 } 476 } 477 } 478 479 482 public UpdateResult[] update(CML statements) throws RemoteException , 483 RepositoryFault 484 { 485 UpdateResult[] result = null; 486 UserTransaction tx = null; 487 488 try 489 { 490 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 491 tx.begin(); 492 493 result = this.cmlUtil.executeCML(statements); 494 495 tx.commit(); 497 498 return result; 499 } 500 catch (Throwable e) 501 { 502 try 504 { 505 if (tx != null) 506 { 507 tx.rollback(); 508 } 509 } catch (Exception ex) 510 { 511 } 512 513 if (logger.isDebugEnabled()) 514 { 515 logger.error("Unexpected error occurred", e); 516 } 517 518 throw new RepositoryFault(0, e.getMessage()); 519 } 520 } 521 522 525 public NodeDefinition[] describe(Predicate items) throws RemoteException , 526 RepositoryFault 527 { 528 NodeDefinition[] nodeDefs = null; 529 UserTransaction tx = null; 530 531 try 532 { 533 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 534 tx.begin(); 535 536 List <NodeRef> nodes = Utils 537 .resolvePredicate(items, this.nodeService, 538 this.searchService, this.namespaceService); 539 nodeDefs = new NodeDefinition[nodes.size()]; 540 541 for (int x = 0; x < nodes.size(); x++) 542 { 543 nodeDefs[x] = setupNodeDefObject(nodes.get(x)); 544 } 545 546 tx.commit(); 548 549 return nodeDefs; 550 } catch (Throwable e) 551 { 552 try 554 { 555 if (tx != null) 556 { 557 tx.rollback(); 558 } 559 } catch (Exception ex) 560 { 561 } 562 563 if (logger.isDebugEnabled()) 564 { 565 logger.error("Unexpected error occurred", e); 566 } 567 568 throw new RepositoryFault(0, e.getMessage()); 569 } 570 } 571 572 579 private NodeDefinition setupNodeDefObject(NodeRef nodeRef) 580 { 581 if (logger.isDebugEnabled()) 582 logger.debug("Building NodeDefinition for node: " + nodeRef); 583 584 TypeDefinition ddTypeDef = this.dictionaryService 585 .getType(this.nodeService.getType(nodeRef)); 586 587 ClassDefinition typeDef = Utils.setupClassDefObject(ddTypeDef); 589 590 ClassDefinition[] aspectDefs = null; 592 List <AspectDefinition> aspects = ddTypeDef.getDefaultAspects(); 593 if (aspects != null) 594 { 595 aspectDefs = new ClassDefinition[aspects.size()]; 596 int pos = 0; 597 for (AspectDefinition ddAspectDef : aspects) 598 { 599 aspectDefs[pos] = Utils.setupClassDefObject(ddAspectDef); 600 pos++; 601 } 602 } 603 604 return new NodeDefinition(typeDef, aspectDefs); 605 } 606 607 613 public Node[] get(Predicate where) throws RemoteException , RepositoryFault 614 { 615 Node[] nodes = null; 616 UserTransaction tx = null; 617 618 try 619 { 620 tx = Utils.getUserTransaction(MessageContext.getCurrentContext()); 621 tx.begin(); 622 623 List <NodeRef> nodeRefs = Utils.resolvePredicate(where, this.nodeService, this.searchService, this.namespaceService); 625 nodes = new Node[nodeRefs.size()]; 626 int index = 0; 627 for (NodeRef nodeRef : nodeRefs) 628 { 629 Reference reference = Utils.convertToReference(nodeRef); 631 632 String type = this.nodeService.getType(nodeRef).toString(); 634 635 Set <QName> aspectQNames = this.nodeService.getAspects(nodeRef); 637 String [] aspects = new String [aspectQNames.size()]; 638 int aspectIndex = 0; 639 for (QName aspectQName : aspectQNames) 640 { 641 aspects[aspectIndex] = aspectQName.toString(); 642 aspectIndex++; 643 } 644 645 Map <QName, Serializable > propertyMap = this.nodeService.getProperties(nodeRef); 647 NamedValue[] properties = new NamedValue[propertyMap.size()]; 648 int propertyIndex = 0; 649 for (Map.Entry <QName, Serializable > entry : propertyMap.entrySet()) 650 { 651 String value = null; 652 try 653 { 654 value = DefaultTypeConverter.INSTANCE.convert(String .class, entry.getValue()); 655 } 656 catch (Throwable exception) 657 { 658 value = entry.getValue().toString(); 659 } 660 properties[propertyIndex] = new NamedValue(entry.getKey().toString(), value); 661 propertyIndex++; 662 } 663 664 Node node = new Node(reference, type, aspects, properties); 666 nodes[index] = node; 667 668 index++; 669 } 670 671 tx.commit(); 673 } 674 catch (Throwable e) 675 { 676 try 678 { 679 if (tx != null) 680 { 681 tx.rollback(); 682 } 683 } 684 catch (Exception ex) 685 { 686 } 688 689 if (logger.isDebugEnabled()) 690 { 691 logger.error("Unexpected error occurred", e); 692 } 693 694 throw new RepositoryFault(0, e.getMessage()); 695 } 696 697 return nodes; 698 } 699 } 700 | Popular Tags |