1 17 package org.alfresco.repo.node.db.hibernate; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.List ; 22 23 import org.alfresco.model.ContentModel; 24 import org.alfresco.repo.domain.ChildAssoc; 25 import org.alfresco.repo.domain.Node; 26 import org.alfresco.repo.domain.NodeAssoc; 27 import org.alfresco.repo.domain.NodeKey; 28 import org.alfresco.repo.domain.NodeStatus; 29 import org.alfresco.repo.domain.Store; 30 import org.alfresco.repo.domain.StoreKey; 31 import org.alfresco.repo.domain.hibernate.ChildAssocImpl; 32 import org.alfresco.repo.domain.hibernate.NodeAssocImpl; 33 import org.alfresco.repo.domain.hibernate.NodeImpl; 34 import org.alfresco.repo.domain.hibernate.NodeStatusImpl; 35 import org.alfresco.repo.domain.hibernate.StoreImpl; 36 import org.alfresco.repo.node.db.NodeDaoService; 37 import org.alfresco.repo.transaction.AlfrescoTransactionSupport; 38 import org.alfresco.service.cmr.dictionary.InvalidTypeException; 39 import org.alfresco.service.cmr.repository.ChildAssociationRef; 40 import org.alfresco.service.namespace.QName; 41 import org.alfresco.util.GUID; 42 import org.hibernate.ObjectDeletedException; 43 import org.hibernate.Query; 44 import org.hibernate.Session; 45 import org.springframework.dao.DataAccessException; 46 import org.springframework.dao.DataIntegrityViolationException; 47 import org.springframework.orm.hibernate3.HibernateCallback; 48 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 49 50 55 public class HibernateNodeDaoServiceImpl extends HibernateDaoSupport implements NodeDaoService 56 { 57 private static final String QUERY_GET_ALL_STORES = "store.GetAllStores"; 58 private static final String QUERY_GET_NODE_ASSOC = "node.GetNodeAssoc"; 59 private static final String QUERY_GET_NODE_ASSOC_TARGETS = "node.GetNodeAssocTargets"; 60 private static final String QUERY_GET_NODE_ASSOC_SOURCES = "node.GetNodeAssocSources"; 61 private static final String QUERY_GET_CONTENT_DATA_STRINGS = "node.GetContentDataStrings"; 62 63 64 private String uuid; 65 66 69 public HibernateNodeDaoServiceImpl() 70 { 71 this.uuid = GUID.generate(); 72 } 73 74 77 public boolean equals(Object obj) 78 { 79 if (obj == null) 80 { 81 return false; 82 } 83 else if (!(obj instanceof HibernateNodeDaoServiceImpl)) 84 { 85 return false; 86 } 87 HibernateNodeDaoServiceImpl that = (HibernateNodeDaoServiceImpl) obj; 88 return this.uuid.equals(that.uuid); 89 } 90 91 94 public int hashCode() 95 { 96 return uuid.hashCode(); 97 } 98 99 105 public boolean isDirty() 106 { 107 HibernateCallback callback = new HibernateCallback() 109 { 110 public Object doInHibernate(Session session) 111 { 112 return session.isDirty(); 113 } 114 }; 115 return ((Boolean )getHibernateTemplate().execute(callback)).booleanValue(); 117 } 118 119 122 @SuppressWarnings ("unchecked") 123 public List <Store> getStores() 124 { 125 HibernateCallback callback = new HibernateCallback() 126 { 127 public Object doInHibernate(Session session) 128 { 129 Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_ALL_STORES); 130 return query.list(); 131 } 132 }; 133 List <Store> queryResults = (List ) getHibernateTemplate().execute(callback); 134 return queryResults; 136 } 137 138 141 public Store createStore(String protocol, String identifier) 142 { 143 Store store = getStore(protocol, identifier); 145 if (store != null) 146 { 147 throw new RuntimeException ("A store already exists: \n" + 148 " protocol: " + protocol + "\n" + 149 " identifier: " + identifier + "\n" + 150 " store: " + store); 151 } 152 153 store = new StoreImpl(); 154 store.setKey(new StoreKey(protocol, identifier)); 156 getHibernateTemplate().save(store); 158 Node rootNode = newNode( 160 store, 161 GUID.generate(), 162 ContentModel.TYPE_STOREROOT); 163 store.setRootNode(rootNode); 164 return store; 166 } 167 168 public Store getStore(String protocol, String identifier) 169 { 170 StoreKey storeKey = new StoreKey(protocol, identifier); 171 Store store = (Store) getHibernateTemplate().get(StoreImpl.class, storeKey); 172 return store; 174 } 175 176 public Node newNode(Store store, String id, QName nodeTypeQName) throws InvalidTypeException 177 { 178 NodeKey key = new NodeKey(store.getKey(), id); 179 180 NodeStatus nodeStatus = (NodeStatus) getHibernateTemplate().get(NodeStatusImpl.class, key); 182 if (nodeStatus == null) 183 { 184 nodeStatus = new NodeStatusImpl(); 185 } 186 nodeStatus.setKey(key); 188 nodeStatus.setDeleted(false); 189 nodeStatus.setChangeTxnId(AlfrescoTransactionSupport.getTransactionId()); 190 getHibernateTemplate().save(nodeStatus); 192 193 Node node = new NodeImpl(); 195 node.setKey(key); 197 node.setTypeQName(nodeTypeQName); 198 node.setStore(store); 199 node.setStatus(nodeStatus); 200 getHibernateTemplate().save(node); 202 return node; 204 } 205 206 public Node getNode(String protocol, String identifier, String id) 207 { 208 try 209 { 210 NodeKey nodeKey = new NodeKey(protocol, identifier, id); 211 Object obj = getHibernateTemplate().get(NodeImpl.class, nodeKey); 212 return (Node) obj; 214 } 215 catch (ObjectDeletedException e) 216 { 217 return null; 218 } 219 catch (DataAccessException e) 220 { 221 if (e.contains(ObjectDeletedException.class)) 222 { 223 return null; 225 } 226 throw e; 227 } 228 } 229 230 233 public void deleteNode(Node node, boolean cascade) 234 { 235 Collection <ChildAssoc> parentAssocs = node.getParentAssocs(); 237 parentAssocs = new ArrayList <ChildAssoc>(parentAssocs); 238 for (ChildAssoc assoc : parentAssocs) 239 { 240 deleteChildAssoc(assoc, false); } 242 Collection <ChildAssoc> childAssocs = node.getChildAssocs(); 244 childAssocs = new ArrayList <ChildAssoc>(childAssocs); 245 for (ChildAssoc assoc : childAssocs) 246 { 247 deleteChildAssoc(assoc, cascade); } 249 Collection <NodeAssoc> targetAssocs = node.getTargetNodeAssocs(); 251 targetAssocs = new ArrayList <NodeAssoc>(targetAssocs); 252 for (NodeAssoc assoc : targetAssocs) 253 { 254 deleteNodeAssoc(assoc); 255 } 256 Collection <NodeAssoc> sourceAssocs = node.getSourceNodeAssocs(); 258 sourceAssocs = new ArrayList <NodeAssoc>(sourceAssocs); 259 for (NodeAssoc assoc : sourceAssocs) 260 { 261 deleteNodeAssoc(assoc); 262 } 263 NodeStatus nodeStatus = node.getStatus(); 265 nodeStatus.setDeleted(true); 266 nodeStatus.setChangeTxnId(AlfrescoTransactionSupport.getTransactionId()); 267 getHibernateTemplate().delete(node); 269 } 271 272 275 public NodeStatus getNodeStatus(String protocol, String identifier, String id) 276 { 277 try 278 { 279 NodeKey nodeKey = new NodeKey(protocol, identifier, id); 280 Object obj = getHibernateTemplate().get(NodeStatusImpl.class, nodeKey); 281 return (NodeStatus) obj; 283 } 284 catch (DataAccessException e) 285 { 286 if (e.contains(ObjectDeletedException.class)) 287 { 288 return null; 290 } 291 throw e; 292 } 293 } 294 295 public ChildAssoc newChildAssoc( 296 Node parentNode, 297 Node childNode, 298 boolean isPrimary, 299 QName assocTypeQName, 300 QName qname) 301 { 302 ChildAssoc assoc = new ChildAssocImpl(); 303 assoc.setTypeQName(assocTypeQName); 304 assoc.setIsPrimary(isPrimary); 305 assoc.setQname(qname); 306 assoc.buildAssociation(parentNode, childNode); 307 getHibernateTemplate().save(assoc); 309 return assoc; 311 } 312 313 public ChildAssoc getChildAssoc( 314 Node parentNode, 315 Node childNode, 316 QName assocTypeQName, 317 QName qname) 318 { 319 ChildAssociationRef childAssocRef = new ChildAssociationRef( 320 assocTypeQName, 321 parentNode.getNodeRef(), 322 qname, 323 childNode.getNodeRef()); 324 Collection <ChildAssoc> assocs = parentNode.getChildAssocs(); 326 for (ChildAssoc assoc : assocs) 328 { 329 if (!assoc.getChildAssocRef().equals(childAssocRef)) { 332 continue; 333 } 334 else 335 { 336 return assoc; 337 } 338 } 339 return null; 341 } 342 343 346 public void deleteChildAssoc(ChildAssoc assoc, boolean cascade) 347 { 348 Node childNode = assoc.getChild(); 349 350 assoc.removeAssociation(); 352 getHibernateTemplate().delete(assoc); 354 355 if (cascade && assoc.getIsPrimary()) { 357 deleteNode(childNode, cascade); 359 364 } 365 } 366 367 public ChildAssoc getPrimaryParentAssoc(Node node) 368 { 369 Collection <ChildAssoc> parentAssocs = node.getParentAssocs(); 371 ChildAssoc primaryAssoc = null; 372 for (ChildAssoc assoc : parentAssocs) 373 { 374 if (!assoc.getIsPrimary()) 376 { 377 continue; 378 } 379 else if (primaryAssoc != null) 380 { 381 throw new DataIntegrityViolationException( 383 "Multiple primary associations: \n" + 384 " child: " + node + "\n" + 385 " first primary assoc: " + primaryAssoc + "\n" + 386 " second primary assoc: " + assoc); 387 } 388 primaryAssoc = assoc; 389 } 391 if (primaryAssoc == null) 393 { 394 Store store = node.getStore(); 396 Node rootNode = store.getRootNode(); 397 if (rootNode == null) 398 { 399 throw new DataIntegrityViolationException("Store has no root node: \n" + 401 " store: " + store); 402 } 403 if (!rootNode.equals(node)) 404 { 405 throw new DataIntegrityViolationException("Non-root node has no primary parent: \n" + 407 " child: " + node); 408 } 409 } 410 return primaryAssoc; 412 } 413 414 public NodeAssoc newNodeAssoc(Node sourceNode, Node targetNode, QName assocTypeQName) 415 { 416 NodeAssoc assoc = new NodeAssocImpl(); 417 assoc.setTypeQName(assocTypeQName); 418 assoc.buildAssociation(sourceNode, targetNode); 419 getHibernateTemplate().save(assoc); 421 return assoc; 423 } 424 425 public NodeAssoc getNodeAssoc( 426 final Node sourceNode, 427 final Node targetNode, 428 final QName assocTypeQName) 429 { 430 final NodeKey sourceKey = sourceNode.getKey(); 431 final NodeKey targetKey = targetNode.getKey(); 432 HibernateCallback callback = new HibernateCallback() 433 { 434 public Object doInHibernate(Session session) 435 { 436 Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_NODE_ASSOC); 437 query.setString("sourceKeyProtocol", sourceKey.getProtocol()) 438 .setString("sourceKeyIdentifier", sourceKey.getIdentifier()) 439 .setString("sourceKeyGuid", sourceKey.getGuid()) 440 .setString("assocTypeQName", assocTypeQName.toString()) 441 .setString("targetKeyProtocol", targetKey.getProtocol()) 442 .setString("targetKeyIdentifier", targetKey.getIdentifier()) 443 .setString("targetKeyGuid", targetKey.getGuid()); 444 query.setMaxResults(1); 445 return query.uniqueResult(); 446 } 447 }; 448 Object queryResult = getHibernateTemplate().execute(callback); 449 if (queryResult == null) 450 { 451 return null; 452 } 453 NodeAssoc assoc = (NodeAssoc) queryResult; 454 return assoc; 456 } 457 458 @SuppressWarnings ("unchecked") 459 public Collection <Node> getNodeAssocTargets(final Node sourceNode, final QName assocTypeQName) 460 { 461 final NodeKey sourceKey = sourceNode.getKey(); 462 HibernateCallback callback = new HibernateCallback() 463 { 464 public Object doInHibernate(Session session) 465 { 466 Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_NODE_ASSOC_TARGETS); 467 query.setString("sourceKeyProtocol", sourceKey.getProtocol()) 468 .setString("sourceKeyIdentifier", sourceKey.getIdentifier()) 469 .setString("sourceKeyGuid", sourceKey.getGuid()) 470 .setString("assocTypeQName", assocTypeQName.toString()); 471 return query.list(); 472 } 473 }; 474 List <Node> queryResults = (List ) getHibernateTemplate().execute(callback); 475 return queryResults; 477 } 478 479 @SuppressWarnings ("unchecked") 480 public Collection <Node> getNodeAssocSources(final Node targetNode, final QName assocTypeQName) 481 { 482 final NodeKey targetKey = targetNode.getKey(); 483 HibernateCallback callback = new HibernateCallback() 484 { 485 public Object doInHibernate(Session session) 486 { 487 Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_NODE_ASSOC_SOURCES); 488 query.setString("targetKeyProtocol", targetKey.getProtocol()) 489 .setString("targetKeyIdentifier", targetKey.getIdentifier()) 490 .setString("targetKeyGuid", targetKey.getGuid()) 491 .setString("assocTypeQName", assocTypeQName.toString()); 492 return query.list(); 493 } 494 }; 495 List <Node> queryResults = (List ) getHibernateTemplate().execute(callback); 496 return queryResults; 498 } 499 500 public void deleteNodeAssoc(NodeAssoc assoc) 501 { 502 assoc.removeAssociation(); 504 getHibernateTemplate().delete(assoc); 506 } 507 508 @SuppressWarnings ("unchecked") 509 public List <String > getContentDataStrings() 510 { 511 HibernateCallback callback = new HibernateCallback() 512 { 513 public Object doInHibernate(Session session) 514 { 515 Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_CONTENT_DATA_STRINGS); 516 return query.list(); 517 } 518 }; 519 List <String > queryResults = (List ) getHibernateTemplate().execute(callback); 520 return queryResults; 521 } 522 } 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | Popular Tags |