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.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.util.PropositionTreeUtil;
22  import org.kuali.student.common.krms.exceptions.KRMSOptimisticLockingException;
23  import org.kuali.student.lum.lu.ui.krms.dto.CluSetWrapper;
24  import org.kuali.student.lum.lu.ui.krms.dto.LUPropositionEditor;
25  import org.kuali.student.lum.lu.ui.krms.util.LUKRMSConstants;
26  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
27  import org.kuali.student.common.util.security.ContextUtils;
28  import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
29  import org.kuali.student.r2.lum.clu.dto.CluSetInfo;
30  import org.kuali.student.r2.lum.util.constants.CluServiceConstants;
31  
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * @author Kuali Student Team
38   */
39  public class ProgramComponentBuilder extends CluComponentBuilder {
40  
41  
42      @Override
43      public void initialize(LUPropositionEditor propositionEditor) {
44          propositionEditor.setProgramSet(new CluSetWrapper());
45      }
46  
47      @Override
48      public List<String> getComponentIds() {
49          return null;
50      }
51  
52      @Override
53      public void resolveTermParameters(LUPropositionEditor propositionEditor, Map<String, String> termParameters) {
54          String cluSetId = termParameters.get(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_PROGRAM_CLUSET_KEY);
55          if (cluSetId != null) {
56              try {
57                  propositionEditor.setProgramSet(this.getCluInfoHelper().getCluSetWrapper(cluSetId));
58              } catch (Exception e) {
59                  throw new RuntimeException(e);
60              }
61  
62          }
63      }
64  
65      @Override
66      public Map<String, String> buildTermParameters(LUPropositionEditor propositionEditor) {
67          Map<String, String> termParameters = new HashMap<String, String>();
68          if (propositionEditor.getProgramSet() != null) {
69              termParameters.put(KSKRMSServiceConstants.TERM_PARAMETER_TYPE_PROGRAM_CLUSET_KEY, propositionEditor.getProgramSet().getId());
70          }
71  
72          return termParameters;
73      }
74  
75      @Override
76      public void onSubmit(LUPropositionEditor propositionEditor) {
77          //Create the courseset
78          try {
79              if (propositionEditor.getProgramSet()!= null) {
80                  CluSetInfo cluSetInfo = this.buildProgramSet(propositionEditor.getProgramSet());
81                  if (cluSetInfo.getId() == null) {
82                      cluSetInfo = this.getCluService().createCluSet(cluSetInfo.getTypeKey(), cluSetInfo, ContextUtils.getContextInfo());
83  
84                      ComponentBuilderUtils.updateTermParameter(propositionEditor.getTerm(), KSKRMSServiceConstants.TERM_PARAMETER_TYPE_PROGRAM_CLUSET_KEY, cluSetInfo.getId());
85                      TermDefinition.Builder termBuilder = TermDefinition.Builder.create(propositionEditor.getTerm());
86                      PropositionTreeUtil.getTermParameter(propositionEditor.getParameters()).setTermValue(termBuilder.build());
87  
88                  } else {
89                      this.getCluService().updateCluSet(cluSetInfo.getId(), cluSetInfo, ContextUtils.getContextInfo());
90                  }
91              }
92          } catch (Exception ex) {
93              if(ex instanceof VersionMismatchException){
94                  throw new KRMSOptimisticLockingException("Unable to create a Course Set",ex);
95              }else{
96                  throw new IllegalArgumentException(ex);
97              }
98  
99          }
100     }
101 
102     @Override
103     public void validate(LUPropositionEditor propositionEditor) {
104         CluSetWrapper progCluSet = propositionEditor.getProgramSet();
105         if(progCluSet != null){
106             if(!progCluSet.hasClus() && progCluSet.getCluSets().size()==0 ){
107                 String propName = PropositionTreeUtil.getBindingPath(propositionEditor, "programType");
108                 GlobalVariables.getMessageMap().putError(propName, LUKRMSConstants.KSKRMS_MSG_ERROR_APPROVED_PROGRAM_REQUIRED);
109             }
110         }
111     }
112 
113     /**
114      * This method build the CluSetInfo object based on the CluSetInformation wrapper object.
115      *
116      * Calculates if we require a wrapper cluset or not and the create sub clusets for the different types
117      * of clusets required to save the individual courses of membershipqueries.
118      *
119      * @param programSetInformation
120      * @return
121      */
122     public CluSetInfo buildProgramSet(CluSetWrapper programSetInformation) {
123 
124         CluSetInfo cluSetInfo = super.buildCluSet(programSetInformation);
125         if (cluSetInfo.getTypeKey() == null) {
126             cluSetInfo.setTypeKey(CluServiceConstants.CLUSET_TYPE_CREDIT_COURSE);
127         }
128 
129         boolean hasCluIds = programSetInformation.hasClus();
130 
131         //Set the cluset ids on the cluset
132         if ((programSetInformation.getCluSets() == null) || programSetInformation.getCluSets().isEmpty()) {
133             if (hasCluIds) {
134                 cluSetInfo.setCluIds(programSetInformation.getCluIds());
135                 return cluSetInfo;
136             }
137         } else {
138             for (CluSetWrapper cluset : programSetInformation.getCluSets()) {
139                 cluSetInfo.getCluSetIds().add(cluset.getId());
140             }
141         }
142 
143         if (hasCluIds) {
144             CluSetInfo wrapperCluSet = new CluSetInfo();
145             wrapperCluSet.setCluIds(programSetInformation.getCluIds());
146             cluSetInfo.getCluSetIds().add(saveWrapperCluSet(wrapperCluSet, cluSetInfo));
147         }
148         return cluSetInfo;
149     }
150 
151     /**
152      * This method saves the inner cluset to the database and returns the id to add to the list
153      * of clusets for the wrapper cluset.
154      *
155      * @param wrapperCluSet
156      * @param cluSetInfo
157      * @return
158      */
159     private String saveWrapperCluSet(CluSetInfo wrapperCluSet, CluSetInfo cluSetInfo) {
160 
161         //Set the properties to match parent cluset.
162         wrapperCluSet.setAdminOrg(cluSetInfo.getAdminOrg());
163         wrapperCluSet.setEffectiveDate(cluSetInfo.getEffectiveDate());
164         wrapperCluSet.setExpirationDate(cluSetInfo.getExpirationDate());
165         wrapperCluSet.setIsReusable(false);
166         wrapperCluSet.setIsReferenceable(false);
167         wrapperCluSet.setName(cluSetInfo.getName());
168         wrapperCluSet.setStateKey(cluSetInfo.getStateKey());
169         wrapperCluSet.setTypeKey(cluSetInfo.getTypeKey());
170 
171         try {
172             if (wrapperCluSet.getId() == null) {
173                 wrapperCluSet = this.getCluService().createCluSet(wrapperCluSet.getTypeKey(), wrapperCluSet, ContextUtils.getContextInfo());
174             } else {
175                 this.getCluService().updateCluSet(wrapperCluSet.getId(), wrapperCluSet, ContextUtils.getContextInfo());
176             }
177         } catch (Exception e) {
178             throw new IllegalArgumentException(e);
179         }
180 
181         return wrapperCluSet.getId();
182     }
183 
184 }