1 package org.kuali.rice.krms.impl.util;
2
3 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
4 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
5 import org.kuali.rice.krms.api.repository.RuleManagementService;
6 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
7 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
8 import org.kuali.rice.krms.api.repository.agenda.AgendaTreeDefinition;
9 import org.kuali.rice.krms.api.repository.agenda.AgendaTreeEntryDefinitionContract;
10 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
11 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
12 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
13 import org.kuali.rice.krms.api.repository.proposition.PropositionType;
14 import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
15 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
16 import org.kuali.rice.krms.api.repository.term.TermDefinition;
17 import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import javax.xml.namespace.QName;
22 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23 import org.kuali.rice.krms.api.KrmsConstants;
24 import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
25 import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
26 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
27 import org.kuali.student.r2.common.exceptions.InvalidParameterException;
28 import org.kuali.student.r2.common.exceptions.MissingParameterException;
29 import org.kuali.student.r2.common.exceptions.OperationFailedException;
30 import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
31 import org.kuali.student.r2.common.exceptions.ReadOnlyException;
32 import org.kuali.student.r2.common.exceptions.UnsupportedActionException;
33 import org.kuali.student.r2.common.util.ContextUtils;
34 import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
35 import org.kuali.student.r2.core.krms.naturallanguage.TermParameterTypes;
36 import org.kuali.student.r2.lum.clu.dto.CluSetInfo;
37 import org.kuali.student.r2.lum.clu.service.CluService;
38 import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
39
40
41
42
43
44
45 public class KrmsRuleManagementCopyMethodsImpl implements KrmsRuleManagementCopyMethods {
46
47 private RuleManagementService ruleManagementService;
48 private TermRepositoryService termRepositoryService;
49 private CluService cluService;
50
51 @Override
52 public List<ReferenceObjectBinding> deepCopyReferenceObjectBindingsFromTo(String fromReferenceDiscriminatorType,
53 String fromReferenceObjectId,
54 String toReferenceDiscriminatorType,
55 String toReferenceObjectId,
56 List<String> optionKeys)
57 throws RiceIllegalArgumentException,
58 RiceIllegalStateException, PermissionDeniedException, MissingParameterException, InvalidParameterException, OperationFailedException {
59 _checkEmptyParam(fromReferenceDiscriminatorType, "fromReferenceDiscriminatorType");
60 _checkEmptyParam(fromReferenceObjectId, "fromReferenceObjectId");
61 _checkEmptyParam(toReferenceDiscriminatorType, "toReferenceDiscriminatorType");
62 _checkEmptyParam(toReferenceObjectId, "toReferenceObjectId");
63 List<ReferenceObjectBinding> copiedRefList = new ArrayList<ReferenceObjectBinding>();
64 List<ReferenceObjectBinding> refsToCopy = this.getRuleManagementService().findReferenceObjectBindingsByReferenceObject(fromReferenceDiscriminatorType, fromReferenceObjectId);
65 for (ReferenceObjectBinding reference : refsToCopy) {
66 ReferenceObjectBinding.Builder refBldr = null;
67
68 if (reference.getKrmsDiscriminatorType().equals(KSKRMSServiceConstants.KRMS_DISCRIMINATOR_TYPE_AGENDA)) {
69 AgendaTreeDefinition agendaTree = getRuleManagementService().getAgendaTree(reference.getKrmsObjectId());
70
71 AgendaDefinition copiedAgenda = deepCopyAgenda(agendaTree, toReferenceObjectId);
72 refBldr = ReferenceObjectBinding.Builder.create(reference);
73 refBldr.setId(null);
74 refBldr.setVersionNumber(null);
75 refBldr.setReferenceObjectId(toReferenceObjectId);
76 refBldr.setReferenceDiscriminatorType(toReferenceDiscriminatorType);
77 refBldr.setKrmsObjectId(copiedAgenda.getId());
78 } else {
79
80 throw new RiceIllegalStateException("unknown/unhandled KRMS discriminator type " + reference.getKrmsDiscriminatorType());
81 }
82 ReferenceObjectBinding refBinding = getRuleManagementService().createReferenceObjectBinding(refBldr.build());
83 copiedRefList.add(refBinding);
84 }
85 return copiedRefList;
86 }
87
88 private AgendaDefinition deepCopyAgenda(AgendaTreeDefinition agendaTree, String refObjectId) throws MissingParameterException, InvalidParameterException, OperationFailedException, PermissionDeniedException {
89
90 AgendaDefinition oldAgenda = getRuleManagementService().getAgenda(agendaTree.getAgendaId());
91 AgendaDefinition.Builder copiedAgendaBldr = AgendaDefinition.Builder.create(oldAgenda);
92 copiedAgendaBldr.setId(null);
93 copiedAgendaBldr.setVersionNumber(null);
94 copiedAgendaBldr.setFirstItemId(null);
95 copiedAgendaBldr.setName(refObjectId + ":" + oldAgenda.getTypeId() + ":1");
96 AgendaDefinition copiedAgenda = getRuleManagementService().createAgenda(copiedAgendaBldr.build());
97
98 AgendaItemDefinition.Builder previousAgendaItemBldr = null;
99 AgendaItemDefinition.Builder firstAgendaItemBldr = AgendaItemDefinition.Builder.create(null, copiedAgenda.getId());
100 AgendaItemDefinition firstAgendaItem = getRuleManagementService().createAgendaItem(firstAgendaItemBldr.build());
101 copiedAgendaBldr = AgendaDefinition.Builder.create(copiedAgenda);
102 copiedAgendaBldr.setFirstItemId(firstAgendaItem.getId());
103 getRuleManagementService().updateAgenda(copiedAgendaBldr.build());
104 copiedAgenda = getRuleManagementService().getAgenda(copiedAgenda.getId());
105
106 boolean isFirstItem = true;
107 for (AgendaTreeEntryDefinitionContract entry : agendaTree.getEntries()) {
108 AgendaItemDefinition currentAgendaItem = getRuleManagementService().getAgendaItem(entry.getAgendaItemId());
109 AgendaItemDefinition.Builder copiedAgendaItemBldr = AgendaItemDefinition.Builder.create(currentAgendaItem);
110 deepUpdateAgendaItem(copiedAgendaItemBldr, copiedAgenda.getId(), refObjectId);
111 if (isFirstItem) {
112 copiedAgendaItemBldr.setId(firstAgendaItem.getId());
113 copiedAgendaItemBldr.setVersionNumber(firstAgendaItem.getVersionNumber());
114 firstAgendaItemBldr = copiedAgendaItemBldr;
115 previousAgendaItemBldr = firstAgendaItemBldr;
116 isFirstItem = false;
117 } else {
118 copiedAgendaItemBldr.setId(null);
119 copiedAgendaItemBldr.setVersionNumber(null);
120 previousAgendaItemBldr.setWhenTrue(copiedAgendaItemBldr);
121 previousAgendaItemBldr = copiedAgendaItemBldr;
122 }
123 }
124 getRuleManagementService().updateAgendaItem(firstAgendaItemBldr.build());
125 return copiedAgenda;
126 }
127
128 private void deepUpdateAgendaItem(AgendaItemDefinition.Builder copiedAgendaItemBldr, String copiedAgendaID, String refObjectId) throws MissingParameterException, PermissionDeniedException, InvalidParameterException, OperationFailedException {
129 copiedAgendaItemBldr.setId(null);
130 copiedAgendaItemBldr.setVersionNumber(null);
131 copiedAgendaItemBldr.setAgendaId(copiedAgendaID);
132 if (copiedAgendaItemBldr.getWhenTrueId() != null) {
133 deepUpdateAgendaItem(copiedAgendaItemBldr.getWhenTrue(), copiedAgendaID, refObjectId);
134 copiedAgendaItemBldr.setWhenTrueId(null);
135 }
136 if (copiedAgendaItemBldr.getWhenFalseId() != null) {
137 deepUpdateAgendaItem(copiedAgendaItemBldr.getWhenFalse(), copiedAgendaID, refObjectId);
138 copiedAgendaItemBldr.setWhenFalseId(null);
139 }
140 if (copiedAgendaItemBldr.getAlwaysId() != null) {
141 deepUpdateAgendaItem(copiedAgendaItemBldr.getAlways(), copiedAgendaID, refObjectId);
142 copiedAgendaItemBldr.setAlwaysId(null);
143 }
144 copiedAgendaItemBldr.setRuleId(null);
145 RuleDefinition.Builder copiedRuleBldr = copiedAgendaItemBldr.getRule();
146 copiedRuleBldr.setId(null);
147 copiedRuleBldr.setVersionNumber(null);
148 copiedRuleBldr.setPropId(null);
149 copiedRuleBldr.setName(refObjectId + ":" + copiedRuleBldr.getTypeId() + ":1");
150 if (copiedRuleBldr.getProposition() != null){
151 deepUpdateForProposition(copiedRuleBldr.getProposition());
152 }
153
154 }
155
156 private void deepUpdateForProposition(PropositionDefinition.Builder propBldr) throws PermissionDeniedException, MissingParameterException, InvalidParameterException, OperationFailedException {
157 propBldr.setId(null);
158 propBldr.setRuleId(null);
159 for (PropositionParameter.Builder propParmBldr : propBldr.getParameters()) {
160 propParmBldr.setId(null);
161 propParmBldr.setPropId(null);
162 if (PropositionParameterType.TERM.getCode().equals(propParmBldr.getParameterType())) {
163 TermDefinition termDef = null;
164 if (propParmBldr.getTermValue() != null) {
165 termDef = propParmBldr.getTermValue();
166 } else {
167 termDef = getTermRepositoryService().getTerm(propParmBldr.getValue());
168 }
169 propParmBldr.setValue(null);
170 TermDefinition.Builder termBldr = TermDefinition.Builder.create(termDef);
171 termBldr.setId(null);
172 for (TermParameterDefinition.Builder termParmBldr : termBldr.getParameters()) {
173 termParmBldr.setId(null);
174 termParmBldr.setTermId(null);
175 if (TermParameterTypes.COURSE_CLUSET_KEY.getId().equals(termParmBldr.getName()) || TermParameterTypes.PROGRAM_CLUSET_KEY.getId().equals(termParmBldr.getName()) || TermParameterTypes.CLUSET_KEY.getId().equals(termParmBldr.getName())) {
176 termParmBldr.setValue(createAdhocCluSet(termParmBldr.getValue()));
177 }
178 }
179 propParmBldr.setTermValue(termBldr.build());
180 }
181 }
182 if (PropositionType.COMPOUND.getCode().equals(propBldr.getPropositionTypeCode())) {
183 for (PropositionDefinition.Builder subPropBldr : propBldr.getCompoundComponents()) {
184 deepUpdateForProposition(subPropBldr);
185 }
186 }
187 }
188
189 private String createAdhocCluSet(String cluSetId) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
190 try {
191
192 CluSetInfo cluSet = getCluService().getCluSet(cluSetId, ContextUtils.createDefaultContextInfo());
193
194
195 if(!cluSet.getIsReusable()){
196 cluSet.setId(null);
197
198 if (cluSet.getMembershipQuery() != null) {
199 cluSet.getCluIds().clear();
200 cluSet.getCluSetIds().clear();
201 } else {
202 List<String> subCluSetIds = new ArrayList<String>();
203 for(String subCluSetid : cluSet.getCluSetIds()){
204 subCluSetIds.add(createAdhocCluSet(subCluSetid));
205 }
206 cluSet.setCluSetIds(subCluSetIds);
207 }
208
209 cluSet = getCluService().createCluSet(cluSet.getTypeKey(), cluSet, ContextUtils.createDefaultContextInfo());
210 return cluSet.getId();
211 }
212 } catch (DoesNotExistException e) {
213 throw new OperationFailedException(e.getMessage());
214 } catch (UnsupportedActionException e) {
215 throw new OperationFailedException(e.getMessage());
216 } catch (DataValidationErrorException e) {
217 throw new OperationFailedException(e.getMessage());
218 } catch (ReadOnlyException e) {
219 throw new OperationFailedException(e.getMessage());
220 }
221 return cluSetId;
222 }
223
224 @Override
225 public int deleteReferenceObjectBindingsCascade(String referenceDiscriminatorType,
226 String referenceObjectId)
227 throws RiceIllegalArgumentException, RiceIllegalStateException {
228 _checkEmptyParam(referenceDiscriminatorType, "referenceDiscriminatorType");
229 _checkEmptyParam(referenceObjectId, "referenceObjectId");
230
231 List<ReferenceObjectBinding> refsToDelete = this.getRuleManagementService().findReferenceObjectBindingsByReferenceObject(referenceDiscriminatorType,
232 referenceObjectId);
233 for (ReferenceObjectBinding refToDelete : refsToDelete) {
234 if (refToDelete.getKrmsDiscriminatorType().equals(KSKRMSServiceConstants.KRMS_DISCRIMINATOR_TYPE_AGENDA)) {
235 this._deleteAgendaCascade(refToDelete.getKrmsObjectId());
236 } else {
237 throw new RiceIllegalStateException("unknown/unhandled KRMS discriminator type " + refToDelete.getKrmsDiscriminatorType());
238 }
239 }
240 for (ReferenceObjectBinding refToDelete : refsToDelete) {
241 this.getRuleManagementService().deleteReferenceObjectBinding(refToDelete.getId());
242 }
243 return refsToDelete.size();
244 }
245
246 private int _deleteAgendaCascade(String agendaId) {
247 AgendaDefinition agenda = this.getRuleManagementService().getAgenda(agendaId);
248 this._deleteAgendaItemCascade(agenda.getFirstItemId());
249 this.getRuleManagementService().deleteAgenda(agendaId);
250 return 1;
251 }
252
253 private int _deleteAgendaItemCascade(String agendaItemId) {
254 AgendaItemDefinition item = this.getRuleManagementService().getAgendaItem(agendaItemId);
255 if (item.getAlwaysId() != null) {
256 AgendaItemDefinition.Builder bldr = AgendaItemDefinition.Builder.create(item);
257 bldr.setAlways(null);
258 bldr.setAlwaysId(null);
259 this.getRuleManagementService().updateAgendaItem(bldr.build());
260 this._deleteAgendaItemCascade(item.getAlwaysId());
261 item = this.getRuleManagementService().getAgendaItem(agendaItemId);
262 }
263 if (item.getWhenTrueId() != null) {
264 AgendaItemDefinition.Builder bldr = AgendaItemDefinition.Builder.create(item);
265 bldr.setWhenTrue(null);
266 bldr.setWhenTrueId(null);
267 this.getRuleManagementService().updateAgendaItem(bldr.build());
268 this._deleteAgendaItemCascade(item.getWhenTrueId());
269 item = this.getRuleManagementService().getAgendaItem(agendaItemId);
270 }
271 if (item.getWhenFalseId() != null) {
272 AgendaItemDefinition.Builder bldr = AgendaItemDefinition.Builder.create(item);
273 bldr.setWhenFalse(null);
274 bldr.setWhenFalseId(null);
275 this.getRuleManagementService().updateAgendaItem(bldr.build());
276 this._deleteAgendaItemCascade(item.getWhenFalseId());
277 item = this.getRuleManagementService().getAgendaItem(agendaItemId);
278 }
279 if (item.getSubAgendaId() != null) {
280 this._deleteAgendaCascade(item.getSubAgendaId());
281 }
282 if (item.getRuleId() != null) {
283 AgendaItemDefinition.Builder bldr = AgendaItemDefinition.Builder.create(item);
284 bldr.setRule(null);
285 bldr.setRuleId(null);
286 this.getRuleManagementService().updateAgendaItem(bldr.build());
287 this._deleteRuleCascade(item.getRuleId());
288 }
289 this.getRuleManagementService().deleteAgendaItem(agendaItemId);
290 return 1;
291 }
292
293
294 private int _deleteRuleCascade(String ruleId) {
295 RuleDefinition rule = this.getRuleManagementService().getRule(ruleId);
296
297 this.getRuleManagementService().deleteRule(ruleId);
298 return 1;
299 }
300
301 private void _checkEmptyParam(String param, String message)
302 throws RiceIllegalArgumentException {
303 if (param == null) {
304 throw new RiceIllegalArgumentException(message);
305 }
306 if (param.trim().isEmpty()) {
307 throw new RiceIllegalArgumentException(message);
308 }
309 }
310
311 public RuleManagementService getRuleManagementService() {
312 if (ruleManagementService == null) {
313 ruleManagementService = (RuleManagementService) GlobalResourceLoader.getService(new QName(KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0, "ruleManagementService"));
314 }
315 return ruleManagementService;
316 }
317
318 public void setRuleManagementService(RuleManagementService ruleManagementService) {
319 this.ruleManagementService = ruleManagementService;
320 }
321
322 public TermRepositoryService getTermRepositoryService() {
323 if (termRepositoryService == null) {
324 termRepositoryService = (TermRepositoryService) GlobalResourceLoader.getService(new QName(KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0, "termRepositoryService"));
325 }
326 return termRepositoryService;
327 }
328
329 public void setTermRepositoryService(TermRepositoryService termRepositoryService) {
330 this.termRepositoryService = termRepositoryService;
331 }
332
333 public CluService getCluService() {
334 if (cluService == null) {
335 cluService = (CluService) GlobalResourceLoader.getService(new QName(CluServiceConstants.NAMESPACE, "CluService"));
336 }
337 return cluService;
338 }
339
340 public void setCluService(CluService cluService) {
341 this.cluService = cluService;
342 }
343 }