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