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.inquiry;
017
018import java.util.Set;
019
020import org.kuali.rice.krad.bo.Exporter;
021import org.kuali.rice.krad.datadictionary.DataObjectEntry;
022import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
023import org.kuali.rice.krad.uif.view.InquiryView;
024import org.kuali.rice.krad.uif.view.View;
025import org.kuali.rice.krad.uif.view.ViewPresentationControllerBase;
026import org.kuali.rice.krad.util.KRADConstants;
027import org.kuali.rice.krad.web.form.InquiryForm;
028import org.kuali.rice.krad.web.form.UifFormBase;
029
030/**
031 * Implementation of {@link org.kuali.rice.krad.uif.view.ViewPresentationController} for
032 * {@link InquiryView} instances
033 *
034 * <p>
035 * Adds flag for export of inquiry record
036 * </p>
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040public class InquiryViewPresentationControllerBase extends ViewPresentationControllerBase {
041    private static final long serialVersionUID = 7504225899471226403L;
042
043    /**
044     * @see org.kuali.rice.krad.uif.view.ViewPresentationController#getActionFlags(org.kuali.rice.krad.uif.view.View,
045     * org.kuali.rice.krad.web.form.UifFormBase)
046     */
047    @Override
048    public Set<String> getActionFlags(View view, UifFormBase model) {
049        Set<String> actionFlags = super.getActionFlags(view, model);
050
051        InquiryView inquiryView = (InquiryView) view;
052        InquiryForm inquiryForm = (InquiryForm) model;
053
054        if (isExportSupported(inquiryView, inquiryForm)) {
055            actionFlags.add(KRADConstants.KUALI_ACTION_CAN_EXPORT);
056        }
057
058        return actionFlags;
059    }
060
061    /**
062     * Examines the data objects data dictionary entry to determine if it supports XML export or not
063     * @param view inquiry view
064     *
065     * @return boolean true if it supports export, false if not
066     */
067    protected boolean isExportSupported(InquiryView view, InquiryForm form) {
068        Object dataObject = form.getDataObject();
069
070        if (dataObject == null) {
071            return false;
072        }
073
074        DataObjectEntry dataObjectEntry =
075                KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary().getDataObjectEntry(
076                        view.getDataObjectClassName().getName());
077
078        Class<? extends Exporter> exporterClass = dataObjectEntry.getExporterClass();
079        if (exporterClass != null) {
080            try {
081                Exporter exporter = exporterClass.newInstance();
082                if (exporter.getSupportedFormats(dataObjectEntry.getDataObjectClass()).contains(
083                        KRADConstants.XML_FORMAT)) {
084                    return true;
085                }
086            } catch (Exception e) {
087                throw new RuntimeException("Failed to locate or create exporter class: " + exporterClass);
088            }
089        }
090
091        return false;
092    }
093}