View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 1.0 (the
5    * "License"); 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/ecl1.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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.kuali.student.enrollment.class1.hold.controller;
17  
18  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
19  import org.kuali.rice.krad.uif.UifParameters;
20  import org.kuali.rice.krad.util.GlobalVariables;
21  import org.kuali.rice.krad.web.controller.UifControllerBase;
22  import org.kuali.rice.krad.web.form.UifFormBase;
23  import org.kuali.student.enrollment.class1.hold.form.HoldIssueInfoCreateForm;
24  import org.kuali.student.enrollment.class2.courseoffering.form.DeleteTargetTermForm;
25  import org.kuali.student.enrollment.class2.courseoffering.service.CourseOfferingViewHelperService;
26  import org.kuali.student.enrollment.courseofferingset.dto.SocInfo;
27  import org.kuali.student.mock.utilities.TestHelper;
28  import org.kuali.student.r2.common.dto.ContextInfo;
29  import org.kuali.student.r2.common.dto.RichTextInfo;
30  import org.kuali.student.r2.common.util.constants.CourseOfferingSetServiceConstants;
31  import org.kuali.student.r2.core.constants.HoldServiceConstants;
32  import org.kuali.student.r2.common.util.constants.OrganizationServiceConstants;
33  import org.kuali.student.r2.core.hold.dto.HoldIssueInfo;
34  import org.kuali.student.r2.core.hold.service.HoldService;
35  import org.kuali.student.r2.core.organization.service.OrganizationService;
36  import org.kuali.student.r2.core.organization.dto.OrgInfo;
37  import org.springframework.stereotype.Controller;
38  import org.springframework.validation.BindingResult;
39  import org.springframework.web.bind.annotation.ModelAttribute;
40  import org.springframework.web.bind.annotation.RequestMapping;
41  import org.springframework.web.bind.annotation.RequestMethod;
42  import org.springframework.web.servlet.ModelAndView;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  import javax.xml.namespace.QName;
47  import java.util.Properties;
48  
49  /**
50   * This controller handles all the request from Academic calendar UI.
51   *
52   * @author Kuali Student Team
53   */
54  
55  @Controller
56  @RequestMapping(value = "/createHold")
57  public class HoldIssueInfoCreateController extends UifControllerBase {
58  
59      private transient HoldService holdService;
60      private transient OrganizationService organizationService;
61      private ContextInfo contextInfo;
62      private HoldIssueInfo holdIssueInfo;
63      private OrgInfo orgInfo;
64  
65      @Override
66      protected UifFormBase createInitialForm(HttpServletRequest request) {
67          return new HoldIssueInfoCreateForm();
68      }
69  
70      /**
71       * This GET method loads an academic calendar based on the parameters passed into the request.
72       *
73       * These are the supported request parameters
74       * 1. id - Academic Calendar Id to load in to UI
75       * 2. readOnlyView - If true, sets the view as read only
76       * 3. selectTab - can be 'info' or 'term'
77       *
78       */
79      @Override
80      @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
81      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
82                                HttpServletRequest request, HttpServletResponse response) {
83          HoldIssueInfoCreateForm holdForm = (HoldIssueInfoCreateForm) form;
84  
85          return super.start(form, result, request, response);
86      }
87  
88      @RequestMapping(params = "methodToCall=create")
89      public ModelAndView create(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
90                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
91          HoldIssueInfoCreateForm createForm = (HoldIssueInfoCreateForm) form;
92          holdIssueInfo = new HoldIssueInfo();
93          holdIssueInfo.setName(createForm.getName());
94          holdIssueInfo.setTypeKey(createForm.getTypeKey());
95          holdIssueInfo.setStateKey("kuali.hold.issue.state.active");
96          holdIssueInfo.setOrganizationId(createForm.getOrganizationId());
97          RichTextInfo richTextInfo = new RichTextInfo();
98          richTextInfo.setPlain(createForm.getDescr());
99          holdIssueInfo.setDescr(richTextInfo);
100 
101         HoldIssueInfo createHoldIssueInfo;
102         try {
103             holdService = getHoldService();
104 
105                 createHoldIssueInfo = holdService.createHoldIssue(holdIssueInfo.getTypeKey(), holdIssueInfo, getContextInfo() );
106         } catch (Exception e) {
107 
108            return getUIFModelAndView(createForm);
109             //throw new RuntimeException("Create new failed. ", e);
110         }
111         createForm.setValidateDirty(false);
112         createForm.setId(createHoldIssueInfo.getId());
113         createForm.setStateKey(createHoldIssueInfo.getStateKey());
114         return close(createForm, result, request, response);
115     }
116 
117   @RequestMapping(params = "methodToCall=modify")
118  public ModelAndView modity(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
119                             HttpServletRequest request, HttpServletResponse response) throws Exception {
120       HoldIssueInfoCreateForm modifyForm = (HoldIssueInfoCreateForm) form;
121       if (modifyForm.getTypeKey().equals("") || modifyForm.getTypeKey().length() == 0) {
122           GlobalVariables.getMessageMap().putError("typeKey", "error.enroll.hold.type.empty");
123           return getUIFModelAndView(form);
124       }
125 
126       if (modifyForm.getOrganizationId().equals("") || modifyForm.getOrganizationId().length() == 0) {
127           GlobalVariables.getMessageMap().putError("organizationId", "error.enroll.hold.organization.empty");
128           return getUIFModelAndView(form);
129       }
130 
131       holdIssueInfo = new HoldIssueInfo();
132       holdIssueInfo.setId(modifyForm.getId());
133       holdIssueInfo.setName(modifyForm.getName());
134       holdIssueInfo.setTypeKey(modifyForm.getTypeKey());
135       holdIssueInfo.setStateKey(modifyForm.getStateKey());
136       holdIssueInfo.setOrganizationId(modifyForm.getOrganizationId());
137       RichTextInfo richTextInfo = new RichTextInfo();
138       richTextInfo.setPlain(modifyForm.getDescr());
139       holdIssueInfo.setDescr(richTextInfo);
140 
141       try {
142           holdService = getHoldService();
143           HoldIssueInfo modifyHoldIssueInfo = holdService.updateHoldIssue(holdIssueInfo.getId(), holdIssueInfo, getContextInfo() );
144       } catch (Exception e) {
145           e.printStackTrace();
146           throw new RuntimeException("Modify Hold failed. ", e);
147       }
148       GlobalVariables.getMessageMap().putInfo("holdIssueInfo", "info.enroll.hold.modify.success");
149       return getUIFModelAndView(form);
150  }
151 
152     @RequestMapping(params = "methodToCall=view")
153     public ModelAndView view(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
154                               HttpServletRequest request, HttpServletResponse response) {
155         HoldIssueInfoCreateForm holdIssueForm = (HoldIssueInfoCreateForm) form;
156         String holdIssueId = request.getParameter("id");
157         String viewType = request.getParameter(UifParameters.VIEW_ID);
158         String orgName = request.getParameter("orgName");
159         if ((holdIssueId != null) && !holdIssueId.trim().isEmpty()) {
160             try {
161                 HoldIssueInfo holdIssueInfo = getHoldService().getHoldIssue(holdIssueId, getContextInfo());
162                 holdIssueForm.setName(holdIssueInfo.getName());
163                 holdIssueForm.setTypeKey(holdIssueInfo.getTypeKey());
164                 holdIssueForm.setDescr(holdIssueInfo.getDescr().getPlain());
165                 holdIssueForm.setOrganizationId(holdIssueInfo.getOrganizationId());
166                 holdIssueForm.setStateKey(holdIssueInfo.getStateKey());
167                 holdIssueForm.setOrgName(orgName);
168             } catch (Exception ex) {
169                 throw new RuntimeException("unable to get hold issue");
170             }
171         }
172 
173 /*        if(viewType.equals("holdView")) {
174             holdIssueForm.getView().setReadOnly(true);
175         }*/
176 
177         return super.start(holdIssueForm, result, request, response);
178     }
179 
180     private ContextInfo getContextInfo() {
181         if (null == contextInfo) {
182             //TODO - get real ContextInfo
183             contextInfo = TestHelper.getContext1();
184         }
185         return contextInfo;
186     }
187 
188     protected HoldService getHoldService(){
189         if(holdService == null) {
190             holdService = (HoldService) GlobalResourceLoader.getService(new QName(HoldServiceConstants.NAMESPACE, HoldServiceConstants.SERVICE_NAME_LOCAL_PART));
191         }
192         return holdService;
193     }
194     protected OrganizationService getOrganizationService(){
195         if(organizationService == null) {
196             organizationService = (OrganizationService) GlobalResourceLoader.getService(new QName(OrganizationServiceConstants.NAMESPACE, OrganizationServiceConstants.SERVICE_NAME_LOCAL_PART));
197         }
198         return organizationService;
199     }
200 }