001 /**
002 * Copyright 2005-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krad.web.controller;
017
018 import java.util.Collections;
019
020 import javax.servlet.http.HttpServletRequest;
021 import javax.servlet.http.HttpServletResponse;
022
023 import org.kuali.rice.krad.bo.Exporter;
024 import org.kuali.rice.krad.datadictionary.DataObjectEntry;
025 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026 import org.kuali.rice.krad.util.GlobalVariables;
027 import org.kuali.rice.krad.util.KRADConstants;
028 import org.kuali.rice.krad.util.KRADUtils;
029 import org.kuali.rice.krad.web.form.InquiryForm;
030 import org.kuali.rice.krad.web.form.UifFormBase;
031 import org.springframework.stereotype.Controller;
032 import org.springframework.validation.BindingResult;
033 import org.springframework.web.bind.annotation.ModelAttribute;
034 import org.springframework.web.bind.annotation.RequestMapping;
035 import org.springframework.web.servlet.ModelAndView;
036
037 /**
038 * Controller for <code>InquiryView</code> screens which handle
039 * initial requests for the inquiry and actions coming from the
040 * inquiry view such as export
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044 @Controller
045 @RequestMapping(value = "/inquiry")
046 public class InquiryController extends UifControllerBase {
047 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InquiryController.class);
048
049 /**
050 * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
051 */
052 @Override
053 protected InquiryForm createInitialForm(HttpServletRequest request) {
054 return new InquiryForm();
055 }
056
057 /**
058 * Invoked to display the inquiry view for a data object record
059 *
060 * <p>
061 * Data object class name and values for a primary or alternate key set must
062 * be sent in the request
063 * </p>
064 *
065 * <p>
066 * Invokes the inquirable to perform the query for the data object record, if not found
067 * an exception will be thrown. If found the object is set on the form and then the view
068 * is rendered
069 * </p>
070 */
071 @RequestMapping(params = "methodToCall=start")
072 @Override
073 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
074 HttpServletRequest request, HttpServletResponse response) {
075 InquiryForm inquiryForm = (InquiryForm) form;
076
077 // initialize data object class in inquirable
078 try {
079 inquiryForm.getInquirable().setDataObjectClass(Class.forName(inquiryForm.getDataObjectClassName()));
080 } catch (ClassNotFoundException e) {
081 LOG.error("Unable to get new instance for object class: " + inquiryForm.getDataObjectClassName(), e);
082 throw new RuntimeException(
083 "Unable to get new instance for object class: " + inquiryForm.getDataObjectClassName(), e);
084 }
085
086 // invoke inquirable to retrieve inquiry data object
087 Object dataObject = inquiryForm.getInquirable()
088 .retrieveDataObject(KRADUtils.translateRequestParameterMap(request.getParameterMap()));
089
090 if (dataObject == null && GlobalVariables.getMessageMap().hasNoMessages()) {
091 LOG.error("The record you have inquired on does not exist.");
092 inquiryForm.setView(getViewService().getViewById("InquiryNoResultView"));
093 // throw new UnsupportedOperationException("The record you have inquired on does not exist.");
094 }
095 inquiryForm.setDataObject(dataObject);
096
097 return getUIFModelAndView(inquiryForm);
098 }
099
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 }