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.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.QueryByCriteria;
20  import org.kuali.rice.core.api.criteria.QueryResults;
21  import org.kuali.rice.krad.data.DataObjectService;
22  import org.kuali.rice.krad.data.PersistenceOption;
23  import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
24  import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBindingQueryResults;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.LinkedList;
31  import java.util.List;
32  import java.util.Map;
33  
34  import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findMatching;
35  
36  /**
37   * Implementation of the @{link ReferenceObjectBindingBoService} interface for accessing  {@link ReferenceObjectBindingBo} related business objects.
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   * 
41   */
42  public class ReferenceObjectBindingBoServiceImpl implements ReferenceObjectBindingBoService {
43  
44      private DataObjectService dataObjectService;
45      private KrmsAttributeDefinitionService attributeDefinitionService;
46  
47      /**
48       * Sets the value of DataObjectService to the given value.
49       * 
50       * @param dataObjectService the DataObjectService value to set.
51       */
52      public void setDataObjectService(DataObjectService dataObjectService) {
53          this.dataObjectService = dataObjectService;
54      }
55  
56      public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
57          this.attributeDefinitionService = attributeDefinitionService;
58      }
59  
60      public KrmsAttributeDefinitionService getAttributeDefinitionService() {
61          if (attributeDefinitionService == null) {
62              attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
63          }
64  
65          return attributeDefinitionService;
66      }
67  
68      @Override
69      public ReferenceObjectBinding createReferenceObjectBinding(ReferenceObjectBinding referenceObjectBinding) {
70          incomingParamCheck(referenceObjectBinding , "referenceObjectBinding");
71          final String referenceObjectBindingIdKey = referenceObjectBinding.getId();
72          final ReferenceObjectBinding existing = getReferenceObjectBinding(referenceObjectBindingIdKey);
73  
74          if (existing != null) {
75              throw new IllegalStateException("the ReferenceObjectBinding to create already exists: " + referenceObjectBinding);
76          }
77  
78          ReferenceObjectBindingBo bo = dataObjectService.save(from(referenceObjectBinding), PersistenceOption.FLUSH);
79  
80          return ReferenceObjectBindingBo.to(bo);
81      }
82  
83      @Override
84      public ReferenceObjectBinding getReferenceObjectBinding(String referenceObjectBindingId) {
85          incomingParamCheck(referenceObjectBindingId , "referenceObjectBindingId");
86          ReferenceObjectBindingBo bo = dataObjectService.find(ReferenceObjectBindingBo.class, referenceObjectBindingId);
87  
88          return ReferenceObjectBindingBo.to(bo);
89      }
90  
91      @Override
92      public ReferenceObjectBinding updateReferenceObjectBinding(ReferenceObjectBinding referenceObjectBinding) {
93          incomingParamCheck(referenceObjectBinding , "referenceObjectBinding");
94          final ReferenceObjectBinding existing = getReferenceObjectBinding(referenceObjectBinding.getId());
95  
96          if (existing == null) {
97              throw new IllegalStateException("the ReferenceObjectBinding to update does not exists: " + referenceObjectBinding);
98          }
99  
100         final ReferenceObjectBinding toUpdate;
101 
102         if (!existing.getId().equals(referenceObjectBinding.getId())){
103             // if passed in id does not match existing id, correct it
104             final ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(referenceObjectBinding);
105             builder.setId(existing.getId());
106             toUpdate = builder.build();
107         } else {
108             toUpdate = referenceObjectBinding;
109         }
110 
111         // copy all updateable fields to bo
112         ReferenceObjectBindingBo boToUpdate = from(toUpdate);
113 
114         // update the rule and create new attributes
115         ReferenceObjectBindingBo updatedData = dataObjectService.save(boToUpdate, PersistenceOption.FLUSH);
116 
117         return to(updatedData);
118     }
119 
120     @Override
121     public void deleteReferenceObjectBinding(String referenceObjectBindingId) {
122         incomingParamCheck(referenceObjectBindingId , "referenceObjectBindingId");
123         final ReferenceObjectBinding existing = getReferenceObjectBinding(referenceObjectBindingId);
124 
125         if (existing == null) {
126             throw new IllegalStateException("the ReferenceObjectBinding to delete does not exists: " + referenceObjectBindingId);
127         }
128 
129         dataObjectService.delete(from(existing));
130     }
131 
132     @Override
133     public List<ReferenceObjectBinding> findReferenceObjectBindingsByCollectionName(String collectionName) {
134         if (org.apache.commons.lang.StringUtils.isBlank(collectionName)) {
135             throw new IllegalArgumentException("collectionName is null or blank");
136         }
137 
138         final Map<String, Object> map = new HashMap<String, Object>();
139         map.put("collectionName", collectionName);
140         List<ReferenceObjectBindingBo> bos = findMatching(dataObjectService, ReferenceObjectBindingBo.class, map);
141 
142         return convertBosToImmutables(bos);
143     }
144 
145     @Override
146     public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsDiscriminatorType(String krmsDiscriminatorType) {
147         if (org.apache.commons.lang.StringUtils.isBlank(krmsDiscriminatorType)) {
148             throw new IllegalArgumentException("krmsDiscriminatorType is null or blank");
149         }
150 
151         final Map<String, Object> map = new HashMap<String, Object>();
152         map.put("krmsDiscriminatorType", krmsDiscriminatorType);
153         List<ReferenceObjectBindingBo> bos = findMatching(dataObjectService, ReferenceObjectBindingBo.class, map);
154 
155         return convertBosToImmutables(bos);
156     }
157 
158     @Override
159     public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsObject(String krmsObjectId) {
160         if (org.apache.commons.lang.StringUtils.isBlank(krmsObjectId)) {
161             throw new IllegalArgumentException("krmsObjectId is null or blank");
162         }
163 
164         final Map<String, Object> map = new HashMap<String, Object>();
165         map.put("krmsObjectId", krmsObjectId);
166         List<ReferenceObjectBindingBo> bos = findMatching(dataObjectService, ReferenceObjectBindingBo.class, map);
167 
168         return convertBosToImmutables(bos);
169     }
170 
171     @Override
172     public List<ReferenceObjectBinding> findReferenceObjectBindingsByNamespace(String namespace) {
173         if (org.apache.commons.lang.StringUtils.isBlank(namespace)) {
174             throw new IllegalArgumentException("namespace is null or blank");
175         }
176 
177         final Map<String, Object> map = new HashMap<String, Object>();
178         map.put("namespace", namespace);
179         List<ReferenceObjectBindingBo> bos = findMatching(dataObjectService, ReferenceObjectBindingBo.class, map);
180 
181         return convertBosToImmutables(bos);
182     }
183 
184     @Override
185     public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceDiscriminatorType(String referenceDiscriminatorType) {
186         if (org.apache.commons.lang.StringUtils.isBlank(referenceDiscriminatorType)) {
187             throw new IllegalArgumentException("referenceDiscriminatorType is null or blank");
188         }
189 
190         final Map<String, Object> map = new HashMap<String, Object>();
191         map.put("referenceDiscriminatorType", referenceDiscriminatorType);
192         List<ReferenceObjectBindingBo> bos = findMatching(dataObjectService, ReferenceObjectBindingBo.class, map);
193 
194         return convertBosToImmutables(bos);
195     }
196 
197     @Override
198     public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceObject(String referenceObjectId) {
199         if (org.apache.commons.lang.StringUtils.isBlank(referenceObjectId)) {
200             throw new IllegalArgumentException("referenceObjectId is null or blank");
201         }
202 
203         final Map<String, Object> map = new HashMap<String, Object>();
204         map.put("referenceObjectId", referenceObjectId);
205         List<ReferenceObjectBindingBo> bos = findMatching(dataObjectService, ReferenceObjectBindingBo.class, map);
206 
207         return convertBosToImmutables(bos);
208     }
209 
210     @Override
211     public List<String> findReferenceObjectBindingIds(final QueryByCriteria queryByCriteria) {
212         incomingParamCheck(queryByCriteria, "queryByCriteria");
213         ReferenceObjectBindingQueryResults results = findReferenceObjectBindings(queryByCriteria);
214         List<String> result = new ArrayList<String>();
215 
216         for (ReferenceObjectBinding referenceObjectBinding: results.getResults()) {
217             result.add(referenceObjectBinding.getId());
218         }
219 
220         return Collections.unmodifiableList(result);
221     }
222 
223     @Override
224     public ReferenceObjectBindingQueryResults findReferenceObjectBindings(final QueryByCriteria queryByCriteria) {
225         QueryResults<ReferenceObjectBindingBo> results =
226                 dataObjectService.findMatching(ReferenceObjectBindingBo.class, queryByCriteria);
227 
228         ReferenceObjectBindingQueryResults.Builder builder = ReferenceObjectBindingQueryResults.Builder.create();
229         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
230         builder.setTotalRowCount(results.getTotalRowCount());
231         final List<ReferenceObjectBinding.Builder> ims = new ArrayList<ReferenceObjectBinding.Builder>();
232 
233         for (ReferenceObjectBindingBo bo : results.getResults()) {
234             ims.add(ReferenceObjectBinding.Builder.create(bo));
235         }
236 
237         builder.setResults(ims);
238 
239         return builder.build();
240     }
241 
242     public List<ReferenceObjectBinding> convertBosToImmutables(final Collection<ReferenceObjectBindingBo> referenceObjectBindingBos) {
243         List<ReferenceObjectBinding> immutables = new LinkedList<ReferenceObjectBinding>();
244         if (referenceObjectBindingBos != null) {
245             ReferenceObjectBinding immutable = null;
246 
247             for (ReferenceObjectBindingBo bo : referenceObjectBindingBos ) {
248                 immutable = to(bo);
249                 immutables.add(immutable);
250             }
251         }
252 
253         return Collections.unmodifiableList(immutables);
254     }
255 
256     @Override
257     public ReferenceObjectBinding to(ReferenceObjectBindingBo referenceObjectBindingBo) {
258         return ReferenceObjectBindingBo.to(referenceObjectBindingBo);
259     }
260 
261     public ReferenceObjectBindingBo from(ReferenceObjectBinding referenceObjectBinding) {
262         return ReferenceObjectBindingBo.from(referenceObjectBinding);
263     }
264 
265     private void incomingParamCheck(Object object, String name) {
266         if (object == null) {
267             throw new IllegalArgumentException(name + " was null");
268         } else if (object instanceof String
269                 && StringUtils.isBlank((String)object)) {
270             throw new IllegalArgumentException(name + " was blank");
271         }
272     }
273 }