From 705b111ed306ae7daefb7d02247a7e677aea1cbd Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Mon, 24 Oct 2022 08:35:59 +0200 Subject: [PATCH] Introduce JCR query part --- org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java | 17 +++++ .../cms/jcr/swt/JcrQueryTabularPart.java | 63 +++++++++++++++++++ .../cms/jcr/swt/PagedJcrQueryTabularPart.java | 58 +++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/JcrQueryTabularPart.java create mode 100644 swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/PagedJcrQueryTabularPart.java diff --git a/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java b/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java index 49b008d..2ff49e7 100644 --- a/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java +++ b/org.argeo.cms.jcr/src/org/argeo/jcr/Jcr.java @@ -609,6 +609,23 @@ public class Jcr { } } + /** + * Retrieve a {@link PropertyType#DATE} property as an {@link Instant}. + * + * @return the property value, or null 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 index 0000000..2ee35a2 --- /dev/null +++ b/swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/JcrQueryTabularPart.java @@ -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 { + 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 index 0000000..a463aac --- /dev/null +++ b/swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/jcr/swt/PagedJcrQueryTabularPart.java @@ -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 { + 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(); + } + +} -- 2.39.2