1 21 package com.jaspersoft.jasperserver.war.action; 22 23 import java.util.Arrays ; 24 25 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl; 26 import com.jaspersoft.jasperserver.api.metadata.common.domain.ListOfValues; 27 import com.jaspersoft.jasperserver.api.metadata.common.domain.ListOfValuesItem; 28 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup; 29 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.ListOfValuesItemImpl; 30 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 31 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria; 32 import com.jaspersoft.jasperserver.war.dto.BaseDTO; 33 import com.jaspersoft.jasperserver.war.dto.ListOfValuesDTO; 34 import com.jaspersoft.jasperserver.war.validation.ListOfValuesValidator; 35 import org.springframework.validation.DataBinder; 36 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor; 37 import org.springframework.webflow.AttributeMap; 38 import org.springframework.webflow.Event; 39 import org.springframework.webflow.RequestContext; 40 import org.springframework.webflow.ScopeType; 41 import org.springframework.webflow.action.FormAction; 42 43 47 public class ListOfValuesAction extends FormAction 48 { 49 private static final String FORM_OBJECT_KEY = "listOfValuesDTO"; 50 private static final String LISTOFVALUES_ATTR = "listOfValues"; 51 private static final String PARENT_FOLDER_ATTR = "parentFolder"; 52 private static final String IS_EDIT = "isEdit"; 54 private RepositoryService repository; 55 56 public RepositoryService getRepository() { 57 return repository; 58 } 59 60 public void setRepository(RepositoryService repository) { 61 this.repository = repository; 62 } 63 64 65 protected void initBinder(RequestContext context, DataBinder binder) { 66 binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); 67 } 68 69 70 73 public ListOfValuesAction(){ 74 setFormObjectClass(ListOfValuesDTO.class); setFormObjectName(FORM_OBJECT_KEY); 76 setFormObjectScope(ScopeType.FLOW); setValidator(new ListOfValuesValidator()); 78 } 79 80 81 84 public Event lovList(RequestContext context) 85 { 86 ResourceLookup[] resources = repository.findResource(null, FilterCriteria.createFilter(ListOfValues.class)); 87 88 context.getRequestScope().put("resources", Arrays.asList(resources)); 89 return success(); 90 } 91 92 93 96 public Event setupEditForm(RequestContext context) throws Exception 97 { 98 AttributeMap rs = context.getRequestScope(); 99 rs.put("folders", repository.getAllFolders(null)); 100 rs.put(FORM_OBJECT_KEY, getFormObject(context)); 101 return success(); 102 } 103 104 105 108 public Object loadFormObject(RequestContext context) 109 { 110 ListOfValues listOfValues; 111 ListOfValuesDTO listOfValuesDTO; 112 ExecutionContextImpl executionContext = new ExecutionContextImpl(); 113 114 if (context.getFlowScope().get(IS_EDIT) != null) 115 { 116 String currentDataType = (String ) context.getFlowScope().get(LISTOFVALUES_ATTR); 117 listOfValues = (ListOfValues) repository.getResource(executionContext, currentDataType); 118 listOfValuesDTO = new ListOfValuesDTO(listOfValues); 119 listOfValuesDTO.setMode(BaseDTO.MODE_STAND_ALONE_EDIT); 120 } 121 else 122 { 123 listOfValues = (ListOfValues) repository.newResource(executionContext, ListOfValues.class); 124 String parentFolder = (String ) context.getFlowScope().get(PARENT_FOLDER_ATTR); 125 if (parentFolder == null || parentFolder.trim().length() == 0) 126 parentFolder = "/"; 127 listOfValues.setParentFolder(parentFolder); 128 listOfValuesDTO = new ListOfValuesDTO(listOfValues); 129 listOfValuesDTO.setMode(BaseDTO.MODE_STAND_ALONE_NEW); 130 } 131 132 return listOfValuesDTO; 133 } 134 135 136 139 public Event addLovItem(RequestContext context) throws Exception 140 { 141 ListOfValuesDTO listOfValuesDTO = (ListOfValuesDTO) getFormObject(context); 142 ListOfValues listOfValues = listOfValuesDTO.getListOfValues(); 143 ListOfValuesItem item = new ListOfValuesItemImpl(); 144 item.setLabel(listOfValuesDTO.getNewLabel()); 145 item.setValue(listOfValuesDTO.getNewValue()); 146 boolean exists = false; 147 ListOfValuesItem[] values = listOfValues.getValues(); 148 for (int i = 0; i < values.length && !exists; i++) { 149 if (values[i].getLabel().equals(item.getLabel())) { 150 exists = true; 151 values[i].setValue(item.getValue()); 152 } 153 } 154 if (!exists) 155 listOfValues.addValue(item); 156 157 listOfValuesDTO.setNewLabel(null); 158 listOfValuesDTO.setNewValue(null); 159 return success(); 160 } 161 162 163 166 public Event removeLovItem(RequestContext context) throws Exception 167 { 168 ListOfValuesDTO listOfValuesDTO = (ListOfValuesDTO) getFormObject(context); 169 ListOfValues listOfValues = listOfValuesDTO.getListOfValues(); 170 String [] items; 171 try { 172 items = context.getRequestParameters().getArray("itemToDelete"); 173 } catch (IllegalArgumentException e) { 174 items=new String [1]; 176 items[0]=context.getRequestParameters().get("itemToDelete"); 177 } 178 if(items!=null && items[0]!=null){ 179 ListOfValuesItem[] values = listOfValues.getValues(); 180 for (int i = 0; i < values.length; i++) { 181 for (int j = 0; j < items.length; j++) { 182 if (values[i].getLabel().equals(items[j])) { 183 listOfValues.removeValue(values[i]); 184 break; 185 } 186 } 187 } 188 } 189 return success(); 190 } 191 192 195 public Event writeLov(RequestContext context) throws Exception 196 { 197 ListOfValuesDTO listOfValuesDTO = (ListOfValuesDTO) getFormObject(context); 198 if (listOfValuesDTO.isStandAloneMode()) { 199 repository.saveResource(null, listOfValuesDTO.getListOfValues()); 200 } 201 202 return success(); 203 } 204 205 } 206 | Popular Tags |