Moving to root below branch label

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2005-12-08 07:13:07 +00:00
commit e1e6508fec
1095 changed files with 230566 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,260 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.node.db;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.transaction.UserTransaction;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.domain.NodeStatus;
import org.alfresco.repo.node.BaseNodeServiceTest;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.TransactionUtil;
import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
/**
* @see org.alfresco.repo.node.db.DbNodeServiceImpl
*
* @author Derek Hulley
*/
public class DbNodeServiceImplTest extends BaseNodeServiceTest
{
private TransactionService txnService;
private NodeDaoService nodeDaoService;
protected NodeService getNodeService()
{
return (NodeService) applicationContext.getBean("NodeService");
}
@Override
protected void onSetUpInTransaction() throws Exception
{
super.onSetUpInTransaction();
txnService = (TransactionService) applicationContext.getBean("transactionComponent");
nodeDaoService = (NodeDaoService) applicationContext.getBean("nodeDaoService");
}
/**
* Deletes a child node and then iterates over the children of the parent node,
* getting the QName. This caused some issues after we did some optimization
* using lazy loading of the associations.
*/
public void testLazyLoadIssue() throws Exception
{
Map<QName, ChildAssociationRef> assocRefs = buildNodeGraph();
// commit results
setComplete();
endTransaction();
UserTransaction userTransaction = txnService.getUserTransaction();
try
{
userTransaction.begin();
ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8"));
NodeRef n6Ref = n6pn8Ref.getParentRef();
NodeRef n8Ref = n6pn8Ref.getChildRef();
// delete n8
nodeService.deleteNode(n8Ref);
// get the parent children
List<ChildAssociationRef> assocs = nodeService.getChildAssocs(n6Ref);
for (ChildAssociationRef assoc : assocs)
{
// just checking
}
userTransaction.commit();
}
catch(Exception e)
{
try { userTransaction.rollback(); } catch (IllegalStateException ee) {}
throw e;
}
}
/**
* Checks that the node status changes correctly during:
* <ul>
* <li>creation</li>
* <li>property changes</li>
* <li>aspect changes</li>
* <li>moving</li>
* <li>deletion</li>
* </ul>
*/
public void testNodeStatus() throws Exception
{
Map<QName, ChildAssociationRef> assocRefs = buildNodeGraph();
// get the node to play with
ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8"));
final NodeRef n6Ref = n6pn8Ref.getParentRef();
final NodeRef n8Ref = n6pn8Ref.getChildRef();
final Map<QName, Serializable> properties = nodeService.getProperties(n6Ref);
// commit results
setComplete();
endTransaction();
// change property - check status
TransactionWork<Object> changePropertiesWork = new TransactionWork<Object>()
{
public Object doWork()
{
nodeService.setProperty(n6Ref, ContentModel.PROP_CREATED, new Date());
return null;
}
};
executeAndCheck(n6Ref, changePropertiesWork);
// add an aspect
TransactionWork<Object> addAspectWork = new TransactionWork<Object>()
{
public Object doWork()
{
nodeService.addAspect(n6Ref, ASPECT_QNAME_TEST_MARKER, null);
return null;
}
};
executeAndCheck(n6Ref, addAspectWork);
// remove an aspect
TransactionWork<Object> removeAspectWork = new TransactionWork<Object>()
{
public Object doWork()
{
nodeService.removeAspect(n6Ref, ASPECT_QNAME_TEST_MARKER);
return null;
}
};
executeAndCheck(n6Ref, removeAspectWork);
// move the node
TransactionWork<Object> moveNodeWork = new TransactionWork<Object>()
{
public Object doWork()
{
nodeService.moveNode(
n6Ref,
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName(NAMESPACE, "moved"));
return null;
}
};
executeAndCheck(n6Ref, moveNodeWork);
// delete the node
TransactionWork<Object> deleteNodeWork = new TransactionWork<Object>()
{
public Object doWork()
{
nodeService.deleteNode(n6Ref);
return null;
}
};
executeAndCheck(n6Ref, deleteNodeWork);
// check cascade-deleted nodes
TransactionWork<Object> checkCascadeWork = new TransactionWork<Object>()
{
public Object doWork()
{
// check n6
NodeStatus n6Status = nodeDaoService.getNodeStatus(
n6Ref.getStoreRef().getProtocol(),
n6Ref.getStoreRef().getIdentifier(),
n6Ref.getId());
if (!n6Status.isDeleted())
{
throw new RuntimeException("Deleted node does not have deleted status");
}
// n8 is a primary child - it should be deleted too
NodeStatus n8Status = nodeDaoService.getNodeStatus(
n8Ref.getStoreRef().getProtocol(),
n8Ref.getStoreRef().getIdentifier(),
n8Ref.getId());
if (!n8Status.isDeleted())
{
throw new RuntimeException("Cascade-deleted node does not have deleted status");
}
return null;
}
};
TransactionUtil.executeInUserTransaction(txnService, checkCascadeWork);
// check node recreation
TransactionWork<Object> checkRecreateWork = new TransactionWork<Object>()
{
public Object doWork()
{
properties.put(ContentModel.PROP_STORE_PROTOCOL, n6Ref.getStoreRef().getProtocol());
properties.put(ContentModel.PROP_STORE_IDENTIFIER, n6Ref.getStoreRef().getIdentifier());
properties.put(ContentModel.PROP_NODE_UUID, n6Ref.getId());
// recreate n6
nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName(NAMESPACE, "recreated-n6"),
ContentModel.TYPE_CONTAINER,
properties);
return null;
}
};
TransactionUtil.executeInUserTransaction(txnService, checkRecreateWork);
}
private void executeAndCheck(NodeRef nodeRef, TransactionWork<Object> work) throws Exception
{
UserTransaction txn = txnService.getUserTransaction();
txn.begin();
NodeRef.Status currentStatus = nodeService.getNodeStatus(nodeRef);
assertNotNull(currentStatus);
String currentTxnId = AlfrescoTransactionSupport.getTransactionId();
assertNotNull(currentTxnId);
assertNotSame(currentTxnId, currentStatus.getChangeTxnId());
try
{
work.doWork();
// get the status
NodeRef.Status newStatus = nodeService.getNodeStatus(nodeRef);
assertNotNull(newStatus);
// check
assertEquals("Change didn't update status", currentTxnId, newStatus.getChangeTxnId());
txn.commit();
}
catch (Exception e)
{
try { txn.rollback(); } catch (Throwable ee) {}
throw e;
}
}
}

View File

@@ -0,0 +1,179 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.node.db;
import java.util.Collection;
import java.util.List;
import org.alfresco.repo.domain.ChildAssoc;
import org.alfresco.repo.domain.Node;
import org.alfresco.repo.domain.NodeAssoc;
import org.alfresco.repo.domain.NodeStatus;
import org.alfresco.repo.domain.Store;
import org.alfresco.service.cmr.dictionary.InvalidTypeException;
import org.alfresco.service.namespace.QName;
/**
* Service layer accessing persistent <b>node</b> entities directly
*
* @author Derek Hulley
*/
public interface NodeDaoService
{
/**
* Are there any pending changes which must be synchronized with the store?
*
* @return true => changes are pending
*/
public boolean isDirty();
/**
* Fetch a list of all stores in the repository
*
* @return Returns a list of stores
*/
public List<Store> getStores();
/**
* Creates a unique store for the given protocol and identifier combination
*
* @param protocol a protocol, e.g. {@link org.alfresco.service.cmr.repository.StoreRef#PROTOCOL_WORKSPACE}
* @param identifier a protocol-specific identifier
* @return Returns the new persistent entity
*/
public Store createStore(String protocol, String identifier);
/**
* @param protocol the protocol that the store serves
* @param identifier the protocol-specific identifer
* @return Returns a store with the given values or null if one doesn't exist
*/
public Store getStore(String protocol, String identifier);
/**
* @param store the store to which the node must belong
* @param id the node store-unique identifier
* @param nodeTypeQName the type of the node
* @return Returns a new node of the given type and attached to the store
* @throws InvalidTypeException if the node type is invalid or if the node type
* is not a valid real node
*/
public Node newNode(Store store, String id, QName nodeTypeQName) throws InvalidTypeException;
/**
* @param protocol the store protocol
* @param identifier the store identifier for the given protocol
* @param id the store-specific node identifier
* @return Returns the <b>node</b> entity
*/
public Node getNode(String protocol, String identifier, String id);
/**
* Deletes the node instance, taking care of any cascades that are required over
* and above those provided by the persistence mechanism.
* <p>
* A caller must able to delete the node using this method and not have to follow
* up with any other ancillary deletes
*
* @param node the entity to delete
* @param cascade true if the assoc deletions must cascade to primary child nodes
*/
public void deleteNode(Node node, boolean cascade);
/**
* @return Returns the persisted and filled association
*
* @see ChildAssoc
*/
public ChildAssoc newChildAssoc(
Node parentNode,
Node childNode,
boolean isPrimary,
QName assocTypeQName,
QName qname);
/**
* @return Returns a matching association or null if one was not found
*
* @see ChildAssoc
*/
public ChildAssoc getChildAssoc(
Node parentNode,
Node childNode,
QName assocTypeQName,
QName qname);
/**
* @param assoc the child association to remove
* @param cascade true if the assoc deletions must cascade to primary child nodes
*/
public void deleteChildAssoc(ChildAssoc assoc, boolean cascade);
/**
* Finds the association between the node's primary parent and the node itself
*
* @param node the child node
* @return Returns the primary <code>ChildAssoc</code> instance where the given node is the child.
* The return value could be null for a root node - but ONLY a root node
*/
public ChildAssoc getPrimaryParentAssoc(Node node);
/**
* @return Returns the persisted and filled association
* @see NodeAssoc
*/
public NodeAssoc newNodeAssoc(
Node sourceNode,
Node targetNode,
QName assocTypeQName);
/**
* @return Returns the node association or null if not found
*/
public NodeAssoc getNodeAssoc(
Node sourceNode,
Node targetNode,
QName assocTypeQName);
/**
* @return Returns the target nodes for the association
*/
public Collection<Node> getNodeAssocTargets(Node sourceNode, QName assocTypeQName);
/**
* @return Returns the source nodes for the association
*/
public Collection<Node> getNodeAssocSources(Node targetNode, QName assocTypeQName);
/**
* @param assoc the node association to remove
*/
public void deleteNodeAssoc(NodeAssoc assoc);
/**
* Gets the node's status. If the node <i>never</i> existed, then
* <code>null</code> is returned.
*
* @param protocol the store protocol
* @param identifier the store identifier for the given protocol
* @param id the store-specific node status identifier
* @return Returns the node status if the node exists or once existed, otherwise
* returns <code>null</code>.
*/
public NodeStatus getNodeStatus(String protocol, String identifier, String id);
}

View File

@@ -0,0 +1,507 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.node.db.hibernate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.domain.ChildAssoc;
import org.alfresco.repo.domain.Node;
import org.alfresco.repo.domain.NodeAssoc;
import org.alfresco.repo.domain.NodeKey;
import org.alfresco.repo.domain.NodeStatus;
import org.alfresco.repo.domain.Store;
import org.alfresco.repo.domain.StoreKey;
import org.alfresco.repo.domain.hibernate.ChildAssocImpl;
import org.alfresco.repo.domain.hibernate.NodeAssocImpl;
import org.alfresco.repo.domain.hibernate.NodeImpl;
import org.alfresco.repo.domain.hibernate.NodeStatusImpl;
import org.alfresco.repo.domain.hibernate.StoreImpl;
import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.service.cmr.dictionary.InvalidTypeException;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.GUID;
import org.hibernate.ObjectDeletedException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Hibernate-specific implementation of the persistence-independent <b>node</b> DAO interface
*
* @author Derek Hulley
*/
public class HibernateNodeDaoServiceImpl extends HibernateDaoSupport implements NodeDaoService
{
public static final String QUERY_GET_ALL_STORES = "store.GetAllStores";
public static final String QUERY_GET_CHILD_ASSOC = "node.GetChildAssoc";
public static final String QUERY_GET_NODE_ASSOC = "node.GetNodeAssoc";
public static final String QUERY_GET_NODE_ASSOC_TARGETS = "node.GetNodeAssocTargets";
public static final String QUERY_GET_NODE_ASSOC_SOURCES = "node.GetNodeAssocSources";
/** a uuid identifying this unique instance */
private String uuid;
/**
*
*/
public HibernateNodeDaoServiceImpl()
{
this.uuid = GUID.generate();
}
/**
* Checks equality by type and uuid
*/
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
else if (!(obj instanceof HibernateNodeDaoServiceImpl))
{
return false;
}
HibernateNodeDaoServiceImpl that = (HibernateNodeDaoServiceImpl) obj;
return this.uuid.equals(that.uuid);
}
/**
* @see #uuid
*/
public int hashCode()
{
return uuid.hashCode();
}
/**
* Does this <tt>Session</tt> contain any changes which must be
* synchronized with the store?
*
* @return true => changes are pending
*/
public boolean isDirty()
{
// create a callback for the task
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
return session.isDirty();
}
};
// execute the callback
return ((Boolean)getHibernateTemplate().execute(callback)).booleanValue();
}
/**
* @see #QUERY_GET_ALL_STORES
*/
@SuppressWarnings("unchecked")
public List<Store> getStores()
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_ALL_STORES);
return query.list();
}
};
List<Store> queryResults = (List) getHibernateTemplate().execute(callback);
// done
return queryResults;
}
/**
* Ensures that the store protocol/identifier combination is unique
*/
public Store createStore(String protocol, String identifier)
{
// ensure that the name isn't in use
Store store = getStore(protocol, identifier);
if (store != null)
{
throw new RuntimeException("A store already exists: \n" +
" protocol: " + protocol + "\n" +
" identifier: " + identifier + "\n" +
" store: " + store);
}
store = new StoreImpl();
// set key
store.setKey(new StoreKey(protocol, identifier));
// persist so that it is present in the hibernate cache
getHibernateTemplate().save(store);
// create and assign a root node
Node rootNode = newNode(
store,
GUID.generate(),
ContentModel.TYPE_STOREROOT);
store.setRootNode(rootNode);
// done
return store;
}
public Store getStore(String protocol, String identifier)
{
StoreKey storeKey = new StoreKey(protocol, identifier);
Store store = (Store) getHibernateTemplate().get(StoreImpl.class, storeKey);
// done
return store;
}
public Node newNode(Store store, String id, QName nodeTypeQName) throws InvalidTypeException
{
NodeKey key = new NodeKey(store.getKey(), id);
// create (or reuse) the mandatory node status
NodeStatus nodeStatus = (NodeStatus) getHibernateTemplate().get(NodeStatusImpl.class, key);
if (nodeStatus == null)
{
nodeStatus = new NodeStatusImpl();
}
// set required status properties
nodeStatus.setKey(key);
nodeStatus.setDeleted(false);
nodeStatus.setChangeTxnId(AlfrescoTransactionSupport.getTransactionId());
// persist the nodestatus
getHibernateTemplate().save(nodeStatus);
// build a concrete node based on a bootstrap type
Node node = new NodeImpl();
// set other required properties
node.setKey(key);
node.setTypeQName(nodeTypeQName);
node.setStore(store);
node.setStatus(nodeStatus);
// persist the node
getHibernateTemplate().save(node);
// done
return node;
}
public Node getNode(String protocol, String identifier, String id)
{
try
{
NodeKey nodeKey = new NodeKey(protocol, identifier, id);
Object obj = getHibernateTemplate().get(NodeImpl.class, nodeKey);
// done
return (Node) obj;
}
catch (ObjectDeletedException e)
{
return null;
}
catch (DataAccessException e)
{
if (e.contains(ObjectDeletedException.class))
{
// the object no loner exists
return null;
}
throw e;
}
}
/**
* Manually ensures that all cascading of associations is taken care of
*/
public void deleteNode(Node node, boolean cascade)
{
// delete all parent assocs
Collection<ChildAssoc> parentAssocs = node.getParentAssocs();
parentAssocs = new ArrayList<ChildAssoc>(parentAssocs);
for (ChildAssoc assoc : parentAssocs)
{
deleteChildAssoc(assoc, false); // we don't cascade upwards
}
// delete all child assocs
Collection<ChildAssoc> childAssocs = node.getChildAssocs();
childAssocs = new ArrayList<ChildAssoc>(childAssocs);
for (ChildAssoc assoc : childAssocs)
{
deleteChildAssoc(assoc, cascade); // potentially cascade downwards
}
// delete all target assocs
Collection<NodeAssoc> targetAssocs = node.getTargetNodeAssocs();
targetAssocs = new ArrayList<NodeAssoc>(targetAssocs);
for (NodeAssoc assoc : targetAssocs)
{
deleteNodeAssoc(assoc);
}
// delete all source assocs
Collection<NodeAssoc> sourceAssocs = node.getSourceNodeAssocs();
sourceAssocs = new ArrayList<NodeAssoc>(sourceAssocs);
for (NodeAssoc assoc : sourceAssocs)
{
deleteNodeAssoc(assoc);
}
// update the node status
NodeStatus nodeStatus = node.getStatus();
nodeStatus.setDeleted(true);
nodeStatus.setChangeTxnId(AlfrescoTransactionSupport.getTransactionId());
// finally delete the node
getHibernateTemplate().delete(node);
// done
}
/**
* Fetch the node status, if it exists
*/
public NodeStatus getNodeStatus(String protocol, String identifier, String id)
{
try
{
NodeKey nodeKey = new NodeKey(protocol, identifier, id);
Object obj = getHibernateTemplate().get(NodeStatusImpl.class, nodeKey);
// done
return (NodeStatus) obj;
}
catch (DataAccessException e)
{
if (e.contains(ObjectDeletedException.class))
{
// the object no loner exists
return null;
}
throw e;
}
}
public ChildAssoc newChildAssoc(
Node parentNode,
Node childNode,
boolean isPrimary,
QName assocTypeQName,
QName qname)
{
ChildAssoc assoc = new ChildAssocImpl();
assoc.setTypeQName(assocTypeQName);
assoc.setIsPrimary(isPrimary);
assoc.setQname(qname);
assoc.buildAssociation(parentNode, childNode);
// persist
getHibernateTemplate().save(assoc);
// done
return assoc;
}
public ChildAssoc getChildAssoc(
Node parentNode,
Node childNode,
QName assocTypeQName,
QName qname)
{
ChildAssociationRef childAssocRef = new ChildAssociationRef(
assocTypeQName,
parentNode.getNodeRef(),
qname,
childNode.getNodeRef());
// get all the parent's child associations
Collection<ChildAssoc> assocs = parentNode.getChildAssocs();
// hunt down the desired assoc
for (ChildAssoc assoc : assocs)
{
// is it a match?
if (!assoc.getChildAssocRef().equals(childAssocRef)) // not a match
{
continue;
}
else
{
return assoc;
}
}
// not found
return null;
}
/**
* Manually enforces cascade deletions down primary associations
*/
public void deleteChildAssoc(ChildAssoc assoc, boolean cascade)
{
Node childNode = assoc.getChild();
// maintain inverse association sets
assoc.removeAssociation();
// remove instance
getHibernateTemplate().delete(assoc);
if (cascade && assoc.getIsPrimary()) // the assoc is primary
{
// delete the child node
deleteNode(childNode, cascade);
/*
* The child node deletion will cascade delete all assocs to
* and from it, but we have safely removed this one, so no
* duplicate call will be received to do this
*/
}
}
public ChildAssoc getPrimaryParentAssoc(Node node)
{
// get the assocs pointing to the node
Collection<ChildAssoc> parentAssocs = node.getParentAssocs();
ChildAssoc primaryAssoc = null;
for (ChildAssoc assoc : parentAssocs)
{
// ignore non-primary assocs
if (!assoc.getIsPrimary())
{
continue;
}
else if (primaryAssoc != null)
{
// we have more than one somehow
throw new DataIntegrityViolationException(
"Multiple primary associations: \n" +
" child: " + node + "\n" +
" first primary assoc: " + primaryAssoc + "\n" +
" second primary assoc: " + assoc);
}
primaryAssoc = assoc;
// we keep looping to hunt out data integrity issues
}
// did we find a primary assoc?
if (primaryAssoc == null)
{
// the only condition where this is allowed is if the given node is a root node
Store store = node.getStore();
Node rootNode = store.getRootNode();
if (rootNode == null)
{
// a store without a root node - the entire store is hosed
throw new DataIntegrityViolationException("Store has no root node: \n" +
" store: " + store);
}
if (!rootNode.equals(node))
{
// it wasn't the root node
throw new DataIntegrityViolationException("Non-root node has no primary parent: \n" +
" child: " + node);
}
}
// done
return primaryAssoc;
}
public NodeAssoc newNodeAssoc(Node sourceNode, Node targetNode, QName assocTypeQName)
{
NodeAssoc assoc = new NodeAssocImpl();
assoc.setTypeQName(assocTypeQName);
assoc.buildAssociation(sourceNode, targetNode);
// persist
getHibernateTemplate().save(assoc);
// done
return assoc;
}
public NodeAssoc getNodeAssoc(
final Node sourceNode,
final Node targetNode,
final QName assocTypeQName)
{
final NodeKey sourceKey = sourceNode.getKey();
final NodeKey targetKey = targetNode.getKey();
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_NODE_ASSOC);
query.setString("sourceKeyProtocol", sourceKey.getProtocol())
.setString("sourceKeyIdentifier", sourceKey.getIdentifier())
.setString("sourceKeyGuid", sourceKey.getGuid())
.setString("assocTypeQName", assocTypeQName.toString())
.setString("targetKeyProtocol", targetKey.getProtocol())
.setString("targetKeyIdentifier", targetKey.getIdentifier())
.setString("targetKeyGuid", targetKey.getGuid());
query.setMaxResults(1);
return query.uniqueResult();
}
};
Object queryResult = getHibernateTemplate().execute(callback);
if (queryResult == null)
{
return null;
}
NodeAssoc assoc = (NodeAssoc) queryResult;
// done
return assoc;
}
@SuppressWarnings("unchecked")
public Collection<Node> getNodeAssocTargets(final Node sourceNode, final QName assocTypeQName)
{
final NodeKey sourceKey = sourceNode.getKey();
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_NODE_ASSOC_TARGETS);
query.setString("sourceKeyProtocol", sourceKey.getProtocol())
.setString("sourceKeyIdentifier", sourceKey.getIdentifier())
.setString("sourceKeyGuid", sourceKey.getGuid())
.setString("assocTypeQName", assocTypeQName.toString());
return query.list();
}
};
List<Node> queryResults = (List) getHibernateTemplate().execute(callback);
// done
return queryResults;
}
@SuppressWarnings("unchecked")
public Collection<Node> getNodeAssocSources(final Node targetNode, final QName assocTypeQName)
{
final NodeKey targetKey = targetNode.getKey();
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_NODE_ASSOC_SOURCES);
query.setString("targetKeyProtocol", targetKey.getProtocol())
.setString("targetKeyIdentifier", targetKey.getIdentifier())
.setString("targetKeyGuid", targetKey.getGuid())
.setString("assocTypeQName", assocTypeQName.toString());
return query.list();
}
};
List<Node> queryResults = (List) getHibernateTemplate().execute(callback);
// done
return queryResults;
}
public void deleteNodeAssoc(NodeAssoc assoc)
{
// maintain inverse association sets
assoc.removeAssociation();
// remove instance
getHibernateTemplate().delete(assoc);
}
}