KickJava   Java API By Example, From Geeks To Geeks.

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


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.validation.DataBinder;
29 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
30 import org.springframework.webflow.Event;
31 import org.springframework.webflow.RequestContext;
32 import org.springframework.webflow.ScopeType;
33 import org.springframework.webflow.action.FormAction;
34
35 import com.jaspersoft.jasperserver.api.JSException;
36 import com.jaspersoft.jasperserver.api.common.domain.ValidationResult;
37 import com.jaspersoft.jasperserver.api.engine.common.service.EngineService;
38 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource;
39 import com.jaspersoft.jasperserver.api.metadata.common.domain.InputControl;
40 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource;
41 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup;
42 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference;
43 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
44 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JdbcReportDataSource;
45 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JndiJdbcReportDataSource;
46 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
47 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit;
48 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria;
49 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl;
50 import com.jaspersoft.jasperserver.war.dto.BaseDTO;
51 import com.jaspersoft.jasperserver.war.dto.FileResourceWrapper;
52 import com.jaspersoft.jasperserver.war.dto.InputControlWrapper;
53 import com.jaspersoft.jasperserver.war.dto.ReportDataSourceWrapper;
54 import com.jaspersoft.jasperserver.war.dto.ReportUnitWrapper;
55 import com.jaspersoft.jasperserver.war.dto.ResourceReferenceDTO;
56 import com.jaspersoft.jasperserver.war.validation.ReportDetailsValidator;
57
58 public class ReportUnitAction extends FormAction {
59
60     private static final String JavaDoc DATA_SOURCE_PARENT_TYPE = "reportUnit";
61     private static final String JavaDoc RU_URI_PARAM = "resource";
62
63     private static final String JavaDoc FORM_OBJECT_KEY = "wrapper";
64
65     private static final String JavaDoc REPORT_UNIT_SUFFIX = "_unit";
66
67     private static final String JavaDoc REPORT_UNIT_KEY = "unit";
68
69     private static final String JavaDoc CONTROL_OBJECT_KEY = "control";
70
71     private static final String JavaDoc RESOURCE_OBJECT_KEY = "resource";
72
73     private static final String JavaDoc DATASOURCE_OBJECT_KEY = "dataResource";
74
75     private static final String JavaDoc PARENT_FOLDER_ATTR = "parentFolder";
76
77     private static final String JavaDoc PARAM_FROM_PAGE = "frompage";
78
79     private static final String JavaDoc PARAM_RESOURCE_NAME = "resourceName";
80     
81     private static final String JavaDoc UNPARSABLE_JRXML_ATTR = "jrxmlUnparsable";
82     
83     private static final String JavaDoc UNPARSABLE_JRXML_MESSAGE = "The selected Jrxml could not be parsed";
84     
85     private static final String JavaDoc LOCAL_JRXML_SUFFIX = "_";
86
87     protected final Log log = LogFactory.getLog(this.getClass());
88
89     private RepositoryService repository;
90
91     private EngineService engine;
92
93     private JasperServerConstImpl constants = new JasperServerConstImpl();
94     
95     private String JavaDoc queryReferenceReqAttrName;
96     private String JavaDoc queryReferenceOutpuAttrName;
97
98     public ReportUnitAction() {
99         setFormObjectClass(ReportUnitWrapper.class);
100         setFormObjectName(FORM_OBJECT_KEY);
101         setFormObjectScope(ScopeType.FLOW);
102         setValidator(new ReportDetailsValidator());
103     }
104
105     public Event initAction(RequestContext context) throws Exception JavaDoc {
106         ReportUnitWrapper wrapper = (ReportUnitWrapper) getFormObject(context);
107         // Get a list of all jrxmls in repo and set in the formObject
108
FilterCriteria criteria = FilterCriteria
109                 .createFilter(FileResource.class);
110         criteria.addFilterElement(FilterCriteria.createPropertyEqualsFilter(
111                 "fileType", FileResource.TYPE_JRXML));
112         ResourceLookup[] lookups = repository.findResource(null, criteria);
113         List JavaDoc allJrxmls = null;
114         if (lookups != null && lookups.length != 0) {
115             log("Found JRXML lookups size=" + lookups.length);
116             allJrxmls = new ArrayList JavaDoc(lookups.length);
117             for (int i = 0; i < lookups.length; i++) {
118                 Resource fileR = (Resource) lookups[i];
119                 allJrxmls.add(fileR.getURIString());
120                 log("added uri=" + fileR.getURIString());
121             }
122             wrapper.setReusableJrxmls(allJrxmls);
123         } else {
124             log("No reusable Jrxmls found");
125         }
126         /* In new Mode get a list of all resources already present in the chosen
127          * folder, to validate report name's uniqueness */

128         if (wrapper.isNewMode()) {
129             String JavaDoc folderURI = wrapper.getReportUnit().getParentFolder();
130             if (folderURI == null)
131             {
132                 folderURI = "/";
133             }
134             FilterCriteria resourcesInFolder = FilterCriteria.createFilter();
135             resourcesInFolder.addFilterElement(FilterCriteria
136                     .createParentFolderFilter(folderURI));
137             log("Searching for resources in the chosen folder:"+folderURI);
138             ResourceLookup[] existingResources = repository.findResource(null,
139                     resourcesInFolder);
140             
141             if (existingResources != null && existingResources.length != 0) {
142                 log("res lookup size="+existingResources.length);
143                 List JavaDoc allResources = new ArrayList JavaDoc();
144                 for (int i = 0; i < existingResources.length; i++) {
145                     ResourceLookup rLookup = existingResources[i];
146                     allResources.add(rLookup.getName());
147                     log("adding resource: "+rLookup.getName()+ " to the list");
148                 }
149                 wrapper.setExistingResources(allResources);
150             }
151         }
152         context.getFlowScope().put("constants", constants);
153         return success();
154     }
155
156     public Event uploadJRXML(RequestContext context) throws Exception JavaDoc {
157         ReportUnitWrapper wrapper = (ReportUnitWrapper) getFormObject(context);
158         String JavaDoc jrxmlSource = wrapper.getSource();
159         // jrxmlSource should never be null with validations in place
160
ReportUnit ru = wrapper.getReportUnit();
161         ResourceReference jrxmlRef = ru.getMainReport();
162         if (jrxmlSource != null
163                 && jrxmlSource.equals(constants.getFieldChoiceFile())) {
164             log("JRXML source was " + constants.getFieldChoiceFile());
165             FileResource jrxml;
166             if (!jrxmlRef.isLocal()) {
167                 // The jrxml holds object instance of type FileResourceLookup
168
jrxml = (FileResource) repository.newResource(null,
169                         FileResource.class);
170                 log("Replaced the JRXML lookup instance with a real object");
171                 jrxml.setData(wrapper.getJrxmlData());
172                 jrxml.setFileType(FileResource.TYPE_JRXML);
173                 jrxml.setName(ru.getName() + LOCAL_JRXML_SUFFIX);
174                 jrxmlRef = new ResourceReference(jrxml);
175                 wrapper.setJrxmlChanged(true);
176             } else {
177                 // jrxml hold an instance of type FileResource
178
log("Jrxml instance was FileResource");
179                 jrxml = (FileResource) jrxmlRef.getLocalResource();
180                 if (wrapper.getJrxmlData() != null
181                         && wrapper.getJrxmlData().length != 0) {
182                     // User uploaded a new jrxml file, change the data
183
log("Setting the uploaded data into the jrxml");
184                     if (jrxml.getName() == null) {
185                         log("Jrxml Name was null, setting it now");
186                         jrxml.setName(ru.getName() + LOCAL_JRXML_SUFFIX);
187                     }
188                     jrxml.setData(wrapper.getJrxmlData());
189                     wrapper.setJrxmlChanged(true);
190                 } else {
191                     log("No Jrxml upload detected");
192                     // user didnt upload a new file, JRXML is not changed
193
}
194             }
195             if (jrxml.getLabel() == null)
196                 jrxml.setLabel(ru.getLabel());
197             // jrxml.setDescription(ru.getDescription());
198
ru.setMainReport(jrxml);
199         } else {
200             if (jrxmlRef.isLocal()) {
201                 // The jrxml object is not a FileResourceLookup object, change
202
// the object
203
jrxmlRef = new ResourceReference(wrapper.getJrxmlUri());
204                 log("Replaced the JRXML instance with a lookup object");
205                 log("Jrxml uri set to " + jrxmlRef.getReferenceURI());
206                 wrapper.setJrxmlChanged(true);
207             } else {
208                 // jrxml is an instance of JRXML Lookup
209
log("Setting the lookup uri onto the JRXML lookup");
210                 if (!jrxmlRef.getReferenceURI()
211                         .equals(wrapper.getOldJrxmlUri())) {
212                     // User changed the jrxml URI selection, JRXML is changed
213
wrapper.setJrxmlChanged(true);
214                     wrapper.setOldJrxmlUri(jrxmlRef.getReferenceURI());
215                     jrxmlRef.setReference(wrapper.getJrxmlUri());
216                     log("Jrxml uri set to " + jrxmlRef.getReferenceURI());
217                 } else {
218                     log("JRXML URI was not changed");
219                     // The selected URI is same as earlier one, JRXML is not
220
// changed
221
}
222             }
223             ru.setMainReport(jrxmlRef);
224         }
225
226         // If jrxml is changed, parse jrxml to get a list of suggested resources
227
if (wrapper.isJrxmlChanged()) {
228             log("jrxml was changed, parsing now");
229
230             wrapper.setHasSuggestedResources(false);
231             List JavaDoc sugResources = wrapper.getSuggestedResources();
232             if (sugResources != null && !sugResources.isEmpty()) {
233                 // Lose previous unlocated resources (if they were suggested
234
// sometime earlier)
235
for (int i = sugResources.size() - 1; i > -1; i--) {
236                     FileResourceWrapper frW = (FileResourceWrapper) sugResources
237                             .get(i);
238                     if (!frW.isLocated()) {
239                         sugResources.remove(i);
240                         ru.removeResourceLocal(frW.getFileResource().getName());
241                     }
242                 }
243                 // Clear the suggested Resources list
244
sugResources = null;
245             }
246             wrapper.setHasSuggestedControls(false);
247             List JavaDoc sugControls = wrapper.getSuggestedControls();
248             if (sugControls != null && !sugControls.isEmpty()) {
249                 // Lose previous unlocated controls (if they were suggested
250
// sometime earlier)
251
for (int i = sugControls.size() - 1; i > -1; i--) {
252                     InputControlWrapper icW = (InputControlWrapper) sugControls
253                             .get(i);
254                     if (!icW.isLocated()) {
255                         sugControls.remove(i);
256                         ru.removeInputControlLocal(icW.getInputControl()
257                                 .getName());
258                     }
259                 }
260                 // Clear the suggested Controls list
261
sugControls = null;
262             }
263             Resource[] parsedResources=null;
264             try{
265                 parsedResources = engine.getResources(jrxmlRef);
266             }catch(Exception JavaDoc ex){
267                 //The JRXML file couldnt be parsed
268
context.getRequestScope().put(UNPARSABLE_JRXML_ATTR,UNPARSABLE_JRXML_MESSAGE);
269                 wrapper.setJrxmlLocated(false);
270                 return error();
271             }
272             
273             if (parsedResources != null || parsedResources.length != 0) {
274                 log("Parsed " + parsedResources.length + " from JRXML");
275                 for (int i = 0; i < parsedResources.length; i++) {
276                     Resource resource = parsedResources[i];
277
278                     if (FileResource.class
279                             .isAssignableFrom(resource.getClass())) {
280                         // Resource is a FileResource object
281
wrapper.setHasSuggestedResources(true);
282                         FileResource suggestedFileResource = (FileResource) resource;
283                         FileResourceWrapper frW = new FileResourceWrapper();
284                         frW.setFileResource(suggestedFileResource);
285                         log("Detected suggested FileResource : "
286                                 + suggestedFileResource.getName() + " of type "
287                                 + suggestedFileResource.getFileType());
288                         // Check if a resource by this name is already added
289
FileResource addedFr = ru
290                                 .getResourceLocal(suggestedFileResource
291                                         .getName());
292                         if (addedFr != null) {
293                             log("Suggested FileResource is alreday added");
294                             // mark it added, in the wrapper
295
frW.setLocated(true);
296                         } else {
297                             // The resource is not added already, dont mark it
298
// located
299
log("Adding Suggested FileResource to ReportUnit now");
300                             ru.addResource(suggestedFileResource);
301                         }
302                         // Keep a wrapped copy to track
303
if (sugResources == null)
304                             sugResources = new ArrayList JavaDoc(parsedResources.length
305                                     - i);
306                         sugResources.add(frW);
307                     } else {
308                         if (InputControl.class.isAssignableFrom(resource
309                                 .getClass())) {
310                             // Resource object is an InputControl
311
wrapper.setHasSuggestedControls(true);
312                             InputControl suggestedControl = (InputControl) resource;
313                             InputControlWrapper icW = new InputControlWrapper(
314                                     suggestedControl);
315                             log("Detected suggested Control : "
316                                     + suggestedControl.getName());
317
318                             // Check if a resource by this name is already added
319
InputControl removedIc = ru
320                                     .getInputControl(suggestedControl.getName());
321                             if (removedIc != null) {
322                                 // Mark the control as added in the wrapper
323
icW.setLocated(true);
324                             } else {
325                                 // Suggested Control isnt added to the
326
// ReportUnit already
327
ru.addInputControl(suggestedControl);
328                             }
329                             // Keep a wrapped copy to track
330
if (sugControls == null)
331                                 sugControls = new ArrayList JavaDoc(
332                                         parsedResources.length - i);
333                             icW.setSuggested(true);
334                             sugResources.add(icW);
335                         }
336                     }
337                 }
338             } else {
339                 log("Found no resources on parsing jrxml");
340             }
341             wrapper.setSuggestedResources(sugResources);
342             wrapper.setSuggestedControls(sugControls);
343         }
344         return success();
345     }
346
347     /* Events originating from list resources page */
348     public Event removeResource(RequestContext context) throws Exception JavaDoc {
349         log("In removeResource");
350         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
351         String JavaDoc resName = context.getRequestParameters()
352                 .get(PARAM_RESOURCE_NAME);
353         log("recieved resName param=" + resName);
354         if (resName != null && resName.trim().length() != 0) {
355             ReportUnit ru = ruWrapper.getReportUnit();
356             ru.removeResourceLocal(resName);
357             List JavaDoc sugRes = ruWrapper.getSuggestedResources();
358             List JavaDoc allResources = ru.getResources();
359             if (ru.getResources() == null
360                     || ru.getResources().isEmpty()
361                     || (sugRes != null && allResources.size() - sugRes.size() == 0))
362                 ruWrapper.setHasNonSuggestedResources(false);
363             // If the resource figures in the suggested resources list, remove
364

365             if (sugRes != null && !sugRes.isEmpty()) {
366                 for (int i = 0; i < sugRes.size(); i++) {
367                     FileResourceWrapper fr = (FileResourceWrapper) sugRes
368                             .get(i);
369                     if (fr.getFileResource().getName().equals(resName))
370                         fr.setLocated(false);
371                 }
372             }
373
374         }
375         return success();
376     }
377
378     public Event removeControl(RequestContext context) throws Exception JavaDoc {
379         log("In removeControl");
380         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
381         String JavaDoc resName = context.getRequestParameters()
382                 .get(PARAM_RESOURCE_NAME);
383         log("recieved resName param=" + resName);
384         if (resName != null && resName.trim().length() != 0) {
385             ReportUnit ru = ruWrapper.getReportUnit();
386
387             if (ru.getInputControl(resName) != null)
388                 ru.removeInputControlLocal(resName);
389             else
390                 ru.removeInputControlReference(resName);
391             List JavaDoc sugCont = ruWrapper.getSuggestedControls();
392             List JavaDoc allControls = ru.getInputControls();
393             if (allControls == null
394                     || allControls.isEmpty()
395                     || (sugCont != null && allControls.size() - sugCont.size() == 0))
396                 ruWrapper.setHasNonSuggestedControls(false);
397             // If the name figures in suggested controls list mark the control
398
// as Not added
399

400             if (sugCont != null && !sugCont.isEmpty()) {
401                 for (int i = 0; i < sugCont.size(); i++) {
402                     InputControlWrapper icW = (InputControlWrapper) sugCont
403                             .get(i);
404                     if (icW.getInputControl().getName().equals(resName))
405                         icW.setLocated(false);
406                 }
407             }
408
409         }
410         return success();
411     }
412
413     public Event addResource(RequestContext context) throws Exception JavaDoc {
414         log("In addNewResource");
415         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
416         FileResourceWrapper frW = new FileResourceWrapper();
417         FileResource fileR = (FileResource) repository.newResource(null,
418                 FileResource.class);
419         frW.setMode(BaseDTO.MODE_SUB_FLOW_NEW);
420         frW.setFileResource(fileR);
421         // Set current form object to validate duplicate resource names
422
frW.setParentFlowObject(ruWrapper);
423         // Set the FileResourceWrapper object into scope for the subflow
424
context.getFlowScope()
425                 .put(FileResourceAction.getFORM_OBJECT_KEY(), frW);
426         return success();
427     }
428
429
430     private List JavaDoc getControlsList()
431     {
432         List JavaDoc existingPathsList=new ArrayList JavaDoc();
433         FilterCriteria criteria = FilterCriteria.createFilter(InputControl.class);
434         ResourceLookup[] lookups = repository.findResource(null, criteria);
435         if(lookups!=null){
436             for(int i=0;i<lookups.length;i++){
437                 existingPathsList.add(lookups[i].getURIString());
438             }
439         }
440
441         return existingPathsList;
442     }
443
444     public Event addControl(RequestContext context) throws Exception JavaDoc {
445         log("In addNewControl");
446         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
447         InputControlWrapper icW = new InputControlWrapper();
448         InputControl ic = (InputControl) repository.newResource(null,
449                 InputControl.class);
450         icW.setMode(BaseDTO.MODE_SUB_FLOW_NEW);
451         icW.setInputControl(ic);
452         // Set current form object to validate duplicate resource names
453
icW.setParentFlowObject(ruWrapper);
454         // Set the InputControlWrapper into scope for the subflow
455
context.getFlowScope().put(
456                 DefineInputControlsAction.getFORM_OBJECT_KEY(), icW);
457
458         ruWrapper.setInputControlList(getControlsList());
459         ruWrapper.setInputControlSource(JasperServerConstImpl.getFieldChoiceRepo());
460         return success();
461     }
462
463     public Event editResource(RequestContext context) throws Exception JavaDoc {
464         log("In editResource");
465         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
466         String JavaDoc resName = context.getRequestParameters()
467                 .get(PARAM_RESOURCE_NAME);
468         log("recieved resName param=" + resName);
469         if (resName != null && resName.trim().length() != 0) {
470             FileResource fileR = ruWrapper.getReportUnit().getResourceLocal(
471                     resName);
472             if (fileR == null)
473                 log("NO Local resource by that name");
474             else
475                 log("Passed resName=" + fileR.getName() + " type="
476                         + fileR.getFileType());
477             FileResourceWrapper frW = new FileResourceWrapper();
478             frW.setMode(BaseDTO.MODE_SUB_FLOW_EDIT);
479             frW.setFileResource(fileR);
480             // Set the FileResourceWrapper object into scope for the subflow
481
context.getFlowScope().put(FileResourceAction.getFORM_OBJECT_KEY(),
482                     frW);
483         }
484         return success();
485     }
486
487     public Event editControl(RequestContext context) throws Exception JavaDoc {
488         log("In editControl");
489         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
490         String JavaDoc resName = context.getRequestParameters()
491                 .get(PARAM_RESOURCE_NAME);
492         if (resName != null && resName.trim().length() != 0) {
493             InputControl ic = ruWrapper.getReportUnit()
494                     .getInputControl(resName);
495             InputControlWrapper icW = new InputControlWrapper();
496             icW.setMode(BaseDTO.MODE_SUB_FLOW_EDIT);
497             icW.setInputControl(ic);
498             // Set the FileResourceWrapper object into scope for the subflow
499
context.getFlowScope().put(
500                     DefineInputControlsAction.getFORM_OBJECT_KEY(), icW);
501             ruWrapper.setInputControlList(getControlsList());
502             if (ic == null) {
503                 ruWrapper.setInputControlPath(resName);
504                 ruWrapper.setInputControlSource(JasperServerConstImpl.getFieldChoiceRepo());
505                 icW.setOldInputControlURI(resName);
506             }
507             else
508                 ruWrapper.setInputControlSource(JasperServerConstImpl.getFieldChoiceLocal());
509         }
510
511         return success();
512     }
513
514     public Event selectControlType(RequestContext context) throws Exception JavaDoc
515     {
516         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
517         InputControlWrapper icW = (InputControlWrapper) context.getFlowScope().get(DefineInputControlsAction.getFORM_OBJECT_KEY());
518         String JavaDoc source = context.getRequestParameters().get("inputControlSource");
519         if (source.equals(JasperServerConstImpl.getFieldChoiceLocal())) {
520             if (icW.getInputControl() == null) {
521                 InputControl ic = (InputControl) repository.newResource(null,
522                         InputControl.class);
523                 icW.setInputControl(ic);
524                 icW.setMode(BaseDTO.MODE_SUB_FLOW_NEW);
525             }
526             return yes();
527         } else {
528             String JavaDoc path;// = ruWrapper.getInputControlPath(); FIXME
529
path = context.getRequestParameters().get("inputControlPath");
530             ResourceReference inputControlURI = new ResourceReference(path);
531             icW.setInputControlURI(inputControlURI);
532             return no();
533         }
534     }
535
536     /* End events originating from List resources page */
537
538     public Event saveResource(RequestContext context) throws Exception JavaDoc {
539         log("In saveResource");
540         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
541         FileResourceWrapper frW = (FileResourceWrapper) context.getFlowScope()
542                 .get(FileResourceAction.getFORM_OBJECT_KEY());
543         if (frW != null) {
544             // FileResourceWrapper returned by a subflow, determine if its new
545
// object or edited
546
if (frW.isEditMode()) {
547                 ReportUnit ru = ruWrapper.getReportUnit();
548
549                 // If the FileResource is a suggested one, change that reference
550
// also
551
List JavaDoc sugRes = ruWrapper.getSuggestedResources();
552                 if (sugRes != null && !sugRes.isEmpty()) {
553                     for (int i = 0; i < sugRes.size(); i++) {
554                         FileResourceWrapper sRW = (FileResourceWrapper) sugRes
555                                 .get(i);
556                         if (sRW.getFileResource().getName().equals(
557                                 frW.getFileResource().getName())) {
558                             sRW.setFileResource(frW.getFileResource());
559                             sRW.setLocated(true);
560                             break;
561                         }
562                     }
563                 }
564                 ru.removeResourceLocal(frW.getFileResource().getName());
565             }
566             // In any case save the new/edited FileResource into ReportUnit
567
ruWrapper.getReportUnit().addResource(frW.getFileResource());
568         }
569         return success();
570     }
571
572     public Event saveControl(RequestContext context) throws Exception JavaDoc {
573         log("In saveControl");
574         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
575         InputControlWrapper icW = (InputControlWrapper) context.getFlowScope()
576                 .get(DefineInputControlsAction.getFORM_OBJECT_KEY());
577         if (icW != null) {
578             // InputControlWrapper returned by a subflow
579
if (icW.isEditMode()) {
580
581                 if (icW.getOldInputControlURI() != null) {
582                     ruWrapper.getReportUnit().removeInputControlReference(icW.getOldInputControlURI());
583                 }
584                 else {
585                     ruWrapper.getReportUnit().removeInputControlLocal(
586                             icW.getInputControl().getName());
587                 }
588                 // If the InputControl is suggested, update it
589
List JavaDoc sugControls = ruWrapper.getSuggestedControls();
590                 if (sugControls != null && !sugControls.isEmpty()) {
591                     for (int i = 0; i < sugControls.size(); i++) {
592                         InputControlWrapper sICW = (InputControlWrapper) sugControls
593                                 .get(i);
594                         if (icW.getInputControlURI() != null) {
595                             if (icW.getInputControlURI().getReferenceURI().equals(sICW.getInputControlURI().getReferenceURI())) {
596                                 sICW.setInputControlURI(icW.getInputControlURI());
597                                 sICW.setLocated(true);
598                                 break;
599                             }
600                         } else {
601                             if (icW.getInputControl().getName().equals(
602                                     sICW.getInputControl().getName())) {
603                                 sICW.setInputControl(icW.getInputControl());
604                                 sICW.setLocated(true);
605                                 break;
606                             }
607                         }
608                     }
609                 }
610             }
611             // In any case save the edited/New Input Control into ReportUnit
612

613             if (icW.getOldInputControlURI() != null)
614                 ruWrapper.getReportUnit().removeInputControlReference(icW.getOldInputControlURI());
615             if (icW.getInputControlURI() != null)
616                 ruWrapper.getReportUnit().addInputControl(icW.getInputControlURI());
617             else
618                 ruWrapper.getReportUnit().addInputControl(icW.getInputControl());
619             ruWrapper.setHasNonSuggestedControls(true);
620         }
621         return success();
622     }
623
624     public Event locateDataSource(RequestContext context) throws Exception JavaDoc {
625         log("In locate data source");
626         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
627         ResourceReference dsRef = ruWrapper.getReportUnit().getDataSource();
628         ReportDataSourceWrapper rdWrapper = new ReportDataSourceWrapper();
629         rdWrapper.setParentType(DATA_SOURCE_PARENT_TYPE);
630         rdWrapper.setMode(BaseDTO.MODE_SUB_FLOW_NEW);
631         if (dsRef == null) {
632 // log("Found no previous ReportDataSource, creating new");
633
// ReportDataSource ds = (ReportDataSource) repository.newResource(
634
// null, JdbcReportDataSource.class);
635
// dsRef = new ResourceReference(ds);
636
// rdWrapper.setSource(constants.getFieldChoiceLocal());
637
// rdWrapper.setType(constants.getJDBCDatasourceType());
638
// rdWrapper.setReportDataSource(ds);
639
log("Using no datasource");
640             rdWrapper.setSource(constants.getFieldChoiceNone());
641             rdWrapper.setReportDataSource(null);
642         } else {
643             // if the dataSource exists decide source and type and set in
644
// wrapper
645
if (!dsRef.isLocal()) {
646                 // DataSource object is a lookup
647
log("Found ReportDataSourceLookup");
648                 rdWrapper.setSource(constants.getFieldChoiceRepo());
649                 rdWrapper.setSelectedUri(dsRef.getReferenceURI());
650             } else {
651                 rdWrapper.setSource(constants.getFieldChoiceLocal());
652                 ReportDataSource ds = (ReportDataSource) dsRef
653                         .getLocalResource();
654                 if (JdbcReportDataSource.class.isAssignableFrom(ds.getClass())) {
655                     log("Found JDBCReportDataSource");
656                     rdWrapper.setType(constants.getJDBCDatasourceType());
657                 } else {
658                     log("Found JndiJdbcReportDataSourceLookup");
659                     if (JndiJdbcReportDataSource.class.isAssignableFrom(ds
660                             .getClass()))
661                         rdWrapper.setType(constants.getJNDIDatasourceType());
662                 }
663                 rdWrapper.setReportDataSource(ds);
664             }
665         }
666         // Set the object into scope with the name that the reportDataSourceFlow
667
// can pickup
668
context.getFlowScope().put(ReportDataSourceAction.getFORM_OBJECT_KEY(),
669                 rdWrapper);
670         return success();
671     }
672
673     public Event saveDatasource(RequestContext context) throws Exception JavaDoc {
674         // Save the returned datasource info
675
ReportDataSourceWrapper resource = (ReportDataSourceWrapper) context
676                 .getFlowScope().get(DATASOURCE_OBJECT_KEY);
677         if (resource == null)
678             log("Got null datatsource back from subflow");
679         else
680             log("type was " + resource.getType());
681         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
682         if (resource.getSource().equals(constants.getFieldChoiceRepo())) {
683             ruWrapper.getReportUnit().setDataSourceReference(
684                     resource.getSelectedUri());
685         } else {
686             ruWrapper.getReportUnit().setDataSource(
687                     resource.getReportDataSource());
688         }
689         return success();
690     }
691
692     public Event saveReport(RequestContext context) throws Exception JavaDoc {
693         log("In saveReport");
694         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
695         repository.saveResource(null, ruWrapper.getReportUnit());
696         return success();
697     }
698
699     public Object JavaDoc loadFormObject(RequestContext context) {
700         ReportUnitWrapper formObject = null;
701         String JavaDoc resourceUri = context.getRequestParameters().get(RU_URI_PARAM);
702         if (resourceUri != null && resourceUri.trim().length() != 0) {
703             ReportUnit ru = (ReportUnit) repository.getResource(null,
704                     resourceUri);
705             if (ru == null)
706                 throw new JSException("Could not find resource with URI "
707                         + resourceUri);
708             log("Found resource with uri=" + resourceUri);
709             formObject = new ReportUnitWrapper();
710             formObject.setReportUnit(ru);
711             ResourceReference jrxmlRef = ru.getMainReport();
712             String JavaDoc uri;
713             if (!jrxmlRef.isLocal()) {
714                 // ReportUnit is a lookup object
715
formObject.setSource(constants.FIELD_CHOICE_CONT_REPO);
716                 uri = jrxmlRef.getReferenceURI();
717                 formObject.setJrxmlUri(uri);
718             } else {
719                 formObject.setSource(constants.FIELD_CHOICE_FILE_SYSTEM);
720                 uri = jrxmlRef.getLocalResource().getURIString();
721             }
722             formObject.setOldJrxmlUri(uri);
723             formObject.setOriginalJrxmlUri(uri);
724             formObject.setMode(BaseDTO.MODE_STAND_ALONE_EDIT);
725             formObject.setDatasourceIdentified(true);
726             formObject.setNamed(true);
727             formObject.setJrxmlLocated(true);
728             if (ru.getResources() != null && !ru.getResources().isEmpty())
729                 formObject.setHasNonSuggestedResources(true);
730             else
731                 formObject.setHasNonSuggestedResources(false);
732             if (ru.getInputControls() != null
733                     && !ru.getInputControls().isEmpty())
734                 formObject.setHasNonSuggestedControls(true);
735             else
736                 formObject.setHasNonSuggestedControls(false);
737         }
738         if (formObject == null) {
739             log("ReportUnitAction: Stand alone new mode");
740             formObject = new ReportUnitWrapper();
741             ReportUnit ru = (ReportUnit) repository.newResource(null,
742                     ReportUnit.class);
743             FileResource jrxml = (FileResource) repository.newResource(null,
744                     FileResource.class);
745             jrxml.setFileType(FileResource.TYPE_JRXML);
746             ru.setMainReport(jrxml);
747             formObject.setMode(BaseDTO.MODE_STAND_ALONE_NEW);
748             String JavaDoc parentFolder = (String JavaDoc) context.getFlowScope().get(
749                     PARENT_FOLDER_ATTR);
750             ru.setParentFolder(parentFolder);
751             formObject.setReportUnit(ru);
752             formObject.setSource(constants.FIELD_CHOICE_FILE_SYSTEM);
753             // TODO set default options when creating new RU
754
formObject.setHasNonSuggestedResources(false);
755             formObject.setHasNonSuggestedControls(false);
756         }
757         return formObject;
758     }
759
760     public Event validateReport(RequestContext context) throws Exception JavaDoc {
761         log("In Validate Report");
762         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
763         ReportUnit ru = ruWrapper.getReportUnit();
764         ValidationResult result = engine.validate(null, ru);
765         ruWrapper.setResult(result.getValidationState().equals(
766                 ValidationResult.STATE_VALID));
767         return success();
768     }
769
770     /*
771      * method to get the reposervice object arguments: none returns:
772      * RepositoryService
773      */

774     public RepositoryService getRepository() {
775         return repository;
776     }
777
778     /*
779      * method to set the reposervice object arguments: RepositoryService
780      * returns: void
781      */

782     public void setRepository(RepositoryService repository) {
783         this.repository = repository;
784     }
785
786     public EngineService getEngine() {
787         return engine;
788     }
789
790     public void setEngine(EngineService engine) {
791         this.engine = engine;
792     }
793
794     /**
795      * Registers a byte array editor to allow spring handle File uploads as byte
796      * arrays
797      */

798     protected void initBinder(RequestContext context, DataBinder binder) {
799         binder.registerCustomEditor(byte[].class,
800                 new ByteArrayMultipartFileEditor());
801     }
802
803     private void log(String JavaDoc text) {
804         log.debug(text);
805     }
806     
807     public Event prepareQuery(RequestContext context) throws Exception JavaDoc {
808         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
809         ResourceReference queryRef = ruWrapper.getReportUnit().getQuery();
810         ResourceReferenceDTO queryRefDTO = new ResourceReferenceDTO(queryRef);
811         context.getRequestScope().put(getQueryReferenceReqAttrName(), queryRefDTO);
812         return success();
813     }
814     
815     public Event setQueryReference(RequestContext context) throws Exception JavaDoc {
816         ResourceReferenceDTO queryRef = (ResourceReferenceDTO) context.getFlowScope().getRequired(getQueryReferenceOutpuAttrName(), ResourceReferenceDTO.class);
817         ReportUnitWrapper ruWrapper = (ReportUnitWrapper) getFormObject(context);
818         ruWrapper.getReportUnit().setQuery(queryRef.toResourceReference());
819         return success();
820     }
821     
822     /**
823      * @return Returns the queryReferenceReqAttrName.
824      */

825     public String JavaDoc getQueryReferenceReqAttrName() {
826         return queryReferenceReqAttrName;
827     }
828     
829     /**
830      * @param queryReferenceReqAttrName The queryReferenceReqAttrName to set.
831      */

832     public void setQueryReferenceReqAttrName(String JavaDoc queryReferenceReqAttrName) {
833         this.queryReferenceReqAttrName = queryReferenceReqAttrName;
834     }
835     
836     /**
837      * @return Returns the queryReferenceOutpuAttrName.
838      */

839     public String JavaDoc getQueryReferenceOutpuAttrName() {
840         return queryReferenceOutpuAttrName;
841     }
842     
843     /**
844      * @param queryReferenceOutpuAttrName The queryReferenceOutpuAttrName to set.
845      */

846     public void setQueryReferenceOutpuAttrName(
847             String JavaDoc queryReferenceOutpuAttrName) {
848         this.queryReferenceOutpuAttrName = queryReferenceOutpuAttrName;
849     }
850 }
851
Popular Tags