View Javadoc

1   /**
2    * Copyright 2005-2013 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.student.lum.lu.ui.krms.builder;
17  
18  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
19  import org.kuali.rice.krad.util.GlobalVariables;
20  import org.kuali.rice.krms.builder.ComponentBuilder;
21  import org.kuali.rice.krms.builder.ComponentBuilderUtils;
22  import org.kuali.rice.krms.dto.TermParameterEditor;
23  import org.kuali.rice.krms.util.KRMSConstants;
24  import org.kuali.rice.krms.util.PropositionTreeUtil;
25  import org.kuali.student.lum.lu.ui.krms.dto.CluSetInformation;
26  import org.kuali.student.lum.lu.ui.krms.dto.CluSetRangeInformation;
27  import org.kuali.student.lum.lu.ui.krms.dto.LUPropositionEditor;
28  import org.kuali.student.lum.lu.ui.krms.util.CluInformationHelper;
29  import org.kuali.student.lum.lu.ui.krms.util.CluSetRangeHelper;
30  import org.kuali.student.lum.lu.ui.krms.util.LUKRMSConstants;
31  import org.kuali.student.r2.common.constants.CommonServiceConstants;
32  import org.kuali.student.r2.common.util.ContextUtils;
33  import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
34  import org.kuali.student.r2.common.util.date.DateFormatters;
35  import org.kuali.student.r2.core.search.dto.SearchParamInfo;
36  import org.kuali.student.r2.lum.clu.dto.CluSetInfo;
37  import org.kuali.student.r2.lum.clu.dto.MembershipQueryInfo;
38  import org.kuali.student.r2.lum.clu.service.CluService;
39  import org.kuali.student.r2.lum.course.service.CourseService;
40  import org.kuali.student.r2.lum.lrc.service.LRCService;
41  import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
42  import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
43  
44  import javax.xml.namespace.QName;
45  import java.util.ArrayList;
46  import java.util.Date;
47  import java.util.HashMap;
48  import java.util.List;
49  import java.util.Map;
50  
51  /**
52   * @author Kuali Student Team
53   */
54  public class MultiCourseComponentBuilder extends CluComponentBuilder {
55  
56      @Override
57      public List<String> getComponentIds() {
58          return null;
59      }
60  
61      @Override
62      public void resolveTermParameters(LUPropositionEditor propositionEditor, Map<String, String> termParameters) {
63          String cluSetId = termParameters.get(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_CLUSET_KEY);
64          if (cluSetId != null) {
65              try {
66                  CluSetInformation cluSetInfo = this.getCluInfoHelper().getCluSetInformation(cluSetId);
67                  propositionEditor.setCluSet(cluSetInfo);
68                  populatePropositionWrapper(propositionEditor);
69  
70              } catch (Exception e) {
71                  throw new RuntimeException(e);
72              }
73  
74          }
75      }
76  
77      @Override
78      public Map<String, String> buildTermParameters(LUPropositionEditor propositionEditor) {
79          Map<String, String> termParameters = new HashMap<String, String>();
80          if (propositionEditor.getCluSet() != null) {
81              if (propositionEditor.getCluSet().getCluSetInfo() != null) {
82                  termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_CLUSET_KEY, propositionEditor.getCluSet().getCluSetInfo().getId());
83              } else {
84                  termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_CLUSET_KEY, null);
85              }
86          }
87          if (propositionEditor.getGradeScale() != null) {
88              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_GRADE_TYPE_KEY, propositionEditor.getGradeScale());
89              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_GRADE_KEY, propositionEditor.getTermParameter());
90          }
91          return termParameters;
92      }
93  
94      @Override
95      public void onSubmit(LUPropositionEditor propositionEditor) {
96          //Create the courseset
97          try {
98              propositionEditor.getCluSet().setCluSetInfo(this.buildCourseSet(propositionEditor.getCluSet()));
99              CluSetInfo cluSetInfo = propositionEditor.getCluSet().getCluSetInfo();
100             if (cluSetInfo.getId() == null) {
101                 cluSetInfo = this.getCluService().createCluSet(cluSetInfo.getTypeKey(), cluSetInfo, ContextUtils.getContextInfo());
102                 ComponentBuilderUtils.updateTermParameter(propositionEditor.getTerm(), KSKRMSServiceConstants.TERM_PARAMETER_TYPE_CLUSET_KEY, cluSetInfo.getId());
103 
104             } else {
105                 this.getCluService().updateCluSet(cluSetInfo.getId(), cluSetInfo, ContextUtils.getContextInfo());
106             }
107         } catch (Exception ex) {
108             throw new IllegalArgumentException(ex);
109         }
110     }
111 
112     @Override
113     public void validate(LUPropositionEditor propositionEditor) {
114         CluSetInformation cluSet = propositionEditor.getCluSet();
115         if(!cluSet.hasClus() && !cluSet.hasMembershipQuery() && cluSet.getCluSets().size()==0){
116             String propName = PropositionTreeUtil.getBindingPath(propositionEditor, "multipleCourseType");
117             GlobalVariables.getMessageMap().putError(propName, LUKRMSConstants.KSKRMS_MSG_ERROR_MULTICOURSE_REQUIRED);
118         }
119 
120         //Add warning message if only a single clu is added to the cluset, and no other clusets or cluranges are added.
121         if((cluSet.getClus().size()==1)&&(cluSet.getCluSets().isEmpty())&&(cluSet.getCluSetRanges().isEmpty())){
122             GlobalVariables.getMessageMap().putWarningForSectionId(KRMSConstants.KRMS_PROPOSITION_DETAILSECTION_ID, LUKRMSConstants.KSKRMS_MSG_WARNING_MULTICOURSE_PLURAL);
123         }
124     }
125 
126     private void populatePropositionWrapper(LUPropositionEditor propositionEditor) {
127         for (TermParameterEditor termParameterEditor : (List<TermParameterEditor>) propositionEditor.getTerm().getParameters()) {
128             if (termParameterEditor.getName().equals(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_GRADE_KEY)) {
129                 propositionEditor.setTermParameter(termParameterEditor.getValue());
130             } else if (termParameterEditor.getName().equals(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_GRADE_TYPE_KEY)) {
131                 propositionEditor.setGradeScale(termParameterEditor.getValue());
132             }
133         }
134 
135     }
136 
137     /**
138      * This method build the CluSetInfo object based on the CluSetInformation wrapper object.
139      *
140      * Calculates if we require a wrapper cluset or not and the create sub clusets for the different types
141      * of clusets required to save the individual courses of membershipqueries.
142      *
143      * @param cluSetInformation
144      * @return
145      */
146     @Override
147     public CluSetInfo buildCourseSet(CluSetInformation cluSetInformation) {
148 
149         CluSetInfo cluSetInfo = super.buildCourseSet(cluSetInformation);
150         if (cluSetInfo.getTypeKey() == null) {
151             cluSetInfo.setTypeKey(CluServiceConstants.CLUSET_TYPE_CREDIT_COURSE);
152         }
153 
154         boolean hasCluIds = cluSetInformation.hasClus();
155         int nrOfMembershipQueries = cluSetInformation.getCluSetRanges().size();
156 
157         //Check if we require a wrapper cluset, if not set the information and return.
158         if ((cluSetInformation.getCluSets() == null) && (cluSetInformation.getCluSets().isEmpty())) {
159             if (hasCluIds && nrOfMembershipQueries==0) {
160                 cluSetInfo.setCluIds(cluSetInformation.getCluIds());
161                 return cluSetInfo;
162             } else if (!hasCluIds && nrOfMembershipQueries==1) {
163                 cluSetInfo.setMembershipQuery(cluSetInformation.getCluSetRanges().get(0).getMembershipQueryInfo());
164                 return cluSetInfo;
165             }
166         } else {
167             for (CluSetInformation cluset : cluSetInformation.getCluSets()) {
168                 cluSetInfo.getCluSetIds().add(cluset.getCluSetInfo().getId());
169             }
170         }
171 
172         // Add the individual courses to its own cluset and set the cluset on the wrapper cluset.
173         if (hasCluIds) {
174             CluSetInfo wrapperCluSet = new CluSetInfo();
175             wrapperCluSet.setCluIds(cluSetInformation.getCluIds());
176             cluSetInfo.getCluSetIds().add(saveWrapperCluSet(wrapperCluSet, cluSetInformation.getCluSetInfo()));
177         }
178 
179         // Add the course ranges to the wrapper cluset.
180         if (nrOfMembershipQueries>0) {
181             for(CluSetRangeInformation cluSetRange : cluSetInformation.getCluSetRanges()){
182                 CluSetInfo wrapperCluSet = new CluSetInfo();
183                 wrapperCluSet.setMembershipQuery(cluSetRange.getMembershipQueryInfo()); //this.convertDates(cluSetRange.getMembershipQueryInfo(),cluSetInformation ));
184                 cluSetInfo.getCluSetIds().add(saveWrapperCluSet(wrapperCluSet, cluSetInformation.getCluSetInfo()));
185             }
186         }
187 
188         return cluSetInfo;
189     }
190 
191     /**
192      * This method saves the inner cluset to the database and returns the id to add to the list
193      * of clusets for the wrapper cluset.
194      *
195      * @param wrapperCluSet
196      * @param cluSetInfo
197      * @return
198      */
199     private String saveWrapperCluSet(CluSetInfo wrapperCluSet, CluSetInfo cluSetInfo) {
200 
201         //Set the properties to match parent cluset.
202         wrapperCluSet.setAdminOrg(cluSetInfo.getAdminOrg());
203         wrapperCluSet.setEffectiveDate(cluSetInfo.getEffectiveDate());
204         wrapperCluSet.setExpirationDate(cluSetInfo.getExpirationDate());
205         wrapperCluSet.setIsReusable(false);
206         wrapperCluSet.setIsReferenceable(false);
207         wrapperCluSet.setName(cluSetInfo.getName());
208         wrapperCluSet.setStateKey(cluSetInfo.getStateKey());
209         wrapperCluSet.setTypeKey(cluSetInfo.getTypeKey());
210 
211         try {
212             if (wrapperCluSet.getId() == null) {
213                 wrapperCluSet = this.getCluService().createCluSet(wrapperCluSet.getTypeKey(), wrapperCluSet, ContextUtils.getContextInfo());
214             } else {
215                 this.getCluService().updateCluSet(wrapperCluSet.getId(), wrapperCluSet, ContextUtils.getContextInfo());
216             }
217         } catch (Exception e) {
218             throw new IllegalArgumentException(e);
219         }
220 
221         return wrapperCluSet.getId();
222     }
223 
224     /**
225      * Build a new Effective Date Query.
226      *
227      * @param membershipQueryInfo
228      * @param cluSetInformation
229      * @return
230      */
231     private MembershipQueryInfo convertDates(MembershipQueryInfo membershipQueryInfo, CluSetInformation cluSetInformation) {
232 
233         List<SearchParamInfo> queryParams = new ArrayList<SearchParamInfo>();
234 
235         Date firstDate = DateFormatters.DEFAULT_DATE_FORMATTER.parse(cluSetInformation.getRangeHelper().getEffectiveFrom().toString());
236         Date secondDate = DateFormatters.DEFAULT_DATE_FORMATTER.parse(cluSetInformation.getRangeHelper().getEffectiveTo().toString());
237 
238         queryParams.add(createSearchParam(CluSetRangeHelper.CLU_SEARCH_PARM_DATE1, firstDate.toString()));
239         queryParams.add(createSearchParam(CluSetRangeHelper.CLU_SEARCH_PARM_DATE2, secondDate.toString()));
240         membershipQueryInfo.setQueryParamValues(queryParams);
241         return membershipQueryInfo;
242     }
243 
244     /**
245      * @param key
246      * @param value
247      * @return
248      */
249     private SearchParamInfo createSearchParam(String key, String value) {
250         SearchParamInfo param = new SearchParamInfo();
251         param.setKey(key);
252 
253         List<String> values = new ArrayList<String>();
254         values.add(value);
255         param.setValues(values);
256 
257         return param;
258     }
259 
260 }