From 54c6a2c351fcd3ebce4ff91253397069ea6845f2 Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Tue, 10 Nov 2015 15:21:15 +0000
Subject: [PATCH] SSDM-2636 : V3 AS API - sort search results by property
 values - move comparators from openbis_api to openbis project as they are
 executed only on the server-side and are not part of the public API

SVN: 35028
---
 .../method/AbstractSearchMethodExecutor.java  |  2 +-
 .../v3/helper/sort}/AbstractComparator.java   |  2 +-
 .../sort}/AbstractStringComparator.java       |  2 +-
 .../api/v3/helper/sort}/CodeComparator.java   |  4 +-
 .../api/v3/helper/sort/ComparatorFactory.java | 58 +++++++++++++++++++
 .../helper/sort/EntityComparatorFactory.java  | 58 +++++++++++++++++++
 ...EntityWithPropertiesComparatorFactory.java | 53 +++++++++++++++++
 .../sort}/ModificationDateComparator.java     |  4 +-
 .../v3/helper/sort}/PropertyComparator.java   |  4 +-
 .../sort}/RegistrationDateComparator.java     |  4 +-
 .../api/v3/helper}/sort/SortAndPage.java      |  7 ++-
 .../v3/helper/tag/TagComparatorFactory.java   | 55 ++++++++++++++++++
 .../api/v3/helper}/sort/SortAndPageTest.java  |  4 +-
 .../fetchoptions/sort/EntitySortOptions.java  | 32 ++--------
 .../sort/EntityWithPropertiesSortOptions.java | 18 +-----
 .../v3/dto/fetchoptions/sort/SortOptions.java |  6 --
 .../dto/fetchoptions/tag/TagSortOptions.java  | 25 ++------
 17 files changed, 251 insertions(+), 87 deletions(-)
 rename {openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator => openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort}/AbstractComparator.java (94%)
 rename {openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator => openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort}/AbstractStringComparator.java (92%)
 rename {openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator => openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort}/CodeComparator.java (87%)
 create mode 100644 openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/ComparatorFactory.java
 create mode 100644 openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/EntityComparatorFactory.java
 create mode 100644 openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/EntityWithPropertiesComparatorFactory.java
 rename {openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator => openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort}/ModificationDateComparator.java (86%)
 rename {openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator => openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort}/PropertyComparator.java (88%)
 rename {openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator => openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort}/RegistrationDateComparator.java (86%)
 rename {openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions => openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper}/sort/SortAndPage.java (97%)
 create mode 100644 openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/tag/TagComparatorFactory.java
 rename {openbis_api/sourceTest/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions => openbis/sourceTest/java/ch/ethz/sis/openbis/generic/server/api/v3/helper}/sort/SortAndPageTest.java (98%)

diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/method/AbstractSearchMethodExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/method/AbstractSearchMethodExecutor.java
index 170ca882d58..3e029a2c2da 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/method/AbstractSearchMethodExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/method/AbstractSearchMethodExecutor.java
@@ -31,11 +31,11 @@ import ch.ethz.sis.openbis.generic.server.api.v3.cache.SearchCacheEntry;
 import ch.ethz.sis.openbis.generic.server.api.v3.cache.SearchCacheKey;
 import ch.ethz.sis.openbis.generic.server.api.v3.executor.IOperationContext;
 import ch.ethz.sis.openbis.generic.server.api.v3.executor.common.ISearchObjectExecutor;
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.sort.SortAndPage;
 import ch.ethz.sis.openbis.generic.server.api.v3.translator.ITranslator;
 import ch.ethz.sis.openbis.generic.server.api.v3.translator.TranslationContext;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.CacheMode;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.FetchOptions;
-import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.SortAndPage;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.search.AbstractObjectSearchCriteria;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.search.SearchResult;
 import ch.systemsx.cisd.common.logging.LogCategory;
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/AbstractComparator.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/AbstractComparator.java
similarity index 94%
rename from openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/AbstractComparator.java
rename to openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/AbstractComparator.java
index 23a9b7ea079..6cbd8d169ac 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/AbstractComparator.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/AbstractComparator.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator;
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
 
 import java.util.Comparator;
 
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/AbstractStringComparator.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/AbstractStringComparator.java
similarity index 92%
rename from openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/AbstractStringComparator.java
rename to openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/AbstractStringComparator.java
index 6b2f2d5cf1d..a247754147f 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/AbstractStringComparator.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/AbstractStringComparator.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator;
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
 
 import ch.systemsx.cisd.common.collection.AlphanumComparator;
 
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/CodeComparator.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/CodeComparator.java
similarity index 87%
rename from openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/CodeComparator.java
rename to openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/CodeComparator.java
index 2791277619e..1980bc1bab8 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/CodeComparator.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/CodeComparator.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator;
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
 
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.ICodeHolder;
 
@@ -24,8 +24,6 @@ import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.ICodeHold
 public class CodeComparator<OBJECT extends ICodeHolder> extends AbstractStringComparator<OBJECT>
 {
 
-    public static final String CODE = "CODE";
-
     @Override
     protected String getValue(ICodeHolder o)
     {
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/ComparatorFactory.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/ComparatorFactory.java
new file mode 100644
index 00000000000..492d1211a87
--- /dev/null
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/ComparatorFactory.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2015 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
+
+import java.util.Comparator;
+import java.util.LinkedList;
+import java.util.List;
+
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.tag.TagComparatorFactory;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.SortOptions;
+
+/**
+ * @author pkupczyk
+ */
+@SuppressWarnings("rawtypes")
+public abstract class ComparatorFactory
+{
+
+    private static List<ComparatorFactory> factories = new LinkedList<ComparatorFactory>();
+
+    static
+    {
+        factories.add(new TagComparatorFactory());
+        factories.add(new EntityWithPropertiesComparatorFactory());
+        factories.add(new EntityComparatorFactory());
+    }
+
+    public abstract boolean accepts(SortOptions<?> sortOptions);
+
+    public abstract Comparator getComparator(String field);
+
+    public static ComparatorFactory getInstance(SortOptions<?> sortOptions)
+    {
+        for (ComparatorFactory factory : factories)
+        {
+            if (factory.accepts(sortOptions))
+            {
+                return factory;
+            }
+        }
+
+        throw new IllegalArgumentException("Comparator factory for sort options " + sortOptions.getClass() + " not found");
+    }
+}
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/EntityComparatorFactory.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/EntityComparatorFactory.java
new file mode 100644
index 00000000000..b2704aafd6a
--- /dev/null
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/EntityComparatorFactory.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2015 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
+
+import java.util.Comparator;
+
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.ICodeHolder;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IModificationDateHolder;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IRegistrationDateHolder;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.EntitySortOptions;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.SortOptions;
+
+/**
+ * @author pkupczyk
+ */
+public class EntityComparatorFactory<OBJECT extends ICodeHolder & IRegistrationDateHolder & IModificationDateHolder> extends
+        ComparatorFactory
+{
+
+    @Override
+    public boolean accepts(SortOptions<?> sortOptions)
+    {
+        return sortOptions instanceof EntitySortOptions;
+    }
+
+    @Override
+    public Comparator<OBJECT> getComparator(String field)
+    {
+        if (EntitySortOptions.CODE.equals(field))
+        {
+            return new CodeComparator<OBJECT>();
+        } else if (EntitySortOptions.REGISTRATION_DATE.equals(field))
+        {
+            return new RegistrationDateComparator<OBJECT>();
+        } else if (EntitySortOptions.MODIFICATION_DATE.equals(field))
+        {
+            return new ModificationDateComparator<OBJECT>();
+        } else
+        {
+            return null;
+        }
+    }
+
+}
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/EntityWithPropertiesComparatorFactory.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/EntityWithPropertiesComparatorFactory.java
new file mode 100644
index 00000000000..104e10fdf90
--- /dev/null
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/EntityWithPropertiesComparatorFactory.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2015 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
+
+import java.util.Comparator;
+
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.ICodeHolder;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IModificationDateHolder;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IPropertiesHolder;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IRegistrationDateHolder;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.EntityWithPropertiesSortOptions;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.SortOptions;
+
+/**
+ * @author pkupczyk
+ */
+public class EntityWithPropertiesComparatorFactory<OBJECT extends ICodeHolder & IRegistrationDateHolder & IModificationDateHolder & IPropertiesHolder>
+        extends EntityComparatorFactory<OBJECT>
+{
+
+    @Override
+    public boolean accepts(SortOptions<?> sortOptions)
+    {
+        return sortOptions instanceof EntityWithPropertiesSortOptions;
+    }
+
+    @Override
+    public Comparator<OBJECT> getComparator(String field)
+    {
+        if (field.startsWith(EntityWithPropertiesSortOptions.PROPERTY))
+        {
+            return new PropertyComparator<OBJECT>(field.substring(EntityWithPropertiesSortOptions.PROPERTY.length()));
+        } else
+        {
+            return super.getComparator(field);
+        }
+    }
+
+}
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/ModificationDateComparator.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/ModificationDateComparator.java
similarity index 86%
rename from openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/ModificationDateComparator.java
rename to openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/ModificationDateComparator.java
index 0ca8a07eebc..40e41879065 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/ModificationDateComparator.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/ModificationDateComparator.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator;
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
 
 import java.util.Date;
 
@@ -26,8 +26,6 @@ import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IModifica
 public class ModificationDateComparator<OBJECT extends IModificationDateHolder> extends AbstractComparator<OBJECT, Date>
 {
 
-    public static final String MODIFICATION_DATE = "MODIFICATION_DATE";
-
     @Override
     protected Date getValue(IModificationDateHolder o)
     {
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/PropertyComparator.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/PropertyComparator.java
similarity index 88%
rename from openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/PropertyComparator.java
rename to openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/PropertyComparator.java
index 84f12509c30..9b44b737e5b 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/PropertyComparator.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/PropertyComparator.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator;
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
 
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IPropertiesHolder;
 
@@ -24,8 +24,6 @@ import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IProperti
 public class PropertyComparator<OBJECT extends IPropertiesHolder> extends AbstractStringComparator<OBJECT>
 {
 
-    public static final String PROPERTY = "PROPERTY";
-
     private String propertyName;
 
     public PropertyComparator(String propertyName)
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/RegistrationDateComparator.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/RegistrationDateComparator.java
similarity index 86%
rename from openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/RegistrationDateComparator.java
rename to openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/RegistrationDateComparator.java
index efc8ad5c8c0..4d57c20e9b8 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/comparator/RegistrationDateComparator.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/RegistrationDateComparator.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator;
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
 
 import java.util.Date;
 
@@ -26,8 +26,6 @@ import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IRegistra
 public class RegistrationDateComparator<OBJECT extends IRegistrationDateHolder> extends AbstractComparator<OBJECT, Date>
 {
 
-    public static final String REGISTRATION_DATE = "REGISTRATION_DATE";
-
     @Override
     protected Date getValue(IRegistrationDateHolder o)
     {
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortAndPage.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/SortAndPage.java
similarity index 97%
rename from openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortAndPage.java
rename to openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/SortAndPage.java
index 8bb07cd91eb..3d50bb7500e 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortAndPage.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/SortAndPage.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort;
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
 
 import java.beans.PropertyDescriptor;
 import java.lang.reflect.Method;
@@ -32,6 +32,8 @@ import java.util.Set;
 import org.springframework.beans.BeanUtils;
 
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.FetchOptions;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.SortOptions;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.Sorting;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.view.AbstractCollectionView;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.view.ListView;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.view.SetView;
@@ -218,7 +220,8 @@ public class SortAndPage
                 {
                     if (sorting.getField() != null)
                     {
-                        Comparator aComparator = sortBy.getComparator(sorting.getField());
+                        Comparator aComparator = ComparatorFactory.getInstance(sortBy).getComparator(sorting.getField());
+
                         if (aComparator == null)
                         {
                             throw new IllegalArgumentException("Comparator for field " + sorting.getField() + " not found");
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/tag/TagComparatorFactory.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/tag/TagComparatorFactory.java
new file mode 100644
index 00000000000..390da594629
--- /dev/null
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/tag/TagComparatorFactory.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2015 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.tag;
+
+import java.util.Comparator;
+
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.sort.CodeComparator;
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.sort.ComparatorFactory;
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.sort.RegistrationDateComparator;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.tag.Tag;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.SortOptions;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.tag.TagSortOptions;
+
+/**
+ * @author pkupczyk
+ */
+public class TagComparatorFactory extends ComparatorFactory
+{
+
+    @Override
+    public boolean accepts(SortOptions<?> sortOptions)
+    {
+        return sortOptions instanceof TagSortOptions;
+    }
+
+    @Override
+    public Comparator<Tag> getComparator(String field)
+    {
+        if (TagSortOptions.CODE.equals(field))
+        {
+            return new CodeComparator<Tag>();
+        } else if (TagSortOptions.REGISTRATION_DATE.equals(field))
+        {
+            return new RegistrationDateComparator<Tag>();
+        } else
+        {
+            return null;
+        }
+    }
+
+}
diff --git a/openbis_api/sourceTest/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortAndPageTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/SortAndPageTest.java
similarity index 98%
rename from openbis_api/sourceTest/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortAndPageTest.java
rename to openbis/sourceTest/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/SortAndPageTest.java
index 344f49e89c4..02f6b101fc4 100644
--- a/openbis_api/sourceTest/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortAndPageTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sort/SortAndPageTest.java
@@ -1,6 +1,7 @@
-package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort;
+package ch.ethz.sis.openbis.generic.server.api.v3.helper.sort;
 
 import static org.testng.AssertJUnit.assertEquals;
+
 import java.sql.Date;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -13,6 +14,7 @@ import java.util.Set;
 
 import org.testng.annotations.Test;
 
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.sort.SortAndPage;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.material.Material;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.person.Person;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.project.Project;
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/EntitySortOptions.java b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/EntitySortOptions.java
index 04296ab2505..d1efa1ebb45 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/EntitySortOptions.java
+++ b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/EntitySortOptions.java
@@ -16,18 +16,9 @@
 
 package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort;
 
-import static ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.CodeComparator.CODE;
-import static ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.ModificationDateComparator.MODIFICATION_DATE;
-import static ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.RegistrationDateComparator.REGISTRATION_DATE;
-
-import java.util.Comparator;
-
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.ICodeHolder;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IModificationDateHolder;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IRegistrationDateHolder;
-import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.CodeComparator;
-import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.ModificationDateComparator;
-import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.RegistrationDateComparator;
 import ch.systemsx.cisd.base.annotation.JsonObject;
 
 /**
@@ -39,6 +30,12 @@ public class EntitySortOptions<OBJECT extends ICodeHolder & IRegistrationDateHol
 
     private static final long serialVersionUID = 1L;
 
+    public static final String CODE = "CODE";
+
+    public static final String MODIFICATION_DATE = "MODIFICATION_DATE";
+
+    public static final String REGISTRATION_DATE = "REGISTRATION_DATE";
+
     public SortOrder code()
     {
         return getOrCreateSorting(CODE);
@@ -69,21 +66,4 @@ public class EntitySortOptions<OBJECT extends ICodeHolder & IRegistrationDateHol
         return getSorting(MODIFICATION_DATE);
     }
 
-    @Override
-    public Comparator<OBJECT> getComparator(String field)
-    {
-        if (CODE.equals(field))
-        {
-            return new CodeComparator<OBJECT>();
-        } else if (REGISTRATION_DATE.equals(field))
-        {
-            return new RegistrationDateComparator<OBJECT>();
-        } else if (MODIFICATION_DATE.equals(field))
-        {
-            return new ModificationDateComparator<OBJECT>();
-        } else
-        {
-            return null;
-        }
-    }
 }
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/EntityWithPropertiesSortOptions.java b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/EntityWithPropertiesSortOptions.java
index f819b2ab60d..5fb28e384ca 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/EntityWithPropertiesSortOptions.java
+++ b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/EntityWithPropertiesSortOptions.java
@@ -16,15 +16,10 @@
 
 package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort;
 
-import static ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.PropertyComparator.PROPERTY;
-
-import java.util.Comparator;
-
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.ICodeHolder;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IModificationDateHolder;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IPropertiesHolder;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.interfaces.IRegistrationDateHolder;
-import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.PropertyComparator;
 import ch.systemsx.cisd.base.annotation.JsonObject;
 
 /**
@@ -37,6 +32,8 @@ public class EntityWithPropertiesSortOptions<OBJECT extends ICodeHolder & IRegis
 
     private static final long serialVersionUID = 1L;
 
+    public static final String PROPERTY = "PROPERTY";
+
     public SortOrder property(String propertyName)
     {
         return getOrCreateSorting(PROPERTY + propertyName);
@@ -47,15 +44,4 @@ public class EntityWithPropertiesSortOptions<OBJECT extends ICodeHolder & IRegis
         return getSorting(PROPERTY + propertyName);
     }
 
-    @Override
-    public Comparator<OBJECT> getComparator(String field)
-    {
-        if (field.startsWith(PROPERTY))
-        {
-            return new PropertyComparator<OBJECT>(field.substring(PROPERTY.length()));
-        } else
-        {
-            return super.getComparator(field);
-        }
-    }
 }
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortOptions.java b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortOptions.java
index 3ed31b2e4e5..3bc34e365d8 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortOptions.java
+++ b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/sort/SortOptions.java
@@ -17,7 +17,6 @@
 package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort;
 
 import java.io.Serializable;
-import java.util.Comparator;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -34,11 +33,6 @@ public abstract class SortOptions<OBJECT> implements Serializable
 
     private List<Sorting> sortings = new LinkedList<>();
 
-    public Comparator<OBJECT> getComparator(String field)
-    {
-        return null;
-    }
-
     protected SortOrder getOrCreateSorting(String field)
     {
         SortOrder order = getSorting(field);
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/tag/TagSortOptions.java b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/tag/TagSortOptions.java
index d2ede6f23cf..41408647a4d 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/tag/TagSortOptions.java
+++ b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/dto/fetchoptions/tag/TagSortOptions.java
@@ -16,16 +16,9 @@
 
 package ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.tag;
 
-import static ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.CodeComparator.CODE;
-import static ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.RegistrationDateComparator.REGISTRATION_DATE;
-
-import java.util.Comparator;
-
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.tag.Tag;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.SortOptions;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.SortOrder;
-import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.CodeComparator;
-import ch.ethz.sis.openbis.generic.shared.api.v3.dto.fetchoptions.sort.comparator.RegistrationDateComparator;
 import ch.systemsx.cisd.base.annotation.JsonObject;
 
 /**
@@ -37,6 +30,10 @@ public class TagSortOptions extends SortOptions<Tag>
 
     private static final long serialVersionUID = 1L;
 
+    public static final String CODE = "CODE";
+
+    public static final String REGISTRATION_DATE = "REGISTRATION_DATE";
+
     public SortOrder code()
     {
         return getOrCreateSorting(CODE);
@@ -57,18 +54,4 @@ public class TagSortOptions extends SortOptions<Tag>
         return getSorting(REGISTRATION_DATE);
     }
 
-    @Override
-    public Comparator<Tag> getComparator(String field)
-    {
-        if (CODE.equals(field))
-        {
-            return new CodeComparator<Tag>();
-        } else if (REGISTRATION_DATE.equals(field))
-        {
-            return new RegistrationDateComparator<Tag>();
-        } else
-        {
-            return null;
-        }
-    }
 }
-- 
GitLab