Change typologies structure.
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 6 Nov 2020 07:35:15 +0000 (08:35 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 6 Nov 2020 07:35:15 +0000 (08:35 +0100)
knowledge/org.argeo.support.odk/OSGI-INF/odkFormListServlet.xml
knowledge/org.argeo.support.odk/src/org/argeo/support/odk/OdkUtils.java [new file with mode: 0644]
org.argeo.entity.api/src/org/argeo/entity/EntityNames.java
org.argeo.entity.api/src/org/argeo/entity/entity.cnd
org.argeo.suite.core/src/org/argeo/suite/core/SuiteMaintenanceService.java

index e290933fd9bfd91701dc9ef685a5c6262ae1c2fe..937b474a198f9e53d5a278c2a3324db671cfb68a 100644 (file)
@@ -4,7 +4,7 @@
    <service>
       <provide interface="javax.servlet.Servlet"/>
    </service>
-   <property name="osgi.http.whiteboard.servlet.pattern" type="String" value="/formList"/>
+   <property name="osgi.http.whiteboard.servlet.pattern" type="String" value="/formList/*"/>
    <property name="osgi.http.whiteboard.context.select" type="String" value="(osgi.http.whiteboard.context.name=odkServletContext)"/>
    <reference bind="addForm" cardinality="0..n" interface="org.argeo.support.odk.OdkForm" name="OdkForm" policy="dynamic" unbind="removeForm"/>
    <reference bind="setRepository" cardinality="1..1" interface="javax.jcr.Repository" name="Repository" policy="static" target="(cn=odk)"/>
diff --git a/knowledge/org.argeo.support.odk/src/org/argeo/support/odk/OdkUtils.java b/knowledge/org.argeo.support.odk/src/org/argeo/support/odk/OdkUtils.java
new file mode 100644 (file)
index 0000000..4c106a2
--- /dev/null
@@ -0,0 +1,78 @@
+package org.argeo.support.odk;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.jcr.ImportUUIDBehavior;
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.entity.EntityType;
+import org.argeo.jcr.JcrUtils;
+import org.argeo.jcr.JcrxApi;
+import org.argeo.util.DigestUtils;
+
+/** Utilities around ODK. */
+public class OdkUtils {
+       private final static Log log = LogFactory.getLog(OdkUtils.class);
+
+       public static void loadOdkForm(Node formBase, String name, InputStream in) throws RepositoryException, IOException {
+               if (!formBase.isNodeType(EntityType.formSet.get()))
+                       throw new IllegalArgumentException(
+                                       "Parent path " + formBase + " must be of type " + EntityType.formSet.get());
+               Node form = JcrUtils.getOrAdd(formBase, name, OrxListType.xform.get(), NodeType.MIX_SIMPLE_VERSIONABLE);
+
+               String previousCsum = JcrxApi.getChecksum(form, JcrxApi.MD5);
+
+               Session s = formBase.getSession();
+//             String res = "/odk/apafSession.odk.xml";
+//             try (InputStream in = getClass().getClassLoader().getResourceAsStream(res)) {
+               s.importXML(form.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
+//             }
+
+               if (previousCsum == null)// save before checksuming
+                       s.save();
+               String newCsum;
+               try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+                       s.exportDocumentView(form.getPath() + "/" + OdkNames.H_HTML, out, true, false);
+                       newCsum = DigestUtils.digest(DigestUtils.MD5, out.toByteArray());
+               }
+               if (previousCsum == null) {
+                       JcrxApi.addChecksum(form, newCsum);
+                       JcrUtils.updateLastModified(form);
+                       s.save();
+                       s.getWorkspace().getVersionManager().checkpoint(form.getPath());
+                       if (log.isDebugEnabled())
+                               log.debug("New form " + form);
+               } else {
+                       if (newCsum.equals(previousCsum)) {
+                               // discard
+                               s.refresh(false);
+                               if (log.isDebugEnabled())
+                                       log.debug("Unmodified form " + form);
+                               return;
+                       } else {
+                               JcrxApi.addChecksum(form, newCsum);
+                               JcrUtils.updateLastModified(form);
+                               s.save();
+                               s.getWorkspace().getVersionManager().checkpoint(form.getPath());
+                               if (log.isDebugEnabled()) {
+                                       log.debug("Updated form " + form);
+                                       log.debug("Previous csum " + previousCsum);
+                                       log.debug("New csum " + newCsum);
+                               }
+                       }
+               }
+       }
+
+       /** Singleton. */
+       private OdkUtils() {
+
+       }
+
+}
index a1081cc1edfcb4464ad25eccb53de2b495f60366..3cd4a73ee1f72159be295a162e233ff9617d7695 100644 (file)
@@ -4,13 +4,16 @@ import org.argeo.naming.LdapAttrs;
 
 /** Constants used to name entity structures. */
 public interface EntityNames {
+       @Deprecated
        final String FORM_BASE = "form";
        final String SUBMISSIONS_BASE = "submissions";
-       final String TERM_BASE = "term";
+       @Deprecated
+       final String TERM = "term";
+       final String NAME = "name";
 
        final String ENTITY_DEFINITIONS_PATH = "/entity";
        @Deprecated
-       final String TYPOLOGIES_PATH = "/" + TERM_BASE;
+       final String TYPOLOGIES_PATH = "/" + TERM;
        /** Administrative units. */
        final String ADM = "adm";
 
index 16ff3b79a86956807cd97d7ece329b71bb8cb640..46b3f07d186322dd5f19500b8e504732149ae789 100644 (file)
@@ -36,11 +36,13 @@ orderable
 
 [entity:term]
 orderable
-+ * (entity:term) = entity:term *
+- name (NAME) m
+- * (*)
++ term (entity:term) = entity:term *
 
 [entity:terms] > mix:referenceable
 orderable
-+ * (entity:term) = entity:term *
++ term (entity:term) = entity:term *
 
 //
 // FORM
@@ -51,7 +53,7 @@ mixin
 [entity:formSubmission]
 mixin
 
-[entity:formSet]
+[entity:formSet] > mix:title
 mixin
 
 // LDAP-LIKE ENTITIES
index 44d218c4764d89ec1c935993e5c2abc4059c2152..91f20433c5379fc8c24fe7084a48fe7c246df5d0 100644 (file)
@@ -2,12 +2,9 @@ package org.argeo.suite.core;
 
 import java.io.IOException;
 
-import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
-import org.argeo.entity.EntityNames;
-import org.argeo.entity.EntityType;
 import org.argeo.maintenance.AbstractMaintenanceService;
 
 /** Initialises an Argeo Suite backend. */
@@ -16,13 +13,13 @@ public class SuiteMaintenanceService extends AbstractMaintenanceService {
        @Override
        public boolean prepareJcrTree(Session adminSession) throws RepositoryException, IOException {
                boolean modified = false;
-               Node rootNode = adminSession.getRootNode();
-               if (!rootNode.hasNode(EntityNames.TERM_BASE)) {
-                       rootNode.addNode(EntityNames.TERM_BASE, EntityType.typologies.get());
-                       modified = true;
-               }
-               if (modified)
-                       adminSession.save();
+//             Node rootNode = adminSession.getRootNode();
+//             if (!rootNode.hasNode(EntityNames.TERM)) {
+//                     rootNode.addNode(EntityNames.TERM, EntityType.typologies.get());
+//                     modified = true;
+//             }
+//             if (modified)
+//                     adminSession.save();
                return modified;
        }