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.net;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  
21  import org.seasar.tuigwaa.database.DataRow;
22  import org.seasar.tuigwaa.database.DataTable;
23  import org.seasar.tuigwaa.model.DataService;
24  import org.seasar.tuigwaa.model.ModelService;
25  import org.seasar.tuigwaa.model.common.EntityUtils;
26  import org.seasar.tuigwaa.model.core.TgwAttribute;
27  import org.seasar.tuigwaa.model.core.TgwEntity;
28  import org.seasar.tuigwaa.system.Constants;
29  
30  public class WebServiceImpl implements WebService {
31  
32  	private ModelService model;
33  
34  	private DataService database;
35  
36  	public WebServiceImpl(ModelService model, DataService database) {
37  		this.model = model;
38  		this.database = database;
39  	}
40  
41  	public String echo(String str) {
42  		return str;
43  	}
44  
45  	public Long saveOrUpdate(TableInfo info, TableData[] data) {
46  		TgwEntity entity = toEntity(info);
47  		Object obj = toObject(data, entity);
48  		return database.saveOrUpdate(entity, obj);
49  	}
50  
51  	public Long saveOrUpdate(TableInfo info, TableData[] data,
52  			String customFormName) {
53  		TgwEntity entity = toEntity(info);
54  		Object obj = toObject(data, entity);
55  		return database.saveOrUpdate(entity, obj, customFormName);
56  	}
57  
58  	public TableData[] load(TableInfo info, Long id) {
59  		TgwEntity entity = toEntity(info);
60  		Object obj = database.load(entity, id);
61  		return toTableData(obj, entity);
62  	}
63  
64  	public TableData[][] list(TableInfo info) {
65  		TgwEntity entity = toEntity(info);
66  		DataTable dataTable = database.list(entity);
67  		TableData[][] data = toTableDataSet(dataTable);
68  		return data;
69  	}
70  
71  	public void delete(TableInfo info, Long id) {
72  		TgwEntity entity = toEntity(info);
73  		database.delete(entity, id);
74  	}
75  
76  	public void deleteAll(TableInfo info) {
77  		TgwEntity entity = toEntity(info);
78  		database.deleteAll(entity);
79  	}
80  
81  	public void deleteByFilter(TableInfo info, String filterName) {
82  		TgwEntity entity = toEntity(info);
83  		database.deleteByFilter(entity, filterName);
84  	}
85  
86  	public TableData[][] search(TableInfo info, String methodName, TableData[] data) {
87  		TgwEntity entity = toEntity(info);
88  		Object dto = toObject(data, entity);
89  		DataTable dataTable = database.search(entity, methodName, dto);
90  		return toTableDataSet(dataTable);
91  	}
92  
93  	public TableData[][] find(TableInfo info, String methodName) {
94  		TgwEntity entity = toEntity(info);
95  		DataTable dataTable = database.find(entity, methodName);
96  		return toTableDataSet(dataTable);
97  	}
98  
99  	public Object getValue(TableInfo info, String methodName) {
100 		TgwEntity entity = toEntity(info);
101 		return database.getValue(entity, methodName);
102 	}
103 
104 	// [Start] ------- private method------------------
105 
106 	private Object toObject(TableData[] data, TgwEntity entity) {
107 		Object obj = entity.newInstance();
108 		for (int i = 0; i < data.length; i++) {
109 			String name = data[i].getName();
110 			String value = data[i].getValue();
111 			TgwAttribute attr = entity.getAttributeByDisplayName(name);
112 			EntityUtils.convertAndSetProperty(obj, attr, value);
113 		}
114 		return obj;
115 	}
116 
117 	private TgwEntity toEntity(TableInfo info) {
118 		String siteName = info.getSiteName();
119 		String entityName = info.getEntityName();
120 		return model.getEntityByDisplayName(siteName, entityName);
121 	}
122 
123 	private TableData[] toTableData(Object obj, TgwEntity entity) {
124 		Collection entityList = entity.getFieldList();
125 		int size = entityList.size();
126 		TableData[] data = new TableData[size + 1];
127 
128 		data[0] = new TableData(Constants.ENTITY_BUILTIN_ID, ""
129 				+ EntityUtils.getId(obj));
130 
131 		Iterator itr = entityList.iterator();
132 		for (int i = 1; itr.hasNext(); i++) {
133 			TgwAttribute attr = (TgwAttribute) itr.next();
134 			data[i] = new TableData(attr.getDisplayName(), ""
135 					+ EntityUtils.getProperty(obj, attr));
136 
137 		}
138 		return data;
139 	}
140 
141 	private TableData[][] toTableDataSet(DataTable dataTable) {
142 		TableData[][] data = new TableData[dataTable.getRowSize()][dataTable
143 				.getColumnSize()];
144 
145 		for (int i = 0; dataTable.hasNext(); i++) {
146 			DataRow row = dataTable.nextRow();
147 			for (int j = 0; row.hasNext(); j++) {
148 				String name = (String) dataTable.getHeaders().get(j);
149 				Object value = row.next();
150 				data[i][j] = new TableData(name, String.valueOf(value));
151 			}
152 		}
153 		return data;
154 	}
155 }