...
|
...
|
@@ -20,6 +20,9 @@ import org.apache.commons.lang3.StringUtils; |
20
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
21
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
22
|
22
|
import org.springframework.stereotype.Repository;
|
|
23
|
+import org.springframework.transaction.TransactionStatus;
|
|
24
|
+import org.springframework.transaction.support.TransactionCallback;
|
|
25
|
+import org.springframework.transaction.support.TransactionTemplate;
|
23
|
26
|
import org.thingsboard.server.common.data.EntityType;
|
24
|
27
|
import org.thingsboard.server.common.data.id.CustomerId;
|
25
|
28
|
import org.thingsboard.server.common.data.id.EntityId;
|
...
|
...
|
@@ -58,37 +61,36 @@ import java.util.stream.Collectors; |
58
|
61
|
@Repository
|
59
|
62
|
@Slf4j
|
60
|
63
|
public class DefaultEntityQueryRepository implements EntityQueryRepository {
|
61
|
|
- //TODO: rafactoring to protect from SQL injections;
|
62
|
64
|
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)" +
|
|
65
|
+ private static final String SELECT_PHONE = " CASE WHEN entity.entity_type = 'TENANT' THEN (select phone from tenant where id = entity_id)" +
|
64
|
66
|
" 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)" +
|
|
67
|
+ private static final String SELECT_ZIP = " CASE WHEN entity.entity_type = 'TENANT' THEN (select zip from tenant where id = entity_id)" +
|
66
|
68
|
" 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'" +
|
|
69
|
+ private static final String SELECT_ADDRESS_2 = " CASE WHEN entity.entity_type = 'TENANT'" +
|
68
|
70
|
" THEN (select address2 from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
|
69
|
71
|
" 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'" +
|
|
72
|
+ private static final String SELECT_ADDRESS = " CASE WHEN entity.entity_type = 'TENANT'" +
|
71
|
73
|
" THEN (select address from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
|
72
|
74
|
" 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'" +
|
|
75
|
+ private static final String SELECT_CITY = " CASE WHEN entity.entity_type = 'TENANT'" +
|
74
|
76
|
" THEN (select city from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
|
75
|
77
|
" 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'" +
|
|
78
|
+ private static final String SELECT_STATE = " CASE WHEN entity.entity_type = 'TENANT'" +
|
77
|
79
|
" THEN (select state from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
|
78
|
80
|
" 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'" +
|
|
81
|
+ private static final String SELECT_COUNTRY = " CASE WHEN entity.entity_type = 'TENANT'" +
|
80
|
82
|
" THEN (select country from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
|
81
|
83
|
" 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'" +
|
|
84
|
+ private static final String SELECT_TITLE = " CASE WHEN entity.entity_type = 'TENANT'" +
|
83
|
85
|
" THEN (select title from tenant where id = entity_id) WHEN entity.entity_type = 'CUSTOMER' " +
|
84
|
86
|
" 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'" +
|
|
87
|
+ private static final String SELECT_LAST_NAME = " CASE WHEN entity.entity_type = 'USER'" +
|
86
|
88
|
" 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'" +
|
|
89
|
+ private static final String SELECT_FIRST_NAME = " CASE WHEN entity.entity_type = 'USER'" +
|
88
|
90
|
" 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'" +
|
|
91
|
+ private static final String SELECT_REGION = " CASE WHEN entity.entity_type = 'TENANT'" +
|
90
|
92
|
" THEN (select region from tenant where id = entity_id) END as region";
|
91
|
|
- public static final String SELECT_EMAIL = " CASE" +
|
|
93
|
+ private static final String SELECT_EMAIL = " CASE" +
|
92
|
94
|
" WHEN entity.entity_type = 'TENANT'" +
|
93
|
95
|
" THEN (select email from tenant where id = entity_id)" +
|
94
|
96
|
" WHEN entity.entity_type = 'CUSTOMER' " +
|
...
|
...
|
@@ -96,7 +98,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
96
|
98
|
" WHEN entity.entity_type = 'USER'" +
|
97
|
99
|
" THEN (select email from tb_user where id = entity_id)" +
|
98
|
100
|
" END as email";
|
99
|
|
- public static final String SELECT_CUSTOMER_ID = "CASE" +
|
|
101
|
+ private static final String SELECT_CUSTOMER_ID = "CASE" +
|
100
|
102
|
" WHEN entity.entity_type = 'TENANT'" +
|
101
|
103
|
" THEN UUID('" + TenantId.NULL_UUID + "')" +
|
102
|
104
|
" WHEN entity.entity_type = 'CUSTOMER' THEN entity_id" +
|
...
|
...
|
@@ -112,7 +114,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
112
|
114
|
" WHEN entity.entity_type = 'ENTITY_VIEW'" +
|
113
|
115
|
" THEN (select customer_id from entity_view where id = entity_id)" +
|
114
|
116
|
" END as customer_id";
|
115
|
|
- public static final String SELECT_TENANT_ID = "SELECT CASE" +
|
|
117
|
+ private static final String SELECT_TENANT_ID = "SELECT CASE" +
|
116
|
118
|
" WHEN entity.entity_type = 'TENANT' THEN entity_id" +
|
117
|
119
|
" WHEN entity.entity_type = 'CUSTOMER'" +
|
118
|
120
|
" THEN (select tenant_id from customer where id = entity_id)" +
|
...
|
...
|
@@ -127,7 +129,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
127
|
129
|
" WHEN entity.entity_type = 'ENTITY_VIEW'" +
|
128
|
130
|
" THEN (select tenant_id from entity_view where id = entity_id)" +
|
129
|
131
|
" END as tenant_id";
|
130
|
|
- public static final String SELECT_CREATED_TIME = " CASE" +
|
|
132
|
+ private static final String SELECT_CREATED_TIME = " CASE" +
|
131
|
133
|
" WHEN entity.entity_type = 'TENANT'" +
|
132
|
134
|
" THEN (select created_time from tenant where id = entity_id)" +
|
133
|
135
|
" WHEN entity.entity_type = 'CUSTOMER' " +
|
...
|
...
|
@@ -143,7 +145,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
143
|
145
|
" WHEN entity.entity_type = 'ENTITY_VIEW'" +
|
144
|
146
|
" THEN (select created_time from entity_view where id = entity_id)" +
|
145
|
147
|
" END as created_time";
|
146
|
|
- public static final String SELECT_NAME = " CASE" +
|
|
148
|
+ private static final String SELECT_NAME = " CASE" +
|
147
|
149
|
" WHEN entity.entity_type = 'TENANT'" +
|
148
|
150
|
" THEN (select title from tenant where id = entity_id)" +
|
149
|
151
|
" WHEN entity.entity_type = 'CUSTOMER' " +
|
...
|
...
|
@@ -159,7 +161,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
159
|
161
|
" WHEN entity.entity_type = 'ENTITY_VIEW'" +
|
160
|
162
|
" THEN (select name from entity_view where id = entity_id)" +
|
161
|
163
|
" END as name";
|
162
|
|
- public static final String SELECT_TYPE = " CASE" +
|
|
164
|
+ private static final String SELECT_TYPE = " CASE" +
|
163
|
165
|
" WHEN entity.entity_type = 'USER'" +
|
164
|
166
|
" THEN (select authority from tb_user where id = entity_id)" +
|
165
|
167
|
" WHEN entity.entity_type = 'ASSET'" +
|
...
|
...
|
@@ -169,7 +171,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
169
|
171
|
" WHEN entity.entity_type = 'ENTITY_VIEW'" +
|
170
|
172
|
" THEN (select type from entity_view where id = entity_id)" +
|
171
|
173
|
" ELSE entity.entity_type END as type";
|
172
|
|
- public static final String SELECT_LABEL = " CASE" +
|
|
174
|
+ private static final String SELECT_LABEL = " CASE" +
|
173
|
175
|
" WHEN entity.entity_type = 'TENANT'" +
|
174
|
176
|
" THEN (select title from tenant where id = entity_id)" +
|
175
|
177
|
" WHEN entity.entity_type = 'CUSTOMER' " +
|
...
|
...
|
@@ -196,7 +198,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
196
|
198
|
entityTableMap.put(EntityType.TENANT, "tenant");
|
197
|
199
|
}
|
198
|
200
|
|
199
|
|
- public static final String HIERARCHICAL_QUERY_TEMPLATE = " FROM (WITH RECURSIVE related_entities(from_id, from_type, to_id, to_type, relation_type, lvl) AS (" +
|
|
201
|
+ private static final String HIERARCHICAL_QUERY_TEMPLATE = " FROM (WITH RECURSIVE related_entities(from_id, from_type, to_id, to_type, relation_type, lvl) AS (" +
|
200
|
202
|
" SELECT from_id, from_type, to_id, to_type, relation_type, 1 as lvl" +
|
201
|
203
|
" FROM relation" +
|
202
|
204
|
" WHERE $in_id = :relation_root_id and $in_type = :relation_root_type and relation_type_group = 'COMMON'" +
|
...
|
...
|
@@ -209,11 +211,16 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
209
|
211
|
" SELECT re.$out_id entity_id, re.$out_type entity_type, re.lvl lvl" +
|
210
|
212
|
" from related_entities re" +
|
211
|
213
|
" %s ) entity";
|
212
|
|
- public static final String HIERARCHICAL_TO_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "to").replace("$out", "from");
|
213
|
|
- public static final String HIERARCHICAL_FROM_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "from").replace("$out", "to");
|
|
214
|
+ private static final String HIERARCHICAL_TO_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "to").replace("$out", "from");
|
|
215
|
+ private static final String HIERARCHICAL_FROM_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "from").replace("$out", "to");
|
214
|
216
|
|
215
|
|
- @Autowired
|
216
|
|
- protected NamedParameterJdbcTemplate jdbcTemplate;
|
|
217
|
+ private final NamedParameterJdbcTemplate jdbcTemplate;
|
|
218
|
+ private final TransactionTemplate transactionTemplate;
|
|
219
|
+
|
|
220
|
+ public DefaultEntityQueryRepository(NamedParameterJdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) {
|
|
221
|
+ this.jdbcTemplate = jdbcTemplate;
|
|
222
|
+ this.transactionTemplate = transactionTemplate;
|
|
223
|
+ }
|
217
|
224
|
|
218
|
225
|
@Override
|
219
|
226
|
public long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query) {
|
...
|
...
|
@@ -223,89 +230,91 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
223
|
230
|
ctx.append(addEntityTableQuery(ctx, query.getEntityFilter(), entityType));
|
224
|
231
|
ctx.append(" e where ");
|
225
|
232
|
ctx.append(buildEntityWhere(ctx, tenantId, customerId, query.getEntityFilter(), Collections.emptyList(), entityType));
|
226
|
|
- return jdbcTemplate.queryForObject(ctx.getQuery(), ctx, Long.class);
|
|
233
|
+ return transactionTemplate.execute(status -> jdbcTemplate.queryForObject(ctx.getQuery(), ctx, Long.class));
|
227
|
234
|
}
|
228
|
235
|
|
229
|
236
|
@Override
|
230
|
237
|
public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) {
|
231
|
|
- QueryContext ctx = new QueryContext();
|
232
|
|
- EntityType entityType = resolveEntityType(query.getEntityFilter());
|
233
|
|
- EntityDataPageLink pageLink = query.getPageLink();
|
234
|
|
-
|
235
|
|
- List<EntityKeyMapping> mappings = EntityKeyMapping.prepareKeyMapping(query);
|
236
|
|
-
|
237
|
|
- List<EntityKeyMapping> selectionMapping = mappings.stream().filter(EntityKeyMapping::isSelection)
|
238
|
|
- .collect(Collectors.toList());
|
239
|
|
- List<EntityKeyMapping> entityFieldsSelectionMapping = selectionMapping.stream().filter(mapping -> !mapping.isLatest())
|
240
|
|
- .collect(Collectors.toList());
|
241
|
|
- List<EntityKeyMapping> latestSelectionMapping = selectionMapping.stream().filter(EntityKeyMapping::isLatest)
|
242
|
|
- .collect(Collectors.toList());
|
243
|
|
-
|
244
|
|
- List<EntityKeyMapping> filterMapping = mappings.stream().filter(EntityKeyMapping::hasFilter)
|
245
|
|
- .collect(Collectors.toList());
|
246
|
|
- List<EntityKeyMapping> entityFieldsFiltersMapping = filterMapping.stream().filter(mapping -> !mapping.isLatest())
|
247
|
|
- .collect(Collectors.toList());
|
248
|
|
- List<EntityKeyMapping> latestFiltersMapping = filterMapping.stream().filter(EntityKeyMapping::isLatest)
|
249
|
|
- .collect(Collectors.toList());
|
250
|
|
-
|
251
|
|
- List<EntityKeyMapping> allLatestMappings = mappings.stream().filter(EntityKeyMapping::isLatest)
|
252
|
|
- .collect(Collectors.toList());
|
253
|
|
-
|
254
|
|
-
|
255
|
|
- String entityWhereClause = this.buildEntityWhere(ctx, tenantId, customerId, query.getEntityFilter(), entityFieldsFiltersMapping, entityType);
|
256
|
|
- String latestJoins = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings);
|
257
|
|
- String whereClause = this.buildWhere(ctx, latestFiltersMapping, query.getEntityFilter().getType(), entityType);
|
258
|
|
- String textSearchQuery = this.buildTextSearchQuery(ctx, selectionMapping, pageLink.getTextSearch());
|
259
|
|
- String entityFieldsSelection = EntityKeyMapping.buildSelections(entityFieldsSelectionMapping, query.getEntityFilter().getType(), entityType);
|
260
|
|
- String entityTypeStr;
|
261
|
|
- if (query.getEntityFilter().getType().equals(EntityFilterType.RELATIONS_QUERY)) {
|
262
|
|
- entityTypeStr = "e.entity_type";
|
263
|
|
- } else {
|
264
|
|
- entityTypeStr = "'" + entityType.name() + "'";
|
265
|
|
- }
|
266
|
|
- if (!StringUtils.isEmpty(entityFieldsSelection)) {
|
267
|
|
- entityFieldsSelection = String.format("e.id id, %s entity_type, %s", entityTypeStr, entityFieldsSelection);
|
268
|
|
- } else {
|
269
|
|
- entityFieldsSelection = String.format("e.id id, %s entity_type", entityTypeStr);
|
270
|
|
- }
|
271
|
|
- String latestSelection = EntityKeyMapping.buildSelections(latestSelectionMapping, query.getEntityFilter().getType(), entityType);
|
272
|
|
- String topSelection = "entities.*";
|
273
|
|
- if (!StringUtils.isEmpty(latestSelection)) {
|
274
|
|
- topSelection = topSelection + ", " + latestSelection;
|
275
|
|
- }
|
|
238
|
+ return transactionTemplate.execute(status -> {
|
|
239
|
+ QueryContext ctx = new QueryContext();
|
|
240
|
+ EntityType entityType = resolveEntityType(query.getEntityFilter());
|
|
241
|
+ EntityDataPageLink pageLink = query.getPageLink();
|
|
242
|
+
|
|
243
|
+ List<EntityKeyMapping> mappings = EntityKeyMapping.prepareKeyMapping(query);
|
|
244
|
+
|
|
245
|
+ List<EntityKeyMapping> selectionMapping = mappings.stream().filter(EntityKeyMapping::isSelection)
|
|
246
|
+ .collect(Collectors.toList());
|
|
247
|
+ List<EntityKeyMapping> entityFieldsSelectionMapping = selectionMapping.stream().filter(mapping -> !mapping.isLatest())
|
|
248
|
+ .collect(Collectors.toList());
|
|
249
|
+ List<EntityKeyMapping> latestSelectionMapping = selectionMapping.stream().filter(EntityKeyMapping::isLatest)
|
|
250
|
+ .collect(Collectors.toList());
|
|
251
|
+
|
|
252
|
+ List<EntityKeyMapping> filterMapping = mappings.stream().filter(EntityKeyMapping::hasFilter)
|
|
253
|
+ .collect(Collectors.toList());
|
|
254
|
+ List<EntityKeyMapping> entityFieldsFiltersMapping = filterMapping.stream().filter(mapping -> !mapping.isLatest())
|
|
255
|
+ .collect(Collectors.toList());
|
|
256
|
+ List<EntityKeyMapping> latestFiltersMapping = filterMapping.stream().filter(EntityKeyMapping::isLatest)
|
|
257
|
+ .collect(Collectors.toList());
|
|
258
|
+
|
|
259
|
+ List<EntityKeyMapping> allLatestMappings = mappings.stream().filter(EntityKeyMapping::isLatest)
|
|
260
|
+ .collect(Collectors.toList());
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+ String entityWhereClause = DefaultEntityQueryRepository.this.buildEntityWhere(ctx, tenantId, customerId, query.getEntityFilter(), entityFieldsFiltersMapping, entityType);
|
|
264
|
+ String latestJoins = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings);
|
|
265
|
+ String whereClause = DefaultEntityQueryRepository.this.buildWhere(ctx, latestFiltersMapping, query.getEntityFilter().getType(), entityType);
|
|
266
|
+ String textSearchQuery = DefaultEntityQueryRepository.this.buildTextSearchQuery(ctx, selectionMapping, pageLink.getTextSearch());
|
|
267
|
+ String entityFieldsSelection = EntityKeyMapping.buildSelections(entityFieldsSelectionMapping, query.getEntityFilter().getType(), entityType);
|
|
268
|
+ String entityTypeStr;
|
|
269
|
+ if (query.getEntityFilter().getType().equals(EntityFilterType.RELATIONS_QUERY)) {
|
|
270
|
+ entityTypeStr = "e.entity_type";
|
|
271
|
+ } else {
|
|
272
|
+ entityTypeStr = "'" + entityType.name() + "'";
|
|
273
|
+ }
|
|
274
|
+ if (!StringUtils.isEmpty(entityFieldsSelection)) {
|
|
275
|
+ entityFieldsSelection = String.format("e.id id, %s entity_type, %s", entityTypeStr, entityFieldsSelection);
|
|
276
|
+ } else {
|
|
277
|
+ entityFieldsSelection = String.format("e.id id, %s entity_type", entityTypeStr);
|
|
278
|
+ }
|
|
279
|
+ String latestSelection = EntityKeyMapping.buildSelections(latestSelectionMapping, query.getEntityFilter().getType(), entityType);
|
|
280
|
+ String topSelection = "entities.*";
|
|
281
|
+ if (!StringUtils.isEmpty(latestSelection)) {
|
|
282
|
+ topSelection = topSelection + ", " + latestSelection;
|
|
283
|
+ }
|
276
|
284
|
|
277
|
|
- String fromClause = String.format("from (select %s from (select %s from %s e where %s) entities %s %s) result %s",
|
278
|
|
- topSelection,
|
279
|
|
- entityFieldsSelection,
|
280
|
|
- addEntityTableQuery(ctx, query.getEntityFilter(), entityType),
|
281
|
|
- entityWhereClause,
|
282
|
|
- latestJoins,
|
283
|
|
- whereClause,
|
284
|
|
- textSearchQuery);
|
285
|
|
-
|
286
|
|
- int totalElements = jdbcTemplate.queryForObject(String.format("select count(*) %s", fromClause), ctx, Integer.class);
|
287
|
|
-
|
288
|
|
- String dataQuery = String.format("select * %s", fromClause);
|
289
|
|
-
|
290
|
|
- EntityDataSortOrder sortOrder = pageLink.getSortOrder();
|
291
|
|
- if (sortOrder != null) {
|
292
|
|
- Optional<EntityKeyMapping> sortOrderMappingOpt = mappings.stream().filter(EntityKeyMapping::isSortOrder).findFirst();
|
293
|
|
- if (sortOrderMappingOpt.isPresent()) {
|
294
|
|
- EntityKeyMapping sortOrderMapping = sortOrderMappingOpt.get();
|
295
|
|
- dataQuery = String.format("%s order by %s", dataQuery, sortOrderMapping.getValueAlias());
|
296
|
|
- if (sortOrder.getDirection() == EntityDataSortOrder.Direction.ASC) {
|
297
|
|
- dataQuery += " asc";
|
298
|
|
- } else {
|
299
|
|
- dataQuery += " desc";
|
|
285
|
+ String fromClause = String.format("from (select %s from (select %s from %s e where %s) entities %s %s) result %s",
|
|
286
|
+ topSelection,
|
|
287
|
+ entityFieldsSelection,
|
|
288
|
+ addEntityTableQuery(ctx, query.getEntityFilter(), entityType),
|
|
289
|
+ entityWhereClause,
|
|
290
|
+ latestJoins,
|
|
291
|
+ whereClause,
|
|
292
|
+ textSearchQuery);
|
|
293
|
+
|
|
294
|
+ int totalElements = jdbcTemplate.queryForObject(String.format("select count(*) %s", fromClause), ctx, Integer.class);
|
|
295
|
+
|
|
296
|
+ String dataQuery = String.format("select * %s", fromClause);
|
|
297
|
+
|
|
298
|
+ EntityDataSortOrder sortOrder = pageLink.getSortOrder();
|
|
299
|
+ if (sortOrder != null) {
|
|
300
|
+ Optional<EntityKeyMapping> sortOrderMappingOpt = mappings.stream().filter(EntityKeyMapping::isSortOrder).findFirst();
|
|
301
|
+ if (sortOrderMappingOpt.isPresent()) {
|
|
302
|
+ EntityKeyMapping sortOrderMapping = sortOrderMappingOpt.get();
|
|
303
|
+ dataQuery = String.format("%s order by %s", dataQuery, sortOrderMapping.getValueAlias());
|
|
304
|
+ if (sortOrder.getDirection() == EntityDataSortOrder.Direction.ASC) {
|
|
305
|
+ dataQuery += " asc";
|
|
306
|
+ } else {
|
|
307
|
+ dataQuery += " desc";
|
|
308
|
+ }
|
300
|
309
|
}
|
301
|
310
|
}
|
302
|
|
- }
|
303
|
|
- int startIndex = pageLink.getPageSize() * pageLink.getPage();
|
304
|
|
- if (pageLink.getPageSize() > 0) {
|
305
|
|
- dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex);
|
306
|
|
- }
|
307
|
|
- List<Map<String, Object>> rows = jdbcTemplate.queryForList(dataQuery, ctx);
|
308
|
|
- return EntityDataAdapter.createEntityData(pageLink, selectionMapping, rows, totalElements);
|
|
311
|
+ int startIndex = pageLink.getPageSize() * pageLink.getPage();
|
|
312
|
+ if (pageLink.getPageSize() > 0) {
|
|
313
|
+ dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex);
|
|
314
|
+ }
|
|
315
|
+ List<Map<String, Object>> rows = jdbcTemplate.queryForList(dataQuery, ctx);
|
|
316
|
+ return EntityDataAdapter.createEntityData(pageLink, selectionMapping, rows, totalElements);
|
|
317
|
+ });
|
309
|
318
|
}
|
310
|
319
|
|
311
|
320
|
private String buildEntityWhere(QueryContext ctx,
|
...
|
...
|
|