X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=jcr%2Forg.argeo.cms.jcr%2Fsrc%2Forg%2Fargeo%2Fcms%2Fjcr%2Facr%2FJcrContent.java;h=41672a0e9e24b68ca9ca0a49f64776330d2948d7;hb=5c9bae758b5bb13ff3b046ad680886ae540865e8;hp=dab41979498957a91a3906eb0402acfe4c85b57d;hpb=279880f110272df643cee670aa976ae5ab9ec396;p=lgpl%2Fargeo-commons.git
diff --git a/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java b/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
index dab419794..41672a0e9 100644
--- a/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
+++ b/jcr/org.argeo.cms.jcr/src/org/argeo/cms/jcr/acr/JcrContent.java
@@ -12,6 +12,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
+import java.util.TreeSet;
import java.util.concurrent.ForkJoinPool;
import javax.jcr.Node;
@@ -27,11 +28,12 @@ import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.argeo.api.acr.Content;
-import org.argeo.api.acr.ContentUtils;
import org.argeo.api.acr.NamespaceUtils;
-import org.argeo.api.acr.spi.AbstractContent;
+import org.argeo.api.acr.spi.ContentProvider;
import org.argeo.api.acr.spi.ProvidedSession;
import org.argeo.api.cms.CmsConstants;
+import org.argeo.cms.acr.AbstractContent;
+import org.argeo.cms.acr.ContentUtils;
import org.argeo.jcr.Jcr;
import org.argeo.jcr.JcrException;
import org.argeo.jcr.JcrUtils;
@@ -41,13 +43,12 @@ public class JcrContent extends AbstractContent {
// private Node jcrNode;
private JcrContentProvider provider;
- private ProvidedSession session;
private String jcrWorkspace;
private String jcrPath;
protected JcrContent(ProvidedSession session, JcrContentProvider provider, String jcrWorkspace, String jcrPath) {
- this.session = session;
+ super(session);
this.provider = provider;
this.jcrWorkspace = jcrWorkspace;
this.jcrPath = jcrPath;
@@ -102,7 +103,7 @@ public class JcrContent extends AbstractContent {
public Node getJcrNode() {
try {
// TODO caching?
- return provider.getJcrSession(session, jcrWorkspace).getNode(jcrPath);
+ return provider.getJcrSession(getSession(), jcrWorkspace).getNode(jcrPath);
} catch (RepositoryException e) {
throw new JcrException("Cannot retrieve " + jcrPath + " from workspace " + jcrWorkspace, e);
}
@@ -164,7 +165,7 @@ public class JcrContent extends AbstractContent {
@Override
public Content next() {
- current = new JcrContent(session, provider, jcrWorkspace, Jcr.getPath(nodeIterator.nextNode()));
+ current = new JcrContent(getSession(), provider, jcrWorkspace, Jcr.getPath(nodeIterator.nextNode()));
return current;
}
@@ -179,7 +180,9 @@ public class JcrContent extends AbstractContent {
@Override
public Content getParent() {
- return new JcrContent(session, provider, jcrWorkspace, Jcr.getParentPath(getJcrNode()));
+ if (Jcr.isRoot(getJcrNode())) // root
+ return null;
+ return new JcrContent(getSession(), provider, jcrWorkspace, Jcr.getParentPath(getJcrNode()));
}
@Override
@@ -219,9 +222,18 @@ public class JcrContent extends AbstractContent {
}
+ boolean exists() {
+ try {
+ return provider.getJcrSession(getSession(), jcrWorkspace).itemExists(jcrPath);
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot check whether " + jcrPath + " exists", e);
+ }
+ }
+
/*
* ADAPTERS
*/
+ @SuppressWarnings("unchecked")
public A adapt(Class clss) {
if (Source.class.isAssignableFrom(clss)) {
// try {
@@ -229,7 +241,7 @@ public class JcrContent extends AbstractContent {
ForkJoinPool.commonPool().execute(() -> {
try (PipedOutputStream out = new PipedOutputStream(in)) {
- provider.getJcrSession(session, jcrWorkspace).exportDocumentView(jcrPath, out, true, false);
+ provider.getJcrSession(getSession(), jcrWorkspace).exportDocumentView(jcrPath, out, true, false);
out.flush();
} catch (IOException | RepositoryException e) {
throw new RuntimeException("Cannot export " + jcrPath + " in workspace " + jcrWorkspace, e);
@@ -245,6 +257,7 @@ public class JcrContent extends AbstractContent {
return super.adapt(clss);
}
+ @SuppressWarnings("unchecked")
@Override
public C open(Class clss) throws IOException, IllegalArgumentException {
if (InputStream.class.isAssignableFrom(clss)) {
@@ -260,31 +273,74 @@ public class JcrContent extends AbstractContent {
return super.open(clss);
}
-// class JcrKeyIterator implements Iterator {
-// private final PropertyIterator propertyIterator;
-//
-// protected JcrKeyIterator(PropertyIterator propertyIterator) {
-// this.propertyIterator = propertyIterator;
-// }
-//
-// @Override
-// public boolean hasNext() {
-// return propertyIterator.hasNext();
-// }
-//
-// @Override
-// public QName next() {
-// Property property = null;
-// try {
-// property = propertyIterator.nextProperty();
-// // TODO map standard property names
-// return NamespaceUtils.parsePrefixedName(provider, property.getName());
-// } catch (RepositoryException e) {
-// throw new JcrException("Cannot retrieve property " + property, null);
+ @Override
+ public ContentProvider getProvider() {
+ return provider;
+ }
+
+ @Override
+ public String getSessionLocalId() {
+ try {
+ return getJcrNode().getIdentifier();
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot get identifier for " + getJcrNode(), e);
+ }
+ }
+
+ /*
+ * TYPING
+ */
+ @Override
+ public List getTypes() {
+ try {
+// Node node = getJcrNode();
+// List res = new ArrayList<>();
+// res.add(nodeTypeToQName(node.getPrimaryNodeType()));
+// for (NodeType mixin : node.getMixinNodeTypes()) {
+// res.add(nodeTypeToQName(mixin));
+// }
+// return res;
+ Node context = getJcrNode();
+
+ List res = new ArrayList<>();
+ // primary node type
+ NodeType primaryType = context.getPrimaryNodeType();
+ res.add(nodeTypeToQName(primaryType));
+
+ Set secondaryTypes = new TreeSet<>(NamespaceUtils.QNAME_COMPARATOR);
+ for (NodeType mixinType : context.getMixinNodeTypes()) {
+ secondaryTypes.add(nodeTypeToQName(mixinType));
+ }
+ for (NodeType superType : primaryType.getDeclaredSupertypes()) {
+ secondaryTypes.add(nodeTypeToQName(superType));
+ }
+ // mixins
+ for (NodeType mixinType : context.getMixinNodeTypes()) {
+ for (NodeType superType : mixinType.getDeclaredSupertypes()) {
+ secondaryTypes.add(nodeTypeToQName(superType));
+ }
+ }
+// // entity type
+// if (context.isNodeType(EntityType.entity.get())) {
+// if (context.hasProperty(EntityNames.ENTITY_TYPE)) {
+// String entityTypeName = context.getProperty(EntityNames.ENTITY_TYPE).getString();
+// if (byType.containsKey(entityTypeName)) {
+// types.add(entityTypeName);
+// }
// }
// }
-//
-// }
+ res.addAll(secondaryTypes);
+ return res;
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot list node types from " + getJcrNode(), e);
+ }
+ }
+
+ private QName nodeTypeToQName(NodeType nodeType) {
+ String name = nodeType.getName();
+ return QName.valueOf(name);
+ }
+
/*
* STATIC UTLITIES
*/