package org.argeo.cms.jcr.acr;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
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;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeType;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.argeo.api.acr.Content;
import org.argeo.api.acr.NamespaceUtils;
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;
/** A JCR {@link Node} accessed as {@link Content}. */
public class JcrContent extends AbstractContent {
// private Node jcrNode;
private JcrContentProvider provider;
private String jcrWorkspace;
private String jcrPath;
protected JcrContent(ProvidedSession session, JcrContentProvider provider, String jcrWorkspace, String jcrPath) {
super(session);
this.provider = provider;
this.jcrWorkspace = jcrWorkspace;
this.jcrPath = jcrPath;
}
@Override
public QName getName() {
String name = Jcr.getName(getJcrNode());
if (name.equals("")) {// root
String mountPath = provider.getMountPath();
name = ContentUtils.getParentPath(mountPath)[1];
// name = Jcr.getWorkspaceName(getJcrNode());
}
return NamespaceUtils.parsePrefixedName(provider, name);
}
@SuppressWarnings("unchecked")
@Override
public Optional get(QName key, Class clss) {
if (isDefaultAttrTypeRequested(clss)) {
return Optional.of((A) get(getJcrNode(), key.toString()));
}
return Optional.of((A) Jcr.get(getJcrNode(), key.toString()));
}
@Override
public Iterator iterator() {
try {
return new JcrContentIterator(getJcrNode().getNodes());
} catch (RepositoryException e) {
throw new JcrException("Cannot list children of " + getJcrNode(), e);
}
}
@Override
protected Iterable keys() {
try {
Set keys = new HashSet<>();
for (PropertyIterator propertyIterator = getJcrNode().getProperties(); propertyIterator.hasNext();) {
Property property = propertyIterator.nextProperty();
// TODO convert standard names
// TODO skip technical properties
QName name = NamespaceUtils.parsePrefixedName(provider, property.getName());
keys.add(name);
}
return keys;
} catch (RepositoryException e) {
throw new JcrException("Cannot list properties of " + getJcrNode(), e);
}
}
public Node getJcrNode() {
try {
// TODO caching?
return provider.getJcrSession(getSession(), jcrWorkspace).getNode(jcrPath);
} catch (RepositoryException e) {
throw new JcrException("Cannot retrieve " + jcrPath + " from workspace " + jcrWorkspace, e);
}
}
/** Cast to a standard Java object. */
static Object get(Node node, String property) {
try {
Property p = node.getProperty(property);
if (p.isMultiple()) {
Value[] values = p.getValues();
List