View Javadoc

1   /**
2    * Copyright 2005-2013 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      @RequestMapping(params = "methodToCall=start")
81      @Override
82      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
83              HttpServletRequest request, HttpServletResponse response) {
84          InquiryForm inquiryForm = (InquiryForm) form;
85  
86          // if request is not a redirect, determine if we need to redirect for an externalizable object inquiry
87          if (!inquiryForm.isRedirectedInquiry()) {
88              Class inquiryObjectClass = null;
89              try {
90                  inquiryObjectClass = Class.forName(inquiryForm.getDataObjectClassName());
91              } catch (ClassNotFoundException e) {
92                  throw new RiceRuntimeException("Unable to get class for name: " + inquiryForm.getDataObjectClassName());
93              }
94  
95              ModuleService responsibleModuleService =
96                      KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass);
97              if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) {
98                  String inquiryUrl = responsibleModuleService.getExternalizableDataObjectInquiryUrl(inquiryObjectClass,
99                          KRADUtils.convertRequestMapToProperties(request.getParameterMap()));
100 
101                 Properties redirectUrlProps = new Properties();
102                 redirectUrlProps.put(UifParameters.REDIRECTED_INQUIRY, "true");
103 
104                 // clear current form from session
105                 GlobalVariables.getUifFormManager().removeSessionForm(form);
106 
107                 return performRedirect(form, inquiryUrl, redirectUrlProps);
108             }
109         }
110 
111         // initialize data object class in inquirable
112         try {
113             inquiryForm.getInquirable().setDataObjectClass(Class.forName(inquiryForm.getDataObjectClassName()));
114         } catch (ClassNotFoundException e) {
115             LOG.error("Unable to get new instance for object class: " + inquiryForm.getDataObjectClassName(), e);
116             throw new RuntimeException(
117                     "Unable to get new instance for object class: " + inquiryForm.getDataObjectClassName(), e);
118         }
119 
120         // invoke inquirable to retrieve inquiry data object
121         Object dataObject = inquiryForm.getInquirable().retrieveDataObject(KRADUtils.translateRequestParameterMap(
122                 request.getParameterMap()));
123 
124         inquiryForm.setDataObject(dataObject);
125 
126         return super.start(form, result, request, response);
127     }
128 
129     /**
130      * Handles exporting the BusinessObject for this Inquiry to XML if it has a custom XML exporter available.
131      */
132     @RequestMapping(params = "methodToCall=export")
133     public ModelAndView export(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
134             HttpServletRequest request, HttpServletResponse response) throws Exception {
135         InquiryForm inquiryForm = (InquiryForm) form;
136 
137         Object dataObject = inquiryForm.getDataObject();
138         if (dataObject != null) {
139             DataObjectEntry dataObjectEntry =
140                     KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDataObjectEntry(
141                             inquiryForm.getDataObjectClassName());
142 
143             Class<? extends Exporter> exporterClass = dataObjectEntry.getExporterClass();
144             if (exporterClass != null) {
145                 Exporter exporter = exporterClass.newInstance();
146 
147                 response.setContentType(KRADConstants.XML_MIME_TYPE);
148                 response.setHeader("Content-disposition", "attachment; filename=export.xml");
149                 exporter.export(dataObjectEntry.getDataObjectClass(), Collections.singletonList(dataObject),
150                         KRADConstants.XML_FORMAT, response.getOutputStream());
151             }
152         }
153 
154         return null;
155     }
156 }