X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.akb%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fakb%2Futils%2FAkbJcrUtils.java;h=b7d0ba7df6ebf49729538e32e165ab70837c8045;hb=44b4c78e6a082d51d0410c3995b5edf353d417b9;hp=b0c8353d810f9df4504652a18edc750577b4bb24;hpb=93ad856792ccb84b534b2226e602c6b652799933;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/utils/AkbJcrUtils.java b/runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/utils/AkbJcrUtils.java index b0c8353d8..b7d0ba7df 100644 --- a/runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/utils/AkbJcrUtils.java +++ b/runtime/org.argeo.slc.akb/src/main/java/org/argeo/slc/akb/utils/AkbJcrUtils.java @@ -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 getDefinedTemplate(Session session) { + try { + if (session.nodeExists(AkbNames.AKB_TEMPLATES_BASE_PATH)) { + NodeIterator ni = session.getNode( + AkbNames.AKB_TEMPLATES_BASE_PATH).getNodes(); + List templates = new ArrayList(); + 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).