View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (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
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Class to help with adapting BusinessObjectService calls to use the DataObjectService instead
31   */
32  public class BusinessObjectServiceMigrationUtils {
33  
34      /**
35       * Adapts BusinessObjectService.findByPrimaryKey calls to use the DataObjectService.
36       *
37       * @param dos the DataObjectService instance
38       * @param entityClass
39       * @param queryAttrs attribute:value pairs that will be ANDed together in the query
40       * @param <T> the type of the entity class
41       * @return the matching entity
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       * Adapts BusinessObjectService.findMatching calls to use the DataObjectService.
62       *
63       * @param dos the DataObjectService instance
64       * @param entityClass
65       * @param queryAttrs attribute:value pairs that will be ANDed together in the query
66       * @param <T> the type of the entity class
67       * @return the matching entities
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       * Adapts BusinessObjectService.findMatchingOrderBy calls to use the DataObjectService.
82       *
83       * @param dos the DataObjectService instance
84       * @param entityClass
85       * @param queryAttrs attribute:value pairs that will be ANDed together in the query
86       * @param orderByField
87       * @param sortAscending
88       * @param <T> the type of the entity class
89       * @return the matching entities
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      * Adapts BusinessObjectService.deleteMatching calls to use the DataObjectService.
107      *
108      * @param dos the DataObjectService instance
109      * @param entityClass
110      * @param queryAttrs attribute:value pairs that will be ANDed together in the query
111      * @param <T> the type of the entity class
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 }