View Javadoc
1   /**
2    * Copyright 2005-2016 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 
110         final Map<String, Object> map = new HashMap<String, Object>();
111         map.put("typeId", typeId);
112         Collection<PropositionBo> bos = (Collection<PropositionBo>) businessObjectService.findMatching(PropositionBo.class, map);
113 
114         return convertBosToImmutables(bos);
115     }
116 
117     @Override
118     public Set<PropositionDefinition> getPropositionsByRule(String ruleId) {
119         if (org.apache.commons.lang.StringUtils.isBlank(ruleId)) {
120             throw new IllegalArgumentException("ruleId is null or blank");
121         }
122 
123         final Map<String, Object> map = new HashMap<String, Object>();
124         map.put("ruleId", ruleId);
125         Collection<PropositionBo> bos = (Collection<PropositionBo>) businessObjectService.findMatching(PropositionBo.class, map);
126 
127         return convertBosToImmutables(bos);
128     }
129 
130     public Set<PropositionDefinition> convertBosToImmutables(final Collection<PropositionBo> propositionBos) {
131         Set<PropositionDefinition> immutables = new HashSet<PropositionDefinition>();
132         if (propositionBos != null) {
133             PropositionDefinition immutable = null;
134             for (PropositionBo bo : propositionBos ) {
135                 immutable = to(bo);
136                 immutables.add(immutable);
137             }
138         }
139         return Collections.unmodifiableSet(immutables);
140     }
141 
142     public PropositionDefinition to(PropositionBo propositionBo) {
143         return PropositionBo.to(propositionBo);
144     }
145 
146 	/**
147 	 * This overridden method creates a PropositionParameter if it does not 
148 	 * already exist in the repository.
149 	 * 
150 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#createParameter(org.kuali.rice.krms.api.repository.proposition.PropositionParameter)
151 	 */
152 	@Override
153 	public void createParameter(PropositionParameter parameter) {
154 		if (parameter == null){
155 	        throw new IllegalArgumentException("parameter is null");
156 		}
157 		final String propIdKey = parameter.getPropId();
158 		final Integer seqNoKey = parameter.getSequenceNumber();
159 		final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
160 		if (existing != null && existing.getPropId().equals(propIdKey) && existing.getSequenceNumber().equals(seqNoKey)){
161             throw new IllegalStateException("the parameter to create already exists: " + parameter);			
162 		}
163 		
164 		businessObjectService.save(PropositionParameterBo.from(parameter));
165 	}
166 
167 	/**
168 	 * This overridden method updates an existing proposition parameter
169 	 * 
170 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#updateParameter(org.kuali.rice.krms.api.repository.proposition.PropositionParameter)
171 	 */
172 	@Override
173 	public void updateParameter(PropositionParameter parameter) {
174         if (parameter == null) {
175             throw new IllegalArgumentException("parameter is null");
176         }
177 		final String propIdKey = parameter.getPropId();
178 		final Integer seqNoKey = parameter.getSequenceNumber();
179 		final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
180         if (existing == null) {
181             throw new IllegalStateException("the parameter does not exist: " + parameter);
182         }
183         final PropositionParameter toUpdate;
184         if (!existing.getId().equals(parameter.getId())){
185         	final PropositionParameter.Builder builder = PropositionParameter.Builder.create(parameter);
186         	builder.setId(existing.getId());
187         	toUpdate = builder.build();
188         } else {
189         	toUpdate = parameter;
190         }
191         
192         businessObjectService.save(PropositionParameterBo.from(toUpdate));
193 	}
194 
195     @Override
196     public void deleteProposition(String propId) {
197         if (propId == null){ throw new IllegalArgumentException("propId is null"); }
198         final PropositionDefinition existing = getPropositionById(propId);
199         if (existing == null){ throw new IllegalStateException("the Proposition to delete does not exists: " + propId);}
200 
201         List<PropositionParameter> propositionParameters = existing.getParameters();
202         for( PropositionParameter prop : propositionParameters) {
203             businessObjectService.delete(PropositionParameterBo.from(prop));
204         }
205 
206         businessObjectService.delete(from(existing));
207     }
208     
209 	/**
210 	 * This overridden method retrieves a list of parameters for a given proposition
211 	 * 
212 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameters(java.lang.Long)
213 	 */
214 	@Override
215 	public List<PropositionParameter> getParameters(String propId) {
216 		if (StringUtils.isBlank(propId)) {
217             throw new IllegalArgumentException("propId is null or blank");
218 		}
219         final Map<String, Object> map = new HashMap<String, Object>();
220         map.put("propId", propId);
221 		List<PropositionParameterBo> bos = (List<PropositionParameterBo>) businessObjectService.findMatchingOrderBy(PropositionParameterBo.class, map, "sequenceNumber", true);
222 		return PropositionParameterBo.to(bos);
223 	}
224 
225 	/**
226 	 * This overridden method gets a parameter by the parameter id
227 	 * 
228 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameterById(java.lang.String)
229 	 */
230 	@Override
231 	public PropositionParameter getParameterById(String id) {
232 		if (StringUtils.isBlank(id)) {
233             throw new IllegalArgumentException("id is null or blank");
234 		}
235 		PropositionParameterBo bo = businessObjectService.findBySinglePrimaryKey(PropositionParameterBo.class, id);
236 		return PropositionParameterBo.to(bo);
237 	}
238 
239 	/**
240 	 * This overridden method gets a parameter by the Proposition Id and Sequence Number
241 	 * 
242 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameterByPropIdAndSequenceNumber(java.lang.String, java.lang.String)
243 	 */
244 	@Override
245 	public PropositionParameter getParameterByPropIdAndSequenceNumber(
246 			String propId, Integer sequenceNumber) {
247 		if (StringUtils.isBlank(propId)) {
248             throw new IllegalArgumentException("propId is null or blank");
249 		}
250 		if (sequenceNumber == null) {
251             throw new IllegalArgumentException("sequenceNumber is null");
252 		}
253         final Map<String, Object> map = new HashMap<String, Object>();
254         map.put("propId", propId);
255         map.put("sequenceNumber", sequenceNumber);
256 		PropositionParameterBo bo = businessObjectService.findByPrimaryKey(PropositionParameterBo.class, map);
257 		return PropositionParameterBo.to(bo);
258 	}
259     /**
260      * Converts a immutable {@link PropositionDefinition} to its mutable {@link PropositionBo} counterpart.
261      * @param proposition the immutable object.
262      * @return a {@link PropositionBo} the mutable PropositionBo.
263      *
264      */
265     public PropositionBo from(PropositionDefinition proposition) {
266         if (proposition == null) { return null; }
267         PropositionBo propositionBo = new PropositionBo();
268         propositionBo.setDescription(proposition.getDescription());
269         propositionBo.setTypeId(proposition.getTypeId());
270         propositionBo.setRuleId(proposition.getRuleId());
271         propositionBo.setPropositionTypeCode(proposition.getPropositionTypeCode());
272         propositionBo.setCompoundOpCode(proposition.getCompoundOpCode());
273         propositionBo.setId(proposition.getId());
274         propositionBo.setVersionNumber(proposition.getVersionNumber());
275         return propositionBo;
276     }
277 
278     /**
279      * Sets the businessObjectService attribute value.
280      *
281      * @param businessObjectService The businessObjectService to set.
282      */
283     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
284         this.businessObjectService = businessObjectService;
285     }
286 
287 }