View Javadoc

1   /**
2    * Copyright 2005-2013 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 void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
64          this.attributeDefinitionService = attributeDefinitionService;
65      }
66  
67      public KrmsAttributeDefinitionService getAttributeDefinitionService() {
68          if (attributeDefinitionService == null) {
69              attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
70          }
71          return attributeDefinitionService;
72      }
73  
74      @Override
75      public ReferenceObjectBinding createReferenceObjectBinding(ReferenceObjectBinding referenceObjectBinding) {
76          incomingParamCheck(referenceObjectBinding , "referenceObjectBinding");
77          final String referenceObjectBindingIdKey = referenceObjectBinding.getId();
78          final ReferenceObjectBinding existing = getReferenceObjectBinding(referenceObjectBindingIdKey);
79          if (existing != null){ throw new IllegalStateException("the ReferenceObjectBinding to create already exists: " + referenceObjectBinding);	}
80          ReferenceObjectBindingBo bo = (ReferenceObjectBindingBo)businessObjectService.save(from(referenceObjectBinding));
81          return ReferenceObjectBindingBo.to(bo);
82      }
83  
84      @Override
85      public ReferenceObjectBinding getReferenceObjectBinding(String referenceObjectBindingId) {
86          incomingParamCheck(referenceObjectBindingId , "referenceObjectBindingId");
87          ReferenceObjectBindingBo bo = businessObjectService.findBySinglePrimaryKey(ReferenceObjectBindingBo.class, referenceObjectBindingId);
88          return ReferenceObjectBindingBo.to(bo);
89      }
90  
91      @Override
92      public void updateReferenceObjectBinding(ReferenceObjectBinding referenceObjectBinding) {
93          incomingParamCheck(referenceObjectBinding , "referenceObjectBinding");
94          final ReferenceObjectBinding existing = getReferenceObjectBinding(referenceObjectBinding.getId());
95          if (existing == null){ throw new IllegalStateException("the ReferenceObjectBinding to update does not exists: " + referenceObjectBinding);}
96          final ReferenceObjectBinding toUpdate;
97          if (!existing.getId().equals(referenceObjectBinding.getId())){
98              // if passed in id does not match existing id, correct it
99              final ReferenceObjectBinding.Builder builder = ReferenceObjectBinding.Builder.create(referenceObjectBinding);
100             builder.setId(existing.getId());
101             toUpdate = builder.build();
102         } else {
103             toUpdate = referenceObjectBinding;
104         }
105 
106         // copy all updateable fields to bo
107         ReferenceObjectBindingBo boToUpdate = from(toUpdate);
108 
109         // update the rule and create new attributes
110          businessObjectService.save(boToUpdate);
111     }
112 
113     @Override
114     public void deleteReferenceObjectBinding(String referenceObjectBindingId) {
115         incomingParamCheck(referenceObjectBindingId , "referenceObjectBindingId");
116         final ReferenceObjectBinding existing = getReferenceObjectBinding(referenceObjectBindingId);
117         if (existing == null){ throw new IllegalStateException("the ReferenceObjectBinding to delete does not exists: " + referenceObjectBindingId);}
118         businessObjectService.delete(from(existing));
119     }
120 
121     @Override
122     public List<ReferenceObjectBinding> findReferenceObjectBindingsByCollectionName(String collectionName) {
123         if (org.apache.commons.lang.StringUtils.isBlank(collectionName)) {
124             throw new IllegalArgumentException("collectionName is null or blank");
125         }
126         final Map<String, Object> map = new HashMap<String, Object>();
127         map.put("collectionName", collectionName);
128         List<ReferenceObjectBindingBo> bos = (List<ReferenceObjectBindingBo>) businessObjectService.findMatching(ReferenceObjectBindingBo.class, map);
129         return convertBosToImmutables(bos);
130     }
131 
132     @Override
133     public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsDiscriminatorType(String krmsDiscriminatorType) {
134         if (org.apache.commons.lang.StringUtils.isBlank(krmsDiscriminatorType)) {
135             throw new IllegalArgumentException("krmsDiscriminatorType is null or blank");
136         }
137         final Map<String, Object> map = new HashMap<String, Object>();
138         map.put("krmsDiscriminatorType", krmsDiscriminatorType);
139         List<ReferenceObjectBindingBo> bos = (List<ReferenceObjectBindingBo>) businessObjectService.findMatching(ReferenceObjectBindingBo.class, map);
140         return convertBosToImmutables(bos);
141     }
142 
143     @Override
144     public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsObject(String krmsObjectId) {
145         if (org.apache.commons.lang.StringUtils.isBlank(krmsObjectId)) {
146             throw new IllegalArgumentException("krmsObjectId is null or blank");
147         }
148         final Map<String, Object> map = new HashMap<String, Object>();
149         map.put("krmsObjectId", krmsObjectId);
150         List<ReferenceObjectBindingBo> bos = (List<ReferenceObjectBindingBo>) businessObjectService.findMatching(ReferenceObjectBindingBo.class, map);
151         return convertBosToImmutables(bos);
152     }
153 
154     @Override
155     public List<ReferenceObjectBinding> findReferenceObjectBindingsByNamespace(String namespace) {
156         if (org.apache.commons.lang.StringUtils.isBlank(namespace)) {
157             throw new IllegalArgumentException("namespace is null or blank");
158         }
159         final Map<String, Object> map = new HashMap<String, Object>();
160         map.put("namespace", namespace);
161         List<ReferenceObjectBindingBo> bos = (List<ReferenceObjectBindingBo>) businessObjectService.findMatching(ReferenceObjectBindingBo.class, map);
162         return convertBosToImmutables(bos);
163     }
164 
165     @Override
166     public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceDiscriminatorType(String referenceDiscriminatorType) {
167         if (org.apache.commons.lang.StringUtils.isBlank(referenceDiscriminatorType)) {
168             throw new IllegalArgumentException("referenceDiscriminatorType is null or blank");
169         }
170         final Map<String, Object> map = new HashMap<String, Object>();
171         map.put("referenceDiscriminatorType", referenceDiscriminatorType);
172         List<ReferenceObjectBindingBo> bos = (List<ReferenceObjectBindingBo>) businessObjectService.findMatching(ReferenceObjectBindingBo.class, map);
173         return convertBosToImmutables(bos);
174     }
175 
176     @Override
177     public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceObject(String referenceObjectId) {
178         if (org.apache.commons.lang.StringUtils.isBlank(referenceObjectId)) {
179             throw new IllegalArgumentException("referenceObjectId is null or blank");
180         }
181         final Map<String, Object> map = new HashMap<String, Object>();
182         map.put("referenceObjectId", referenceObjectId);
183         List<ReferenceObjectBindingBo> bos = (List<ReferenceObjectBindingBo>) businessObjectService.findMatching(ReferenceObjectBindingBo.class, map);
184         return convertBosToImmutables(bos);
185     }
186 
187     @Override
188     public List<String> findReferenceObjectBindingIds(final QueryByCriteria queryByCriteria) {
189         incomingParamCheck(queryByCriteria, "queryByCriteria");
190         ReferenceObjectBindingQueryResults results = findReferenceObjectBindings(queryByCriteria);
191         List<String> result = new ArrayList<String>();
192         for (ReferenceObjectBinding referenceObjectBinding: results.getResults()) {
193             result.add(referenceObjectBinding.getId());
194         }
195         return Collections.unmodifiableList(result);
196     }
197 
198     @Override
199     public ReferenceObjectBindingQueryResults findReferenceObjectBindings(final QueryByCriteria queryByCriteria) {
200         LookupCustomizer.Builder<ReferenceObjectBindingBo> lc = LookupCustomizer.Builder.create();
201         lc.setPredicateTransform(AttributeTransform.getInstance());
202         GenericQueryResults<ReferenceObjectBindingBo> results = criteriaLookupService.lookup(ReferenceObjectBindingBo.class, queryByCriteria, lc.build());
203         ReferenceObjectBindingQueryResults.Builder builder = ReferenceObjectBindingQueryResults.Builder.create();
204         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
205         builder.setTotalRowCount(results.getTotalRowCount());
206         final List<ReferenceObjectBinding.Builder> ims = new ArrayList<ReferenceObjectBinding.Builder>();
207         for (ReferenceObjectBindingBo bo : results.getResults()) {
208             ims.add(ReferenceObjectBinding.Builder.create(bo));
209         }
210         builder.setResults(ims);
211         return builder.build();
212     }
213 
214     public List<ReferenceObjectBinding> convertBosToImmutables(final Collection<ReferenceObjectBindingBo> referenceObjectBindingBos) {
215         List<ReferenceObjectBinding> immutables = new LinkedList<ReferenceObjectBinding>();
216         if (referenceObjectBindingBos != null) {
217             ReferenceObjectBinding immutable = null;
218             for (ReferenceObjectBindingBo bo : referenceObjectBindingBos ) {
219                 immutable = to(bo);
220                 immutables.add(immutable);
221             }
222         }
223         return Collections.unmodifiableList(immutables);
224     }
225 
226     @Override
227     public ReferenceObjectBinding to(ReferenceObjectBindingBo referenceObjectBindingBo) {
228         return ReferenceObjectBindingBo.to(referenceObjectBindingBo);
229     }
230 
231     public ReferenceObjectBindingBo from(ReferenceObjectBinding referenceObjectBinding) {
232         return ReferenceObjectBindingBo.from(referenceObjectBinding);
233     }
234 
235     private void incomingParamCheck(Object object, String name) {
236         if (object == null) {
237             throw new IllegalArgumentException(name + " was null");
238         } else if (object instanceof String
239                 && StringUtils.isBlank((String)object)) {
240             throw new IllegalArgumentException(name + " was blank");
241         }
242     }
243 
244 }