View Javadoc

1   /**
2    * Copyright 2005-2012 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.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.rice.krad.service.BusinessObjectService;
25  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
26  import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
27  
28  public final class PropositionBoServiceImpl implements PropositionBoService {
29  
30      private BusinessObjectService businessObjectService;
31  
32  	/**
33  	 * This overridden method creates a Proposition if it does not 
34  	 * already exist in the repository.
35  	 * 
36  	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#createProposition(org.kuali.rice.krms.api.repository.proposition.PropositionDefinition)
37  	 */
38  	@Override
39  	public PropositionDefinition createProposition(PropositionDefinition prop) {
40  		if (prop == null){
41  	        throw new IllegalArgumentException("proposition is null");
42  		}
43  		if (null != prop.getId()) {
44  	            throw new IllegalStateException("for creation, PropositionDefinition.id must be null");
45  	    }
46  		
47  		PropositionBo propositionBo = PropositionBo.from(prop);
48  		businessObjectService.save(propositionBo);
49  		return PropositionBo.to(propositionBo);
50  	}
51  
52  	/**
53  	 * This overridden method updates an existing proposition
54  	 * 
55  	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#updateProposition(org.kuali.rice.krms.api.repository.proposition.PropositionDefinition)
56  	 */
57  	@Override
58  	public void updateProposition(PropositionDefinition prop) {
59          if (prop == null) {
60              throw new IllegalArgumentException("proposition is null");
61          }
62  		final String propIdKey = prop.getId();
63  		final PropositionDefinition existing = getPropositionById(propIdKey);
64          if (existing == null) {
65              throw new IllegalStateException("the proposition does not exist: " + prop);
66          }
67          final PropositionDefinition toUpdate;
68          if (!existing.getId().equals(prop.getId())){
69          	final PropositionDefinition.Builder builder = PropositionDefinition.Builder.create(prop);
70          	builder.setId(existing.getId());
71          	toUpdate = builder.build();
72          } else {
73          	toUpdate = prop;
74          }
75          
76          businessObjectService.save(PropositionBo.from(toUpdate));
77  	}
78  
79  	/**
80  	 * This overridden method retrieves a proposition by the give proposition id.
81  	 * 
82  	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getPropositionById(java.lang.String)
83  	 */
84  	@Override
85  	public PropositionDefinition getPropositionById(String propId) {
86  		if (StringUtils.isBlank(propId)){
87              throw new IllegalArgumentException("propId is null or blank");			
88  		}
89  		PropositionBo bo = businessObjectService.findBySinglePrimaryKey(PropositionBo.class, propId);
90  		return PropositionBo.to(bo);
91  	}
92  
93  	/**
94  	 * This overridden method creates a PropositionParameter if it does not 
95  	 * already exist in the repository.
96  	 * 
97  	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#createParameter(org.kuali.rice.krms.api.repository.proposition.PropositionParameter)
98  	 */
99  	@Override
100 	public void createParameter(PropositionParameter parameter) {
101 		if (parameter == null){
102 	        throw new IllegalArgumentException("parameter is null");
103 		}
104 		final String propIdKey = parameter.getPropId();
105 		final Integer seqNoKey = parameter.getSequenceNumber();
106 		final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
107 		if (existing != null && existing.getPropId().equals(propIdKey) && existing.getSequenceNumber().equals(seqNoKey)){
108             throw new IllegalStateException("the parameter to create already exists: " + parameter);			
109 		}
110 		
111 		businessObjectService.save(PropositionParameterBo.from(parameter));
112 	}
113 
114 	/**
115 	 * This overridden method updates an existing proposition parameter
116 	 * 
117 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#updateParameter(org.kuali.rice.krms.api.repository.proposition.PropositionParameter)
118 	 */
119 	@Override
120 	public void updateParameter(PropositionParameter parameter) {
121         if (parameter == null) {
122             throw new IllegalArgumentException("parameter is null");
123         }
124 		final String propIdKey = parameter.getPropId();
125 		final Integer seqNoKey = parameter.getSequenceNumber();
126 		final PropositionParameter existing = getParameterByPropIdAndSequenceNumber(propIdKey, seqNoKey);
127         if (existing == null) {
128             throw new IllegalStateException("the parameter does not exist: " + parameter);
129         }
130         final PropositionParameter toUpdate;
131         if (!existing.getId().equals(parameter.getId())){
132         	final PropositionParameter.Builder builder = PropositionParameter.Builder.create(parameter);
133         	builder.setId(existing.getId());
134         	toUpdate = builder.build();
135         } else {
136         	toUpdate = parameter;
137         }
138         
139         businessObjectService.save(PropositionParameterBo.from(toUpdate));
140 	}
141 
142 	/**
143 	 * This overridden method retrieves a list of parameters for a given proposition
144 	 * 
145 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameters(java.lang.Long)
146 	 */
147 	@Override
148 	public List<PropositionParameter> getParameters(String propId) {
149 		if (StringUtils.isBlank(propId)) {
150             throw new IllegalArgumentException("propId is null or blank");
151 		}
152         final Map<String, Object> map = new HashMap<String, Object>();
153         map.put("propId", propId);
154 		List<PropositionParameterBo> bos = (List<PropositionParameterBo>) businessObjectService.findMatchingOrderBy(PropositionParameterBo.class, map, "sequenceNumber", true);
155 		return PropositionParameterBo.to(bos);
156 	}
157 
158 	/**
159 	 * This overridden method gets a parameter by the parameter id
160 	 * 
161 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameterById(java.lang.String)
162 	 */
163 	@Override
164 	public PropositionParameter getParameterById(String id) {
165 		if (StringUtils.isBlank(id)) {
166             throw new IllegalArgumentException("id is null or blank");
167 		}
168 		PropositionParameterBo bo = businessObjectService.findBySinglePrimaryKey(PropositionParameterBo.class, id);
169 		return PropositionParameterBo.to(bo);
170 	}
171 
172 	/**
173 	 * This overridden method gets a parameter by the Proposition Id and Sequence Number
174 	 * 
175 	 * @see org.kuali.rice.krms.impl.repository.PropositionBoService#getParameterByPropIdAndSequenceNumber(java.lang.String, java.lang.String)
176 	 */
177 	@Override
178 	public PropositionParameter getParameterByPropIdAndSequenceNumber(
179 			String propId, Integer sequenceNumber) {
180 		if (StringUtils.isBlank(propId)) {
181             throw new IllegalArgumentException("propId is null or blank");
182 		}
183 		if (sequenceNumber == null) {
184             throw new IllegalArgumentException("sequenceNumber is null");
185 		}
186         final Map<String, Object> map = new HashMap<String, Object>();
187         map.put("propId", propId);
188         map.put("sequenceNumber", sequenceNumber);
189 		PropositionParameterBo bo = businessObjectService.findByPrimaryKey(PropositionParameterBo.class, map);
190 		return PropositionParameterBo.to(bo);
191 	}
192 	
193     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
194         this.businessObjectService = businessObjectService;
195     }
196 
197 }