1 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 48 public class EditQueryAction extends FormAction 49 { 50 private static final String DATA_SOURCE_PARENT_TYPE = "query"; 51 private static final String FORM_OBJECT_KEY = "query"; 52 private static final String PARENT_FOLDER_ATTR = "parentFolder"; 53 private static final String CURRENT_QUERY_ATTR = "currentQuery"; 54 private static final String IS_EDIT = "isEdit"; private static final String DATASOURCE_OBJECT_KEY = "dataResource"; 56 57 private RepositoryService repository; 58 private JasperServerConstImpl constants = new JasperServerConstImpl(); 59 60 private String queryLanguagesRequestAttrName; 61 private String [] 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 80 public EditQueryAction(){ 81 setFormObjectClass(QueryWrapper.class); setFormObjectName(FORM_OBJECT_KEY); 83 setFormObjectScope(ScopeType.FLOW); setValidator(new QueryValidator()); 85 } 86 87 88 91 public Object 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 currentQuery = (String ) 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 parentFolder = (String ) 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 122 public Event initAction(RequestContext context) throws Exception 123 { 124 AttributeMap rs = context.getRequestScope(); 125 rs.put(FORM_OBJECT_KEY, getFormObject(context)); 126 return success(); 127 } 128 129 130 133 public Event saveQuery(RequestContext context) throws Exception 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 146 public Event locateDataSource(RequestContext context) throws Exception { 147 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 162 rdWrapper.setSource(constants.getFieldChoiceNone()); 163 rdWrapper.setReportDataSource(null); 164 165 } else { 166 if (!dsRef.isLocal()) { 169 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 rdWrapper.setType(constants.getJDBCDatasourceType()); 180 } else { 181 if (JndiJdbcReportDataSource.class.isAssignableFrom(ds 183 .getClass())) 184 rdWrapper.setType(constants.getJNDIDatasourceType()); 185 } 186 rdWrapper.setReportDataSource(ds); 187 } 188 } 189 context.getFlowScope().put(ReportDataSourceAction.getFORM_OBJECT_KEY(), 192 rdWrapper); 193 return success(); 194 } 195 196 197 200 public Event saveDatasource(RequestContext context) throws Exception { 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 [] getQueryLanguages() { 224 return queryLanguages; 225 } 226 227 public void setQueryLanguages(String [] queryLanguages) { 228 this.queryLanguages = queryLanguages; 229 } 230 231 public String getQueryLanguagesRequestAttrName() { 232 return queryLanguagesRequestAttrName; 233 } 234 235 public void setQueryLanguagesRequestAttrName( 236 String queryLanguagesRequestAttrName) { 237 this.queryLanguagesRequestAttrName = queryLanguagesRequestAttrName; 238 } 239 240 } 241 242 | Popular Tags |