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.beans.PropertyDescriptor;
19  import java.lang.reflect.InvocationTargetException;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.beanutils.PropertyUtils;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  import org.apache.struts.actions.MappingDispatchAction;
34  import org.json.JSONArray;
35  import org.json.JSONObject;
36  import org.seasar.tuigwaa.database.DataTable;
37  import org.seasar.tuigwaa.model.DataService;
38  import org.seasar.tuigwaa.model.ModelService;
39  import org.seasar.tuigwaa.model.core.TgwEntity;
40  import org.seasar.tuigwaa.util.TgwContext;
41  import org.seasar.tuigwaa.view.EntityFormComponent;
42  import org.seasar.tuigwaa.view.HtmlService;
43  
44  /***
45   * まだ未完成
46   * @deprecated
47   */
48  public class JsAction extends MappingDispatchAction {
49  
50  	private ModelService model;
51  
52  	private ControllerService controller;
53  
54  	private HtmlService htmlService;
55  
56  	private DataService dataService;
57  
58  	public JsAction(ModelService model, ControllerService controller,
59  			HtmlService htmlService, DataService dataService) {
60  		this.model = model;
61  		this.controller = controller;
62  		this.htmlService = htmlService;
63  		this.dataService = dataService;
64  	}
65  
66  	public ActionForward readTable(ActionMapping mapping, ActionForm form,
67  			HttpServletRequest request, HttpServletResponse response)
68  			throws Exception {
69  		String siteName = TgwContext.getSiteName();
70  		String entityName = request.getParameter("entityName");
71  		
72  		TgwEntity entity = model.getEntity(siteName, entityName);
73  		DataTable dataTable = dataService.list(entity);
74  		
75  		List list = new ArrayList();
76  		while(dataTable.hasNext()){
77  			dataTable.next();
78  			JSONObject json = toJOSN(dataTable.getRowObject());
79  			list.add(json);
80  		}
81  		
82  		
83  		request.setAttribute("entity", entity);
84  		request.setAttribute("json", new JSONArray(list).toString());
85  		
86  		return mapping.findForward("success");
87  	}
88  		
89  	public ActionForward readData(ActionMapping mapping, ActionForm form,
90  				HttpServletRequest request, HttpServletResponse response)
91  				throws Exception {
92  			
93  		String siteName = TgwContext.getSiteName();
94  		String entityName = request.getParameter("entityName");
95  		String id = request.getParameter("id");
96  		
97  		TgwEntity entity = model.getEntity(siteName, entityName);
98  		
99  		Object obj = dataService.load(entity, new Long(id));
100 		String json = toJOSN(obj).toString();
101 		
102 		request.setAttribute("entity", entity);
103 		request.setAttribute("json", json);
104 		
105 		return mapping.findForward("success");
106 	}
107 
108 	public ActionForward readForm(ActionMapping mapping, ActionForm form,
109 			HttpServletRequest request, HttpServletResponse response)
110 			throws Exception {
111 
112 		String siteName = TgwContext.getSiteName();
113 		String entityName = request.getParameter("entityName");
114 		TgwEntity entity = model.getEntity(siteName, entityName);
115 
116 		String scheme = request.getScheme();
117 		String server = request.getServerName();
118 		int port = request.getServerPort();
119 		String serverURL = new URL(scheme, server, port, "").toString();
120 
121 		EntityFormComponent formComponent = (EntityFormComponent) htmlService
122 				.createFormComponent(entity, null);
123 		formComponent.setServerUrl(serverURL);
124 
125 		request.setAttribute("form", formComponent);
126 
127 		return mapping.findForward("success");
128 	}
129 
130 	
131 	private JSONObject toJOSN(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
132 		PropertyDescriptor[] descs = PropertyUtils.getPropertyDescriptors(obj);
133 		Map map = new HashMap();
134 		for(int i=0; i<descs.length;i++){
135 			String prop = descs[i].getName();
136 			if("class".equals(prop)){
137 				continue;
138 			}
139 			Object bean = PropertyUtils.getProperty(obj, prop);
140 			map.put(prop, bean);
141 		}
142 		return new JSONObject(map);
143 	}
144 }