View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.controller;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.LinkedHashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.apache.struts.action.ActionErrors;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionMapping;
31  import org.apache.struts.config.FormBeanConfig;
32  import org.json.JSONArray;
33  import org.json.JSONObject;
34  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
35  import org.seasar.tuigwaa.database.DataTable;
36  import org.seasar.tuigwaa.model.common.EntityUtils;
37  import org.seasar.tuigwaa.model.core.TgwEntity;
38  import org.seasar.tuigwaa.util.TgwContext;
39  import org.seasar.tuigwaa.util.TgwUtils;
40  import org.seasar.tuigwaa.util.ajax.AbstractRowSetForm;
41  import org.seasar.tuigwaa.util.ajax.AjaxEvent;
42  import org.seasar.tuigwaa.view.TableFormComponent;
43  
44  import com.isenshi.util.extlib.StrutsUtil;
45  
46  /***
47   * Excel編集のフォームに対応
48   * サイト管理者側とエンドユーザ側の両方に対応している
49   *
50   * @author nishioka
51   */
52  public class RecordTableForm extends AbstractRowSetForm {
53  
54  	private static final long serialVersionUID = -9103223516928827364L;
55  
56  	private List deletedRows = new ArrayList();
57  
58  	private Set updatedRows = new LinkedHashSet();
59  
60  	private TgwEntity entity;
61  
62  	private FormBeanConfig formBeanConfig;
63  
64  	private TableFormComponent formComponent;
65  
66  	private DataTable dataTable;
67  
68  	private String _pageName_;
69  
70  	private String tableName;
71  
72  	private String methodName;
73  
74  	private ActionMapping mapping;
75  
76  	public RecordTableForm() {
77  	}
78  
79  	public RecordTableForm(TgwEntity entity, TableFormComponent formComponet,
80  			DataTable dataTable) {
81  		this.formComponent = formComponet;
82  		this.entity = entity;
83  		this.dataTable = dataTable;
84  		String siteName = entity.getDomainName();
85  		this.formBeanConfig = StrutsUtil.getModuleConfig(siteName)
86  				.findFormBeanConfig(formComponet.getOriginalFormName());
87  		initRecordTable();
88  	}
89  
90  	// [Start] ----- Ajax Method -----
91  
92  	public void changeCellByAjax(long id, int col, String value) {
93  		try {
94  			for (Iterator i = getRowSet().iterator(); i.hasNext();) {
95  				Object obj = i.next();
96  				Long curId = EntityUtils.getId(obj, true);
97  				if (curId != null && curId.longValue() == id) {
98  					updatedRows.add(obj);
99  				}
100 			}
101 			sendEvent(new SpreadSheetEvent(SpreadSheetEvent.CELL_CHANGED, id,
102 					col, value));
103 		} catch (Exception e) {
104 			e.printStackTrace();
105 		}
106 	}
107 
108 	public String checkEventsByAjax() {
109 		try {
110 			List events = retrieveEvents();
111 			List list = new ArrayList();
112 			if (events != null) {
113 				for (Iterator i = events.iterator(); i.hasNext();) {
114 					SpreadSheetEvent event = (SpreadSheetEvent) i.next();
115 					Map jsonMap = new HashMap();
116 					jsonMap.put("type", new Integer(event.type));
117 					jsonMap.put("id", new Long(event.id));
118 					jsonMap.put("col", new Integer(event.col));
119 					jsonMap.put("value", event.value);
120 					list.add(new JSONObject(jsonMap));
121 				}
122 			}
123 			return new JSONArray(list).toString();
124 		} catch (Exception e) {
125 			e.printStackTrace();
126 		}
127 		return null;
128 	}
129 
130 	// [End] ----- Ajax Method -----
131 
132 	// ----- Setter/Getter Method -----
133 
134 	public void set_pageName_(String name_) {
135 		_pageName_ = name_;
136 	}
137 
138 	public String get_pageName_() {
139 		return _pageName_;
140 	}
141 
142 	public void setTableName(String tableName) {
143 		this.tableName = tableName;
144 	}
145 
146 	public String getTableName() {
147 		return tableName;
148 	}
149 
150 	public String getMethodName() {
151 		return methodName;
152 	}
153 
154 	public void setMethodName(String methodName) {
155 		this.methodName = methodName;
156 	}
157 
158 	public TgwEntity getEntity() {
159 		return entity;
160 	}
161 
162 	public List getDeletedRows() {
163 		return deletedRows;
164 	}
165 
166 	public boolean isSharable() {
167 		return true;
168 	}
169 
170 	public String getSharedKey() {
171 		return EntityUtils.getKeys(entity);
172 	}
173 
174 	public String getUpdateMethodName() {
175 		return formComponent.getCustomFormName();
176 	}
177 
178 	public DataTable getDataTable() {
179 		return dataTable;
180 	}
181 
182 	public String getFormName() {
183 		return formBeanConfig.getName();
184 	}
185 
186 	public String getEntityName() {
187 		return entity.getName();
188 	}
189 
190 	public Iterator getChangedRowIterator() {
191 		// if (getUpdateMethodName() == null) {
192 		if (getMethodName() == null) {
193 			return updatedRows.iterator();
194 		} else {
195 			return getRowSet().iterator();
196 		}
197 	}
198 
199 	// [Start] ----- Override Method ------
200 
201 	protected void addRow(Object row) {
202 		super.addRow(row);
203 		Long id = EntityUtils.getId(row, true);
204 		if (id == null || id.longValue() <= 0) {
205 			updatedRows.add(row);
206 		}
207 	}
208 
209 	public void reset(ActionMapping mapping, HttpServletRequest request) {
210 		this.mapping = mapping;
211 		super.reset(mapping, request);
212 	}
213 
214 	public ActionErrors validate(ActionMapping mapping,
215 			HttpServletRequest request) {
216 
217 		for (Iterator i = getRowSet().iterator(); i.hasNext();) {
218 			String siteName = TgwContext.getSiteName();
219 			String formName = getFormName();
220 			TgwUtils.convert(siteName, formName, i.next());
221 		}
222 		// bind pagename
223 		String pageName = get_pageName_();
224 		TgwContext.bindPageName(pageName);
225 
226 		return super.validate(mapping, request);
227 	}
228 
229 	protected Object removeRow(int index) {
230 		Object row = super.removeRow(index);
231 		Long id = EntityUtils.getId(row, true);
232 		if (id != null && id.longValue() > 0) {
233 			deletedRows.add(id);
234 		}
235 		return row;
236 	}
237 
238 	protected void removeRow(int index, Object row) {
239 	}
240 
241 	protected Object createNewRow() {
242 		ActionForm form = null;
243 		try {
244 			if (formBeanConfig == null) {
245 				String siteName = TgwContext.getSiteName();
246 
247 				String customFromName = ((ControllerService) SingletonS2ContainerFactory
248 						.getContainer().getComponent(ControllerService.class))
249 						.getCustomFormName(siteName, mapping.getPath());
250 				this.formBeanConfig = StrutsUtil.getModuleConfig(siteName)
251 						.findFormBeanConfig(customFromName);
252 			}
253 			form = formBeanConfig.createActionForm(TgwUtils
254 					.getTuigwaaActionServlet());
255 		} catch (IllegalAccessException e) {
256 			e.printStackTrace();
257 		} catch (InstantiationException e) {
258 			e.printStackTrace();
259 		}
260 		return form;
261 	}
262 
263 	protected Object createRow(int index, String value) {
264 		return null;
265 	}
266 
267 	protected String[] getHtmlData(int index, Object row) {
268 		Object valueObject = null;
269 		Long id = EntityUtils.getId(row, true);
270 		if (id != null && id.longValue() > 0 && dataTable.getRowSize() > index) {
271 			valueObject = dataTable.getRowObject(index);
272 		}
273 		String removeButton = createRemoveButtonHtml(index,
274 				getMessage("button.remove"));
275 		String[] data = formComponent.getFieldElems(index, valueObject,
276 				removeButton);
277 		if(data == null){
278 			return null;
279 		}
280 		
281 		String[] retData = new String[data.length + 1];
282 		retData[0] = "" + id;
283 		for(int i=0; i<data.length; i++){
284 			retData[i+1] = data[i];
285 		}
286 		return retData;
287 	}
288 
289 	// [Start] ----- Private Method ------
290 
291 	private void initRecordTable() {
292 		for (int i = 0; i < dataTable.getRowSize(); i++) {
293 			Object obj = createNewRow();
294 			Long id = EntityUtils.getId(dataTable.getRowObject(i));
295 			EntityUtils.setId(obj, id, true);
296 			addRow(obj);
297 		}
298 		if (dataTable.getRowSize() == 0) {
299 			Object obj = createNewRow();
300 			addRow(obj);
301 		}
302 	}
303 
304 	// [Start] ----- Inner Class ------
305 
306 	static class SpreadSheetEvent implements AjaxEvent {
307 
308 		static final int CELL_CHANGED = 0;
309 
310 		int type;
311 
312 		long id;
313 
314 		int col;
315 
316 		String value;
317 
318 		SpreadSheetEvent(int type, long id, int col, String value) {
319 			this.type = type;
320 			this.id = id;
321 			this.col = col;
322 			this.value = value;
323 		}
324 	}
325 }