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.krad.data.DataObjectService;
20  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
21  import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.*;
32  
33  /**
34   * Implementation of the interface for accessing KRMS repository Proposition related
35   * business objects.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   */
40  public final class PropositionBoServiceImpl implements PropositionBoService {
41  
42      private DataObjectService dataObjectService;
43  
44      /**
45       * This overridden method creates a Proposition if it does not
46       * already exist in the repository.
47       *
48       * @see org.kuali.rice.krms.impl.repository.PropositionBoService#createProposition(org.kuali.rice.krms.api.repository.proposition.PropositionDefinition)
49       */
50      @Override
51      public PropositionDefinition createProposition(PropositionDefinition prop) {
52          if (prop == null){
53              throw new IllegalArgumentException("proposition is null");
54          }
55          if (null != prop.getId()) {
56              throw new IllegalStateException("for creation, PropositionDefinition.id must be null");
57          }
58  
59          PropositionBo propositionBo = PropositionBo.from(prop);
60          propositionBo = dataObjectService.save(propositionBo);
61  
62          return PropositionBo.to(propositionBo);
63      }
64  
65      /**
66       * This overridden method updates an existing proposition
67       *
68       * @see org.kuali.rice.krms.impl.repository.PropositionBoService#updateProposition(org.kuali.rice.krms.api.repository.proposition.PropositionDefinition)
69       */
70      @Override
71      public void updateProposition(PropositionDefinition prop) {
72          if (prop == null) {
73              throw new IllegalArgumentException("proposition is null");
74          }
75  
76          final String propIdKey = prop.getId();
77          final PropositionDefinition existing = getPropositionById(propIdKey);
78  
79          if (existing == null) {
80              throw new IllegalStateException("the proposition does not exist: " + prop);
81          }
82  
83          final PropositionDefinition toUpdate;
84  
85          if (!existing.getId().equals(prop.getId())) {
86          	final PropositionDefinition.Builder builder = PropositionDefinition.Builder.create(prop);
87          	builder.setId(existing.getId());
88          	toUpdate = builder.build();
89          } else {
90          	toUpdate = prop;
91          }
92          
93          dataObjectService.save(PropositionBo.from(toUpdate));
94      }
95  
96      /**
97       * This overridden method retrieves a proposition by the give proposition id.
98       *
99       * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getPropositionById(java.lang.String)
100      */
101     @Override
102     public PropositionDefinition getPropositionById(String propId) {
103         if (StringUtils.isBlank(propId)){
104             throw new IllegalArgumentException("propId is null or blank");
105         }
106 
107         PropositionBo bo = dataObjectService.find(PropositionBo.class, propId);
108 
109         return PropositionBo.to(bo);
110     }
111 
112     @Override
113     public Set<PropositionDefinition> getPropositionsByType(String typeId) {
114         if (org.apache.commons.lang.StringUtils.isBlank(typeId)) {
115             throw new IllegalArgumentException("typeId is null or blank");
116         }
117 
118         final Map<String, Object> map = new HashMap<String, Object>();
119         map.put("typeId", typeId);
120         Collection<PropositionBo> bos = findMatching(dataObjectService, PropositionBo.class, map);
121 
122         return convertBosToImmutables(bos);
123     }
124 
125     @Override
126     public Set<PropositionDefinition> getPropositionsByRule(String ruleId) {
127         if (org.apache.commons.lang.StringUtils.isBlank(ruleId)) {
128             throw new IllegalArgumentException("ruleId is null or blank");
129         }
130 
131         final Map<String, Object> map = new HashMap<String, Object>();
132         map.put("ruleId", ruleId);
133         Collection<PropositionBo> bos = findMatching(dataObjectService, PropositionBo.class, map);
134 
135         return convertBosToImmutables(bos);
136     }
137 
138     public Set<PropositionDefinition> convertBosToImmutables(final Collection<PropositionBo> propositionBos) {
139         Set<PropositionDefinition> immutables = new HashSet<PropositionDefinition>();
140 
141         if (propositionBos != null) {
142             PropositionDefinition immutable = null;
143             for (PropositionBo bo : propositionBos ) {
144                 immutable = to(bo);
145                 immutables.add(immutable);
146             }
147         }
148 
149         return Collections.unmodifiableSet(immutables);
150     }
151 
152     public PropositionDefinition to(PropositionBo propositionBo) {
153         return PropositionBo.to(propositionBo);
154     }
155 
156     /**
157      * This overridden method creates a PropositionParameter if it does not
158      * already exist in the repository.
159      *
160      * @see org.kuali.rice.krms.impl.repository.PropositionBoService#createParameter(org.kuali.rice.krms.api.repository.proposition.PropositionParameter)
161      */
162     @Override
163     public void createParameter(PropositionParameter parameter) {
164         if (parameter == null){
165             throw new IllegalArgumentException("parameter is null");
166         }
167 
168         final String propIdKey = parameter.getPropId();
169         final Integer seqNoKey = parameter.getSequenceNumber();
170         final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
171 
172         if (existing != null && existing.getPropId().equals(propIdKey) && existing.getSequenceNumber().equals(seqNoKey)){
173             throw new IllegalStateException("the parameter to create already exists: " + parameter);
174         }
175 
176         dataObjectService.save(PropositionParameterBo.from(parameter));
177     }
178 
179     /**
180      * This overridden method updates an existing proposition parameter
181      *
182      * @see org.kuali.rice.krms.impl.repository.PropositionBoService#updateParameter(org.kuali.rice.krms.api.repository.proposition.PropositionParameter)
183      */
184     @Override
185     public void updateParameter(PropositionParameter parameter) {
186         if (parameter == null) {
187             throw new IllegalArgumentException("parameter is null");
188         }
189 
190         final String propIdKey = parameter.getPropId();
191         final Integer seqNoKey = parameter.getSequenceNumber();
192         final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
193 
194         if (existing == null) {
195             throw new IllegalStateException("the parameter does not exist: " + parameter);
196         }
197 
198         final PropositionParameter toUpdate;
199 
200         if (!existing.getId().equals(parameter.getId())){
201         	final PropositionParameter.Builder builder = PropositionParameter.Builder.create(parameter);
202         	builder.setId(existing.getId());
203         	toUpdate = builder.build();
204         } else {
205         	toUpdate = parameter;
206         }
207         
208         dataObjectService.save(PropositionParameterBo.from(toUpdate));
209 	}
210 
211     @Override
212     public void deleteProposition(String propId) {
213         if (propId == null) { throw new IllegalArgumentException("propId is null"); }
214 
215         final PropositionDefinition existing = getPropositionById(propId);
216 
217         if (existing == null){
218             throw new IllegalStateException("the Proposition to delete does not exists: " + propId);
219         }
220 
221         dataObjectService.delete(from(existing));
222     }
223 
224     /**
225      * This overridden method retrieves a list of parameters for a given proposition
226      *
227      * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameters(java.lang.String)
228      */
229     @Override
230     public List<PropositionParameter> getParameters(String propId) {
231         if (StringUtils.isBlank(propId)) {
232             throw new IllegalArgumentException("propId is null or blank");
233         }
234 
235         final Map<String, Object> criteriaMap = Collections.<String, Object>singletonMap("propId", propId);
236 
237         List<PropositionParameterBo> bos =
238                 findMatchingOrderBy(dataObjectService, PropositionParameterBo.class, criteriaMap, "sequenceNumber", true);
239 
240         return PropositionParameterBo.to(bos);
241     }
242 
243     /**
244      * This overridden method gets a parameter by the parameter id
245      *
246      * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameterById(java.lang.String)
247      */
248     @Override
249     public PropositionParameter getParameterById(String id) {
250         if (StringUtils.isBlank(id)) {
251             throw new IllegalArgumentException("id is null or blank");
252         }
253 
254         PropositionParameterBo bo = dataObjectService.find(PropositionParameterBo.class, id);
255 
256         return PropositionParameterBo.to(bo);
257     }
258 
259     /**
260      * This overridden method gets a parameter by the Proposition Id and Sequence Number
261      *
262      * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameterByPropIdAndSequenceNumber(String, Integer)
263      */
264     @Override
265     public PropositionParameter getParameterByPropIdAndSequenceNumber(String propId, Integer sequenceNumber) {
266         if (StringUtils.isBlank(propId)) {
267             throw new IllegalArgumentException("propId is null or blank");
268         }
269         if (sequenceNumber == null) {
270             throw new IllegalArgumentException("sequenceNumber is null");
271         }
272 
273         final Map<String, Object> map = new HashMap<String, Object>();
274         map.put("propId", propId);
275         map.put("sequenceNumber", sequenceNumber);
276         PropositionParameterBo bo = findSingleMatching(dataObjectService, PropositionParameterBo.class, map);
277 
278         return PropositionParameterBo.to(bo);
279     }
280 
281     /**
282      * Converts a immutable {@link PropositionDefinition} to its mutable {@link PropositionBo} counterpart.
283      * @param proposition the immutable object.
284      * @return a {@link PropositionBo} the mutable PropositionBo.
285      *
286      */
287     public PropositionBo from(PropositionDefinition proposition) {
288         if (proposition == null) { return null; }
289 
290         PropositionBo propositionBo = new PropositionBo();
291         propositionBo.setDescription(proposition.getDescription());
292         propositionBo.setTypeId(proposition.getTypeId());
293         propositionBo.setRuleId(proposition.getRuleId());
294         propositionBo.setPropositionTypeCode(proposition.getPropositionTypeCode());
295         propositionBo.setCompoundOpCode(proposition.getCompoundOpCode());
296         propositionBo.setId(proposition.getId());
297         propositionBo.setVersionNumber(proposition.getVersionNumber());
298 
299         return propositionBo;
300     }
301 
302     /**
303      * Sets the dataObjectService attribute value.
304      *
305      * @param dataObjectService The dataObjectService to set.
306      */
307     public void setDataObjectService(DataObjectService dataObjectService) {
308         this.dataObjectService = dataObjectService;
309     }
310 
311 }