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.kuali.rice.core.api.config.property.ConfigContext;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.krad.web.controller.UifControllerBase;
23  import org.kuali.rice.krad.web.form.UifFormBase;
24  import org.kuali.student.enrollment.class2.courseoffering.form.TestStatePropagationForm;
25  import org.kuali.student.enrollment.class2.courseoffering.service.TestStatePropagationViewHelperService;
26  import org.kuali.student.enrollment.class2.courseofferingset.service.decorators.CourseOfferingSetServiceAftDecorator;
27  import org.kuali.student.enrollment.class2.courseofferingset.util.CourseOfferingSetUtil;
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.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  import org.springframework.stereotype.Controller;
46  import org.springframework.transaction.annotation.Transactional;
47  import org.springframework.validation.BindingResult;
48  import org.springframework.web.bind.annotation.ModelAttribute;
49  import org.springframework.web.bind.annotation.RequestMapping;
50  import org.springframework.web.bind.annotation.RequestMethod;
51  import org.springframework.web.servlet.ModelAndView;
52  
53  import javax.naming.OperationNotSupportedException;
54  import javax.servlet.http.HttpServletRequest;
55  import javax.servlet.http.HttpServletResponse;
56  import javax.xml.namespace.QName;
57  import java.util.ArrayList;
58  import java.util.List;
59  import java.util.Map;
60  
61  /**
62   * This class provides controller methods to test state propagation through the ui
63   *
64   * @author Kuali Student Team
65   */
66  @Controller
67  @RequestMapping(value = "/testStatePropagation")
68  public class TestStatePropagationController extends UifControllerBase {
69      private TestStatePropagationViewHelperService viewHelperService;
70  
71      private static final Logger LOGGER = LoggerFactory.getLogger(TestStatePropagationController.class);
72  
73      public static final String PAGE_ID = "pageId";
74  
75      public static final String CHANGE_SOC_STATE_DEFAULT_TERM = "201301";
76  
77      private CourseOfferingSetServiceAftDecorator socService;
78      private AcademicCalendarService acalService;
79  
80      @Override
81      protected UifFormBase createInitialForm(@SuppressWarnings("unused") HttpServletRequest request) {
82          return new TestStatePropagationForm();
83      }
84  
85      @Override
86      @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
87      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form,
88                                @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
89          if (!(form instanceof TestStatePropagationForm)){
90              throw new RuntimeException("Form object passed into start method was not of expected type TestServiceCallForm. Got " + form.getClass().getSimpleName());
91          }
92  
93          TestStatePropagationForm theForm = (TestStatePropagationForm) form;
94          populateFormWithTargetSocInfo( theForm );
95          Map paramMap = request.getParameterMap();
96          if (paramMap.containsKey(PAGE_ID)) {
97              String pageId = ((String []) paramMap.get(PAGE_ID))[0];
98              if (pageId.equals("testStatePropagationPageId")) {
99                  return _startStatePropagationTest(form, request, response);
100             }
101         }
102 
103         return getUIFModelAndView(theForm);
104     }
105 
106     private ModelAndView _startStatePropagationTest(@ModelAttribute("KualiForm") UifFormBase form,
107                                                 @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) {
108         // Doesn't do anything really, but is there for customization
109         TestStatePropagationForm theForm = (TestStatePropagationForm) form;
110         LOGGER.info("firstServiceCall");
111         return getUIFModelAndView(theForm);
112     }
113 
114     @Transactional
115     @RequestMapping(params = "methodToCall=testStatePropagation")
116     public ModelAndView testStatePropagation(@ModelAttribute("KualiForm") TestStatePropagationForm form, @SuppressWarnings("unused") BindingResult result,
117                                              @SuppressWarnings("unused") HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
118         TestStatePropagationViewHelperService helper = getViewHelperService(form);
119         helper.runTests(form);
120         return getUIFModelAndView(form);
121     }
122 
123     public TestStatePropagationViewHelperService getViewHelperService(TestStatePropagationForm serviceCallForm) {
124         if (viewHelperService == null) {
125             viewHelperService = (TestStatePropagationViewHelperService) serviceCallForm.getViewHelperService();
126         }
127         return viewHelperService;
128     }
129 
130     @Transactional
131     @RequestMapping(params = "methodToCall=changeSocState")
132     public ModelAndView changeSocState( @ModelAttribute("KualiForm") TestStatePropagationForm form ) throws PermissionDeniedException, OperationFailedException, OperationNotSupportedException, InvalidParameterException, MissingParameterException, DoesNotExistException, DataValidationErrorException, VersionMismatchException, ReadOnlyException {
133 
134         String environment = StringUtils.defaultIfBlank( ConfigContext.getCurrentContextConfig().getProperty( "environment" ), StringUtils.EMPTY );
135         if( !"DEV".equalsIgnoreCase( environment ) ) {
136             throw new OperationNotSupportedException( "Cannot change state of SOC in non-dev environment (env is:" + environment + ")" );
137         }
138 
139         // update soc-state
140         ContextInfo contextInfo = new ContextInfo();
141         SocInfo targetSocInfo = getTargetSocInfoForTerm( form.getTermCodeForSocStateChange(), contextInfo );
142         targetSocInfo.setStateKey( form.getNewSocStateForSocStateChange() );
143         putBypassBusinessLogicFlagOntoContext( contextInfo );
144         this.getSocService().updateSoc( targetSocInfo.getId(), targetSocInfo, contextInfo );
145 
146         populateFormWithTargetSocInfo( form );
147 
148         return getUIFModelAndView(form);
149     }
150 
151     private void populateFormWithTargetSocInfo( TestStatePropagationForm form ) {
152         try {
153             ContextInfo contextInfo = new ContextInfo();
154             String targetTermId = StringUtils.defaultIfEmpty( form.getTermCodeForSocStateChange(), CHANGE_SOC_STATE_DEFAULT_TERM );
155             SocInfo targetSocInfo = getTargetSocInfoForTerm( targetTermId, contextInfo );
156             TermInfo targetTermInfo = this.getAcalService().getTerm( targetSocInfo.getTermId(), contextInfo );
157 
158             form.setTermCodeForSocStateChange( targetTermInfo.getCode() );
159             form.setNewSocStateForSocStateChange( targetSocInfo.getStateKey() );
160         } catch (Exception e) {
161             throw new RuntimeException(e);
162         }
163     }
164 
165     private SocInfo getTargetSocInfoForTerm( String targetTermCode, ContextInfo contextInfo ) throws MissingParameterException, PermissionDeniedException, OperationFailedException, InvalidParameterException, DoesNotExistException {
166         String targetTermId = this.getAcalService().getTermsByCode( targetTermCode, contextInfo ).get(0).getId();
167         return CourseOfferingSetUtil.getMainSocForTermId(targetTermId, contextInfo);
168     }
169 
170     private void putBypassBusinessLogicFlagOntoContext( ContextInfo contextInfo ) {
171         List<AttributeInfo> attrs = new ArrayList<AttributeInfo>();
172         attrs.add(new AttributeInfo(CourseOfferingSetServiceConstants.BYPASS_BUSINESS_LOGIC_ON_SOC_STATE_CHANGE_FOR_AFT_TESTING, String.valueOf(true)));
173         contextInfo.setAttributes(attrs);
174     }
175 
176     private CourseOfferingSetServiceAftDecorator getSocService() {
177         if( socService == null ) {
178             socService = (CourseOfferingSetServiceAftDecorator) GlobalResourceLoader.getService( new QName(CourseOfferingSetServiceConstants.NAMESPACE, CourseOfferingSetServiceConstants.SERVICE_NAME_LOCAL_PART ) );
179         }
180         return socService;
181     }
182 
183     private AcademicCalendarService getAcalService() {
184         if( acalService == null ) {
185             acalService = GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
186         }
187         return acalService;
188     }
189 
190 }