Commit 4592ab895aa13c843c786a3fe7fa56fa2cc0c741

Authored by Andrii Shvaika
1 parent c1164cd0

Implementation of hierarchical query

@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at 6 * You may obtain a copy of the License at
7 * 7 *
8 - * http://www.apache.org/licenses/LICENSE-2.0 8 + * http://www.apache.org/licenses/LICENSE-2.0
9 * 9 *
10 * Unless required by applicable law or agreed to in writing, software 10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, 11 * distributed under the License is distributed on an "AS IS" BASIS,
@@ -60,6 +60,131 @@ import java.util.stream.Collectors; @@ -60,6 +60,131 @@ import java.util.stream.Collectors;
60 public class DefaultEntityQueryRepository implements EntityQueryRepository { 60 public class DefaultEntityQueryRepository implements EntityQueryRepository {
61 //TODO: rafactoring to protect from SQL injections; 61 //TODO: rafactoring to protect from SQL injections;
62 private static final Map<EntityType, String> entityTableMap = new HashMap<>(); 62 private static final Map<EntityType, String> entityTableMap = new HashMap<>();
  63 + public static final String SELECT_PHONE = " CASE WHEN entity.entity_type = 'TENANT' THEN (select phone from tenant where id = entity_id)" +
  64 + " WHEN entity.entity_type = 'CUSTOMER' THEN (select phone from customer where id = entity_id) END as phone";
  65 + public static final String SELECT_ZIP = " CASE WHEN entity.entity_type = 'TENANT' THEN (select zip from tenant where id = entity_id)" +
  66 + " WHEN entity.entity_type = 'CUSTOMER' THEN (select zip from customer where id = entity_id) END as zip";
  67 + public static final String SELECT_ADDRESS_2 = " CASE WHEN entity.entity_type = 'TENANT'" +
  68 + " THEN (select address2 from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
  69 + " THEN (select address2 from customer where id = entity_id) END as address2";
  70 + public static final String SELECT_ADDRESS = " CASE WHEN entity.entity_type = 'TENANT'" +
  71 + " THEN (select address from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
  72 + " THEN (select address from customer where id = entity_id) END as address";
  73 + public static final String SELECT_CITY = " CASE WHEN entity.entity_type = 'TENANT'" +
  74 + " THEN (select city from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
  75 + " THEN (select city from customer where id = entity_id) END as city";
  76 + public static final String SELECT_STATE = " CASE WHEN entity.entity_type = 'TENANT'" +
  77 + " THEN (select state from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
  78 + " THEN (select state from customer where id = entity_id) END as state";
  79 + public static final String SELECT_COUNTRY = " CASE WHEN entity.entity_type = 'TENANT'" +
  80 + " THEN (select country from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
  81 + " THEN (select country from customer where id = entity_id) END as country";
  82 + public static final String SELECT_TITLE = " CASE WHEN entity.entity_type = 'TENANT'" +
  83 + " THEN (select title from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
  84 + " THEN (select title from customer where id = entity_id) END as title";
  85 + public static final String SELECT_LAST_NAME = " CASE WHEN entity.entity_type = 'USER'" +
  86 + " THEN (select last_name from tb_user where id = entity_id) END as last_name";
  87 + public static final String SELECT_FIRST_NAME = " CASE WHEN entity.entity_type = 'USER'" +
  88 + " THEN (select first_name from tb_user where id = entity_id) END as first_name";
  89 + public static final String SELECT_REGION = " CASE WHEN entity.entity_type = 'TENANT'" +
  90 + " THEN (select region from tenant where id = entity_id) END as region";
  91 + public static final String SELECT_EMAIL = " CASE" +
  92 + " WHEN entity.entity_type = 'TENANT'" +
  93 + " THEN (select email from tenant where id = entity_id)" +
  94 + " WHEN entity.entity_type = 'CUSTOMER' " +
  95 + " THEN (select email from customer where id = entity_id)" +
  96 + " WHEN entity.entity_type = 'USER'" +
  97 + " THEN (select email from tb_user where id = entity_id)" +
  98 + " END as email";
  99 + public static final String SELECT_CUSTOMER_ID = "CASE" +
  100 + " WHEN entity.entity_type = 'TENANT'" +
  101 + " THEN UUID('" + TenantId.NULL_UUID + "')" +
  102 + " WHEN entity.entity_type = 'CUSTOMER' THEN entity_id" +
  103 + " WHEN entity.entity_type = 'USER'" +
  104 + " THEN (select customer_id from tb_user where id = entity_id)" +
  105 + " WHEN entity.entity_type = 'DASHBOARD'" +
  106 + //TODO: parse assigned customers or use contains?
  107 + " THEN NULL" +
  108 + " WHEN entity.entity_type = 'ASSET'" +
  109 + " THEN (select customer_id from asset where id = entity_id)" +
  110 + " WHEN entity.entity_type = 'DEVICE'" +
  111 + " THEN (select customer_id from device where id = entity_id)" +
  112 + " WHEN entity.entity_type = 'ENTITY_VIEW'" +
  113 + " THEN (select customer_id from entity_view where id = entity_id)" +
  114 + " END as customer_id";
  115 + public static final String SELECT_TENANT_ID = "SELECT CASE" +
  116 + " WHEN entity.entity_type = 'TENANT' THEN entity_id" +
  117 + " WHEN entity.entity_type = 'CUSTOMER'" +
  118 + " THEN (select tenant_id from customer where id = entity_id)" +
  119 + " WHEN entity.entity_type = 'USER'" +
  120 + " THEN (select tenant_id from tb_user where id = entity_id)" +
  121 + " WHEN entity.entity_type = 'DASHBOARD'" +
  122 + " THEN (select tenant_id from dashboard where id = entity_id)" +
  123 + " WHEN entity.entity_type = 'ASSET'" +
  124 + " THEN (select tenant_id from asset where id = entity_id)" +
  125 + " WHEN entity.entity_type = 'DEVICE'" +
  126 + " THEN (select tenant_id from device where id = entity_id)" +
  127 + " WHEN entity.entity_type = 'ENTITY_VIEW'" +
  128 + " THEN (select tenant_id from entity_view where id = entity_id)" +
  129 + " END as tenant_id";
  130 + public static final String SELECT_CREATED_TIME = " CASE" +
  131 + " WHEN entity.entity_type = 'TENANT'" +
  132 + " THEN (select created_time from tenant where id = entity_id)" +
  133 + " WHEN entity.entity_type = 'CUSTOMER' " +
  134 + " THEN (select created_time from customer where id = entity_id)" +
  135 + " WHEN entity.entity_type = 'USER'" +
  136 + " THEN (select created_time from tb_user where id = entity_id)" +
  137 + " WHEN entity.entity_type = 'DASHBOARD'" +
  138 + " THEN (select created_time from dashboard where id = entity_id)" +
  139 + " WHEN entity.entity_type = 'ASSET'" +
  140 + " THEN (select created_time from asset where id = entity_id)" +
  141 + " WHEN entity.entity_type = 'DEVICE'" +
  142 + " THEN (select created_time from device where id = entity_id)" +
  143 + " WHEN entity.entity_type = 'ENTITY_VIEW'" +
  144 + " THEN (select created_time from entity_view where id = entity_id)" +
  145 + " END as created_time";
  146 + public static final String SELECT_NAME = " CASE" +
  147 + " WHEN entity.entity_type = 'TENANT'" +
  148 + " THEN (select title from tenant where id = entity_id)" +
  149 + " WHEN entity.entity_type = 'CUSTOMER' " +
  150 + " THEN (select title from customer where id = entity_id)" +
  151 + " WHEN entity.entity_type = 'USER'" +
  152 + " THEN (select CONCAT (first_name, ' ', last_name) from tb_user where id = entity_id)" +
  153 + " WHEN entity.entity_type = 'DASHBOARD'" +
  154 + " THEN (select title from dashboard where id = entity_id)" +
  155 + " WHEN entity.entity_type = 'ASSET'" +
  156 + " THEN (select name from asset where id = entity_id)" +
  157 + " WHEN entity.entity_type = 'DEVICE'" +
  158 + " THEN (select name from device where id = entity_id)" +
  159 + " WHEN entity.entity_type = 'ENTITY_VIEW'" +
  160 + " THEN (select name from entity_view where id = entity_id)" +
  161 + " END as name";
  162 + public static final String SELECT_TYPE = " CASE" +
  163 + " WHEN entity.entity_type = 'USER'" +
  164 + " THEN (select authority from tb_user where id = entity_id)" +
  165 + " WHEN entity.entity_type = 'ASSET'" +
  166 + " THEN (select type from asset where id = entity_id)" +
  167 + " WHEN entity.entity_type = 'DEVICE'" +
  168 + " THEN (select type from device where id = entity_id)" +
  169 + " WHEN entity.entity_type = 'ENTITY_VIEW'" +
  170 + " THEN (select type from entity_view where id = entity_id)" +
  171 + " ELSE entity.entity_type END as type";
  172 + public static final String SELECT_LABEL = " CASE" +
  173 + " WHEN entity.entity_type = 'TENANT'" +
  174 + " THEN (select title from tenant where id = entity_id)" +
  175 + " WHEN entity.entity_type = 'CUSTOMER' " +
  176 + " THEN (select title from customer where id = entity_id)" +
  177 + " WHEN entity.entity_type = 'USER'" +
  178 + " THEN (select CONCAT (first_name, ' ', last_name) from tb_user where id = entity_id)" +
  179 + " WHEN entity.entity_type = 'DASHBOARD'" +
  180 + " THEN (select title from dashboard where id = entity_id)" +
  181 + " WHEN entity.entity_type = 'ASSET'" +
  182 + " THEN (select label from asset where id = entity_id)" +
  183 + " WHEN entity.entity_type = 'DEVICE'" +
  184 + " THEN (select label from device where id = entity_id)" +
  185 + " WHEN entity.entity_type = 'ENTITY_VIEW'" +
  186 + " THEN (select name from entity_view where id = entity_id)" +
  187 + " END as label";
63 188
64 static { 189 static {
65 entityTableMap.put(EntityType.ASSET, "asset"); 190 entityTableMap.put(EntityType.ASSET, "asset");
@@ -293,9 +418,14 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { @@ -293,9 +418,14 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
293 private String relationQuery(EntityQueryContext ctx, RelationsQueryFilter entityFilter) { 418 private String relationQuery(EntityQueryContext ctx, RelationsQueryFilter entityFilter) {
294 EntityId rootId = entityFilter.getRootEntity(); 419 EntityId rootId = entityFilter.getRootEntity();
295 String lvlFilter = getLvlFilter(entityFilter.getMaxLevel()); 420 String lvlFilter = getLvlFilter(entityFilter.getMaxLevel());
296 - String selectFields = getSelectCreatedTime() + ", " + getSelectTenantId() + ", " + getSelectCustomerId() + ", " +  
297 - " entity.entity_id as id," + getSelectType() + ", " + getSelectName() + ", " +  
298 - getSelectLabel() + ", entity.entity_type as entity_type"; 421 + String selectFields = SELECT_TENANT_ID + ", " + SELECT_CUSTOMER_ID
  422 + + ", " + SELECT_CREATED_TIME + ", " +
  423 + " entity.entity_id as id,"
  424 + + SELECT_TYPE + ", " + SELECT_NAME + ", " + SELECT_LABEL + ", " +
  425 + SELECT_FIRST_NAME + ", " + SELECT_LAST_NAME + ", " + SELECT_EMAIL + ", " + SELECT_REGION + ", " +
  426 + SELECT_TITLE + ", " + SELECT_COUNTRY + ", " + SELECT_STATE + ", " + SELECT_CITY + ", " +
  427 + SELECT_ADDRESS + ", " + SELECT_ADDRESS_2 + ", " + SELECT_ZIP + ", " + SELECT_PHONE +
  428 + ", entity.entity_type as entity_type";
299 String from = getQueryTemplate(entityFilter.getDirection()); 429 String from = getQueryTemplate(entityFilter.getDirection());
300 ctx.addUuidParameter("relation_root_id", rootId.getId()); 430 ctx.addUuidParameter("relation_root_id", rootId.getId());
301 ctx.addStringParameter("relation_root_type", rootId.getEntityType().name()); 431 ctx.addStringParameter("relation_root_type", rootId.getEntityType().name());
@@ -347,98 +477,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { @@ -347,98 +477,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository {
347 return from; 477 return from;
348 } 478 }
349 479
350 - private String getSelectCreatedTime() {  
351 - return "created_time";  
352 - }  
353 -  
354 - private String getSelectTenantId() {  
355 - return "SELECT CASE" +  
356 - " WHEN entity.entity_type = 'TENANT' THEN entity_id" +  
357 - " WHEN entity.entity_type = 'CUSTOMER'" +  
358 - " THEN (select tenant_id from customer where id = entity_id)" +  
359 - " WHEN entity.entity_type = 'USER'" +  
360 - " THEN (select tenant_id from tb_user where id = entity_id)" +  
361 - " WHEN entity.entity_type = 'DASHBOARD'" +  
362 - " THEN (select tenant_id from dashboard where id = entity_id)" +  
363 - " WHEN entity.entity_type = 'ASSET'" +  
364 - " THEN (select tenant_id from asset where id = entity_id)" +  
365 - " WHEN entity.entity_type = 'DEVICE'" +  
366 - " THEN (select tenant_id from device where id = entity_id)" +  
367 - " WHEN entity.entity_type = 'ENTITY_VIEW'" +  
368 - " THEN (select tenant_id from entity_view where id = entity_id)" +  
369 - " END as tenant_id";  
370 - }  
371 -  
372 - private String getSelectCustomerId() {  
373 - return "CASE" +  
374 - " WHEN entity.entity_type = 'TENANT'" +  
375 - " THEN UUID('" + TenantId.NULL_UUID + "')" +  
376 - " WHEN entity.entity_type = 'CUSTOMER' THEN entity_id" +  
377 - " WHEN entity.entity_type = 'USER'" +  
378 - " THEN (select customer_id from tb_user where id = entity_id)" +  
379 - " WHEN entity.entity_type = 'DASHBOARD'" +  
380 - //TODO: parse assigned customers or use contains?  
381 - " THEN NULL" +  
382 - " WHEN entity.entity_type = 'ASSET'" +  
383 - " THEN (select customer_id from asset where id = entity_id)" +  
384 - " WHEN entity.entity_type = 'DEVICE'" +  
385 - " THEN (select customer_id from device where id = entity_id)" +  
386 - " WHEN entity.entity_type = 'ENTITY_VIEW'" +  
387 - " THEN (select customer_id from entity_view where id = entity_id)" +  
388 - " END as customer_id";  
389 - }  
390 -  
391 - private String getSelectName() {  
392 - return " CASE" +  
393 - " WHEN entity.entity_type = 'TENANT'" +  
394 - " THEN (select title from tenant where id = entity_id)" +  
395 - " WHEN entity.entity_type = 'CUSTOMER' " +  
396 - " THEN (select title from customer where id = entity_id)" +  
397 - " WHEN entity.entity_type = 'USER'" +  
398 - " THEN (select CONCAT (first_name, ' ', last_name) from tb_user where id = entity_id)" +  
399 - " WHEN entity.entity_type = 'DASHBOARD'" +  
400 - " THEN (select title from dashboard where id = entity_id)" +  
401 - " WHEN entity.entity_type = 'ASSET'" +  
402 - " THEN (select name from asset where id = entity_id)" +  
403 - " WHEN entity.entity_type = 'DEVICE'" +  
404 - " THEN (select name from device where id = entity_id)" +  
405 - " WHEN entity.entity_type = 'ENTITY_VIEW'" +  
406 - " THEN (select name from entity_view where id = entity_id)" +  
407 - " END as name";  
408 - }  
409 -  
410 - private String getSelectType() {  
411 - return " CASE" +  
412 - " WHEN entity.entity_type = 'USER'" +  
413 - " THEN (select authority from tb_user where id = entity_id)" +  
414 - " WHEN entity.entity_type = 'ASSET'" +  
415 - " THEN (select type from asset where id = entity_id)" +  
416 - " WHEN entity.entity_type = 'DEVICE'" +  
417 - " THEN (select type from device where id = entity_id)" +  
418 - " WHEN entity.entity_type = 'ENTITY_VIEW'" +  
419 - " THEN (select type from entity_view where id = entity_id)" +  
420 - " ELSE entity.entity_type END as type";  
421 - }  
422 -  
423 - private String getSelectLabel() {  
424 - return " CASE" +  
425 - " WHEN entity.entity_type = 'TENANT'" +  
426 - " THEN (select title from tenant where id = entity_id)" +  
427 - " WHEN entity.entity_type = 'CUSTOMER' " +  
428 - " THEN (select title from customer where id = entity_id)" +  
429 - " WHEN entity.entity_type = 'USER'" +  
430 - " THEN (select CONCAT (first_name, ' ', last_name) from tb_user where id = entity_id)" +  
431 - " WHEN entity.entity_type = 'DASHBOARD'" +  
432 - " THEN (select title from dashboard where id = entity_id)" +  
433 - " WHEN entity.entity_type = 'ASSET'" +  
434 - " THEN (select label from asset where id = entity_id)" +  
435 - " WHEN entity.entity_type = 'DEVICE'" +  
436 - " THEN (select label from device where id = entity_id)" +  
437 - " WHEN entity.entity_type = 'ENTITY_VIEW'" +  
438 - " THEN (select name from entity_view where id = entity_id)" +  
439 - " END as label";  
440 - }  
441 -  
442 private String buildWhere(EntityQueryContext ctx, List<EntityKeyMapping> latestFiltersMapping) { 480 private String buildWhere(EntityQueryContext ctx, List<EntityKeyMapping> latestFiltersMapping) {
443 String latestFilters = EntityKeyMapping.buildQuery(ctx, latestFiltersMapping); 481 String latestFilters = EntityKeyMapping.buildQuery(ctx, latestFiltersMapping);
444 if (!StringUtils.isEmpty(latestFilters)) { 482 if (!StringUtils.isEmpty(latestFilters)) {
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at 6 * You may obtain a copy of the License at
7 * 7 *
8 - * http://www.apache.org/licenses/LICENSE-2.0 8 + * http://www.apache.org/licenses/LICENSE-2.0
9 * 9 *
10 * Unless required by applicable law or agreed to in writing, software 10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, 11 * distributed under the License is distributed on an "AS IS" BASIS,
@@ -50,6 +50,7 @@ public class EntityKeyMapping { @@ -50,6 +50,7 @@ public class EntityKeyMapping {
50 50
51 private static final Map<EntityType, Set<String>> allowedEntityFieldMap = new HashMap<>(); 51 private static final Map<EntityType, Set<String>> allowedEntityFieldMap = new HashMap<>();
52 private static final Map<String, String> entityFieldColumnMap = new HashMap<>(); 52 private static final Map<String, String> entityFieldColumnMap = new HashMap<>();
  53 + private static final Map<EntityType, Map<String, String>> aliases = new HashMap<>();
53 54
54 private static final String CREATED_TIME = "createdTime"; 55 private static final String CREATED_TIME = "createdTime";
55 private static final String ENTITY_TYPE = "entityType"; 56 private static final String ENTITY_TYPE = "entityType";
@@ -71,10 +72,10 @@ public class EntityKeyMapping { @@ -71,10 +72,10 @@ public class EntityKeyMapping {
71 72
72 public static final List<String> commonEntityFields = Arrays.asList(CREATED_TIME, ENTITY_TYPE, NAME); 73 public static final List<String> commonEntityFields = Arrays.asList(CREATED_TIME, ENTITY_TYPE, NAME);
73 public static final List<String> labeledEntityFields = Arrays.asList(CREATED_TIME, ENTITY_TYPE, NAME, TYPE, LABEL); 74 public static final List<String> labeledEntityFields = Arrays.asList(CREATED_TIME, ENTITY_TYPE, NAME, TYPE, LABEL);
74 - public static final List<String> contactBasedEntityFields = Arrays.asList(CREATED_TIME, ENTITY_TYPE, NAME, EMAIL, TITLE, COUNTRY, STATE, CITY, ADDRESS, ADDRESS_2, ZIP, PHONE); 75 + public static final List<String> contactBasedEntityFields = Arrays.asList(CREATED_TIME, ENTITY_TYPE, EMAIL, TITLE, COUNTRY, STATE, CITY, ADDRESS, ADDRESS_2, ZIP, PHONE);
75 76
76 public static final Set<String> commonEntityFieldsSet = new HashSet<>(commonEntityFields); 77 public static final Set<String> commonEntityFieldsSet = new HashSet<>(commonEntityFields);
77 - public static final Set<String> relationQueryEntityFieldsSet = new HashSet<>(Arrays.asList(CREATED_TIME, ENTITY_TYPE, NAME, TYPE, LABEL)); 78 + public static final Set<String> relationQueryEntityFieldsSet = new HashSet<>(Arrays.asList(CREATED_TIME, ENTITY_TYPE, NAME, TYPE, LABEL, FIRST_NAME, LAST_NAME, EMAIL, REGION, TITLE, COUNTRY, STATE, CITY, ADDRESS, ADDRESS_2, ZIP, PHONE));
78 79
79 static { 80 static {
80 allowedEntityFieldMap.put(EntityType.DEVICE, new HashSet<>(labeledEntityFields)); 81 allowedEntityFieldMap.put(EntityType.DEVICE, new HashSet<>(labeledEntityFields));
@@ -110,6 +111,25 @@ public class EntityKeyMapping { @@ -110,6 +111,25 @@ public class EntityKeyMapping {
110 entityFieldColumnMap.put(ADDRESS_2, ModelConstants.ADDRESS2_PROPERTY); 111 entityFieldColumnMap.put(ADDRESS_2, ModelConstants.ADDRESS2_PROPERTY);
111 entityFieldColumnMap.put(ZIP, ModelConstants.ZIP_PROPERTY); 112 entityFieldColumnMap.put(ZIP, ModelConstants.ZIP_PROPERTY);
112 entityFieldColumnMap.put(PHONE, ModelConstants.PHONE_PROPERTY); 113 entityFieldColumnMap.put(PHONE, ModelConstants.PHONE_PROPERTY);
  114 +
  115 + Map<String, String> contactBasedAliases = new HashMap<>();
  116 + contactBasedAliases.put(NAME, TITLE);
  117 + contactBasedAliases.put(LABEL, TITLE);
  118 + aliases.put(EntityType.TENANT, contactBasedAliases);
  119 + aliases.put(EntityType.CUSTOMER, contactBasedAliases);
  120 + Map<String, String> commonEntityAliases = new HashMap<>();
  121 + commonEntityAliases.put(TITLE, NAME);
  122 + aliases.put(EntityType.DEVICE, commonEntityAliases);
  123 + aliases.put(EntityType.ASSET, commonEntityAliases);
  124 + aliases.put(EntityType.ENTITY_VIEW, commonEntityAliases);
  125 + aliases.put(EntityType.DASHBOARD, commonEntityAliases);
  126 + aliases.put(EntityType.WIDGETS_BUNDLE, commonEntityAliases);
  127 +
  128 + Map<String, String> userEntityAliases = new HashMap<>();
  129 + userEntityAliases.put(TITLE, EMAIL);
  130 + userEntityAliases.put(LABEL, EMAIL);
  131 + userEntityAliases.put(NAME, EMAIL);
  132 + aliases.put(EntityType.USER, userEntityAliases);
113 } 133 }
114 134
115 private int index; 135 private int index;
@@ -141,16 +161,28 @@ public class EntityKeyMapping { @@ -141,16 +161,28 @@ public class EntityKeyMapping {
141 public String toSelection(EntityFilterType filterType, EntityType entityType) { 161 public String toSelection(EntityFilterType filterType, EntityType entityType) {
142 if (entityKey.getType().equals(EntityKeyType.ENTITY_FIELD)) { 162 if (entityKey.getType().equals(EntityKeyType.ENTITY_FIELD)) {
143 Set<String> existingEntityFields; 163 Set<String> existingEntityFields;
  164 + String alias;
144 if (filterType.equals(EntityFilterType.RELATIONS_QUERY)) { 165 if (filterType.equals(EntityFilterType.RELATIONS_QUERY)) {
145 existingEntityFields = relationQueryEntityFieldsSet; 166 existingEntityFields = relationQueryEntityFieldsSet;
  167 + alias = entityKey.getKey();
146 } else { 168 } else {
147 existingEntityFields = allowedEntityFieldMap.get(entityType); 169 existingEntityFields = allowedEntityFieldMap.get(entityType);
148 if (existingEntityFields == null) { 170 if (existingEntityFields == null) {
149 existingEntityFields = commonEntityFieldsSet; 171 existingEntityFields = commonEntityFieldsSet;
150 } 172 }
  173 +
  174 + Map<String, String> entityAliases = aliases.get(entityType);
  175 + if (entityAliases != null) {
  176 + alias = entityAliases.get(entityKey.getKey());
  177 + } else {
  178 + alias = null;
  179 + }
  180 + if (alias == null) {
  181 + alias = entityKey.getKey();
  182 + }
151 } 183 }
152 - if (existingEntityFields.contains(entityKey.getKey())) {  
153 - String column = entityFieldColumnMap.get(entityKey.getKey()); 184 + if (existingEntityFields.contains(alias)) {
  185 + String column = entityFieldColumnMap.get(alias);
154 return String.format("e.%s as %s", column, getValueAlias()); 186 return String.format("e.%s as %s", column, getValueAlias());
155 } else { 187 } else {
156 return String.format("'' as %s", getValueAlias()); 188 return String.format("'' as %s", getValueAlias());