syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ilgro...@apache.org
Subject [1/2] syncope git commit: [SYNCOPE-1077] Enhancing sort support
Date Tue, 09 May 2017 10:13:03 GMT
Repository: syncope
Updated Branches:
  refs/heads/2_0_X b83656d7d -> 19d9925ce
  refs/heads/master 648c45461 -> 268e6ffcd


[SYNCOPE-1077] Enhancing sort support


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/19d9925c
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/19d9925c
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/19d9925c

Branch: refs/heads/2_0_X
Commit: 19d9925ce37b23486b32faf2926ad8765864102f
Parents: b83656d
Author: Francesco Chicchiriccò <ilgrosso@apache.org>
Authored: Tue May 9 12:12:06 2017 +0200
Committer: Francesco Chicchiriccò <ilgrosso@apache.org>
Committed: Tue May 9 12:12:06 2017 +0200

----------------------------------------------------------------------
 .../persistence/jpa/dao/JPAAnySearchDAO.java    |  4 +-
 .../jpa/dao/ElasticsearchAnySearchDAO.java      | 45 ++++++++++++++++++--
 2 files changed, 44 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/19d9925c/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
index 276b570..61d4a7f 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/JPAAnySearchDAO.java
@@ -251,10 +251,10 @@ public class JPAAnySearchDAO extends AbstractAnySearchDAO {
         return orderBy;
     }
 
-    private OrderBySupport parseOrderBy(final AnyTypeKind type, final SearchSupport svs,
+    private OrderBySupport parseOrderBy(final AnyTypeKind kind, final SearchSupport svs,
             final List<OrderByClause> orderByClauses) {
 
-        final AnyUtils attrUtils = anyUtilsFactory.getInstance(type);
+        AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
 
         OrderBySupport obs = new OrderBySupport();
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/19d9925c/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java
----------------------------------------------------------------------
diff --git a/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java
b/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java
index d2beabc..0675254 100644
--- a/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java
+++ b/ext/elasticsearch/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/ElasticsearchAnySearchDAO.java
@@ -18,6 +18,9 @@
  */
 package org.apache.syncope.core.persistence.jpa.dao;
 
+import static org.apache.syncope.core.persistence.jpa.dao.AbstractDAO.LOG;
+
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -42,6 +45,7 @@ import org.apache.syncope.core.persistence.api.dao.search.ResourceCond;
 import org.apache.syncope.core.persistence.api.dao.search.RoleCond;
 import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
 import org.apache.syncope.core.persistence.api.entity.Any;
+import org.apache.syncope.core.persistence.api.entity.AnyUtils;
 import org.apache.syncope.core.persistence.api.entity.PlainAttrValue;
 import org.apache.syncope.core.persistence.api.entity.PlainSchema;
 import org.apache.syncope.core.persistence.api.entity.Realm;
@@ -58,6 +62,7 @@ import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.sort.SortOrder;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.ReflectionUtils;
 
 /**
  * Search engine implementation for users, groups and any objects, based on Elasticsearch.
@@ -112,6 +117,42 @@ public class ElasticsearchAnySearchDAO extends AbstractAnySearchDAO {
         return (int) builder.get().getHits().getTotalHits();
     }
 
+    private void addSort(
+            final SearchRequestBuilder builder,
+            final AnyTypeKind kind,
+            final List<OrderByClause> orderBy) {
+
+        AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
+
+        for (OrderByClause clause : orderBy) {
+            String sortName = null;
+
+            // Manage difference among external key attribute and internal JPA @Id
+            String fieldName = "key".equals(clause.getField()) ? "id" : clause.getField();
+
+            Field anyField = ReflectionUtils.findField(attrUtils.anyClass(), fieldName);
+            if (anyField == null) {
+                PlainSchema schema = schemaDAO.find(fieldName);
+                if (schema != null) {
+                    sortName = schema.getType() == AttrSchemaType.String
+                            || schema.getType() == AttrSchemaType.Enum
+                            ? fieldName + ".keyword"
+                            : fieldName;
+                }
+            } else {
+                sortName = anyField.getType().equals(String.class)
+                        ? fieldName + ".keyword"
+                        : fieldName;
+            }
+
+            if (sortName == null) {
+                LOG.warn("Cannot build any valid clause from {}", clause);
+            } else {
+                builder.addSort(sortName, SortOrder.valueOf(clause.getDirection().name()));
+            }
+        }
+    }
+
     @Override
     protected <T extends Any<?>> List<T> doSearch(
             final Set<String> adminRealms,
@@ -124,9 +165,7 @@ public class ElasticsearchAnySearchDAO extends AbstractAnySearchDAO {
         SearchRequestBuilder builder = searchRequestBuilder(adminRealms, cond, kind).
                 setFrom(page <= 0 ? 0 : page - 1).
                 setSize(itemsPerPage < 0 ? elasticsearchUtils.getIndexMaxResultWindow()
: itemsPerPage);
-        for (OrderByClause clause : orderBy) {
-            builder.addSort(clause.getField(), SortOrder.valueOf(clause.getDirection().name()));
-        }
+        addSort(builder, kind, orderBy);
 
         return buildResult(
                 CollectionUtils.collect(Arrays.asList(builder.get().getHits().getHits()),


Mime
View raw message