KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
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 /**
44  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
45  * @version $Id: ListOfValuesAction.java 4140 2006-08-08 06:57:49Z saiyedm $
46  */

47 public class ListOfValuesAction extends FormAction
48 {
49     private static final String JavaDoc FORM_OBJECT_KEY = "listOfValuesDTO";
50     private static final String JavaDoc LISTOFVALUES_ATTR = "listOfValues";
51     private static final String JavaDoc PARENT_FOLDER_ATTR = "parentFolder";
52     private static final String JavaDoc IS_EDIT = "isEdit";//FIXME use wrapper to disable name in UI
53

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     /**
71      *
72      */

73     public ListOfValuesAction(){
74         setFormObjectClass(ListOfValuesDTO.class); //custom form backing object class
75
setFormObjectName(FORM_OBJECT_KEY);
76         setFormObjectScope(ScopeType.FLOW); //this is a multi-page wizard!
77
setValidator(new ListOfValuesValidator());
78     }
79
80
81     /**
82      *
83      */

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     /**
94      *
95      */

96     public Event setupEditForm(RequestContext context) throws Exception JavaDoc
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     /**
106      *
107      */

108     public Object JavaDoc 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 JavaDoc currentDataType = (String JavaDoc) 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 JavaDoc parentFolder = (String JavaDoc) 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     /**
137      *
138      */

139     public Event addLovItem(RequestContext context) throws Exception JavaDoc
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     /**
164      *
165      */

166     public Event removeLovItem(RequestContext context) throws Exception JavaDoc
167     {
168         ListOfValuesDTO listOfValuesDTO = (ListOfValuesDTO) getFormObject(context);
169         ListOfValues listOfValues = listOfValuesDTO.getListOfValues();
170         String JavaDoc[] items;
171         try {
172             items = context.getRequestParameters().getArray("itemToDelete");
173         } catch (IllegalArgumentException JavaDoc e) {
174             // when only one pair is selected for deletion
175
items=new String JavaDoc[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     /**
193      *
194      */

195     public Event writeLov(RequestContext context) throws Exception JavaDoc
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