Skip to content

Commit

Permalink
Check each value of multi value in a reference or weak reference Prop…
Browse files Browse the repository at this point in the history
…erty when deleting a node

- remove only the node being deleted.

Resolves: https://jira.duraspace.org/browse/FCREPO-1362
  • Loading branch information
whikloj authored and Andrew Woods committed Mar 13, 2015
1 parent b7b5f97 commit 4a77e57
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Expand Up @@ -36,6 +36,7 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
Expand All @@ -48,6 +49,7 @@
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;

Expand Down Expand Up @@ -257,7 +259,21 @@ public void delete() {
final Iterator<Property> inboundProperties = Iterators.concat(references, weakReferences);

while (inboundProperties.hasNext()) {
inboundProperties.next().remove();
final Property prop = inboundProperties.next();
final List<Value> newVals = new ArrayList<>();
final Iterator<Value> propIt = property2values.apply(prop);
while (propIt.hasNext()) {
final Value v = propIt.next();
if (!node.equals(getSession().getNodeByIdentifier(v.getString()))) {
newVals.add(v);
LOGGER.trace("Keeping multivalue reference property when deleting node");
}
}
if (newVals.size() == 0) {
prop.remove();
} else {
prop.setValue(newVals.toArray(new Value[newVals.size()]));
}
}

final Node parent;
Expand Down
Expand Up @@ -812,6 +812,45 @@ public void testHash() throws RepositoryException {
assertFalse(frozenResource.hashCode() == 0);
}

@Test
public void testDeletePartOfMultiValueProperty() throws RepositoryException {
final String pid = UUID.randomUUID().toString();
final String relation = "test:fakeRel";
containerService.findOrCreate(session, pid);
final Container subject = containerService.findOrCreate(session, pid + "/a");
final Container referent1 = containerService.findOrCreate(session, pid + "/b");
final Container referent2 = containerService.findOrCreate(session, pid + "/c");
final Value[] values = new Value[2];
values[0] = session.getValueFactory().createValue(referent1.getNode());
values[1] = session.getValueFactory().createValue(referent2.getNode());
subject.getNode().setProperty(relation, values);

session.save();

final Model model1 = referent1.getTriples(subjects, ReferencesRdfContext.class).asModel();

assertTrue(model1.contains(subjects.reverse().convert(subject),
createProperty("info:fedora/test/fakeRel"),
createResource("info:fedora/" + pid + "/b")));

assertTrue(model1.contains(subjects.reverse().convert(subject),
createProperty("info:fedora/test/fakeRel"),
createResource("info:fedora/" + pid + "/c")));

// This is the test! Ensure that only the delete resource is removed from the "subject" container.
referent2.delete();

final Model model2 = referent1.getTriples(subjects, ReferencesRdfContext.class).asModel();

assertTrue(model2.contains(subjects.reverse().convert(subject),
createProperty("info:fedora/test/fakeRel"),
createResource("info:fedora/" + pid + "/b")));

assertFalse(model2.contains(subjects.reverse().convert(subject),
createProperty("info:fedora/test/fakeRel"),
createResource("info:fedora/" + pid + "/c")));
}

private void addVersionLabel(final String label, final FedoraResource r) throws RepositoryException {
addVersionLabel(label, session.getWorkspace().getVersionManager().getBaseVersion(r.getPath()));
}
Expand Down

0 comments on commit 4a77e57

Please sign in to comment.