001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.krad.dao;
017    
018    import java.util.Collection;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.rice.krad.bo.BusinessObject;
023    import org.kuali.rice.krad.bo.PersistableBusinessObject;
024    
025    /**
026     * This is the generic data access interface for business objects. This should be used for unit testing purposes only.
027     * 
028     * 
029     */
030    public interface BusinessObjectDao {
031        /**
032         * Saves any object that implements the BusinessObject interface.
033         * 
034         * @param bo
035         */
036        public PersistableBusinessObject save(PersistableBusinessObject bo);
037    
038        /**
039         * Saves a List of BusinessObjects.
040         * 
041         * @param businessObjects
042         */
043        public List<? extends PersistableBusinessObject> save(List businessObjects);
044    
045        /**
046         * Retrieves an object instance identified by its primary key. For composite keys, use {@link #findByPrimaryKey(Class, Map)}
047         * 
048         * @param clazz
049         * @param primaryKey
050         * @return
051         */
052        public <T extends BusinessObject> T findBySinglePrimaryKey(Class<T> clazz, Object primaryKey);
053        
054        /**
055         * Retrieves an object instance identified bys it primary keys and values. This can be done by constructing a map where the key
056         * to the map entry is the primary key attribute and the value of the entry being the primary key value. For composite keys,
057         * pass in each primaryKey attribute and its value as a map entry.
058         * 
059         * @param clazz
060         * @param primaryKeys
061         * @return
062         */
063        public <T extends BusinessObject> T findByPrimaryKey(Class<T> clazz, Map<String, ?> primaryKeys);
064        
065        /**
066             * Retrieves an object, based on its PK object
067             * 
068             * @param clazz the class of the object to retrieve
069             * @param pkObject the value of the primary key
070             * @return the retrieved PersistableBusinessObject
071             */
072            public abstract <T  extends BusinessObject> T findByPrimaryKeyUsingKeyObject(Class<T> clazz, Object pkObject);
073    
074        /**
075         * Retrieves an object instance identified by the class of the given object and the object's primary key values.
076         * 
077         * @param object
078         * @return
079         */
080        public PersistableBusinessObject retrieve(PersistableBusinessObject object);
081    
082        /**
083         * Retrieves a collection of business objects populated with data, such that each record in the database populates a new object
084         * instance. This will only retrieve business objects by class type.
085         * 
086         * @param clazz
087         * @return
088         */
089        public <T extends BusinessObject> Collection<T> findAll(Class<T> clazz);
090        
091        /**
092         * Retrieves a collection of business objects populated with data, such that each record in the database populates a new object
093         * instance. This will only retrieve business objects by class type.
094         * 
095         * Adds criteria on active column to return only active records. Assumes there exist a mapping for PropertyConstants.Active
096         * 
097         * @param clazz
098         * @return
099         */
100        public <T extends BusinessObject> Collection<T> findAllActive(Class<T> clazz);
101    
102        public <T extends BusinessObject> Collection<T> findAllInactive(Class<T> clazz);
103        
104        /**
105         * Retrieves a collection of business objects populated with data, such that each record in the database populates a new object
106         * instance. This will only retrieve business objects by class type. Orders the results by the given field.
107         * 
108         * @param clazz
109         * @return
110         */
111        public <T extends BusinessObject> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending);
112        
113        /**
114         * Retrieves a collection of business objects populated with data, such that each record in the database populates a new object
115         * instance. This will only retrieve business objects by class type. Orders the results by the given field.
116         * 
117         * Adds criteria on active column to return only active records. Assumes there exist a mapping for PropertyConstants.Active
118         * @param clazz
119         * @return
120         */
121        public <T extends BusinessObject> Collection<T> findAllActiveOrderBy(Class<T> clazz, String sortField, boolean sortAscending);
122    
123        /**
124         * This method retrieves a collection of business objects populated with data, such that each record in the database populates a
125         * new object instance. This will retrieve business objects by class type and also by criteria passed in as key-value pairs,
126         * specifically attribute name-expected value.
127         * 
128         * @param clazz
129         * @param fieldValues
130         * @return
131         */
132        public <T extends BusinessObject> Collection<T> findMatching(Class<T> clazz, Map<String, ?> fieldValues);
133        
134        /**
135         * Finds all entities matching the passed in Rice JPA criteria
136         * 
137         * @param <T> the type of the entity that will be returned
138         * @param criteria the criteria to form the query with
139         * @return a Collection (most likely a List) of all matching entities 
140         */
141        //public abstract <T extends BusinessObject> Collection<T> findMatching(Criteria criteria);
142        
143        /**
144         * This method retrieves a collection of business objects populated with data, such that each record in the database populates a
145         * new object instance. This will retrieve business objects by class type and also by criteria passed in as key-value pairs,
146         * specifically attribute name-expected value.
147         * 
148         * Adds criteria on active column to return only active records. Assumes there exist a mapping for PropertyConstants.Active
149         * 
150         * @param clazz
151         * @param fieldValues
152         * @return
153         */
154        public <T extends BusinessObject> Collection<T> findMatchingActive(Class<T> clazz, Map<String, ?> fieldValues);
155    
156        /**
157         * @param clazz
158         * @param fieldValues
159         * @return count of BusinessObjects of the given class whose fields match the values in the given Map.
160         */
161        public int countMatching(Class clazz, Map<String, ?> fieldValues);
162    
163        
164        /**
165         * 
166         * This method returns the number of matching result given the positive criterias and
167         * negative criterias. The negative criterias are the ones that will be set to 
168         * "notEqualTo" or "notIn"
169         * 
170         * @param clazz
171         * @param positiveFieldValues  Map of fields and values for positive criteria
172         * @param negativeFieldValues  Map of fields and values for negative criteria
173         * @return
174         */
175        public int countMatching(Class clazz, Map<String, ?> positiveFieldValues, Map<String, ?> negativeFieldValues);
176        
177        /**
178         * This method retrieves a collection of business objects populated with data, such that each record in the database populates a
179         * new object instance. This will retrieve business objects by class type and also by criteria passed in as key-value pairs,
180         * specifically attribute name-expected value. Orders the results by the given field.
181         * 
182         * @param clazz
183         * @param fieldValues
184         * @return
185         */
186        public <T extends BusinessObject> Collection<T> findMatchingOrderBy(Class<T> clazz, Map<String, ?> fieldValues, String sortField, boolean sortAscending);
187    
188        /**
189         * Deletes a business object from the database.
190         * 
191         * @param bo
192         */
193        public void delete(PersistableBusinessObject bo);
194    
195        /**
196         * Deletes each business object in the given List from the database.
197         * 
198         * @param boList
199         */
200        public void delete(List<? extends PersistableBusinessObject> boList);
201    
202        /**
203         * Deletes the business objects matching the given fieldValues
204         * 
205         * @param clazz
206         * @param fieldValues
207         */
208        public void deleteMatching(Class clazz, Map<String, ?> fieldValues);
209        
210        /**
211         * Merges the given business object, but tells the ORM that the object is to be treated as Read Only,
212         * and even if it has changes, it will not be persisted to the database 
213         * 
214         * @param bo the business object to managed
215         * @return the managed copied of the business object
216         */
217        public PersistableBusinessObject manageReadOnly(PersistableBusinessObject bo);
218    }