From b9c73f0d4552b8f740c84658d9cdc72be6efd65d Mon Sep 17 00:00:00 2001
From: anttil <anttil>
Date: Mon, 4 Jun 2012 08:16:34 +0000
Subject: [PATCH] BIS-37 / SP-46: Greater Than and Less Than Filtering with
 Timestamps Doesn't Work

SVN: 25518
---
 .../basic/AlternativesStringFilter.java       | 59 +++++++++++++++++++
 .../resultset/CachedResultSetManager.java     | 21 ++++++-
 2 files changed, 79 insertions(+), 1 deletion(-)

diff --git a/common/source/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilter.java b/common/source/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilter.java
index ff84a61b54d..98e697897b3 100644
--- a/common/source/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilter.java
+++ b/common/source/java/ch/systemsx/cisd/common/shared/basic/AlternativesStringFilter.java
@@ -212,6 +212,48 @@ public class AlternativesStringFilter
         }
     }
 
+    static class DateMatcher implements Matcher
+    {
+        private String filter;
+
+        private ComparisonKind comparisonKind;
+
+        DateMatcher(String filter, ComparisonKind comparison)
+        {
+            this.filter = filter;
+            this.comparisonKind = comparison;
+        }
+
+        @Override
+        public boolean matches(String value)
+        {
+            if (value == null)
+            {
+                return false;
+            }
+
+            if (comparisonKind == null || ComparisonKind.EQ.equals(this.comparisonKind))
+            {
+                return value.startsWith(filter);
+            } else
+            {
+                switch (this.comparisonKind)
+                {
+                    case LT:
+                        return value.compareTo(this.filter) < 0;
+                    case LE:
+                        return value.compareTo(this.filter) <= 0;
+                    case GT:
+                        return value.compareTo(this.filter) > 0;
+                    case GE:
+                        return value.compareTo(this.filter) >= 0;
+                    default:
+                        return false;
+                }
+            }
+        }
+    }
+
     static class NumericMatcher implements Matcher
     {
         protected final double filterValue;
@@ -316,6 +358,23 @@ public class AlternativesStringFilter
         }
     }
 
+    public void setDateFilterValue(String value)
+    {
+        alternatives.clear();
+
+        String filterValue = value;
+        ComparisonKind comparisonKindOrNull = null;
+        if (value != null && value.length() >= 2 && "<>=".indexOf(value.charAt(0)) > -1)
+        {
+            int operatorLength = (value.charAt(1) == '=') ? 2 : 1;
+            String operator = value.substring(0, operatorLength);
+            filterValue = value.substring(operatorLength);
+            comparisonKindOrNull = tryGetComparisonKind(operator);
+        }
+        Matcher matcher = new DateMatcher(filterValue, comparisonKindOrNull);
+        alternatives.add(matcher);
+    }
+
     private Matcher tryGetNumericMatcher(String s)
     {
         if (s.length() < 2)
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CachedResultSetManager.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CachedResultSetManager.java
index 5048ff8a934..abc775d0bdd 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CachedResultSetManager.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CachedResultSetManager.java
@@ -17,9 +17,12 @@
 package ch.systemsx.cisd.openbis.generic.client.web.server.resultset;
 
 import java.io.Serializable;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -62,6 +65,7 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.GridRowModel;
 import ch.systemsx.cisd.openbis.generic.shared.basic.IColumnDefinition;
 import ch.systemsx.cisd.openbis.generic.shared.basic.PrimitiveValue;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataTypeCode;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DateTableCell;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.GridCustomColumn;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SortInfo;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.SortInfo.SortDir;
@@ -96,6 +100,14 @@ public final class CachedResultSetManager<K> implements IResultSetManager<K>, Se
             final GridRowModel<T> row)
     {
         Comparable<?> value = definition.tryGetComparableValue(row);
+
+        if (value != null && value instanceof DateTableCell)
+        {
+            final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+            Date d = ((DateTableCell) value).getDateTime();
+            return df.format(d);
+        }
+
         return value == null ? "" : value.toString();
     }
 
@@ -509,7 +521,14 @@ public final class CachedResultSetManager<K> implements IResultSetManager<K>, Se
         {
             final AlternativesStringFilter result = new AlternativesStringFilter();
             final String pattern = gridFilterInfo.tryGetFilterPattern().toLowerCase();
-            result.setFilterValue(pattern);
+
+            if (DataTypeCode.TIMESTAMP.equals(gridFilterInfo.getFilteredField().tryToGetDataType()))
+            {
+                result.setDateFilterValue(pattern);
+            } else
+            {
+                result.setFilterValue(pattern);
+            }
             return result;
         }
 
-- 
GitLab