1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.data;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import net.sf.jasperreports.annotations.properties.Property;
34 import net.sf.jasperreports.annotations.properties.PropertyScope;
35 import net.sf.jasperreports.engine.JRException;
36 import net.sf.jasperreports.engine.JRPropertiesUtil;
37 import net.sf.jasperreports.engine.ParameterContributor;
38 import net.sf.jasperreports.engine.ParameterContributorContext;
39 import net.sf.jasperreports.engine.ParameterContributorFactory;
40 import net.sf.jasperreports.properties.PropertyConstants;
41 import net.sf.jasperreports.repo.DataAdapterResource;
42 import net.sf.jasperreports.repo.RepositoryContext;
43 import net.sf.jasperreports.repo.RepositoryResourceContext;
44 import net.sf.jasperreports.repo.RepositoryUtil;
45 import net.sf.jasperreports.repo.ResourceInfo;
46 import net.sf.jasperreports.repo.SimpleRepositoryContext;
47 import net.sf.jasperreports.repo.SimpleRepositoryResourceContext;
48
49 /**
50  * @author Teodor Danciu (teodord@users.sourceforge.net)
51  */

52 public final class DataAdapterParameterContributorFactory implements ParameterContributorFactory
53 {
54
55     private static final Log log = LogFactory.getLog(DataAdapterParameterContributorFactory.class);
56     
57     /**
58      * A report/dataset level property that provides the location of a data adapter resource 
59      * to be used for the dataset.
60      */

61     @Property(
62             category = PropertyConstants.CATEGORY_DATA_SOURCE,
63             valueType = DataAdapter.class,//for JSS
64             scopes = {PropertyScope.CONTEXT, PropertyScope.DATASET},
65             sinceVersion = PropertyConstants.VERSION_4_1_1
66             )
67     public static final String PROPERTY_DATA_ADAPTER_LOCATION = JRPropertiesUtil.PROPERTY_PREFIX + "data.adapter";
68
69     private static final DataAdapterParameterContributorFactory INSTANCE = new DataAdapterParameterContributorFactory();
70     
71     private DataAdapterParameterContributorFactory()
72     {
73     }
74     
75     /**
76      * 
77      */

78     public static DataAdapterParameterContributorFactory getInstance()
79     {
80         return INSTANCE;
81     }
82
83     @Override
84     public List<ParameterContributor> getContributors(ParameterContributorContext context) throws JRException
85     {
86         List<ParameterContributor> contributors = new ArrayList<ParameterContributor>();
87
88         String dataAdapterUri = JRPropertiesUtil.getOwnProperty(context.getDataset(), PROPERTY_DATA_ADAPTER_LOCATION); 
89         if (dataAdapterUri != null)
90         {
91             RepositoryUtil repository = RepositoryUtil.getInstance(context.getRepositoryContext());
92             ResourceInfo resourceInfo = repository.getResourceInfo(dataAdapterUri);
93             
94             String resourceLocation = dataAdapterUri;
95             String contextLocation = null;
96             if (resourceInfo != null)
97             {
98                 resourceLocation = resourceInfo.getRepositoryResourceLocation();
99                 contextLocation = resourceInfo.getRepositoryContextLocation();
100                 if (log.isDebugEnabled())
101                 {
102                     log.debug("data adapter " + dataAdapterUri + " resolved to " + resourceLocation
103                             + ", context " + contextLocation);
104                 }
105             }
106             
107             DataAdapterResource dataAdapterResource = repository.getResourceFromLocation(resourceLocation, DataAdapterResource.class);
108             
109             RepositoryResourceContext currentContext = context.getRepositoryContext().getResourceContext();
110             RepositoryResourceContext adapterResourceContext = SimpleRepositoryResourceContext.of(contextLocation,
111                     currentContext == null ? null : currentContext.getDerivedContextFallback());
112             RepositoryContext adapterRepositoryContext = SimpleRepositoryContext.of(context.getJasperReportsContext(), 
113                     adapterResourceContext);
114             ParameterContributorContext adapterContext = context.withRepositoryContext(adapterRepositoryContext);
115             
116             ParameterContributor dataAdapterService = DataAdapterServiceUtil.getInstance(adapterContext).getService(dataAdapterResource.getDataAdapter());
117             
118             return Collections.singletonList(dataAdapterService);
119         }
120
121         return contributors;
122     }
123     
124 }
125