1 21 package com.jaspersoft.jasperserver.war.action; 22 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.List ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.springframework.webflow.Event; 30 import org.springframework.webflow.RequestContext; 31 import org.springframework.webflow.ScopeType; 32 import org.springframework.webflow.action.FormAction; 33 34 import com.jaspersoft.jasperserver.api.JSException; 35 import com.jaspersoft.jasperserver.api.metadata.common.domain.DataType; 36 import com.jaspersoft.jasperserver.api.metadata.common.domain.InputControl; 37 import com.jaspersoft.jasperserver.api.metadata.common.domain.ListOfValues; 38 import com.jaspersoft.jasperserver.api.metadata.common.domain.Query; 39 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup; 40 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference; 41 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 42 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria; 43 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl; 44 import com.jaspersoft.jasperserver.war.dto.BaseDTO; 45 import com.jaspersoft.jasperserver.war.dto.DataTypeWrapper; 46 import com.jaspersoft.jasperserver.war.dto.InputControlWrapper; 47 import com.jaspersoft.jasperserver.war.dto.ListOfValuesDTO; 48 import com.jaspersoft.jasperserver.war.dto.ResourceReferenceDTO; 49 import com.jaspersoft.jasperserver.war.validation.InputControlsFlowValidator; 50 51 public class DefineInputControlsAction extends FormAction { 52 private final Log log = LogFactory.getLog(this.getClass()); 53 54 private static final String FORM_OBJECT_KEY="control"; 55 private static final String CONSTANTS_KEY="constants"; 56 private static final String DATATYPE_KEY="dataType"; 57 private static final String LOV_KEY="listOfValuesDTO"; 58 private static String DATA_TYPE_FOLDER="dataType"; 59 private static String QUERY_FOLDER="query"; 60 private static String LOV_FOLDER="lov"; 61 private static final String INPUTCONTROLURI_PARAM = "resource"; 62 private static final String PARENT_FOLDER_ATTR = "parentFolder"; 63 private RepositoryService repository; 64 private String queryReferenceReqAttrName; 65 private String queryReferenceOutpuAttrName; 66 67 71 public RepositoryService getRepository() { 72 return repository; 73 } 74 75 79 public void setRepository(RepositoryService repository) { 80 this.repository = repository; 81 } 82 83 public DefineInputControlsAction(){ 84 setFormObjectClass(InputControlWrapper.class); setFormObjectName(FORM_OBJECT_KEY); 86 setFormObjectScope(ScopeType.FLOW); setValidator(new InputControlsFlowValidator()); 88 } 89 90 public Event initControlDefinition(RequestContext context) throws Exception 95 { 96 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 97 context.getFlowScope().put(CONSTANTS_KEY, new JasperServerConstImpl()); 98 100 if(formObject.isAloneNewMode()){ 101 InputControl ic=formObject.getInputControl(); 102 FilterCriteria criteria = FilterCriteria.createFilter(); 103 criteria.addFilterElement(FilterCriteria.createParentFolderFilter(ic.getParentFolder())); 104 ResourceLookup[] resources = repository.findResource(null, criteria); 105 List allResources=Arrays.asList(resources); 106 formObject.setAllResources(allResources); 107 } 108 109 return success(); 110 } 111 112 public Event prepareForControlType(RequestContext context) throws Exception 117 { 118 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 119 InputControl control = formObject.getInputControl(); 120 byte selectedType = control.getType(); 121 if(selectedType==0) 122 { 123 log("Selected type was null"); 124 selectedType=InputControl.TYPE_SINGLE_VALUE; 125 } 126 127 FilterCriteria criteria=null; 128 if(selectedType==InputControl.TYPE_MULTI_SELECT_LIST_OF_VALUES 129 || selectedType==InputControl.TYPE_SINGLE_SELECT_LIST_OF_VALUES 130 || selectedType == InputControl.TYPE_SINGLE_SELECT_LIST_OF_VALUES_RADIO 131 || selectedType == InputControl.TYPE_MULTI_SELECT_LIST_OF_VALUES_CHECKBOX){ 132 criteria=FilterCriteria.createFilter(ListOfValues.class); 133 if (control.getListOfValues() != null) { 134 String uri = control.getListOfValues().getReferenceURI(); 135 if (uri != null) { 136 formObject.setExistingPath(uri); 137 formObject.setSource(JasperServerConstImpl.getFieldChoiceRepo()); 138 } else { 139 formObject.setSource(JasperServerConstImpl.getFieldChoiceLocal()); 140 } 141 } 142 } 143 if(selectedType==InputControl.TYPE_MULTI_SELECT_QUERY 144 || selectedType==InputControl.TYPE_SINGLE_SELECT_QUERY 145 || selectedType==InputControl.TYPE_SINGLE_SELECT_QUERY_RADIO 146 || selectedType==InputControl.TYPE_MULTI_SELECT_QUERY_CHECKBOX){ 147 criteria=FilterCriteria.createFilter(Query.class); 148 if (control.getQuery() != null) { 149 String uri = control.getQuery().getReferenceURI(); 150 if (uri != null) { 151 formObject.setExistingPath(uri); 152 formObject.setSource(JasperServerConstImpl.getFieldChoiceRepo()); 153 } else { 154 formObject.setSource(JasperServerConstImpl.getFieldChoiceLocal()); 155 } 156 } 157 } 158 if(selectedType==InputControl.TYPE_MULTI_VALUE || 159 selectedType==InputControl.TYPE_SINGLE_VALUE){ 160 criteria=FilterCriteria.createFilter(DataType.class); 161 if (control.getDataType() != null) { 162 String uri = control.getDataType().getReferenceURI(); 163 if (uri != null) { 164 formObject.setExistingPath(uri); 165 formObject.setSource(JasperServerConstImpl.getFieldChoiceRepo()); 166 } else { 167 formObject.setSource(JasperServerConstImpl.getFieldChoiceLocal()); 168 } 169 } 170 } 171 if(selectedType==InputControl.TYPE_BOOLEAN){ 172 return success(); 174 } 175 log("control type="+selectedType); 176 List existingPathsList=new ArrayList (); 177 ResourceLookup[] lookups=repository.findResource(null,criteria); 178 if(lookups!=null){ 179 log("Found lookups size="+lookups.length); 180 for(int i=0;i<lookups.length;i++){ 181 existingPathsList.add(lookups[i].getURIString()); 182 if(lookups[i]!=null) 183 log("Found path="+lookups[i].getURIString()); 184 else 185 log("Found null Lookup at "+i); 186 } 187 } 188 formObject.setExistingPathList(existingPathsList); 189 return success(); 191 } 192 193 public Event handleDataTypeControl(RequestContext context) throws Exception 197 { 198 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 199 if(formObject==null) 200 return error(); 201 InputControl control=formObject.getInputControl(); 202 formObject.setSource(context.getRequestParameters().get("source")); 203 204 if (formObject.getSource().equals(JasperServerConstImpl.getFieldChoiceLocal())) { 205 DataTypeWrapper wrapper; 206 if (control.getDataType() == null || control.getDataType().getLocalResource() == null) { 207 DataType dt = (DataType)repository.newResource(null, DataType.class); 208 control.setDataType(dt); 209 wrapper = new DataTypeWrapper(dt); 210 wrapper.setMode(BaseDTO.MODE_SUB_FLOW_NEW); 211 } else { 212 if (control.getDataType().isLocal()) { 213 DataType dt = (DataType) control.getDataType().getLocalResource(); 214 if (dt.getDecimals() != null) 215 formObject.setDtDecimals(dt.getDecimals().toString()); 216 if (dt.getMaxLength() != null) 217 formObject.setDtMaxLength(dt.getMaxLength().toString()); 218 } 219 wrapper = new DataTypeWrapper((DataType) control.getDataType().getLocalResource()); 220 wrapper.setMode(BaseDTO.MODE_SUB_FLOW_EDIT); 221 } 222 context.getFlowScope().put(DATATYPE_KEY, wrapper); 223 } else { 224 String path = context.getRequestParameters().get("existingPath"); 225 control.setDataTypeReference(path); 226 return yes(); 227 } 228 return success(); 229 } 230 231 public Event handleLovControl(RequestContext context) throws Exception 235 { 236 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 237 if(formObject==null) 238 return error(); 239 240 InputControl control = formObject.getInputControl(); 241 formObject.setSource(context.getRequestParameters().get("source")); 242 243 if (formObject.getSource().equals(JasperServerConstImpl.getFieldChoiceLocal())) { 244 ListOfValuesDTO listOfValues; 245 if (control.getListOfValues() == null || control.getListOfValues().getLocalResource() == null) { 246 ListOfValues lov= (ListOfValues)repository.newResource(null, ListOfValues.class); 247 control.setListOfValues(lov); 248 listOfValues = new ListOfValuesDTO(lov); 249 listOfValues.setMode(BaseDTO.MODE_SUB_FLOW_NEW); 250 } 251 else { 252 listOfValues = new ListOfValuesDTO((ListOfValues) control.getListOfValues().getLocalResource()); 253 listOfValues.setMode(BaseDTO.MODE_SUB_FLOW_EDIT); 254 } 255 context.getFlowScope().put(LOV_KEY, listOfValues); 256 } else { 257 String path = context.getRequestParameters().get("existingPath"); 258 control.setListOfValuesReference(path); 259 return yes(); 260 } 261 262 return success(); 263 } 264 265 266 public Event saveInputControl(RequestContext context) throws Exception 267 { 268 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 269 InputControl control = formObject.getInputControl(); 270 if (control.getType() == InputControl.TYPE_SINGLE_VALUE || 271 control.getType() == InputControl.TYPE_MULTI_VALUE) { 272 DataType dataType = (DataType) control.getDataType().getLocalResource(); 273 if (formObject.getDtMaxLength() != null && formObject.getDtMaxLength().length() > 0) 274 dataType.setMaxLength(new Integer (formObject.getDtMaxLength())); 275 if (formObject.getDtDecimals() != null && formObject.getDtDecimals().length() > 0) 276 dataType.setDecimals(new Integer (formObject.getDtDecimals())); 277 } 278 if (formObject.isStandAloneMode()) { 279 repository.saveResource(null, control); 280 } 281 282 return success(); 283 } 284 285 286 public Object loadFormObject(RequestContext context) 287 { 288 InputControlWrapper formObject = null; 289 String resourceUri = context.getRequestParameters().get(INPUTCONTROLURI_PARAM); 290 if( resourceUri!=null && resourceUri.trim().length()!=0){ 291 InputControl resource=(InputControl)repository.getResource(null,resourceUri); 292 if(resource==null) 293 throw new JSException("Could not find resource with URI "+resourceUri); 294 log("Found resource with uri="+resourceUri); 295 formObject = new InputControlWrapper(resource); 296 formObject.setMode(BaseDTO.MODE_STAND_ALONE_EDIT); 297 } 298 if (formObject == null) { 299 InputControl inputControl = (InputControl) repository.newResource(null, InputControl.class); 300 String parentFolder = (String ) context.getFlowScope().get(PARENT_FOLDER_ATTR); 301 inputControl.setParentFolder(parentFolder); 302 formObject=new InputControlWrapper(inputControl); 303 formObject.setMode(BaseDTO.MODE_STAND_ALONE_NEW); 304 } 305 InputControl control=formObject.getInputControl(); 306 if(control.getParentFolder()==null) 307 control.setParentFolder("/"); 308 formObject.setSource(JasperServerConstImpl.getFieldChoiceRepo()); 309 return formObject; 310 } 311 312 313 public Event addVisibleColumn(RequestContext context) throws Exception 314 { 315 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 316 317 formObject.getInputControl().addQueryVisibleColumn(formObject.getNewVisibleColumn()); 318 319 return success(); 320 } 321 322 323 public Event removeVisibleColumn(RequestContext context) throws Exception 324 { 325 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 326 327 String [] selectedColumns; 328 329 if (context.getRequestParameters().contains("itemToDelete")) { 330 Object parameter = context.getRequestParameters().getMap().get("itemToDelete"); 331 if (parameter instanceof String ) 332 selectedColumns = new String []{(String )parameter}; 333 else 334 selectedColumns = (String [])parameter; 335 336 337 for (int i = 0; i < selectedColumns.length; i++) 338 formObject.getInputControl().removeQueryVisibleColumn(selectedColumns[i]); 339 } 340 341 return success(); 342 } 343 344 private void log(String text){ 348 log.debug(text); 349 } 350 351 public static String getFORM_OBJECT_KEY() { 352 return FORM_OBJECT_KEY; 353 } 354 355 public Event prepareQuery(RequestContext context) throws Exception { 356 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 357 ResourceReference queryRef = formObject.getInputControl().getQuery(); 358 ResourceReferenceDTO queryRefDTO = new ResourceReferenceDTO(queryRef); 359 context.getRequestScope().put(getQueryReferenceReqAttrName(), queryRefDTO); 360 return success(); 361 } 362 363 public Event setQueryReference(RequestContext context) throws Exception { 364 ResourceReferenceDTO queryRef = (ResourceReferenceDTO) context.getFlowScope().getRequired(getQueryReferenceOutpuAttrName(), ResourceReferenceDTO.class); 365 InputControlWrapper formObject = (InputControlWrapper) getFormObject(context); 366 formObject.getInputControl().setQuery(queryRef.toResourceReference()); 367 return success(); 368 } 369 370 373 public String getQueryReferenceReqAttrName() { 374 return queryReferenceReqAttrName; 375 } 376 377 380 public void setQueryReferenceReqAttrName(String queryReferenceReqAttrName) { 381 this.queryReferenceReqAttrName = queryReferenceReqAttrName; 382 } 383 384 387 public String getQueryReferenceOutpuAttrName() { 388 return queryReferenceOutpuAttrName; 389 } 390 391 394 public void setQueryReferenceOutpuAttrName( 395 String queryReferenceOutpuAttrName) { 396 this.queryReferenceOutpuAttrName = queryReferenceOutpuAttrName; 397 } 398 } 399 | Popular Tags |