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