View Javadoc
1   /**
2    * Copyright 2005-2014 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.rice.krad.web.controller;
17  
18  import java.util.Collections;
19  import java.util.Map;
20  import java.util.Properties;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.kuali.rice.core.api.exception.RiceRuntimeException;
26  import org.kuali.rice.krad.bo.Exporter;
27  import org.kuali.rice.krad.datadictionary.DataObjectEntry;
28  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  import org.kuali.rice.krad.service.ModuleService;
30  import org.kuali.rice.krad.uif.UifParameters;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  import org.kuali.rice.krad.util.KRADUtils;
34  import org.kuali.rice.krad.web.form.InquiryForm;
35  import org.kuali.rice.krad.web.form.UifFormBase;
36  import org.springframework.stereotype.Controller;
37  import org.springframework.validation.BindingResult;
38  import org.springframework.web.bind.annotation.ModelAttribute;
39  import org.springframework.web.bind.annotation.RequestMapping;
40  import org.springframework.web.servlet.ModelAndView;
41  
42  /**
43   * Controller for <code>InquiryView</code> screens which handle initial requests for the inquiry and
44   * actions coming from the inquiry view such as export
45   *
46   * @author Kuali Rice Team (rice.collab@kuali.org)
47   */
48  @Controller
49  @RequestMapping(value = "/inquiry")
50  public class InquiryController extends UifControllerBase {
51      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InquiryController.class);
52  
53      /**
54       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
55       */
56      @Override
57      protected InquiryForm createInitialForm(HttpServletRequest request) {
58          return new InquiryForm();
59      }
60  
61      /**
62       * Invoked to request an inquiry view for a data object class
63       *
64       * <p>
65       * Checks if the data object is externalizable and we need to redirect to the appropriate inquiry URL, else
66       * continues with the inquiry view display
67       * </p>
68       *
69       * <p>
70       * Data object class name and values for a primary or alternate key set must
71       * be sent in the request
72       * </p>
73       *
74       * <p>
75       * Invokes the inquirable to perform the query for the data object record, if not found
76       * an exception will be thrown. If found the object is set on the form and then the view
77       * is rendered
78       * </p>
79       */
80      @Override
81      @MethodAccessible
82      @RequestMapping(params = "methodToCall=start")
83      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, HttpServletRequest request,
84              HttpServletResponse response) {
85          InquiryForm inquiryForm = (InquiryForm) form;
86  
87          // if request is not a redirect, determine if we need to redirect for an externalizable object inquiry
88          if (!inquiryForm.isRedirectedInquiry()) {
89              Class<?> inquiryObjectClass;
90              try {
91                  inquiryObjectClass = Class.forName(inquiryForm.getDataObjectClassName());
92              } catch (ClassNotFoundException e) {
93                  throw new RiceRuntimeException("Unable to get class for name: " + inquiryForm.getDataObjectClassName(),
94                          e);
95              }
96  
97              ModuleService responsibleModuleService =
98                      KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass);
99              if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) {
100                 String inquiryUrl = responsibleModuleService.getExternalizableDataObjectInquiryUrl(inquiryObjectClass,
101                         KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
102 
103                 Properties redirectUrlProps = new Properties();
104                 redirectUrlProps.put(UifParameters.REDIRECTED_INQUIRY, "true");
105 
106                 // clear current form from session
107                 GlobalVariables.getUifFormManager().removeSessionForm(form);
108 
109                 return performRedirect(form, inquiryUrl, redirectUrlProps);
110             }
111         }
112 
113         // invoke inquirable to retrieve inquiry data object
114         Object dataObject = inquiryForm.getInquirable().retrieveDataObject(KRADUtils.translateRequestParameterMap(
115                 request.getParameterMap()));
116 
117         inquiryForm.setDataObject(dataObject);
118 
119         return super.start(form, request, response);
120     }
121 
122     /**
123      * Handles exporting the BusinessObject for this Inquiry to XML if it has a custom XML exporter available.
124      */
125     @RequestMapping(params = "methodToCall=export")
126     public ModelAndView export(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
127             HttpServletRequest request, HttpServletResponse response) throws Exception {
128         InquiryForm inquiryForm = (InquiryForm) form;
129 
130         Object dataObject = inquiryForm.getDataObject();
131         if (dataObject != null) {
132             DataObjectEntry dataObjectEntry =
133                     KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDataObjectEntry(
134                             inquiryForm.getDataObjectClassName());
135 
136             Class<? extends Exporter> exporterClass = dataObjectEntry.getExporterClass();
137             if (exporterClass != null) {
138                 Exporter exporter = exporterClass.newInstance();
139 
140                 response.setContentType(KRADConstants.XML_MIME_TYPE);
141                 response.setHeader("Content-disposition", "attachment; filename=export.xml");
142                 exporter.export(dataObjectEntry.getDataObjectClass(), Collections.singletonList(dataObject),
143                         KRADConstants.XML_FORMAT, response.getOutputStream());
144             }
145         }
146 
147         return null;
148     }
149 }