001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache 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 *      https://www.apache.org/licenses/LICENSE-2.0
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
017package org.springframework.web.servlet.view.jasperreports;
018
019import java.util.HashMap;
020import java.util.Map;
021import java.util.Properties;
022import javax.sql.DataSource;
023
024import org.springframework.web.servlet.view.AbstractUrlBasedView;
025import org.springframework.web.servlet.view.UrlBasedViewResolver;
026
027/**
028 * {@link org.springframework.web.servlet.ViewResolver} implementation that
029 * resolves instances of {@link AbstractJasperReportsView} by translating
030 * the supplied view name into the URL of the report file.
031 *
032 * @author Rob Harrop
033 * @since 1.2.6
034 */
035public class JasperReportsViewResolver extends UrlBasedViewResolver {
036
037        private String reportDataKey;
038
039        private Properties subReportUrls;
040
041        private String[] subReportDataKeys;
042
043        private Properties headers;
044
045        private Map<String, Object> exporterParameters = new HashMap<String, Object>();
046
047        private DataSource jdbcDataSource;
048
049
050        /**
051         * Requires the view class to be a subclass of {@link AbstractJasperReportsView}.
052         */
053        @Override
054        protected Class<?> requiredViewClass() {
055                return AbstractJasperReportsView.class;
056        }
057
058        /**
059         * Set the {@code reportDataKey} the view class should use.
060         * @see AbstractJasperReportsView#setReportDataKey
061         */
062        public void setReportDataKey(String reportDataKey) {
063                this.reportDataKey = reportDataKey;
064        }
065
066        /**
067         * Set the {@code subReportUrls} the view class should use.
068         * @see AbstractJasperReportsView#setSubReportUrls
069         */
070        public void setSubReportUrls(Properties subReportUrls) {
071                this.subReportUrls = subReportUrls;
072        }
073
074        /**
075         * Set the {@code subReportDataKeys} the view class should use.
076         * @see AbstractJasperReportsView#setSubReportDataKeys
077         */
078        public void setSubReportDataKeys(String... subReportDataKeys) {
079                this.subReportDataKeys = subReportDataKeys;
080        }
081
082        /**
083         * Set the {@code headers} the view class should use.
084         * @see AbstractJasperReportsView#setHeaders
085         */
086        public void setHeaders(Properties headers) {
087                this.headers = headers;
088        }
089
090        /**
091         * Set the {@code exporterParameters} the view class should use.
092         * @see AbstractJasperReportsView#setExporterParameters
093         */
094        public void setExporterParameters(Map<String, Object> exporterParameters) {
095                this.exporterParameters = exporterParameters;
096        }
097
098        /**
099         * Set the {@link DataSource} the view class should use.
100         * @see AbstractJasperReportsView#setJdbcDataSource
101         */
102        public void setJdbcDataSource(DataSource jdbcDataSource) {
103                this.jdbcDataSource = jdbcDataSource;
104        }
105
106
107        @Override
108        protected AbstractUrlBasedView buildView(String viewName) throws Exception {
109                AbstractJasperReportsView view = (AbstractJasperReportsView) super.buildView(viewName);
110                view.setReportDataKey(this.reportDataKey);
111                view.setSubReportUrls(this.subReportUrls);
112                view.setSubReportDataKeys(this.subReportDataKeys);
113                view.setHeaders(this.headers);
114                view.setExporterParameters(this.exporterParameters);
115                view.setJdbcDataSource(this.jdbcDataSource);
116                return view;
117        }
118
119}