Introduce JCR query part
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 24 Oct 2022 06:35:59 +0000 (08:35 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 24 Oct 2022 06:35:59 +0000 (08:35 +0200)
org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java
swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/JcrQueryTabularPart.java [new file with mode: 0644]
swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/PagedJcrQueryTabularPart.java [new file with mode: 0644]

index 49b008d70751e24e4f362c9e5e2b9caab66f06cc..2ff49e7ce92c988902a54d613acd6e39ef39ea08 100644 (file)
@@ -609,6 +609,23 @@ public class Jcr {
                }
        }
 
+       /**
+        * Retrieve a {@link PropertyType#DATE} property as an {@link Instant}.
+        * 
+        * @return the property value, or <code>null</code> if not found.
+        */
+       public static Instant getAsInstant(Node node, String property) {
+               try {
+                       if (!node.hasProperty(property))
+                               return null;
+                       Calendar calendar = node.getProperty(property).getDate();
+                       return calendar.getTime().toInstant();
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get property " + property + " of " + node + " as an instant.", e);
+               }
+
+       }
+
        /**
         * Get a multiple property as a list, doing a best effort to cast it as the
         * target list.
diff --git a/swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/JcrQueryTabularPart.java b/swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/JcrQueryTabularPart.java
new file mode 100644 (file)
index 0000000..2ee35a2
--- /dev/null
@@ -0,0 +1,63 @@
+package org.argeo.cms.jcr.swt;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.query.Query;
+
+import org.argeo.cms.ux.widgets.AbstractTabularPart;
+import org.argeo.jcr.JcrException;
+
+public class JcrQueryTabularPart extends AbstractTabularPart<Query, Node> {
+       private int cursor = 0;
+       private NodeIterator nit = null;
+
+       @Override
+       public int getItemCount() {
+               return (int) nit.getSize();
+       }
+
+       @Override
+       public Node getData(int row) {
+               // System.out.println("Row " + row);
+               Node res;
+               if (row == cursor) {
+                       res = nit.nextNode();
+                       cursor++;
+               } else if (row > cursor) {
+                       nit.skip(row - cursor);
+                       cursor = row;
+                       res = nit.nextNode();
+                       cursor++;
+               } else if (row < cursor) {
+                       try {
+                               nit = getInput().execute().getNodes();
+                       } catch (RepositoryException e) {
+                               throw new JcrException("Cannot refresh query", e);
+                       }
+                       notifyItemCountChange();
+                       nit.skip(row);
+                       cursor = row;
+                       res = nit.nextNode();
+                       cursor++;
+               } else {
+                       throw new IllegalStateException("Cursor is " + cursor + " and row is " + row);
+               }
+               return res;
+       }
+
+       @Override
+       public void refresh() {
+               cursor = 0;
+               try {
+                       nit = getInput().execute().getNodes();
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot refresh query", e);
+               }
+               super.refresh();
+       }
+
+       public long getQuerySize() {
+               return nit.getSize();
+       }
+}
diff --git a/swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/PagedJcrQueryTabularPart.java b/swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/PagedJcrQueryTabularPart.java
new file mode 100644 (file)
index 0000000..a463aac
--- /dev/null
@@ -0,0 +1,58 @@
+package org.argeo.cms.jcr.swt;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.query.Query;
+
+import org.argeo.cms.ux.widgets.AbstractTabularPart;
+import org.argeo.jcr.JcrException;
+
+public class PagedJcrQueryTabularPart extends AbstractTabularPart<Query, Node> {
+       private int pageSize = 100;
+       private int cursor = 0;
+       private int nextUpperBound = pageSize;
+       private NodeIterator nit = null;
+
+       @Override
+       public int getItemCount() {
+               return (nextUpperBound - pageSize) + (int) nit.getSize();
+       }
+
+       @Override
+       public Node getData(int row) {
+               // System.out.println("Row " + row);
+               if (row == cursor) {
+                       cursor++;
+                       Node res = nit.nextNode();
+                       if (cursor == nextUpperBound) {
+                               getInput().setOffset(cursor);
+                               nextUpperBound = cursor + pageSize;
+                               try {
+                                       nit = getInput().execute().getNodes();
+                               } catch (RepositoryException e) {
+                                       throw new JcrException("Cannot refresh query", e);
+                               }
+                               notifyItemCountChange();
+                       }
+                       return res;
+               } else {
+                       return null;
+               }
+       }
+
+       @Override
+       public void refresh() {
+               getInput().setOffset(0);
+               getInput().setLimit(pageSize);
+               cursor = 0;
+               nextUpperBound = pageSize;
+               try {
+                       nit = getInput().execute().getNodes();
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot refresh query", e);
+               }
+               super.refresh();
+       }
+
+}