import java.util.TreeMap;
import javax.jcr.Node;
+import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import org.argeo.jcr.JcrUtils;
import org.argeo.jcr.PropertyDiff;
import org.argeo.slc.akb.AkbException;
+import org.argeo.slc.akb.AkbNames;
+import org.argeo.slc.akb.AkbTypes;
/** Some static utils methods that might be factorized in a near future */
public class AkbJcrUtils {
+ // /////////////////////////
+ // SPECIFIC METHOS
+ /**
+ * Returns the list of environment templates that are visible for the
+ * current user.
+ */
+ public static List<Node> getDefinedTemplate(Session session) {
+ try {
+ if (session.nodeExists(AkbNames.AKB_TEMPLATES_BASE_PATH)) {
+ NodeIterator ni = session.getNode(
+ AkbNames.AKB_TEMPLATES_BASE_PATH).getNodes();
+ List<Node> templates = new ArrayList<Node>();
+ while (ni.hasNext()) {
+ Node currN = ni.nextNode();
+ if (currN.isNodeType(AkbTypes.AKB_ENV_TEMPLATE))
+ templates.add(currN);
+ }
+ return templates;
+ }
+ return null;
+ } catch (RepositoryException re) {
+ throw new AkbException("Unable to list templates", re);
+ }
+ }
+
+ /**
+ * Returns a template given it's name
+ */
+ public static Node getTemplateByName(Session session, String name) {
+ try {
+ if (name == null)
+ return null;
+ if (session.nodeExists(AkbNames.AKB_TEMPLATES_BASE_PATH)) {
+ NodeIterator ni = session.getNode(
+ AkbNames.AKB_TEMPLATES_BASE_PATH).getNodes();
+ while (ni.hasNext()) {
+ Node currN = ni.nextNode();
+ if (name.equals(AkbJcrUtils.get(currN, Property.JCR_TITLE)))
+ return currN;
+ }
+ }
+ return null;
+ } catch (RepositoryException re) {
+ throw new AkbException("Unable to list templates", re);
+ }
+ }
+
+ /**
+ * Return the type of alias that must be used given current item type
+ */
+ public static String getAliasTypeForNode(Node itemTemplate) {
+ try {
+ if (itemTemplate.isNodeType(AkbTypes.AKB_JDBC_QUERY))
+ return AkbTypes.AKB_JDBC_CONNECTOR;
+ else if (itemTemplate.isNodeType(AkbTypes.AKB_SSH_COMMAND)
+ || itemTemplate.isNodeType(AkbTypes.AKB_SSH_FILE))
+ return AkbTypes.AKB_SSH_CONNECTOR;
+ else
+ throw new AkbException("No connector type define for node "
+ + itemTemplate);
+ } catch (RepositoryException re) {
+ throw new AkbException("Unable to login", re);
+ }
+ }
+
+ /**
+ * Return current template depending on the passed node
+ */
+ public static Node getCurrentTemplate(Node akbNode) {
+ try {
+ if (akbNode.getDepth() == 0)
+ // no base path for root node
+ return null;
+ Node parNode = akbNode.getParent();
+
+ while (parNode != null)
+ if (akbNode.isNodeType(AkbTypes.AKB_ENV_TEMPLATE))
+ return akbNode;
+ else if (parNode.getDepth() == 0)
+ // we found not fitting node
+ return null;
+ else {
+ akbNode = parNode;
+ parNode = parNode.getParent();
+ }
+ return null;
+ } catch (RepositoryException re) {
+ throw new AkbException("Unable to find template for node "
+ + akbNode, re);
+ }
+ }
+
+ /**
+ * Return the current env base path
+ */
+ public static String getCurrentEnvBasePath(Node akbNode) {
+ try {
+ if (akbNode.getDepth() == 0)
+ // no base path for root node
+ return null;
+
+ Node parNode = akbNode.getParent();
+
+ while (parNode != null)
+ if (akbNode.isNodeType(AkbTypes.AKB_ENV)
+ || akbNode.isNodeType(AkbTypes.AKB_ENV_TEMPLATE))
+ return akbNode.getPath();
+ else if (parNode.getDepth() == 0)
+ // we found not fitting node
+ return null;
+ else {
+ akbNode = parNode;
+ parNode = parNode.getParent();
+ }
+ return null;
+ } catch (RepositoryException re) {
+ throw new AkbException("Unable to login", re);
+ }
+ }
+
+ // //////////////////////////////////
+ // METHODS THAT CAN BE FACTORIZED
/**
* Call {@link Repository#login()} without exceptions (useful in super
* constructors and dependency injection).