]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-114: Added filter to Nuxeo queries to convert TIMESTAMP values to local server...
authorremillet <remillet@yahoo.com>
Thu, 25 Jan 2018 08:09:39 +0000 (00:09 -0800)
committerremillet <remillet@yahoo.com>
Thu, 25 Jan 2018 08:09:39 +0000 (00:09 -0800)
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionInterface.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionWrapper.java

index 228cf218b6043e09e0821d20279df21d10105db3..1c45a4805a5dee241afe25068a9b22fe19637ddf 100644 (file)
@@ -2,6 +2,7 @@ package org.collectionspace.services.nuxeo.client.java;
 
 import java.security.Principal;
 
+import org.collectionspace.services.common.document.DocumentException;
 import org.nuxeo.ecm.core.api.ClientException;
 import org.nuxeo.ecm.core.api.CoreSession;
 import org.nuxeo.ecm.core.api.DocumentModel;
@@ -59,12 +60,12 @@ public interface CoreSessionInterface {
     public Principal getPrincipal();
 
     public IterableQueryResult queryAndFetch(String query, String queryType,
-            Object... params) throws ClientException;
+            Object... params) throws ClientException, DocumentException;
 
     public DocumentModelList query(String query, Filter filter, long limit,
-            long offset, boolean countTotal) throws ClientException;
+            long offset, boolean countTotal) throws ClientException, DocumentException;
 
-    public DocumentModelList query(String query) throws ClientException;
+    public DocumentModelList query(String query) throws ClientException, DocumentException;
 
     /**
      * Executes the given NXQL query an returns the result.
@@ -73,8 +74,9 @@ public interface CoreSessionInterface {
      * @param max number of document to retrieve
      * @return the query result
      * @throws ClientException
+     * @throws DocumentException 
      */
-    public DocumentModelList query(String query, int max) throws ClientException;
+    public DocumentModelList query(String query, int max) throws ClientException, DocumentException;
     
     /**
      * Executes the given NXQL query and returns the result that matches the
@@ -83,9 +85,10 @@ public interface CoreSessionInterface {
      * @param query the query to execute
      * @param filter the filter to apply to result
      * @return the query result
+     * @throws DocumentException 
      * @throws ClientException
      */
-    public DocumentModelList query(String query, LifeCycleFilter workflowStateFilter);
+    public DocumentModelList query(String query, LifeCycleFilter workflowStateFilter) throws DocumentException;
 
     /**
      * Gets a document model given its reference.
index 2de822bdfafb66250fb3810183fea5815d26279f..e573f9f638e4b10768119c3e60a44834c89a1098 100644 (file)
@@ -1,7 +1,19 @@
 package org.collectionspace.services.nuxeo.client.java;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 import java.security.Principal;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
+import org.collectionspace.services.common.document.DocumentException;
 import org.nuxeo.ecm.core.api.ClientException;
 import org.nuxeo.ecm.core.api.DocumentModel;
 import org.nuxeo.ecm.core.api.DocumentModelList;
@@ -111,35 +123,85 @@ public class CoreSessionWrapper implements CoreSessionInterface {
        public Principal getPrincipal() {
                return repoSession.getPrincipal();
        }
+       
+       private String toLocalTimestamp(String utcTime, boolean base64Encoded) throws DocumentException {
+               String result = null;
+
+               try {
+                       if (base64Encoded == true) {
+                               utcTime = URLDecoder.decode(utcTime, java.nio.charset.StandardCharsets.UTF_8.name());
+                       }
+                       LocalDateTime localTime;
+                       try {
+                               Instant instant = Instant.parse(utcTime);
+                               ZonedDateTime localInstant = instant.atZone(ZoneId.systemDefault()); // DateTimeFormatter.ISO_LOCAL_DATE_TIME
+                               localTime = localInstant.toLocalDateTime();
+                       } catch (DateTimeParseException e) {
+                               localTime = LocalDateTime.parse(utcTime, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
+                       }
+                       result = localTime.toString();
+                       if (base64Encoded == true) {
+                               result = URLEncoder.encode(result, java.nio.charset.StandardCharsets.UTF_8.name());
+                       }
+               } catch (UnsupportedEncodingException e) {
+                       throw new DocumentException(e);
+               }
+               
+               return result;
+       }
+       
+       private String localizeTimestamps(String query) throws DocumentException {
+               String result = query;
+               
+               if (query.contains("TIMESTAMP")) {
+                       StringBuffer stringBuffer = new StringBuffer();
+                       Pattern pattern = Pattern.compile("\\+TIMESTAMP\\+%22(.+?)%22");
+                       Matcher matcher = pattern.matcher(query);
+                       while (matcher.find()) {
+                               String time = matcher.group(1);
+                               String localizedTime = toLocalTimestamp(time, true);
+                               matcher.appendReplacement(stringBuffer, String.format("+TIMESTAMP+%%22%s%%22", localizedTime));
+                       }
+                       matcher.appendTail(stringBuffer);
+                       result = stringBuffer.toString();
+               }
+               
+               return result;
+       }
 
        @Override
        public IterableQueryResult queryAndFetch(String query, String queryType,
-            Object... params) throws ClientException {
+            Object... params) throws ClientException, DocumentException {
+               query = localizeTimestamps(query);
                logQuery(query, queryType);
                return repoSession.queryAndFetch(query, queryType, params);
        }
 
        @Override
        public DocumentModelList query(String query, Filter filter, long limit,
-            long offset, boolean countTotal) throws ClientException {
+            long offset, boolean countTotal) throws ClientException, DocumentException {
+               query = localizeTimestamps(query);
                logQuery(query, filter, limit, offset, countTotal);
                return repoSession.query(query, filter, limit, offset, countTotal);
        }
 
        @Override
-    public DocumentModelList query(String query, int max) throws ClientException {
+    public DocumentModelList query(String query, int max) throws ClientException, DocumentException {
+               query = localizeTimestamps(query);
                logQuery(query);
        return repoSession.query(query, max);
     }
     
        @Override
-       public DocumentModelList query(String query) throws ClientException {
+       public DocumentModelList query(String query) throws ClientException, DocumentException {
+               query = localizeTimestamps(query);
                logQuery(query);
                return repoSession.query(query);
        }
        
        @Override
-       public DocumentModelList query(String query, LifeCycleFilter workflowStateFilter) {
+       public DocumentModelList query(String query, LifeCycleFilter workflowStateFilter) throws DocumentException {
+               query = localizeTimestamps(query);
                return repoSession.query(query, workflowStateFilter);
        }