1 21 package com.jaspersoft.jasperserver.war.action; 22 23 import java.util.ArrayList ; 24 import java.util.List ; 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 FORM_OBJECT_KEY="dataResource"; 53 public static final String DATASOURCEURI_PARAM = "resource"; 54 public static final String 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); setFormObjectName(FORM_OBJECT_KEY); 64 setFormObjectScope(ScopeType.FLOW); setValidator(new ReportDataSourceValidator()); 66 } 67 public Event initAction(RequestContext context) throws Exception 68 { 69 ReportDataSourceWrapper formObject = (ReportDataSourceWrapper) getFormObject(context); 71 72 75 if (formObject.isSubflowMode() && formObject.getAllDatasources() == null){ 76 FilterCriteria criteria = FilterCriteria.createFilter(ReportDataSource.class); 78 79 ResourceLookup[] lookups = repository.findResource(null, criteria); 80 81 List 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 (); 89 allDataSources.add(dr.getURIString()); 90 } 91 } 92 93 formObject.setAllDatasources(allDataSources); 94 95 getAllFolders(formObject); 97 98 String folderURI = (String ) context.getFlowScope().get(PARENT_FOLDER_ATTR); 99 if (folderURI == null) 100 { 101 folderURI = "/"; 102 } 103 104 if (formObject.getReportDataSource() != null) { formObject.getReportDataSource().setParentFolder( 106 (String ) context.getFlowScope().get(PARENT_FOLDER_ATTR)); 107 } 108 } 109 110 log("Type of datasource="+formObject.getType()+" Mode="+formObject.getMode()); 111 context.getFlowScope().put("constants", constants); 113 return success(); 114 } 115 116 private void getAllFolders(ReportDataSourceWrapper wrapper) 117 { 118 List allFolders = repository.getAllFolders(null); 119 wrapper.setAllFolders(new ArrayList ()); 120 for (int i = 0; i < allFolders.size(); i++) { 121 String folderUri = ((Folder) allFolders.get(i)).getURIString(); 122 wrapper.getAllFolders().add(folderUri); 123 } 124 } 125 126 public Event handleTypeSelection(RequestContext context) throws Exception { 127 ReportDataSourceWrapper formObject = (ReportDataSourceWrapper) getFormObject(context); 128 129 131 log("Type=" + formObject.getType()); 132 133 String dsType = formObject.getType(); 134 ReportDataSource ds = formObject.getReportDataSource(); 135 136 if(ds == null){ 137 ds = newReportDataSource(dsType); 139 formObject.setReportDataSource(ds); 140 } else { 141 formObject.setReportDataSource(newReportDataSource(dsType, ds)); 142 } 143 return success(); 159 } 160 public Event saveLookup(RequestContext context) throws Exception { 161 ReportDataSourceWrapper wrapper = (ReportDataSourceWrapper) getFormObject(context); 162 163 log("user selected a reusable ReportDataSource"); 164 165 String 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 { 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 { 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 loadFormObject(RequestContext context) 205 { 206 ReportDataSourceWrapper formObject = new ReportDataSourceWrapper(); 207 formObject.setAllTypes(getDataSourceMappings().getKeys()); 208 String 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 231 formObject.setReportDataSource(dataSource); 232 } 233 if (formObject.getReportDataSource() == null){ 234 String parentFolder = (String ) 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 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 dsType) { 253 return (ReportDataSource) getDataSourceMappings().newResource(null, dsType); 262 } 263 264 private ReportDataSource newReportDataSource(String 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 286 public ResourceFactory getDataSourceMappings() { 287 return dataSourceMappings; 288 } 289 292 public void setDataSourceMappings(ResourceFactory dataSourceMappings) { 293 this.dataSourceMappings = dataSourceMappings; 294 } 295 300 private void log(String text) { 301 log.debug(text); 302 } 303 public static String getDATASOURCEURI_PARAM() { 304 return DATASOURCEURI_PARAM; 305 } 306 public static String getFORM_OBJECT_KEY() { 307 return FORM_OBJECT_KEY; 308 } 309 } 310 | Popular Tags |