KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > action > ReportDataSourceAction


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.action;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.webflow.Event;
29 import org.springframework.webflow.RequestContext;
30 import org.springframework.webflow.ScopeType;
31 import org.springframework.webflow.action.FormAction;
32
33 import com.jaspersoft.jasperserver.api.JSException;
34 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder;
35 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource;
36 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup;
37 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
38 import com.jaspersoft.jasperserver.api.metadata.common.service.ResourceFactory;
39 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
40 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria;
41 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor;
42 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl;
43 import com.jaspersoft.jasperserver.war.dto.BaseDTO;
44 import com.jaspersoft.jasperserver.war.dto.OlapClientConnectionWrapper;
45 import com.jaspersoft.jasperserver.war.dto.ReportDataSourceWrapper;
46 import com.jaspersoft.jasperserver.war.validation.ReportDataSourceValidator;
47
48 public class ReportDataSourceAction extends FormAction {
49
50     protected final Log log = LogFactory.getLog(this.getClass());
51     
52     public static final String JavaDoc FORM_OBJECT_KEY="dataResource";
53     public static final String JavaDoc DATASOURCEURI_PARAM = "resource";
54     public static final String JavaDoc PARENT_FOLDER_ATTR = "parentFolder";
55     
56     private RepositoryService repository;
57     private ResourceFactory dataSourceMappings;
58     
59     private JasperServerConstImpl constants = new JasperServerConstImpl();
60     
61     public ReportDataSourceAction(){
62         setFormObjectClass(ReportDataSourceWrapper.class); //custom form backing object class
63
setFormObjectName(FORM_OBJECT_KEY);
64         setFormObjectScope(ScopeType.FLOW); //this is a multi-page wizard!
65
setValidator(new ReportDataSourceValidator());
66     }
67     public Event initAction(RequestContext context) throws Exception JavaDoc
68     {
69         // Look for any supplied ResourceDTO by any parent flows
70
ReportDataSourceWrapper formObject = (ReportDataSourceWrapper) getFormObject(context);
71
72         // Check for any request parameters sent along
73
// If there is no parent flow, start here **For testing as a stand alone flow
74

75         if (formObject.isSubflowMode() && formObject.getAllDatasources() == null){
76             // get a list of all datasources in repo and set in the formObject
77
FilterCriteria criteria = FilterCriteria.createFilter(ReportDataSource.class);
78             
79             ResourceLookup[] lookups = repository.findResource(null, criteria);
80             
81             List JavaDoc allDataSources = null;
82             
83             if (lookups != null && lookups.length != 0) {
84                 log("Found ReportDataSource lookups size=" + lookups.length);
85                 for (int i = 0; i < lookups.length; i++) {
86                     ResourceLookup dr = lookups[i];
87                     if (allDataSources == null)
88                         allDataSources = new ArrayList JavaDoc();
89                     allDataSources.add(dr.getURIString());
90                 }
91             }
92             
93             formObject.setAllDatasources(allDataSources);
94             
95             // TODO get this from main flow
96
getAllFolders(formObject);
97             
98             String JavaDoc folderURI = (String JavaDoc) context.getFlowScope().get(PARENT_FOLDER_ATTR);
99             if (folderURI == null)
100             {
101                 folderURI = "/";
102             }
103             
104             if (formObject.getReportDataSource() != null) { // TODO put parent folder in flow scope in main flow
105
formObject.getReportDataSource().setParentFolder(
106                         (String JavaDoc) context.getFlowScope().get(PARENT_FOLDER_ATTR));
107             }
108         }
109
110         log("Type of datasource="+formObject.getType()+" Mode="+formObject.getMode());
111         // context.getFlowScope().put(FORM_OBJECT_KEY, formObject);
112
context.getFlowScope().put("constants", constants);
113         return success();
114     }
115     
116     private void getAllFolders(ReportDataSourceWrapper wrapper)
117     {
118         List JavaDoc allFolders = repository.getAllFolders(null);
119         wrapper.setAllFolders(new ArrayList JavaDoc());
120         for (int i = 0; i < allFolders.size(); i++) {
121             String JavaDoc folderUri = ((Folder) allFolders.get(i)).getURIString();
122             wrapper.getAllFolders().add(folderUri);
123         }
124     }
125     
126     public Event handleTypeSelection(RequestContext context) throws Exception JavaDoc{
127         ReportDataSourceWrapper formObject = (ReportDataSourceWrapper) getFormObject(context);
128         
129         //If the object instance held by ReportDataSource is not the type selected copy common things
130

131         log("Type=" + formObject.getType());
132         
133         String JavaDoc dsType = formObject.getType();
134         ReportDataSource ds = formObject.getReportDataSource();
135
136         if(ds == null){
137             // We are starting a new data source
138
ds = newReportDataSource(dsType);
139             formObject.setReportDataSource(ds);
140         } else {
141             formObject.setReportDataSource(newReportDataSource(dsType, ds));
142         }
143 /* if (JdbcReportDataSource.class.isAssignableFrom(ds.getClass())) {
144             if(!dsType.equals(constants.getJDBCDatasourceType())){
145                 formObject.setReportDataSource(newReportDataSource(dsType, ds));
146             }
147         } else if(JndiJdbcReportDataSource.class.isAssignableFrom(ds.getClass())) {
148             // ReportDataSource holds an instance of JdbcReportDataSource
149             if(!dsType.equals(constants.getJNDIDatasourceType())){
150                 formObject.setReportDataSource(newReportDataSource(dsType, ds));
151             }
152         } else {
153             // was BeanReportDataSource
154             if(!dsType.equals(constants.getBeanDatasourceType())){
155                 formObject.setReportDataSource(newReportDataSource(dsType, ds));
156             }
157         }
158 */
return success();
159     }
160     public Event saveLookup(RequestContext context) throws Exception JavaDoc {
161         ReportDataSourceWrapper wrapper = (ReportDataSourceWrapper) getFormObject(context);
162         
163         log("user selected a reusable ReportDataSource");
164         
165         String JavaDoc selectedUri = wrapper.getSelectedUri();
166         
167         Resource resource = repository.getResource(null, selectedUri);
168         
169         wrapper.setReportDataSource((ReportDataSource)resource);
170         
171         return success();
172     }
173
174     public Event saveNone(RequestContext context) throws Exception JavaDoc {
175         ReportDataSourceWrapper wrapper = (ReportDataSourceWrapper) getFormObject(context);
176
177         log("user selected no ReportDataSource");
178         wrapper.setReportDataSource(null);
179
180         return success();
181     }
182
183     public Event saveDatasource(RequestContext context) throws Exception JavaDoc {
184         ReportDataSourceWrapper wrapper=(ReportDataSourceWrapper) getFormObject(context);
185         
186         log("Saving the datasource back ");
187         
188         if (wrapper.isStandAloneMode()){
189             
190             if (wrapper.getType() != null) {
191                 
192                 ReportDataSource ds = wrapper.getReportDataSource();
193                 
194                 log("Saving DataSource name=" + ds.getName() +
195                     " datasource desc=" + ds.getDescription() + " in folder=" + ds.getParentFolder());
196                 
197                 if(ds.getName() != null)
198                     repository.saveResource(null,ds);
199             }
200         }
201         return success();
202     }
203
204     public Object JavaDoc loadFormObject(RequestContext context)
205     {
206         ReportDataSourceWrapper formObject = new ReportDataSourceWrapper();
207         formObject.setAllTypes(getDataSourceMappings().getKeys());
208         String JavaDoc resourceUri = context.getRequestParameters().get(DATASOURCEURI_PARAM);
209         
210         if (resourceUri != null && resourceUri.trim().length() != 0){
211             Resource resource = (Resource)repository.getResource(null,resourceUri);
212             
213             if (resource == null)
214                 throw new JSException("Could not find resource with URI "+resourceUri);
215             
216             log("Found resource with uri=" + resourceUri);
217             
218             formObject.setMode(BaseDTO.MODE_STAND_ALONE_EDIT);
219             
220             ReportDataSource dataSource = (ReportDataSource) resource;
221             
222             formObject.setType(getDataSourceMappings().getIdForClass(dataSource.getClass()));
223             /*
224             if(JdbcReportDataSource.class.isAssignableFrom(dataSource.getClass())){
225                 formObject.setType(constants.getJDBCDatasourceType());
226             }else if(JndiJdbcReportDataSource.class.isAssignableFrom(dataSource.getClass()))
227                     formObject.setType(constants.getJNDIDatasourceType());
228             else
229                 formObject.setType(constants.getBeanDatasourceType());
230             */

231             formObject.setReportDataSource(dataSource);
232         }
233         if (formObject.getReportDataSource() == null){
234             String JavaDoc parentFolder = (String JavaDoc) context.getFlowScope().get(PARENT_FOLDER_ATTR);
235             if (parentFolder == null || parentFolder.trim().length() == 0)
236                 parentFolder="/";
237             
238             log("Datasource flow: Stand alone new mode");
239             
240             formObject.setMode(BaseDTO.MODE_STAND_ALONE_NEW);
241             // set default options for datasource type
242
formObject.setType(constants.getJNDIDatasourceType());
243             formObject.setSource(constants.getFieldChoiceLocal());
244             ReportDataSource jndiSource = (ReportDataSource) newReportDataSource(constants.getJNDIDatasourceType());
245             jndiSource.setParentFolder(parentFolder);
246             jndiSource.setVersion(Resource.VERSION_NEW);
247             formObject.setReportDataSource(jndiSource);
248         }
249         return formObject;
250     }
251
252     private ReportDataSource newReportDataSource(String JavaDoc dsType) {
253 /* ReportDataSource ds;
254         if(dsType.equals(constants.getJNDIDatasourceType())){
255             ds=(JndiJdbcReportDataSource)repository.newResource(null,JndiJdbcReportDataSource.class);
256         } else if (dsType.equals(constants.getJDBCDatasourceType())) {
257             ds=(JdbcReportDataSource)repository.newResource(null,JdbcReportDataSource.class);
258         } else {
259             ds = (BeanReportDataSource) repository.newResource(null,BeanReportDataSource.class);
260         }
261 */
return (ReportDataSource) getDataSourceMappings().newResource(null, dsType);
262     }
263     
264     private ReportDataSource newReportDataSource(String JavaDoc dsType, ReportDataSource oldDS) {
265         ReportDataSource newDS = newReportDataSource(dsType);
266         newDS.setParentFolder(oldDS.getParentFolder());
267         newDS.setName(oldDS.getName());
268         newDS.setLabel(oldDS.getLabel());
269         newDS.setDescription(oldDS.getDescription());
270         newDS.setVersion(oldDS.getVersion());
271         
272         return newDS;
273     }
274     
275     public RepositoryService getRepository() {
276         return repository;
277     }
278
279     public void setRepository(RepositoryService repository) {
280         this.repository = repository;
281     }
282
283     /**
284      * @return Returns the dataSourceMappings.
285      */

286     public ResourceFactory getDataSourceMappings() {
287         return dataSourceMappings;
288     }
289     /**
290      * @param dataSourceMappings The dataSourceMappings to set.
291      */

292     public void setDataSourceMappings(ResourceFactory dataSourceMappings) {
293         this.dataSourceMappings = dataSourceMappings;
294     }
295     /**
296      * Helper method to facilitate easy logging level change
297      *
298      * @param text
299      */

300     private void log(String JavaDoc text) {
301         log.debug(text);
302     }
303     public static String JavaDoc getDATASOURCEURI_PARAM() {
304         return DATASOURCEURI_PARAM;
305     }
306     public static String JavaDoc getFORM_OBJECT_KEY() {
307         return FORM_OBJECT_KEY;
308     }
309 }
310
Popular Tags