View Javadoc

1   /**
2    * Copyright 2005-2011 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  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.kuali.rice.krad.bo.Exporter;
24  import org.kuali.rice.krad.datadictionary.DataObjectEntry;
25  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.util.KRADConstants;
28  import org.kuali.rice.krad.util.KRADUtils;
29  import org.kuali.rice.krad.web.form.InquiryForm;
30  import org.kuali.rice.krad.web.form.UifFormBase;
31  import org.springframework.stereotype.Controller;
32  import org.springframework.validation.BindingResult;
33  import org.springframework.web.bind.annotation.ModelAttribute;
34  import org.springframework.web.bind.annotation.RequestMapping;
35  import org.springframework.web.servlet.ModelAndView;
36  
37  /**
38   * Controller for <code>InquiryView</code> screens which handle
39   * initial requests for the inquiry and actions coming from the
40   * inquiry view such as export
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  @Controller
45  @RequestMapping(value = "/inquiry")
46  public class InquiryController extends UifControllerBase {
47      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InquiryController.class);
48  
49      /**
50       * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
51       */
52      @Override
53      protected InquiryForm createInitialForm(HttpServletRequest request) {
54          return new InquiryForm();
55      }
56  
57      /**
58       * Invoked to display the inquiry view for a data object record
59       *
60       * <p>
61       * Data object class name and values for a primary or alternate key set must
62       * be sent in the request
63       * </p>
64       *
65       * <p>
66       * Invokes the inquirable to perform the query for the data object record, if not found
67       * an exception will be thrown. If found the object is set on the form and then the view
68       * is rendered
69       * </p>
70       */
71      @RequestMapping(params = "methodToCall=start")
72      @Override
73      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
74              HttpServletRequest request, HttpServletResponse response) {
75          InquiryForm inquiryForm = (InquiryForm) form;
76  
77          // initialize data object class in inquirable
78          try {
79              inquiryForm.getInquirable().setDataObjectClass(Class.forName(inquiryForm.getDataObjectClassName()));
80          } catch (ClassNotFoundException e) {
81              LOG.error("Unable to get new instance for object class: " + inquiryForm.getDataObjectClassName(), e);
82              throw new RuntimeException(
83                      "Unable to get new instance for object class: " + inquiryForm.getDataObjectClassName(), e);
84          }
85  
86          // invoke inquirable to retrieve inquiry data object
87          Object dataObject = inquiryForm.getInquirable()
88                  .retrieveDataObject(KRADUtils.translateRequestParameterMap(request.getParameterMap()));
89  
90          if (dataObject == null && GlobalVariables.getMessageMap().hasNoMessages()) {
91              LOG.error("The record you have inquired on does not exist.");
92              inquiryForm.setView(getViewService().getViewById("InquiryNoResultView"));
93  //            throw new UnsupportedOperationException("The record you have inquired on does not exist.");
94          }
95          inquiryForm.setDataObject(dataObject);
96  
97          return getUIFModelAndView(inquiryForm);
98      }
99      
100     /**
101      * Handles exporting the BusinessObject for this Inquiry to XML if it has a custom XML exporter available.
102      */
103     @RequestMapping(params = "methodToCall=export")
104     public ModelAndView export(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
105             HttpServletRequest request, HttpServletResponse response) throws Exception  {
106         InquiryForm inquiryForm = (InquiryForm) form;
107         
108         Object dataObject = inquiryForm.getDataObject();
109         
110         if (dataObject != null) {
111             DataObjectEntry dataObjectEntry = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDataObjectEntry(inquiryForm.getDataObjectClassName());
112             Class<? extends Exporter> exporterClass = dataObjectEntry.getExporterClass();
113             if (exporterClass != null) {
114                 Exporter exporter = exporterClass.newInstance();
115                 response.setContentType(KRADConstants.XML_MIME_TYPE);
116                 response.setHeader("Content-disposition", "attachment; filename=export.xml");
117                 exporter.export(dataObjectEntry.getDataObjectClass(), Collections.singletonList(dataObject), KRADConstants.XML_FORMAT, response.getOutputStream());
118             }
119         }
120         
121          return null;
122     }
123 }