From: remillet Date: Thu, 25 Jan 2018 08:09:39 +0000 (-0800) Subject: DRYD-114: Added filter to Nuxeo queries to convert TIMESTAMP values to local server... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=7b853e028f83ddaaedaf4dfd3a4660ee814d846d;p=tmp%2Fjakarta-migration.git DRYD-114: Added filter to Nuxeo queries to convert TIMESTAMP values to local server time. Clients can provide TIMESTAMP values that are either server local or UTC. UTC values will be converter to local server time. Non-UTC values will be left unfiltered. --- diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionInterface.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionInterface.java index 228cf218b..1c45a4805 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionInterface.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionInterface.java @@ -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. diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionWrapper.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionWrapper.java index 2de822bdf..e573f9f63 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionWrapper.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/CoreSessionWrapper.java @@ -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); }