001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.web.controller;
017
018import java.util.Collections;
019import java.util.Map;
020import java.util.Properties;
021
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024
025import org.kuali.rice.core.api.exception.RiceRuntimeException;
026import org.kuali.rice.krad.bo.Exporter;
027import org.kuali.rice.krad.datadictionary.DataObjectEntry;
028import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
029import org.kuali.rice.krad.service.ModuleService;
030import org.kuali.rice.krad.uif.UifParameters;
031import org.kuali.rice.krad.util.GlobalVariables;
032import org.kuali.rice.krad.util.KRADConstants;
033import org.kuali.rice.krad.util.KRADUtils;
034import org.kuali.rice.krad.web.form.InquiryForm;
035import org.kuali.rice.krad.web.form.UifFormBase;
036import org.springframework.stereotype.Controller;
037import org.springframework.validation.BindingResult;
038import org.springframework.web.bind.annotation.ModelAttribute;
039import org.springframework.web.bind.annotation.RequestMapping;
040import org.springframework.web.servlet.ModelAndView;
041
042/**
043 * Controller for <code>InquiryView</code> screens which handle initial requests for the inquiry and
044 * actions coming from the inquiry view such as export
045 *
046 * @author Kuali Rice Team (rice.collab@kuali.org)
047 */
048@Controller
049@RequestMapping(value = "/inquiry")
050public class InquiryController extends UifControllerBase {
051    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InquiryController.class);
052
053    /**
054     * @see org.kuali.rice.krad.web.controller.UifControllerBase#createInitialForm(javax.servlet.http.HttpServletRequest)
055     */
056    @Override
057    protected InquiryForm createInitialForm(HttpServletRequest request) {
058        return new InquiryForm();
059    }
060
061    /**
062     * Invoked to request an inquiry view for a data object class
063     *
064     * <p>
065     * Checks if the data object is externalizable and we need to redirect to the appropriate inquiry URL, else
066     * continues with the inquiry view display
067     * </p>
068     *
069     * <p>
070     * Data object class name and values for a primary or alternate key set must
071     * be sent in the request
072     * </p>
073     *
074     * <p>
075     * Invokes the inquirable to perform the query for the data object record, if not found
076     * an exception will be thrown. If found the object is set on the form and then the view
077     * is rendered
078     * </p>
079     */
080    @RequestMapping(params = "methodToCall=start")
081    @Override
082    public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
083            HttpServletRequest request, HttpServletResponse response) {
084        InquiryForm inquiryForm = (InquiryForm) form;
085
086        // if request is not a redirect, determine if we need to redirect for an externalizable object inquiry
087        if (!inquiryForm.isRedirectedInquiry()) {
088            Class inquiryObjectClass = null;
089            try {
090                inquiryObjectClass = Class.forName(inquiryForm.getDataObjectClassName());
091            } catch (ClassNotFoundException e) {
092                throw new RiceRuntimeException("Unable to get class for name: " + inquiryForm.getDataObjectClassName());
093            }
094
095            ModuleService responsibleModuleService =
096                    KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass);
097            if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) {
098                String inquiryUrl = responsibleModuleService.getExternalizableDataObjectInquiryUrl(inquiryObjectClass,
099                        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().removeForm(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}