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