KickJava   Java API By Example, From Geeks To Geeks.

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


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.metadata.common.domain.FileResource;
37 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder;
38 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource;
39 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup;
40 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
41 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection;
42 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapClientConnection;
43 import com.jaspersoft.jasperserver.api.metadata.olap.domain.XMLAConnection;
44 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria;
45 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor;
46 import com.jaspersoft.jasperserver.war.common.JasperServerConst;
47 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl;
48 import com.jaspersoft.jasperserver.war.dto.BaseDTO;
49 import com.jaspersoft.jasperserver.war.dto.FileResourceWrapper;
50 import com.jaspersoft.jasperserver.war.dto.OlapClientConnectionWrapper;
51 import com.jaspersoft.jasperserver.war.dto.OlapUnitWrapper;
52 import com.jaspersoft.jasperserver.war.validation.FileResourceValidator;
53
54 public class FileResourceAction extends FormAction {
55
56     protected final Log log = LogFactory.getLog(this.getClass());
57
58     private static final String JavaDoc FORM_OBJECT_KEY = "fileResource";
59
60     private static final String JavaDoc FILERES_URI_PARAM = "resource";
61
62     private static final String JavaDoc PARENT_FOLDER_ATTR = "parentFolder";
63
64     private static final String JavaDoc CONSTANTS_KEY = "constants";
65
66     private RepositoryService repository;
67
68     public FileResourceAction() {
69         setFormObjectClass(FileResourceWrapper.class);
70         setFormObjectName(FORM_OBJECT_KEY);
71         setFormObjectScope(ScopeType.FLOW);
72         setValidator(new FileResourceValidator());
73     }
74
75     public Event initAction(RequestContext context) throws Exception JavaDoc {
76         FileResourceWrapper wrapper = (FileResourceWrapper) getFormObject(context);
77         if (wrapper.isSubflowMode()) {
78             FilterCriteria criteria = FilterCriteria
79                     .createFilter(FileResource.class);
80             if (wrapper.getFileResource().getFileType() != null
81                     && wrapper.getFileResource().getFileType().trim().length() != 0) {
82                 criteria.addFilterElement(FilterCriteria
83                         .createPropertyEqualsFilter("fileType", wrapper
84                                 .getFileResource().getFileType()));
85             }
86             ResourceLookup[] lookups = repository.findResource(null, criteria);
87             List JavaDoc allResources = null;
88             if (lookups != null && lookups.length != 0) {
89                 allResources = new ArrayList JavaDoc();
90                 log("Found lookups size=" + lookups.length);
91                 for (int i = 0; i < lookups.length; i++) {
92                     allResources.add(lookups[i].getURIString());
93                 }
94             }
95             wrapper.setAllResources(allResources);
96         }
97         /* In new Mode get a list of all resources already present in the chosen
98          * folder, to validate resource name's uniqueness */

99         if (wrapper.isNewMode()) {
100             String JavaDoc folderURI = wrapper.getFileResource().getParentFolder();
101             if (folderURI == null)
102             {
103                 folderURI = "/";
104             }
105             FilterCriteria resourcesInFolder = FilterCriteria.createFilter();
106             resourcesInFolder.addFilterElement(FilterCriteria
107                     .createParentFolderFilter(folderURI));
108             log("Searching for resources in the chosen folder:"+folderURI);
109             ResourceLookup[] existingResources = repository.findResource(null,
110                     resourcesInFolder);
111             
112             if (existingResources != null && existingResources.length != 0) {
113                 log("res lookup size="+existingResources.length);
114                 List JavaDoc allResources = new ArrayList JavaDoc();
115                 for (int i = 0; i < existingResources.length; i++) {
116                     ResourceLookup rLookup = existingResources[i];
117                     allResources.add(rLookup.getName());
118                     log("adding resource: "+rLookup.getName()+ " to the list");
119                 }
120                 wrapper.setExistingResources(allResources);
121             }
122         }
123         
124         if (wrapper.isSubflowMode()) {
125             getAllFolders(wrapper); // TODO get this from main flow
126
String JavaDoc folderURI = (String JavaDoc) context.getFlowScope().get(PARENT_FOLDER_ATTR);
127             if (folderURI == null)
128             {
129                 folderURI = "/";
130             }
131             wrapper.getFileResource().setParentFolder( // TODO put parent folder in flow scope in main flow
132
folderURI);
133             // enable file resource jsp display mask
134
if (wrapper.getParentFlowObject() instanceof OlapClientConnectionWrapper) {
135                 wrapper.getFileResource().setFileType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA);
136             }
137         }
138         context.getFlowScope().put(FORM_OBJECT_KEY, wrapper);
139         context.getFlowScope().put(CONSTANTS_KEY, new JasperServerConstImpl());
140         return success();
141     }
142     
143     private void getAllFolders(FileResourceWrapper wrapper)
144     {
145         List JavaDoc allFolders = repository.getAllFolders(null);
146         wrapper.setAllFolders(new ArrayList JavaDoc());
147         for (int i = 0; i < allFolders.size(); i++) {
148             String JavaDoc folderUri = ((Folder) allFolders.get(i)).getURIString();
149             wrapper.getAllFolders().add(folderUri);
150         }
151     }
152
153     public Event determineType(RequestContext context) throws Exception JavaDoc {
154         FileResourceWrapper wrapper = (FileResourceWrapper) getFormObject(context);
155         if (wrapper.getFileResource().getFileType() == null) {
156             String JavaDoc fileExtension = context.getExternalContext().getRequestMap()
157                     .getString(JasperServerConst.UPLOADED_FILE_EXT);
158             if (fileExtension != null && fileExtension.trim().length() != 0) {
159                 wrapper.getFileResource().setFileType(
160                         wrapper.getTypeForExtention(fileExtension));
161             }
162         }
163         if (wrapper.getSource() != null
164                 && wrapper.getSource().equals(
165                         JasperServerConst.FIELD_CHOICE_CONT_REPO)) {
166             // User opted for a lookup URI
167
String JavaDoc newUri = wrapper.getNewUri();
168             if (newUri != null && newUri.trim().length() != 0) {
169                 Resource resource = repository.getResource(null, newUri);
170                 if (resource == null)
171                     throw new JSException("Couldnt find the resource with URI "
172                             + newUri);
173                 if (FileResource.class.isAssignableFrom(resource.getClass())) {
174                     FileResource fileR = (FileResource) resource;
175                     wrapper.getFileResource().setFileType(fileR.getFileType());
176                 }
177                 // for olap subflow reusing an existing schema resource
178
if (wrapper.getFileResource().getFileType().equals(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA)) {
179                     ((FileResource) resource).setReferenceURI(wrapper.getFileResource().getReferenceURI());
180                     wrapper.setFileResource((FileResource) resource);
181                 }
182             }
183         }
184         wrapper.setLocated(true);
185         return success();
186     }
187
188     /**
189      * Saves the changes made in alone mode back to repository
190      *
191      * @param context
192      * @return
193      * @throws Exception
194      */

195     public Event saveResource(RequestContext context) throws Exception JavaDoc {
196         log("In saveresource");
197         FileResourceWrapper wrapper = (FileResourceWrapper) getFormObject(context);
198         if (wrapper.getFileResource().getParentFolder() == null)
199             wrapper.getFileResource().setParentFolder("/");
200         if (wrapper.isStandAloneMode()) {
201             repository.saveResource(null, wrapper.getFileResource());
202         }
203         return success();
204     }
205
206     public Object JavaDoc loadFormObject(RequestContext context) {
207         FileResourceWrapper formObject = null;
208         String JavaDoc resourceUri = context.getRequestParameters().get(
209                 FILERES_URI_PARAM);
210         if (resourceUri != null && resourceUri.trim().length() != 0) {
211             Resource resource = (Resource) repository.getResource(null,
212                     resourceUri);
213             if (resource == null)
214                 throw new JSException("Could not find resource with URI "
215                         + resourceUri);
216             log("Found resource with uri=" + resourceUri);
217             formObject = new FileResourceWrapper();
218             formObject.setFileResource((FileResource) resource);
219             formObject.setMode(BaseDTO.MODE_STAND_ALONE_EDIT);
220             formObject.setLocated(true);
221         }
222         if (formObject == null) {
223             formObject = new FileResourceWrapper();
224             formObject.setMode(BaseDTO.MODE_STAND_ALONE_NEW);
225             // set default option for datasource type
226
String JavaDoc parentFolder = (String JavaDoc) context.getFlowScope().get(
227                     PARENT_FOLDER_ATTR);
228             FileResource fileResource = (FileResource) repository.newResource(
229                     null, FileResource.class);
230             fileResource.setParentFolder(parentFolder);
231             fileResource.setVersion(FileResource.VERSION_NEW);
232             formObject.setFileResource(fileResource);
233         }
234         return formObject;
235     }
236
237     /**
238      * Gets the repository service instance
239      *
240      * @return
241      */

242     public RepositoryService getRepository() {
243         return repository;
244     }
245
246     /**
247      * Sets the Repository service instace, necessary to allow Spring inject the
248      * instance of Repository service
249      *
250      * @param repository
251      */

252     public void setRepository(RepositoryService repository) {
253         this.repository = repository;
254     }
255
256     /**
257      * Registers a byte array editor to allow spring handle File uploads as byte
258      * arrays
259      */

260     protected void initBinder(RequestContext context, DataBinder binder) {
261         binder.registerCustomEditor(byte[].class,
262                 new ByteArrayMultipartFileEditor());
263     }
264
265     private void log(String JavaDoc text) {
266         log.debug(text);
267     }
268
269     public static String JavaDoc getFORM_OBJECT_KEY() {
270         return FORM_OBJECT_KEY;
271     }
272
273     protected void doBind(RequestContext context, DataBinder binder)
274             throws Exception JavaDoc {
275         super.doBind(context, binder);
276         FileResourceWrapper res = (FileResourceWrapper) binder.getTarget();
277         res.afterBind();
278     }
279 }
280
Popular Tags