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