1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.repository;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.kuali.rice.core.api.criteria.OrderByField;
20 import org.kuali.rice.core.api.criteria.OrderDirection;
21 import org.kuali.rice.core.api.criteria.QueryByCriteria;
22 import org.kuali.rice.core.api.criteria.QueryResults;
23 import org.kuali.rice.krad.data.DataObjectService;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28
29
30
31
32 public class BusinessObjectServiceMigrationUtils {
33
34
35
36
37
38
39
40
41
42
43 public static <T> T findSingleMatching(DataObjectService dos, Class<T> entityClass, Map<String,?> queryAttrs) {
44 QueryByCriteria criteria = QueryByCriteria.Builder.andAttributes(queryAttrs).build();
45 QueryResults<T> queryResults = dos.findMatching(entityClass, criteria);
46
47 if (queryResults != null && !CollectionUtils.isEmpty(queryResults.getResults())) {
48 List<T> results = queryResults.getResults();
49
50 if (results.size() != 1) {
51 throw new IllegalArgumentException("multiple results returned from query");
52 }
53
54 return queryResults.getResults().get(0);
55 }
56
57 return null;
58 }
59
60
61
62
63
64
65
66
67
68
69 public static <T> List<T> findMatching(DataObjectService dos, Class<T> entityClass, Map<String,?> queryAttrs) {
70 QueryByCriteria criteria = QueryByCriteria.Builder.andAttributes(queryAttrs).build();
71 QueryResults<T> queryResults = dos.findMatching(entityClass, criteria);
72
73 if (queryResults.getResults() != null) {
74 return queryResults.getResults();
75 }
76
77 return new ArrayList<T>();
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91 public static <T> List<T> findMatchingOrderBy(DataObjectService dos, Class<T> entityClass, Map<String,?> queryAttrs, String orderByField, boolean sortAscending) {
92 QueryByCriteria.Builder critBuilder = QueryByCriteria.Builder.andAttributes(queryAttrs);
93 OrderDirection sortDirection = sortAscending ? OrderDirection.ASCENDING : OrderDirection.DESCENDING;
94 critBuilder.setOrderByFields(OrderByField.Builder.create(orderByField, sortDirection).build());
95
96 QueryResults<T> queryResults = dos.findMatching(entityClass, critBuilder.build());
97
98 if (queryResults.getResults() != null) {
99 return queryResults.getResults();
100 }
101
102 return new ArrayList<T>();
103 }
104
105
106
107
108
109
110
111
112
113 public static <T> void deleteMatching(DataObjectService dos, Class<T> entityClass, Map<String,?> queryAttrs) {
114 QueryByCriteria criteria = QueryByCriteria.Builder.andAttributes(queryAttrs).build();
115 dos.deleteMatching(entityClass, criteria);
116 }
117 }