]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/utils/AkbJcrUtils.java
work on active environments
[gpl/argeo-slc.git] / runtime / org.argeo.slc.akb / src / main / java / org / argeo / slc / akb / utils / AkbJcrUtils.java
index b0c8353d810f9df4504652a18edc750577b4bb24..b7d0ba7df6ebf49729538e32e165ab70837c8045 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Map;
 import java.util.TreeMap;
 
 import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
 import javax.jcr.Repository;
@@ -18,10 +19,134 @@ import javax.jcr.query.RowIterator;
 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).