added debugcontroller method to explicitly delete a Modeshape Reference from a Set.
.*}")
@@ -60,8 +60,10 @@ import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@@ -69,6 +71,7 @@ import java.util.stream.IntStream;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
+import javax.jcr.Property;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
@@ -225,6 +228,68 @@ public class DebugController {
return sw.toString();
}
+ @DELETE
+ @Path("jcr/node-reference/{abspath: .*}")
+ @Produces(MediaType.TEXT_PLAIN)
+ public String deleteJcrTree(@PathParam("abspath") final String abspath, @QueryParam("referenceId") String referenceId) {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+
+ try {
+ metadata.commit(() -> {
+ this.accessController.checkPermission(AccessController.SERVICES, MetadataAccessControl.ADMIN_METADATA);
+ Session session = JcrMetadataAccess.getActiveSession();
+ java.nio.file.Path path = JcrPath.get(abspath);
+ Node node = session.getRootNode().getNode(path.getParent().toString());
+
+ if(node != null) {
+ String referenceNodeName = path.getName(path.getNameCount() -1).toString();
+ Property property = node.getProperty(referenceNodeName);
+ if (property != null) {
+ if (property.isMultiple()){
+ Set<Object> removal = new HashSet<>();
+ JcrPropertyUtil.getSetProperty(node, referenceNodeName).stream().forEach(n -> {
+ try {
+ if (n instanceof Node) {
+ String id = ((Node) n).getIdentifier();
+ if (id.equalsIgnoreCase(referenceId)) {
+ removal.add(n);
+ }
+ }
+ }catch (RepositoryException e){
+ e.printStackTrace();
+ pw.print("Unable to delete node for reference " + abspath+"/"+referenceId+". "+e.getMessage());
+ }
+ });
+ if(!removal.isEmpty()) {
+ removal.stream().forEach(n -> JcrPropertyUtil.removeFromSetProperty(node, referenceNodeName, n));
+
+ pw.print("DELETED " + abspath+"/"+referenceId);
+ }
+ else {
+ pw.print("Not Deleted. Unable to find reference id " +referenceId+" in set for path "+ abspath);
+ }
+ }
+ else {
+ JcrPropertyUtil.setProperty(node,referenceNodeName,null);
+ pw.print("Path is not a set of nodes. Setting valur of node to null. DELETED " + abspath+"/"+referenceId);
+ }
+ }
+ }
+
+ });
+ } catch (Exception e) {
+ e.printStackTrace(pw);
+ throw new RuntimeException(e);
+ }
+
+ pw.flush();
+ return sw.toString();
+ }
+
+
+
+
/**
* Prints the nodes of the JCR path given, for debugging.
*
