KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.validation.DataBinder;
24 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
25 import org.springframework.webflow.AttributeMap;
26 import org.springframework.webflow.Event;
27 import org.springframework.webflow.RequestContext;
28 import org.springframework.webflow.ScopeType;
29 import org.springframework.webflow.action.FormAction;
30
31 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
32 import com.jaspersoft.jasperserver.api.metadata.common.domain.Query;
33 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference;
34 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
35 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JdbcReportDataSource;
36 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JndiJdbcReportDataSource;
37 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
38 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl;
39 import com.jaspersoft.jasperserver.war.dto.BaseDTO;
40 import com.jaspersoft.jasperserver.war.dto.QueryWrapper;
41 import com.jaspersoft.jasperserver.war.dto.ReportDataSourceWrapper;
42 import com.jaspersoft.jasperserver.war.validation.QueryValidator;
43
44 /**
45  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
46  * @version $Id
47  */

48 public class EditQueryAction extends FormAction
49 {
50     private static final String JavaDoc DATA_SOURCE_PARENT_TYPE = "query";
51     private static final String JavaDoc FORM_OBJECT_KEY = "query";
52     private static final String JavaDoc PARENT_FOLDER_ATTR = "parentFolder";
53     private static final String JavaDoc CURRENT_QUERY_ATTR = "currentQuery";
54     private static final String JavaDoc IS_EDIT = "isEdit";//FIXME use wrapper to disable name in UI
55
private static final String JavaDoc DATASOURCE_OBJECT_KEY = "dataResource";
56
57     private RepositoryService repository;
58     private JasperServerConstImpl constants = new JasperServerConstImpl();
59     
60     private String JavaDoc queryLanguagesRequestAttrName;
61     private String JavaDoc[] queryLanguages;
62
63     public RepositoryService getRepository() {
64         return repository;
65     }
66
67     public void setRepository(RepositoryService repository) {
68         this.repository = repository;
69     }
70
71
72     protected void initBinder(RequestContext context, DataBinder binder) {
73         binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
74     }
75
76
77     /**
78      *
79      */

80     public EditQueryAction(){
81         setFormObjectClass(QueryWrapper.class); //custom form backing object class
82
setFormObjectName(FORM_OBJECT_KEY);
83         setFormObjectScope(ScopeType.FLOW); //this is a multi-page wizard!
84
setValidator(new QueryValidator());
85     }
86
87
88     /**
89      *
90      */

91     public Object JavaDoc loadFormObject(RequestContext context)
92     {
93         Query query;
94         QueryWrapper wrapper;
95         ExecutionContextImpl executionContext = new ExecutionContextImpl();
96
97         if (context.getFlowScope().get(IS_EDIT) != null)
98         {
99             String JavaDoc currentQuery = (String JavaDoc) context.getFlowScope().get(CURRENT_QUERY_ATTR);
100             query = (Query) repository.getResource(executionContext, currentQuery);
101             wrapper = new QueryWrapper(query);
102             wrapper.setMode(BaseDTO.MODE_STAND_ALONE_EDIT);
103         }
104         else
105         {
106             query = (Query) repository.newResource(executionContext, Query.class);
107             String JavaDoc parentFolder = (String JavaDoc) context.getFlowScope().get(PARENT_FOLDER_ATTR);
108             if (parentFolder == null || parentFolder.trim().length() == 0)
109                 parentFolder = "/";
110             query.setParentFolder(parentFolder);
111             wrapper = new QueryWrapper(query);
112             wrapper.setMode(BaseDTO.MODE_STAND_ALONE_NEW);
113         }
114
115         return wrapper;
116     }
117
118
119     /**
120      *
121      */

122     public Event initAction(RequestContext context) throws Exception JavaDoc
123     {
124         AttributeMap rs = context.getRequestScope();
125         rs.put(FORM_OBJECT_KEY, getFormObject(context));
126         return success();
127     }
128
129
130     /**
131      *
132      */

133     public Event saveQuery(RequestContext context) throws Exception JavaDoc
134     {
135         QueryWrapper wrapper = (QueryWrapper) getFormObject(context);
136         if (wrapper.isStandAloneMode())
137             repository.saveResource(null, wrapper.getQuery());
138
139         return success();
140     }
141
142
143     /**
144      *
145      */

146     public Event locateDataSource(RequestContext context) throws Exception JavaDoc {
147         //log("In locate data source");
148
QueryWrapper queryWrapper = (QueryWrapper) getFormObject(context);
149         ResourceReference dsRef = queryWrapper.getQuery().getDataSource();
150         ReportDataSourceWrapper rdWrapper = new ReportDataSourceWrapper();
151         rdWrapper.setParentType(DATA_SOURCE_PARENT_TYPE);
152         rdWrapper.setMode(BaseDTO.MODE_SUB_FLOW_NEW);
153         if (dsRef == null) {
154             //log("Found no previous ReportDataSource, creating new");
155
// ReportDataSource ds = (ReportDataSource) repository.newResource(
156
// null, JdbcReportDataSource.class);
157
// dsRef = new ResourceReference(ds);
158
// rdWrapper.setSource(constants.getFieldChoiceLocal());
159
// rdWrapper.setType(constants.getJDBCDatasourceType());
160
// rdWrapper.setReportDataSource(ds);
161

162             rdWrapper.setSource(constants.getFieldChoiceNone());
163             rdWrapper.setReportDataSource(null);
164
165         } else {
166             // if the dataSource exists decide source and type and set in
167
// wrapper
168
if (!dsRef.isLocal()) {
169                 // DataSource object is a lookup
170
//log("Found ReportDataSourceLookup");
171
rdWrapper.setSource(constants.getFieldChoiceRepo());
172                 rdWrapper.setSelectedUri(dsRef.getReferenceURI());
173             } else {
174                 rdWrapper.setSource(constants.getFieldChoiceLocal());
175                 ReportDataSource ds = (ReportDataSource) dsRef
176                         .getLocalResource();
177                 if (JdbcReportDataSource.class.isAssignableFrom(ds.getClass())) {
178                     //log("Found JDBCReportDataSource");
179
rdWrapper.setType(constants.getJDBCDatasourceType());
180                 } else {
181                     //log("Found JndiJdbcReportDataSourceLookup");
182
if (JndiJdbcReportDataSource.class.isAssignableFrom(ds
183                             .getClass()))
184                         rdWrapper.setType(constants.getJNDIDatasourceType());
185                 }
186                 rdWrapper.setReportDataSource(ds);
187             }
188         }
189         // Set the object into scope with the name that the reportDataSourceFlow
190
// can pickup
191
context.getFlowScope().put(ReportDataSourceAction.getFORM_OBJECT_KEY(),
192                 rdWrapper);
193         return success();
194     }
195
196
197     /**
198      *
199      */

200     public Event saveDatasource(RequestContext context) throws Exception JavaDoc {
201         ReportDataSourceWrapper resource = (ReportDataSourceWrapper) context
202                 .getFlowScope().get(DATASOURCE_OBJECT_KEY);
203         QueryWrapper queryWrapper = (QueryWrapper) getFormObject(context);
204         if (resource.getSource().equals(constants.getFieldChoiceRepo())) {
205             queryWrapper.getQuery().setDataSourceReference(
206                     resource.getSelectedUri());
207         } else if (resource.getSource().equals(constants.getFieldChoiceLocal())) {
208             queryWrapper.getQuery().setDataSource(
209                     resource.getReportDataSource());
210         } else {
211             queryWrapper.getQuery().setDataSource((ResourceReference) null);
212     }
213         return success();
214     }
215     
216     
217     public Event prepareQueryTextEdit(RequestContext context) {
218         context.getRequestScope().put(getQueryLanguagesRequestAttrName(), getQueryLanguages());
219         return success();
220     }
221     
222
223     public String JavaDoc[] getQueryLanguages() {
224         return queryLanguages;
225     }
226
227     public void setQueryLanguages(String JavaDoc[] queryLanguages) {
228         this.queryLanguages = queryLanguages;
229     }
230
231     public String JavaDoc getQueryLanguagesRequestAttrName() {
232         return queryLanguagesRequestAttrName;
233     }
234
235     public void setQueryLanguagesRequestAttrName(
236             String JavaDoc queryLanguagesRequestAttrName) {
237         this.queryLanguagesRequestAttrName = queryLanguagesRequestAttrName;
238     }
239
240 }
241
242
Popular Tags