View Javadoc

1   /**
2    * Copyright 2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by Charles on 5/6/13
16   */
17  package org.kuali.student.enrollment.class2.courseoffering.controller;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
23  import org.kuali.rice.krad.web.controller.UifControllerBase;
24  import org.kuali.rice.krad.web.form.UifFormBase;
25  import org.kuali.student.enrollment.class2.courseoffering.form.TestStatePropagationForm;
26  import org.kuali.student.enrollment.class2.courseoffering.service.TestStatePropagationViewHelperService;
27  import org.kuali.student.enrollment.class2.courseofferingset.service.decorators.CourseOfferingSetServiceAftDecorator;
28  import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
29  import org.kuali.student.r2.common.dto.AttributeInfo;
30  import org.kuali.student.r2.common.dto.ContextInfo;
31  import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
32  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
33  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
34  import org.kuali.student.r2.common.exceptions.MissingParameterException;
35  import org.kuali.student.r2.common.exceptions.OperationFailedException;
36  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
37  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
38  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
39  import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
40  import org.kuali.student.r2.core.acal.dto.TermInfo;
41  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
42  import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
43  import org.springframework.stereotype.Controller;
44  import org.springframework.transaction.annotation.Transactional;
45  import org.springframework.validation.BindingResult;
46  import org.springframework.web.bind.annotation.ModelAttribute;
47  import org.springframework.web.bind.annotation.RequestMapping;
48  import org.springframework.web.bind.annotation.RequestMethod;
49  import org.springframework.web.servlet.ModelAndView;
50  
51  import javax.naming.OperationNotSupportedException;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  import javax.xml.namespace.QName;
55  import java.util.ArrayList;
56  import java.util.List;
57  import java.util.Map;
58  
59  /**
60   * This class provides controller methods to test state propagation through the ui
61   *
62   * @author Kuali Student Team
63   */
64  @Controller
65  @RequestMapping(value = "/testStatePropagation")
66  public class TestStatePropagationController extends UifControllerBase {
67      private TestStatePropagationViewHelperService viewHelperService;
68  
69      private static final Logger LOGGER = Logger.getLogger(TestStatePropagationController.class);
70  
71      public static final String PAGE_ID = "pageId";
72  
73      public static final String CHANGE_SOC_STATE_DEFAULT_TERM = "201301";
74  
75      private CourseOfferingSetServiceAftDecorator socService;
76      private AcademicCalendarService acalService;
77  
78      @Override
79      protected UifFormBase createInitialForm(@SuppressWarnings("unused") HttpServletRequest request) {
80          return new TestStatePropagationForm();
81      }
82  
83      @Override
84      @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
85      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
86                                @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
87          if (!(form instanceof TestStatePropagationForm)){
88              throw new RuntimeException("Form object passed into start method was not of expected type TestServiceCallForm. Got " + form.getClass().getSimpleName());
89          }
90  
91          TestStatePropagationForm theForm = (TestStatePropagationForm) form;
92          populateFormWithTargetSocInfo( theForm );
93          Map paramMap = request.getParameterMap();
94          if (paramMap.containsKey(PAGE_ID)) {
95              String pageId = ((String []) paramMap.get(PAGE_ID))[0];
96              if (pageId.equals("testStatePropagationPageId")) {
97                  return _startStatePropagationTest(form, result, request, response);
98              }
99          }
100 
101         return getUIFModelAndView(theForm);
102     }
103 
104     private ModelAndView _startStatePropagationTest(@ModelAttribute("KualiForm") UifFormBase form, @SuppressWarnings("unused") BindingResult result,
105                                                 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
106         // Doesn't do anything really, but is there for customization
107         TestStatePropagationForm theForm = (TestStatePropagationForm) form;
108         LOGGER.info("firstServiceCall");
109         return getUIFModelAndView(theForm);
110     }
111 
112     @Transactional
113     @RequestMapping(params = "methodToCall=testStatePropagation")
114     public ModelAndView testStatePropagation(@ModelAttribute("KualiForm") TestStatePropagationForm form, @SuppressWarnings("unused") BindingResult result,
115                                              @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
116         TestStatePropagationViewHelperService helper = getViewHelperService(form);
117         helper.runTests(form);
118         return getUIFModelAndView(form);
119     }
120 
121     public TestStatePropagationViewHelperService getViewHelperService(TestStatePropagationForm serviceCallForm) {
122         if (viewHelperService == null) {
123             if (serviceCallForm.getView().getViewHelperServiceClass() != null) {
124                 viewHelperService = (TestStatePropagationViewHelperService) serviceCallForm.getView().getViewHelperService();
125             } else {
126                 viewHelperService = (TestStatePropagationViewHelperService) serviceCallForm.getPostedView().getViewHelperService();
127             }
128         }
129         return viewHelperService;
130     }
131 
132     @Transactional
133     @RequestMapping(params = "methodToCall=changeSocState")
134     public ModelAndView changeSocState( @ModelAttribute("KualiForm") TestStatePropagationForm form ) throws PermissionDeniedException, OperationFailedException, OperationNotSupportedException, InvalidParameterException, MissingParameterException, DoesNotExistException, DataValidationErrorException, VersionMismatchException, ReadOnlyException {
135 
136         String environment = StringUtils.defaultIfBlank( ConfigContext.getCurrentContextConfig().getProperty( "environment" ), StringUtils.EMPTY );
137         if( !"DEV".equalsIgnoreCase( environment ) ) {
138             throw new OperationNotSupportedException( "Cannot change state of SOC in non-dev environment (env is:" + environment + ")" );
139         }
140 
141         // update soc-state
142         ContextInfo contextInfo = new ContextInfo();
143         SocInfo targetSocInfo = getTargetSocInfoForTerm( form.getTermCodeForSocStateChange(), contextInfo );
144         targetSocInfo.setStateKey( form.getNewSocStateForSocStateChange() );
145         putBypassBusinessLogicFlagOntoContext( contextInfo );
146         this.getSocService().updateSoc( targetSocInfo.getId(), targetSocInfo, contextInfo );
147 
148         populateFormWithTargetSocInfo( form );
149 
150         return getUIFModelAndView(form);
151     }
152 
153     private void populateFormWithTargetSocInfo( TestStatePropagationForm form ) {
154         try {
155             ContextInfo contextInfo = new ContextInfo();
156             String targetTermId = StringUtils.defaultIfEmpty( form.getTermCodeForSocStateChange(), CHANGE_SOC_STATE_DEFAULT_TERM );
157             SocInfo targetSocInfo = getTargetSocInfoForTerm( targetTermId, contextInfo );
158             TermInfo targetTermInfo = this.getAcalService().getTerm( targetSocInfo.getTermId(), contextInfo );
159 
160             form.setTermCodeForSocStateChange( targetTermInfo.getCode() );
161             form.setNewSocStateForSocStateChange( targetSocInfo.getStateKey() );
162         } catch (Exception e) {
163             throw new RuntimeException(e);
164         }
165     }
166 
167     private SocInfo getTargetSocInfoForTerm( String targetTermCode, ContextInfo contextInfo ) throws MissingParameterException, PermissionDeniedException, OperationFailedException, InvalidParameterException, DoesNotExistException {
168         String targetTermId = this.getAcalService().getTermsByCode( targetTermCode, contextInfo ).get(0).getId();
169         String targetSocId = this.getSocService().getSocIdsByTerm( targetTermId, contextInfo ).get(0);
170 
171         return this.getSocService().getSoc( targetSocId, contextInfo );
172     }
173 
174     private void putBypassBusinessLogicFlagOntoContext( ContextInfo contextInfo ) {
175         List<AttributeInfo> attrs = new ArrayList<AttributeInfo>();
176         attrs.add(new AttributeInfo(CourseOfferingSetServiceConstants.BYPASS_BUSINESS_LOGIC_ON_SOC_STATE_CHANGE_FOR_AFT_TESTING, String.valueOf(true)));
177         contextInfo.setAttributes(attrs);
178     }
179 
180     private CourseOfferingSetServiceAftDecorator getSocService() {
181         if( socService == null ) {
182             socService = (CourseOfferingSetServiceAftDecorator) GlobalResourceLoader.getService( new QName(CourseOfferingSetServiceConstants.NAMESPACE, CourseOfferingSetServiceConstants.SERVICE_NAME_LOCAL_PART ) );
183         }
184         return socService;
185     }
186 
187     private AcademicCalendarService getAcalService() {
188         if( acalService == null ) {
189             acalService = GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
190         }
191         return acalService;
192     }
193 
194 }