1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.lum.lu.ui.krms.builder;
17
18 import org.kuali.rice.krad.util.GlobalVariables;
19 import org.kuali.rice.krms.api.repository.term.TermDefinition;
20 import org.kuali.rice.krms.builder.ComponentBuilderUtils;
21 import org.kuali.rice.krms.dto.TermParameterEditor;
22 import org.kuali.rice.krms.util.KRMSConstants;
23 import org.kuali.rice.krms.util.PropositionTreeUtil;
24 import org.kuali.student.common.krms.exceptions.KRMSOptimisticLockingException;
25 import org.kuali.student.lum.lu.ui.krms.dto.CluSetRangeInformation;
26 import org.kuali.student.lum.lu.ui.krms.dto.CluSetWrapper;
27 import org.kuali.student.lum.lu.ui.krms.dto.LUPropositionEditor;
28 import org.kuali.student.lum.lu.ui.krms.util.LUKRMSConstants;
29 import org.kuali.student.r2.common.exceptions.VersionMismatchException;
30 import org.kuali.student.common.util.security.ContextUtils;
31 import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
32 import org.kuali.student.r2.lum.clu.dto.CluSetInfo;
33 import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
34
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39
40
41
42 public class MultiCourseComponentBuilder extends CluComponentBuilder {
43
44 @Override
45 public void initialize(LUPropositionEditor propositionEditor) {
46 propositionEditor.setCourseSet(new CluSetWrapper());
47 }
48
49 @Override
50 public List<String> getComponentIds() {
51 return null;
52 }
53
54 @Override
55 public void resolveTermParameters(LUPropositionEditor propositionEditor, Map<String, String> termParameters) {
56 String cluSetId = termParameters.get(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_COURSE_CLUSET_KEY);
57 if (cluSetId != null) {
58 try {
59 propositionEditor.setCourseSet(this.getCluInfoHelper().getCluSetWrapper(cluSetId));
60 populatePropositionWrapper(propositionEditor);
61
62 } catch (Exception e) {
63 throw new RuntimeException(e);
64 }
65
66 }
67 }
68
69 @Override
70 public Map<String, String> buildTermParameters(LUPropositionEditor propositionEditor) {
71 Map<String, String> termParameters = new HashMap<String, String>();
72 if (propositionEditor.getCourseSet() != null) {
73 termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_COURSE_CLUSET_KEY, propositionEditor.getCourseSet().getId());
74 }
75 if (propositionEditor.getGradeScale() != null) {
76 termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_GRADE_TYPE_KEY, propositionEditor.getGradeScale());
77 termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_GRADE_KEY, propositionEditor.getTermParameter());
78 }
79 return termParameters;
80 }
81
82 @Override
83 public void onSubmit(LUPropositionEditor propositionEditor) {
84
85 try {
86 CluSetInfo cluSetInfo = this.buildCourseSet(propositionEditor.getCourseSet());
87 if (cluSetInfo.getId() == null) {
88 cluSetInfo = this.getCluService().createCluSet(cluSetInfo.getTypeKey(), cluSetInfo, ContextUtils.getContextInfo());
89
90 ComponentBuilderUtils.updateTermParameter(propositionEditor.getTerm(), KSKRMSServiceConstants.TERM_PARAMETER_TYPE_COURSE_CLUSET_KEY, cluSetInfo.getId());
91 TermDefinition.Builder termBuilder = TermDefinition.Builder.create(propositionEditor.getTerm());
92 PropositionTreeUtil.getTermParameter(propositionEditor.getParameters()).setTermValue(termBuilder.build());
93
94 } else {
95 this.getCluService().updateCluSet(cluSetInfo.getId(), cluSetInfo, ContextUtils.getContextInfo());
96 }
97 } catch (Exception ex) {
98 if(ex instanceof VersionMismatchException){
99 throw new KRMSOptimisticLockingException("Unable to create a Course Set",ex);
100 }else{
101 throw new IllegalArgumentException(ex);
102 }
103 }
104 }
105
106 @Override
107 public void validate(LUPropositionEditor propositionEditor) {
108 CluSetWrapper cluSet = propositionEditor.getCourseSet();
109 if(!cluSet.hasClus() && !cluSet.hasMembershipQuery() && cluSet.getCluSets().size()==0){
110 String propName = PropositionTreeUtil.getBindingPath(propositionEditor, "multipleCourseType");
111 GlobalVariables.getMessageMap().putError(propName, LUKRMSConstants.KSKRMS_MSG_ERROR_MULTICOURSE_REQUIRED);
112 }
113
114
115 if((cluSet.getClus().size()==1)&&(cluSet.getCluSets().isEmpty())&&(cluSet.getCluSetRanges().isEmpty())){
116 GlobalVariables.getMessageMap().putWarningForSectionId(KRMSConstants.KRMS_PROPOSITION_DETAILSECTION_ID, LUKRMSConstants.KSKRMS_MSG_WARNING_MULTICOURSE_PLURAL);
117 }
118 }
119
120 private void populatePropositionWrapper(LUPropositionEditor propositionEditor) {
121 for (TermParameterEditor termParameterEditor : (List<TermParameterEditor>) propositionEditor.getTerm().getParameters()) {
122 if (termParameterEditor.getName().equals(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_GRADE_KEY)) {
123 propositionEditor.setTermParameter(termParameterEditor.getValue());
124 } else if (termParameterEditor.getName().equals(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_GRADE_TYPE_KEY)) {
125 propositionEditor.setGradeScale(termParameterEditor.getValue());
126 }
127 }
128
129 }
130
131
132
133
134
135
136
137
138
139
140 public CluSetInfo buildCourseSet(CluSetWrapper cluSet) {
141
142 CluSetInfo cluSetInfo = super.buildCluSet(cluSet);
143 if (cluSetInfo.getTypeKey() == null) {
144 cluSetInfo.setTypeKey(CluServiceConstants.CLUSET_TYPE_CREDIT_COURSE);
145 }
146
147 boolean hasCluIds = cluSet.hasClus();
148 int nrOfMembershipQueries = cluSet.getCluSetRanges().size();
149
150
151 if ((cluSet.getCluSets() == null) || cluSet.getCluSets().isEmpty()) {
152 if (hasCluIds && nrOfMembershipQueries==0) {
153 cluSetInfo.setCluIds(cluSet.getCluIds());
154 return cluSetInfo;
155 } else if (!hasCluIds && nrOfMembershipQueries==1) {
156 cluSetInfo.setMembershipQuery(cluSet.getCluSetRanges().get(0).getMembershipQueryInfo());
157 return cluSetInfo;
158 }
159 } else {
160 for (CluSetWrapper cluset : cluSet.getCluSets()) {
161 cluSetInfo.getCluSetIds().add(cluset.getId());
162 }
163 }
164
165
166 if (hasCluIds) {
167 CluSetInfo wrapperCluSet = new CluSetInfo();
168 wrapperCluSet.setCluIds(cluSet.getCluIds());
169 cluSetInfo.getCluSetIds().add(saveWrapperCluSet(wrapperCluSet, cluSetInfo));
170 }
171
172
173 if (nrOfMembershipQueries>0) {
174 for(CluSetRangeInformation cluSetRange : cluSet.getCluSetRanges()){
175 CluSetInfo wrapperCluSet = new CluSetInfo();
176 wrapperCluSet.setMembershipQuery(cluSetRange.getMembershipQueryInfo());
177 cluSetInfo.getCluSetIds().add(saveWrapperCluSet(wrapperCluSet, cluSetInfo));
178 }
179 }
180
181 return cluSetInfo;
182 }
183
184
185
186
187
188
189
190
191
192 private String saveWrapperCluSet(CluSetInfo wrapperCluSet, CluSetInfo cluSetInfo) {
193
194
195 wrapperCluSet.setAdminOrg(cluSetInfo.getAdminOrg());
196 wrapperCluSet.setEffectiveDate(cluSetInfo.getEffectiveDate());
197 wrapperCluSet.setExpirationDate(cluSetInfo.getExpirationDate());
198 wrapperCluSet.setIsReusable(false);
199 wrapperCluSet.setIsReferenceable(false);
200 wrapperCluSet.setName(cluSetInfo.getName());
201 wrapperCluSet.setStateKey(cluSetInfo.getStateKey());
202 wrapperCluSet.setTypeKey(cluSetInfo.getTypeKey());
203
204 try {
205 if (wrapperCluSet.getId() == null) {
206 wrapperCluSet = this.getCluService().createCluSet(wrapperCluSet.getTypeKey(), wrapperCluSet, ContextUtils.getContextInfo());
207 } else {
208 this.getCluService().updateCluSet(wrapperCluSet.getId(), wrapperCluSet, ContextUtils.getContextInfo());
209 }
210 } catch (Exception e) {
211 throw new IllegalArgumentException(e);
212 }
213
214 return wrapperCluSet.getId();
215 }
216
217 }