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.apache.commons.lang.BooleanUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.core.api.criteria.PredicateFactory;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23  import org.kuali.rice.krad.util.GlobalVariables;
24  import org.kuali.rice.krms.builder.ComponentBuilder;
25  import org.kuali.rice.krms.util.PropositionTreeUtil;
26  import org.kuali.student.lum.lu.ui.krms.dto.LUPropositionEditor;
27  import org.kuali.student.lum.lu.ui.krms.util.LUKRMSConstants;
28  import org.kuali.student.r2.common.constants.CommonServiceConstants;
29  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
30  import org.kuali.student.r2.common.util.ContextUtils;
31  import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
32  import org.kuali.student.r2.core.acal.dto.TermInfo;
33  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
34  import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
35  import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
36  import org.kuali.student.r2.core.search.dto.SearchResultInfo;
37  import org.kuali.student.r2.core.search.service.SearchService;
38  import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
39  import org.kuali.student.r2.lum.clu.service.CluService;
40  import org.kuali.student.r2.lum.course.dto.CourseInfo;
41  import org.kuali.student.r2.lum.course.service.CourseService;
42  import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
43  
44  import javax.xml.namespace.QName;
45  import java.util.HashMap;
46  import java.util.List;
47  import java.util.Map;
48  
49  /**
50   * @author Kuali Student Team
51   */
52  public class CourseComponentBuilder implements ComponentBuilder<LUPropositionEditor> {
53  
54      private final static Logger LOG = Logger.getLogger(CourseComponentBuilder.class);
55  
56      private CourseService courseService;
57      private CluService cluService;
58      private AcademicCalendarService acalService = null;
59      private SearchService searchService = null;
60  
61      @Override
62      public List<String> getComponentIds() {
63          return null;  //To change body of implemented methods use File | Settings | File Templates.
64      }
65  
66      @Override
67      public void resolveTermParameters(LUPropositionEditor propositionEditor, Map<String, String> termParameters) {
68          String courseId = termParameters.get(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_CLU_KEY);
69          String termCode1 = termParameters.get(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERMCODE_KEY);
70          String termCode2 = termParameters.get(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERMCODE2_KEY);
71  
72          if (courseId != null) {
73              try {
74                  VersionDisplayInfo versionInfo = this.getCluService().getCurrentVersion(CluServiceConstants.CLU_NAMESPACE_URI, courseId, null);
75                  CourseInfo courseInfo = this.getCourseService().getCourse(versionInfo.getId(), ContextUtils.getContextInfo());
76                  propositionEditor.setCourseInfo(courseInfo);
77              } catch (DoesNotExistException e) {
78                  throw new RuntimeException("Clu does not exist");
79              } catch (Exception e) {
80                  throw new RuntimeException(e);
81              }
82  
83          }
84  
85          if (termCode1 != null) {
86              propositionEditor.setTermCode(termCode1);
87              propositionEditor.setTermInfo(this.getTermForTermCode(propositionEditor.getTermCode(), null));
88          }
89          if (termCode2 != null) {
90              propositionEditor.setTermCode2(termCode2);
91              propositionEditor.setTermInfo2(this.getTermForTermCode(propositionEditor.getTermCode2(), null));
92          }
93      }
94  
95      @Override
96      public Map<String, String> buildTermParameters(LUPropositionEditor propositionEditor) {
97          Map<String, String> termParameters = new HashMap<String, String>();
98          if (propositionEditor.getCourseInfo() != null) {
99              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_CLU_KEY, propositionEditor.getCourseInfo().getVersion().getVersionIndId());
100         }
101 
102         if (propositionEditor.getTermInfo() != null) {
103             termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERM_KEY, propositionEditor.getTermInfo().getId());
104             termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERMCODE_KEY, propositionEditor.getTermCode());
105         }
106 
107         if (propositionEditor.getTermInfo2() != null) {
108             termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERM2_KEY, propositionEditor.getTermInfo2().getId());
109             termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_TERMCODE2_KEY, propositionEditor.getTermCode2());
110         }
111 
112         return termParameters;
113     }
114 
115     @Override
116     public void onSubmit(LUPropositionEditor propositionEditor) {
117         //To change body of implemented methods use File | Settings | File Templates.
118     }
119 
120     @Override
121     public void validate(LUPropositionEditor propositionEditor) {
122 
123         if (propositionEditor.getTermCode() != null) {
124             String propName = PropositionTreeUtil.getBindingPath(propositionEditor, "termCode");
125             propositionEditor.setTermInfo(this.getTermForTermCode(propositionEditor.getTermCode(), propName));
126         }
127 
128         if (propositionEditor.getTermCode2() != null) {
129             String propName = PropositionTreeUtil.getBindingPath(propositionEditor, "termCode2");
130             propositionEditor.setTermInfo2(this.getTermForTermCode(propositionEditor.getTermCode2(), propName));
131         }
132     }
133 
134     /**
135      * Method used to
136      * 1) search to get TermInfo based on termCode. Only accept one valid TermInfo. If find more than one TermInfo or
137      * don't find any termInfo, log and report an error message.
138      *
139      * @param termCode
140      * @return termInfo
141      */
142     public TermInfo getTermForTermCode(String termCode, String propName) {
143 
144         QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
145         qbcBuilder.setPredicates(PredicateFactory.equal("atpCode", termCode));
146 
147         List<TermInfo> terms = null;
148         try {
149             terms = getAcalService().searchForTerms(qbcBuilder.build(), ContextUtils.getContextInfo());
150         } catch (Exception e) {
151             throw new RuntimeException(e);
152         }
153 
154 
155         if (terms.isEmpty()) {
156             GlobalVariables.getMessageMap().putError(propName, LUKRMSConstants.KSKRMS_MSG_ERROR_NO_TERM_IS_FOUND, termCode);
157         } else if (terms.size() > 1) {
158             GlobalVariables.getMessageMap().putError(propName, LUKRMSConstants.KSKRMS_MSG_ERROR_FOUND_MORE_THAN_ONE_TERM, termCode);
159         } else {
160             return terms.get(0);
161         }
162 
163         return null;
164     }
165 
166     protected CourseService getCourseService() {
167         if (courseService == null) {
168             courseService = GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "course", "CourseService"));
169         }
170         return courseService;
171     }
172 
173     protected CluService getCluService() {
174         if (cluService == null) {
175             cluService = GlobalResourceLoader.getService(new QName(CluServiceConstants.CLU_NAMESPACE, CluServiceConstants.SERVICE_NAME_LOCAL_PART));
176         }
177         return cluService;
178     }
179 
180     private AcademicCalendarService getAcalService() {
181         if (acalService == null) {
182             acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE,
183                     AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
184         }
185         return acalService;
186     }
187 
188     public SearchService getSearchService() {
189         if (searchService == null) {
190             searchService = (SearchService) GlobalResourceLoader.getService(new QName(CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + "search", SearchService.class.getSimpleName()));
191         }
192         return searchService;
193     }
194 
195 }