use of org.alfresco.service.cmr.repository.AssociationRef in project alfresco-repository by Alfresco.
the class PeerAssociationRepoEventIT method testAddMultiplePeerAssociationDifferentTransaction.
@Test
public void testAddMultiplePeerAssociationDifferentTransaction() {
final NodeRef content1NodeRef = createNode(ContentModel.TYPE_CONTENT);
final NodeRef content2NodeRef = createNode(ContentModel.TYPE_CONTENT);
final NodeRef content3NodeRef = createNode(ContentModel.TYPE_CONTENT);
checkNumOfEvents(3);
RepoEvent<EventData<NodeResource>> resultRepoEvent = getRepoEventWithoutWait(1);
assertEquals("Wrong repo event type.", EventType.NODE_CREATED.getType(), resultRepoEvent.getType());
resultRepoEvent = getRepoEventWithoutWait(2);
assertEquals("Wrong repo event type.", EventType.NODE_CREATED.getType(), resultRepoEvent.getType());
resultRepoEvent = getRepoEventWithoutWait(3);
assertEquals("Wrong repo event type.", EventType.NODE_CREATED.getType(), resultRepoEvent.getType());
retryingTransactionHelper.doInTransaction(() -> {
nodeService.createAssociation(content1NodeRef, content2NodeRef, ContentModel.ASSOC_ORIGINAL);
return null;
});
retryingTransactionHelper.doInTransaction(() -> {
nodeService.createAssociation(content3NodeRef, content2NodeRef, ContentModel.ASSOC_ORIGINAL);
return null;
});
List<AssociationRef> peerAssociationRefs = retryingTransactionHelper.doInTransaction(() -> nodeService.getSourceAssocs(content2NodeRef, ContentModel.ASSOC_ORIGINAL));
assertEquals(2, peerAssociationRefs.size());
checkNumOfEvents(7);
List<RepoEvent<EventData<PeerAssociationResource>>> peerAssocRepoEvent = getFilteredEvents(EventType.PEER_ASSOC_CREATED);
// we should have 2 assoc.peer.Created events
assertEquals("Wrong association events number", 2, peerAssocRepoEvent.size());
assertEquals("Wrong source", content1NodeRef.getId(), getPeerAssocResource(peerAssocRepoEvent.get(0)).getSource().getId());
assertEquals("Wrong target", content2NodeRef.getId(), getPeerAssocResource(peerAssocRepoEvent.get(0)).getTarget().getId());
assertEquals("Wrong assoc type", "cm:original", getPeerAssocResource(peerAssocRepoEvent.get(0)).getAssocType());
assertEquals("Wrong source", content3NodeRef.getId(), getPeerAssocResource(peerAssocRepoEvent.get(1)).getSource().getId());
assertEquals("Wrong target", content2NodeRef.getId(), getPeerAssocResource(peerAssocRepoEvent.get(1)).getTarget().getId());
assertEquals("Wrong assoc type", "cm:original", getPeerAssocResource(peerAssocRepoEvent.get(1)).getAssocType());
}
use of org.alfresco.service.cmr.repository.AssociationRef in project alfresco-repository by Alfresco.
the class PeerAssociationRepoEventIT method testRemovePeerAssociation.
@Test
public void testRemovePeerAssociation() {
final NodeRef content1NodeRef = createNode(ContentModel.TYPE_CONTENT);
final NodeRef content2NodeRef = createNode(ContentModel.TYPE_CONTENT);
checkNumOfEvents(2);
RepoEvent<EventData<NodeResource>> resultRepoEvent = getRepoEventWithoutWait(1);
assertEquals("Wrong repo event type.", EventType.NODE_CREATED.getType(), resultRepoEvent.getType());
resultRepoEvent = getRepoEventWithoutWait(2);
assertEquals("Wrong repo event type.", EventType.NODE_CREATED.getType(), resultRepoEvent.getType());
// Create peer association
retryingTransactionHelper.doInTransaction(() -> nodeService.createAssociation(content1NodeRef, content2NodeRef, ContentModel.ASSOC_ORIGINAL));
List<AssociationRef> peerAssociationRefs = retryingTransactionHelper.doInTransaction(() -> nodeService.getSourceAssocs(content2NodeRef, ContentModel.ASSOC_ORIGINAL));
assertEquals(1, peerAssociationRefs.size());
checkNumOfEvents(4);
// Remove peer association
retryingTransactionHelper.doInTransaction(() -> {
nodeService.removeAssociation(content1NodeRef, content2NodeRef, ContentModel.ASSOC_ORIGINAL);
return null;
});
peerAssociationRefs = retryingTransactionHelper.doInTransaction(() -> nodeService.getSourceAssocs(content2NodeRef, ContentModel.ASSOC_ORIGINAL));
assertEquals(0, peerAssociationRefs.size());
checkNumOfEvents(6);
// Check the peer assoc created event
final RepoEvent<EventData<PeerAssociationResource>> peerAssocRepoEvent = getFilteredEvent(EventType.PEER_ASSOC_CREATED, 0);
assertEquals("Wrong repo event type.", EventType.PEER_ASSOC_CREATED.getType(), peerAssocRepoEvent.getType());
assertNotNull("Repo event ID is not available. ", peerAssocRepoEvent.getId());
assertNotNull("Source is not available", peerAssocRepoEvent.getSource());
assertEquals("Repo event source is not available. ", "/" + descriptorService.getCurrentRepositoryDescriptor().getId(), peerAssocRepoEvent.getSource().toString());
assertNotNull("Repo event creation time is not available. ", peerAssocRepoEvent.getTime());
assertEquals("Repo event datacontenttype", "application/json", peerAssocRepoEvent.getDatacontenttype());
assertNotNull(peerAssocRepoEvent.getDataschema());
assertEquals(EventJSONSchema.PEER_ASSOC_CREATED_V1.getSchema(), peerAssocRepoEvent.getDataschema());
final EventData<PeerAssociationResource> nodeResourceEventData = getEventData(peerAssocRepoEvent);
// EventData attributes
assertNotNull("Event data group ID is not available. ", nodeResourceEventData.getEventGroupId());
assertNull("resourceBefore property is not available", nodeResourceEventData.getResourceBefore());
final PeerAssociationResource peerAssociationResource = getPeerAssocResource(peerAssocRepoEvent);
assertEquals("Wrong source", content1NodeRef.getId(), peerAssociationResource.getSource().getId());
assertEquals("Wrong target", content2NodeRef.getId(), peerAssociationResource.getTarget().getId());
assertEquals("Wrong assoc type", "cm:original", peerAssociationResource.getAssocType());
// Check the peer assoc deleted event
final RepoEvent<EventData<PeerAssociationResource>> peerAssocRepoEvent2 = getFilteredEvent(EventType.PEER_ASSOC_DELETED, 0);
assertEquals("Wrong repo event type.", EventType.PEER_ASSOC_DELETED.getType(), peerAssocRepoEvent2.getType());
assertNotNull("Repo event ID is not available. ", peerAssocRepoEvent2.getId());
assertNotNull("Source is not available", peerAssocRepoEvent2.getSource());
assertEquals("Repo event source is not available. ", "/" + descriptorService.getCurrentRepositoryDescriptor().getId(), peerAssocRepoEvent2.getSource().toString());
assertNotNull("Repo event creation time is not available. ", peerAssocRepoEvent2.getTime());
assertEquals("Repo event datacontenttype", "application/json", peerAssocRepoEvent2.getDatacontenttype());
assertNotNull(peerAssocRepoEvent2.getDataschema());
assertEquals(EventJSONSchema.PEER_ASSOC_DELETED_V1.getSchema(), peerAssocRepoEvent2.getDataschema());
final EventData<PeerAssociationResource> nodeResourceEventData2 = getEventData(peerAssocRepoEvent2);
// EventData attributes
assertNotNull("Event data group ID is not available. ", nodeResourceEventData2.getEventGroupId());
assertNull("resourceBefore property is not available", nodeResourceEventData2.getResourceBefore());
final PeerAssociationResource peerAssociationResource2 = getPeerAssocResource(peerAssocRepoEvent2);
assertEquals("Wrong source", content1NodeRef.getId(), peerAssociationResource2.getSource().getId());
assertEquals("Wrong target", content2NodeRef.getId(), peerAssociationResource2.getTarget().getId());
assertEquals("Wrong assoc type", "cm:original", peerAssociationResource2.getAssocType());
}
use of org.alfresco.service.cmr.repository.AssociationRef in project alfresco-repository by Alfresco.
the class TransferManifestTest method testCreateAndReadSnapshot.
/**
* This unit test creates and reads a manifest.
*
* @throws Exception
*/
public void testCreateAndReadSnapshot() throws Exception {
/**
* create snapshot
*/
String prefix = "TRX-SNAP";
String suffix = ".xml";
String repoId = "repoId";
// where to put snapshot ?
File snapshotFile = TempFileProvider.createTempFile(prefix, suffix);
Writer snapshotWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(snapshotFile), "UTF-8"));
// Write the manifest file
TransferManifestWriter formatter = new XMLTransferManifestWriter();
TransferManifestHeader header = new TransferManifestHeader();
header.setCreatedDate(new Date());
header.setRepositoryId(repoId);
formatter.startTransferManifest(snapshotWriter);
formatter.writeTransferManifestHeader(header);
// node to transmit
TransferManifestNormalNode node = new TransferManifestNormalNode();
NodeRef nodeRefA = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "123");
node.setNodeRef(nodeRefA);
node.setParentPath(new Path());
Set<QName> aspects = new HashSet<QName>();
aspects.add(QName.createQName("{gsxhjsx}", "cm:wobble"));
aspects.add(QName.createQName("{gsxhjsx}", "cm:wibble"));
node.setAspects(aspects);
Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
/**
* String type
*/
properties.put(QName.createQName("{gsxhjsx}", "cm:name"), "brian.jpg");
/**
* Date type
*/
properties.put(QName.createQName("{gsxhjsx}", "cm:created"), new java.util.Date());
/**
* Boolean type
*/
properties.put(QName.createQName("{gsxhjsx}", "trx:enabled"), Boolean.FALSE);
/**
* MLText value
*/
MLText mltext = new MLText();
mltext.addValue(Locale.FRENCH, "Bonjour");
mltext.addValue(Locale.ENGLISH, "Hello");
mltext.addValue(Locale.ITALY, "Buongiorno");
properties.put(QName.createQName("{gsxhjsx}", "cm:title"), mltext);
String password = "helloWorld";
/**
* Put a char array type
*/
properties.put(QName.createQName("{gsxhjsx}", "trx:password"), password.toCharArray());
/**
* Put an ArrayList type
*/
ArrayList<String> a1 = new ArrayList<String>();
a1.add("Rhubarb");
a1.add("Custard");
properties.put(QName.createQName("{gsxhjsx}", "trx:arrayList"), a1);
/**
* Put a HashMap type
*/
HashMap<String, String> m1 = new HashMap<String, String>();
m1.put("Rhubarb", "Rhubarb");
m1.put("Custard", "Custard");
properties.put(QName.createQName("{gsxhjsx}", "trx:hashMap"), m1);
/**
* Put a null value
*/
properties.put(QName.createQName("{gsxhjsx}", "cm:nullTest"), null);
/**
* Put a node ref property
*/
properties.put(QName.createQName("{gsxhjsx}", "trx:nodeRef"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "P1"));
/**
* Put an obscure "unknown type".
*/
// TestPrivateBean obscure = new TestPrivateBean();
// obscure.a = "hello";
// obscure.b = "world";
// properties.put(QName.createQName("{gsxhjsx}", "cm:obscure"), obscure);
List<ChildAssociationRef> parents = new ArrayList<ChildAssociationRef>();
ChildAssociationRef primaryParent = new ChildAssociationRef(QName.createQName("{gsxhjsx}", "cm:contains"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "P1"), QName.createQName("{gsxhjsx}", "app:smashing"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "123"), true, -1);
parents.add(primaryParent);
parents.add(new ChildAssociationRef(QName.createQName("{gsxhjsx}", "app:wibble"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "P1"), QName.createQName("{gsxhjsx}", "app:jskjsdc"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "123"), false, -1));
node.setParentAssocs(parents);
node.setPrimaryParentAssoc(primaryParent);
List<ChildAssociationRef> children = new ArrayList<ChildAssociationRef>();
children.add(new ChildAssociationRef(QName.createQName("{gsxhjsx}", "cm:contains"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "P1"), QName.createQName("{gsxhjsx}", "app:super"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "P5"), true, -1));
node.setChildAssocs(children);
Set<String> values = new HashSet<String>();
values.add("red");
values.add("blue");
values.add("green");
properties.put(QName.createQName("{gsxhjsx}", "xyz:colours"), (Serializable) values);
ContentData contentHeader = new ContentData("http://wibble", "mimeType", 123, "utf-8", Locale.ENGLISH);
properties.put(QName.createQName("{gsxhjsx}", "cm:content"), (Serializable) contentHeader);
node.setProperties(properties);
node.setType(QName.createQName("{gsxhjsx}", "trx:nsbbmbs"));
List<AssociationRef> targetAssocs = new ArrayList<AssociationRef>();
List<AssociationRef> sourceAssocs = new ArrayList<AssociationRef>();
targetAssocs.add(new AssociationRef(null, new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "SA"), QName.createQName("{gsxhjsx}", "app:super"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "TA")));
sourceAssocs.add(new AssociationRef(null, new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "HH"), QName.createQName("{gsxhjsx}", "app:super"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "JJ")));
node.setSourceAssocs(sourceAssocs);
node.setTargetAssocs(targetAssocs);
formatter.writeTransferManifestNode(node);
/**
* Write a second node
*/
TransferManifestNormalNode node2 = new TransferManifestNormalNode();
node2.setNodeRef(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "456"));
node2.setType(QName.createQName("{gsxhjsx}", "trx:dummy"));
formatter.writeTransferManifestNode(node2);
/**
* Write a deleted node
*/
TransferManifestDeletedNode node3 = new TransferManifestDeletedNode();
node3.setNodeRef(new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, "567"));
ChildAssociationRef origPrimaryParent = new ChildAssociationRef(QName.createQName("{gsxhjsx}", "cm:contains"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "P1"), QName.createQName("{gsxhjsx}", "app:whopper"), new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "567"), true, -1);
node3.setPrimaryParentAssoc(origPrimaryParent);
node3.setParentPath(new Path());
formatter.writeTransferManifestNode(node3);
formatter.endTransferManifest();
snapshotWriter.close();
//
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(snapshotFile), "UTF-8"));
String s = reader.readLine();
while (s != null) {
System.out.println(s);
s = reader.readLine();
}
// Now try to parse the snapshot file we have just created
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser parser = saxParserFactory.newSAXParser();
TestTransferManifestProcessor tp = new TestTransferManifestProcessor();
XMLTransferManifestReader xmlReader = new XMLTransferManifestReader(tp);
parser.parse(snapshotFile, xmlReader);
/**
* Now validate the parsed data.
*/
Map<NodeRef, TransferManifestNode> nodes = tp.getNodes();
TransferManifestNormalNode rxNodeA = (TransferManifestNormalNode) nodes.get(nodeRefA);
assertNotNull("rxNodeA is null", rxNodeA);
Map<QName, Serializable> rxNodeAProps = rxNodeA.getProperties();
System.out.println(rxNodeAProps.get(QName.createQName("{gsxhjsx}", "trx:password")));
for (Map.Entry<QName, Serializable> value : rxNodeAProps.entrySet()) {
System.out.println("key = " + value.getKey() + " value =" + value.getValue());
if (value.getValue() != null) {
if (value.getValue().getClass().isArray()) {
System.out.println("arrayValue=" + value.getValue().toString());
char[] chars = (char[]) value.getValue();
System.out.println(chars);
}
}
}
tp.getHeader();
snapshotFile.delete();
}
use of org.alfresco.service.cmr.repository.AssociationRef in project alfresco-repository by Alfresco.
the class NodeServiceImplTest method testGetTargetAssocsByPropertyValue.
/**
* Tests get target associations by property value.</p>
* See <b>MNT-14504</b> for more details.
*/
@Test
public void testGetTargetAssocsByPropertyValue() {
// Create a new versionable node
NodeRef versionableNode = createNewVersionableNode();
QName propertyQName = PROP_1;
Serializable propertyValue = VALUE_1;
// Store the current details of the target associations
List<AssociationRef> origAssocs = this.dbNodeService.getTargetAssocsByPropertyValue(versionableNode, RegexQNamePattern.MATCH_ALL, propertyQName, propertyValue);
// Create a new version
Version version = createVersion(versionableNode, this.versionProperties);
List<AssociationRef> assocs = this.versionStoreNodeService.getTargetAssocsByPropertyValue(version.getFrozenStateNodeRef(), RegexQNamePattern.MATCH_ALL, propertyQName, propertyValue);
assertNotNull(assocs);
assertEquals(origAssocs.size(), assocs.size());
}
use of org.alfresco.service.cmr.repository.AssociationRef in project alfresco-repository by Alfresco.
the class DbNodeServiceImpl method removeAspect.
@Override
@Extend(traitAPI = NodeServiceTrait.class, extensionAPI = NodeServiceExtension.class)
public void removeAspect(NodeRef nodeRef, QName aspectTypeQName) throws InvalidNodeRefException, InvalidAspectException {
// Don't allow spoofed aspect(s) to be removed
if (aspectTypeQName.equals(ContentModel.ASPECT_PENDING_DELETE)) {
throw new IllegalArgumentException("The aspect is reserved for system use: " + aspectTypeQName);
}
/*
* Note: Aspect and property removal is resilient to missing dictionary definitions
*/
// get the node
final Pair<Long, NodeRef> nodePair = getNodePairNotNull(nodeRef);
final Long nodeId = nodePair.getFirst();
boolean hadAspect = nodeDAO.hasNodeAspect(nodeId, aspectTypeQName);
// Invoke policy behaviours
invokeBeforeUpdateNode(nodeRef);
if (hadAspect) {
invokeBeforeRemoveAspect(nodeRef, aspectTypeQName);
nodeDAO.removeNodeAspects(nodeId, Collections.singleton(aspectTypeQName));
}
AspectDefinition aspectDef = dictionaryService.getAspect(aspectTypeQName);
boolean updated = false;
if (aspectDef != null) {
// Remove default properties
Map<QName, PropertyDefinition> propertyDefs = aspectDef.getProperties();
Set<QName> propertyToRemoveQNames = propertyDefs.keySet();
nodeDAO.removeNodeProperties(nodeId, propertyToRemoveQNames);
// Remove child associations
// We have to iterate over the associations and remove all those between the parent and child
final List<Pair<Long, ChildAssociationRef>> assocsToDelete = new ArrayList<Pair<Long, ChildAssociationRef>>(5);
final List<Pair<Long, NodeRef>> nodesToDelete = new ArrayList<Pair<Long, NodeRef>>(5);
NodeDAO.ChildAssocRefQueryCallback callback = new NodeDAO.ChildAssocRefQueryCallback() {
public boolean preLoadNodes() {
return true;
}
@Override
public boolean orderResults() {
return false;
}
public boolean handle(Pair<Long, ChildAssociationRef> childAssocPair, Pair<Long, NodeRef> parentNodePair, Pair<Long, NodeRef> childNodePair) {
if (isPendingDelete(parentNodePair.getSecond()) || isPendingDelete(childNodePair.getSecond())) {
if (logger.isTraceEnabled()) {
logger.trace("Aspect-triggered association removal: " + "Ignoring child associations where one of the nodes is pending delete: " + childAssocPair);
}
return true;
}
// have to delete the child node directly and with full archival.
if (childAssocPair.getSecond().isPrimary()) {
nodesToDelete.add(childNodePair);
} else {
assocsToDelete.add(childAssocPair);
}
// More results
return true;
}
public void done() {
}
};
// Get all the QNames to remove
Set<QName> assocTypeQNamesToRemove = new HashSet<QName>(aspectDef.getChildAssociations().keySet());
nodeDAO.getChildAssocs(nodeId, assocTypeQNamesToRemove, callback);
// Delete all the collected associations
for (Pair<Long, ChildAssociationRef> assocPair : assocsToDelete) {
updated = true;
Long assocId = assocPair.getFirst();
ChildAssociationRef assocRef = assocPair.getSecond();
// delete the association instance - it is not primary
invokeBeforeDeleteChildAssociation(assocRef);
nodeDAO.deleteChildAssoc(assocId);
invokeOnDeleteChildAssociation(assocRef);
}
// Cascade-delete any nodes that were attached to primary associations
for (Pair<Long, NodeRef> childNodePair : nodesToDelete) {
NodeRef childNodeRef = childNodePair.getSecond();
this.deleteNode(childNodeRef);
}
// Gather peer associations to delete
Map<QName, AssociationDefinition> nodeAssocDefs = aspectDef.getAssociations();
List<Long> nodeAssocIdsToRemove = new ArrayList<Long>(13);
List<AssociationRef> assocRefsRemoved = new ArrayList<AssociationRef>(13);
for (Map.Entry<QName, AssociationDefinition> entry : nodeAssocDefs.entrySet()) {
if (isPendingDelete(nodeRef)) {
if (logger.isTraceEnabled()) {
logger.trace("Aspect-triggered association removal: " + "Ignoring peer associations where one of the nodes is pending delete: " + nodeRef);
}
continue;
}
if (entry.getValue().isChild()) {
// Not interested in child assocs
continue;
}
QName assocTypeQName = entry.getKey();
Collection<Pair<Long, AssociationRef>> targetAssocRefs = nodeDAO.getTargetNodeAssocs(nodeId, assocTypeQName);
for (Pair<Long, AssociationRef> assocPair : targetAssocRefs) {
if (isPendingDelete(assocPair.getSecond().getTargetRef())) {
if (logger.isTraceEnabled()) {
logger.trace("Aspect-triggered association removal: " + "Ignoring peer associations where one of the nodes is pending delete: " + assocPair);
}
continue;
}
nodeAssocIdsToRemove.add(assocPair.getFirst());
assocRefsRemoved.add(assocPair.getSecond());
}
// MNT-9580: Daisy chained cm:original associations are cascade-deleted when the first original is deleted
// As a side-effect of the investigation of MNT-9446, it was dicovered that inbound associations (ones pointing *to* this aspect)
// were also being removed. This is incorrect because the aspect being removed here has no say over who points at it.
// Therefore, do not remove inbound associations because we only define outbound associations on types and aspects.
// Integrity checking will ensure that the correct behaviours are in place to maintain model integrity.
}
// Now delete peer associations
int assocsDeleted = nodeDAO.removeNodeAssocs(nodeAssocIdsToRemove);
for (AssociationRef assocRefRemoved : assocRefsRemoved) {
invokeOnDeleteAssociation(assocRefRemoved);
}
updated = updated || assocsDeleted > 0;
}
// Invoke policy behaviours
if (updated) {
invokeOnUpdateNode(nodeRef);
}
if (hadAspect) {
invokeOnRemoveAspect(nodeRef, aspectTypeQName);
}
}
Aggregations