001/** 002 * Copyright 2005-2017 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krms.impl.repository; 017 018import org.apache.commons.collections.CollectionUtils; 019import org.kuali.rice.core.api.criteria.OrderByField; 020import org.kuali.rice.core.api.criteria.OrderDirection; 021import org.kuali.rice.core.api.criteria.QueryByCriteria; 022import org.kuali.rice.core.api.criteria.QueryResults; 023import org.kuali.rice.krad.data.DataObjectService; 024 025import java.util.ArrayList; 026import java.util.List; 027import java.util.Map; 028 029/** 030 * Class to help with adapting BusinessObjectService calls to use the DataObjectService instead 031 */ 032public class BusinessObjectServiceMigrationUtils { 033 034 /** 035 * Adapts BusinessObjectService.findByPrimaryKey calls to use the DataObjectService. 036 * 037 * @param dos the DataObjectService instance 038 * @param entityClass 039 * @param queryAttrs attribute:value pairs that will be ANDed together in the query 040 * @param <T> the type of the entity class 041 * @return the matching entity 042 */ 043 public static <T> T findSingleMatching(DataObjectService dos, Class<T> entityClass, Map<String,?> queryAttrs) { 044 QueryByCriteria criteria = QueryByCriteria.Builder.andAttributes(queryAttrs).build(); 045 QueryResults<T> queryResults = dos.findMatching(entityClass, criteria); 046 047 if (queryResults != null && !CollectionUtils.isEmpty(queryResults.getResults())) { 048 List<T> results = queryResults.getResults(); 049 050 if (results.size() != 1) { 051 throw new IllegalArgumentException("multiple results returned from query"); 052 } 053 054 return queryResults.getResults().get(0); 055 } 056 057 return null; 058 } 059 060 /** 061 * Adapts BusinessObjectService.findMatching calls to use the DataObjectService. 062 * 063 * @param dos the DataObjectService instance 064 * @param entityClass 065 * @param queryAttrs attribute:value pairs that will be ANDed together in the query 066 * @param <T> the type of the entity class 067 * @return the matching entities 068 */ 069 public static <T> List<T> findMatching(DataObjectService dos, Class<T> entityClass, Map<String,?> queryAttrs) { 070 QueryByCriteria criteria = QueryByCriteria.Builder.andAttributes(queryAttrs).build(); 071 QueryResults<T> queryResults = dos.findMatching(entityClass, criteria); 072 073 if (queryResults.getResults() != null) { 074 return queryResults.getResults(); 075 } 076 077 return new ArrayList<T>(); 078 } 079 080 /** 081 * Adapts BusinessObjectService.findMatchingOrderBy calls to use the DataObjectService. 082 * 083 * @param dos the DataObjectService instance 084 * @param entityClass 085 * @param queryAttrs attribute:value pairs that will be ANDed together in the query 086 * @param orderByField 087 * @param sortAscending 088 * @param <T> the type of the entity class 089 * @return the matching entities 090 */ 091 public static <T> List<T> findMatchingOrderBy(DataObjectService dos, Class<T> entityClass, Map<String,?> queryAttrs, String orderByField, boolean sortAscending) { 092 QueryByCriteria.Builder critBuilder = QueryByCriteria.Builder.andAttributes(queryAttrs); 093 OrderDirection sortDirection = sortAscending ? OrderDirection.ASCENDING : OrderDirection.DESCENDING; 094 critBuilder.setOrderByFields(OrderByField.Builder.create(orderByField, sortDirection).build()); 095 096 QueryResults<T> queryResults = dos.findMatching(entityClass, critBuilder.build()); 097 098 if (queryResults.getResults() != null) { 099 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}