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.model;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.seasar.tuigwaa.database.DataRow;
22  import org.seasar.tuigwaa.database.DataTable;
23  import org.seasar.tuigwaa.database.function.CriteriaExeFunction;
24  import org.seasar.tuigwaa.database.function.DaoMethod;
25  import org.seasar.tuigwaa.database.function.SearchExeFunction;
26  import org.seasar.tuigwaa.database.function.aggregation.AggregationFunction;
27  import org.seasar.tuigwaa.database.function.aggregation.RowcountProjectionFunction;
28  import org.seasar.tuigwaa.database.function.criteria.CriteriaFunction;
29  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
30  import org.seasar.tuigwaa.database.function.criteria.FirstResultCriteriaFunction;
31  import org.seasar.tuigwaa.database.function.criteria.MaxResultCriteriaFunction;
32  import org.seasar.tuigwaa.database.function.criteria.OrderCriteriaFunction;
33  import org.seasar.tuigwaa.database.util.FileData;
34  import org.seasar.tuigwaa.database.util.FileDataManager;
35  import org.seasar.tuigwaa.database.util.FileDataManager.FileDataCriteria;
36  import org.seasar.tuigwaa.model.common.EntityDAO;
37  import org.seasar.tuigwaa.model.common.EntityUtils;
38  import org.seasar.tuigwaa.model.core.TgwEntity;
39  import org.seasar.tuigwaa.plugin.TgwPluginUtils;
40  import org.seasar.tuigwaa.system.Constants;
41  import org.seasar.tuigwaa.system.TgwRuntimeException;
42  import org.seasar.tuigwaa.util.TgwContext;
43  
44  public class DataServiceImpl implements DataService {
45  
46  	private DAOService daoService;
47  
48  	private ModelService model;
49  
50  	public DataServiceImpl() {
51  	}
52  
53  	public void setDaoService(DAOService daoService) {
54  		this.daoService = daoService;
55  	}
56  
57  	public void setModel(ModelService model) {
58  		this.model = model;
59  	}
60  
61  	public Long saveOrUpdate(TgwEntity entity, Object dto) {
62  		daoService.getDAO(entity).saveOrUpdate(dto);
63  		manageFileData(EntityUtils.getId(dto));
64  		return bindEntityId(entity, dto);
65  	}
66  
67  	public Long saveOrUpdate(TgwEntity entity, Object dto, String methodName) {
68  		DaoMethod method = getMethod(entity, methodName);
69  		method.evaluate(dto);
70  		manageFileData(EntityUtils.getId(dto));
71  		return bindEntityId(entity, dto);
72  	}
73  
74  	public Object load(TgwEntity entity, Long id) {
75  		return daoService.getDAO(entity).load(id);
76  	}
77  
78  	public Object loadByValue(TgwEntity entity, String attrName, Object value) {
79  		return daoService.getDAO(entity).loadByValue(attrName, value);
80  	}
81  
82  	public void delete(TgwEntity entity, Long id) {
83  		daoService.getDAO(entity).delete(id);
84  		deleteFileData(entity, id);
85  	}
86  
87  	/***
88  	 * フィルタで指定されたデータを削除する
89  	 */
90  	public void deleteByFilter(TgwEntity entity, String methodName) {
91  		DataTable dataTable = find(entity, methodName);
92  		if (dataTable == null) {
93  			return;
94  		}
95  		while (dataTable.hasNext()) {
96  			DataRow dataRow = dataTable.nextRow();
97  			Long id = (Long) dataRow.getCell(Constants.ENTITY_BUILTIN_ID);
98  			delete(entity, id);
99  		}
100 	}
101 
102 	public void deleteAll(TgwEntity entity) {
103 		daoService.getDAO(entity).deleteAll();
104 	}
105 
106 	public DataTable list(TgwEntity entity) {
107 		return daoService.getDAO(entity).list();
108 	}
109 
110 	public DataTable find(TgwEntity entity, String methodName) {
111 		return find(entity, methodName, null);
112 	}
113 
114 	public DataTable find(TgwEntity entity, String methodName,
115 			CriteriaFunction extraCriteria) {
116 		return (DataTable) doFind(entity, methodName, extraCriteria);
117 	}
118 
119 	public Object getValue(TgwEntity entity, String methodName) {
120 		return getValue(entity, methodName, null);
121 	}
122 
123 	public Object getValue(TgwEntity entity, String methodName,
124 			CriteriaFunction extraCriteria) {
125 		return doFind(entity, methodName, extraCriteria);
126 	}
127 
128 	public DataTable search(TgwEntity entity, String methodName, Object dto) {
129 		return search(entity, methodName, dto, null);
130 	}
131 
132 	public DataTable search(TgwEntity entity, String methodName, Object dto,
133 			CriteriaFunction extraCriteria) {
134 		DaoMethod method = getMethod(entity, methodName);
135 		return (DataTable) method.evaluate(dto, extraCriteria);
136 	}
137 
138 	public DataTable searchByFilter(TgwEntity entity, String searchMethodName,
139 			Object dto, String filterMethodName) {
140 		return searchByFilter(entity, searchMethodName, dto, filterMethodName,
141 				null);
142 	}
143 
144 	public DataTable searchByFilter(TgwEntity entity, String searchMethodName,
145 			Object dto, String filterMethodName, CriteriaFunction extraCriteria) {
146 		if (filterMethodName == null || filterMethodName.length() == 0) {
147 			return search(entity, searchMethodName, dto, extraCriteria);
148 		}
149 		CriteriaListFunction listFunction = new CriteriaListFunction();
150 		listFunction.addFunction(extraCriteria);
151 		if (searchMethodName != null) {
152 			listFunction.addFunction(((SearchExeFunction) getMethod(entity,
153 					searchMethodName)).createCriteriaFunction(dto));
154 		}
155 		return find(entity, filterMethodName, listFunction);
156 	}
157 
158 	public int rowCount(TgwEntity entity) {
159 		DaoMethod method = getMethod(entity, EntityDAO.INJECT_CRITERIA);
160 		DataTable dataTable = (DataTable) method.evaluate(rowCriteria());
161 		return toCount(dataTable);
162 	}
163 
164 	public int rowCountBySearch(TgwEntity entity, String searchMethodName,
165 			Object dto) {
166 		CriteriaFunction criteria = rowCriteria();
167 		DataTable dataTable = search(entity, searchMethodName, dto, criteria);
168 		return toCount(dataTable);
169 	}
170 
171 	public int rowCountByFilter(TgwEntity entity, String filterName,
172 			String searchName, Object dto) {
173 		CriteriaFunction criteria = rowCriteria();
174 		DataTable dataTable = searchByFilter(entity, searchName, dto,
175 				filterName, criteria);
176 
177 		int rows = toCount(dataTable);
178 		if(filterName != null && !"".equals(filterName)){
179 			CriteriaExeFunction function = 
180 				(CriteriaExeFunction) daoService.getDataFilterFunctionMap(entity).get(filterName);			
181 			
182 			List functionList = 
183 				((CriteriaListFunction)function.getUnaryCriteriaFunction()).getFunctionList();
184 			
185 			for(Iterator itr=functionList.iterator();itr.hasNext();){
186 				CriteriaFunction criteriaFunction = (CriteriaFunction) itr.next();
187 				if (criteriaFunction instanceof MaxResultCriteriaFunction) {					
188 					int maxValue = ((Integer)criteriaFunction.getValue()).intValue();					
189 					if (maxValue < rows) {
190 						rows = maxValue;						
191 					}
192 					break;
193 				}				
194 			}									
195 		}
196 		return rows;
197 	}
198 
199 	public int rowCountByFilter(TgwEntity entity, String filterName,
200 			CriteriaFunction extraCriteria) {
201 		CriteriaFunction criteria = rowCriteria();
202 		if (extraCriteria != null) {
203 			CriteriaListFunction list = new CriteriaListFunction();
204 			list.addFunction(criteria);
205 			list.addFunction(extraCriteria);
206 			criteria = list;
207 		}
208 		DataTable dataTable = find(entity, filterName, criteria);
209 		return toCount(dataTable);
210 	}
211 
212 	// [Start] ------ Private Method -----
213 
214 	protected CriteriaFunction rowCriteria() {
215 		AggregationFunction aggFunc = new AggregationFunction();
216 		RowcountProjectionFunction proj = new RowcountProjectionFunction();
217 		proj.setAlias("_tgw_search__");
218 		aggFunc.addFunction(proj);
219 		CriteriaListFunction criteria = new CriteriaListFunction();
220 		criteria.addFunction(aggFunc);
221 		return criteria;
222 	}
223 
224 	protected final DaoMethod getMethod(TgwEntity entity, String methodName) {
225 		return daoService.getDAO(entity).getMethod(methodName);
226 	}
227 
228 	protected final Object doFind(TgwEntity entity, String methodName,
229 			CriteriaFunction extraCriteria) {
230 		DaoMethod method = null;
231 		if (methodName == null || methodName.equals("")) {
232 			method = getMethod(entity, EntityDAO.INJECT_CRITERIA);
233 		} else {
234 			method = getMethod(entity, methodName);
235 		}
236 		if (method == null) {
237 			throw new TgwRuntimeException("ETGW2003",
238 					new Object[] { methodName });
239 		}
240 		return method.evaluate(extraCriteria);
241 	}
242 
243 	protected final Long bindEntityId(TgwEntity entity, Object bean) {
244 		Long id = EntityUtils.getId(bean);
245 		TgwPluginUtils.bindEntityId(entity, id);
246 		return id;
247 	}
248 
249 	protected final int toCount(DataTable dataTable) {
250 		if (dataTable.getRowSize() > 0) {
251 			DataRow dataRow = (DataRow) dataTable.next();
252 			Integer integer = (Integer) dataRow.getDataObject();
253 			return integer.intValue();
254 		} else {
255 			return 0;
256 		}
257 	}
258 
259 	protected final void manageFileData(Long id) {
260 		List filedataList = FileDataManager.getFileDataList();
261 		if (filedataList != null) {
262 			TgwEntity entity = getFileEntity();
263 			EntityDAO dao = daoService.getDAO(entity);
264 			Iterator itr = filedataList.iterator();
265 			while (itr.hasNext()) {
266 				FileData filedata = (FileData) itr.next();
267 				manageFileData(entity, dao, filedata, id);
268 			}
269 		}
270 	}
271 
272 	private final void manageFileData(TgwEntity fileEntity, EntityDAO dao,
273 			FileData filedata, Long id) {
274 		if (filedata.getBytes() == null) {
275 			DataTable dataTable = (DataTable) find(fileEntity, null,
276 					FileDataManager.getCriteria(filedata));
277 			if (dataTable.hasNext()) {
278 				Long filedataId = ((FileData) dataTable.nextRow()
279 						.getDataObject()).getId();
280 				dao.delete(filedataId);
281 			}
282 		} else if (filedata.hasEntityId()) {
283 			DataTable dataTable = (DataTable) find(fileEntity, null,
284 					FileDataManager.getCriteria(filedata));
285 			if (dataTable.hasNext()) {
286 				filedata.setId(((FileData) dataTable.nextRow().getDataObject())
287 						.getId());
288 			}
289 		} else {
290 			filedata.setEntityId("" + id);
291 			DataTable dataTable = (DataTable) find(fileEntity, null,
292 					FileDataManager.getCriteria(filedata));
293 			if (dataTable.hasNext()) {
294 				FileData newFileDate = (FileData) dataTable.nextRow()
295 						.getDataObject();
296 				newFileDate.setBytes(filedata.getBytes());
297 				filedata = newFileDate;
298 				// dao.delete(filedataId);
299 			}
300 		}
301 		dao.saveOrUpdate(filedata);
302 	}
303 
304 	private final void deleteFileData(TgwEntity entity, Long entityId) {
305 		if (hasFileEntity()) {
306 			TgwEntity fileEntity = getFileEntity();
307 			DataTable dataTable = (DataTable) find(fileEntity, null,
308 					new FileDataCriteria(entity.getName(), null, "" + entityId));
309 			while (dataTable.hasNext()) {
310 				FileData fileData = (FileData) dataTable.nextRow()
311 						.getDataObject();
312 				Long id = fileData.getId();
313 				getFileEntityDao().delete(id);
314 			}
315 		}
316 	}
317 
318 	protected boolean hasFileEntity() {
319 		return getFileEntity() != null;
320 	}
321 
322 	protected TgwEntity getFileEntity() {
323 		String domainName = TgwContext.getSiteName();
324 		String entityName = TgwPluginUtils.toPluginEntityName(FileData.class);
325 		return model.getEntity(domainName, entityName);
326 	}
327 
328 	protected EntityDAO getFileEntityDao() {
329 		return daoService.getDAO(getFileEntity());
330 	}
331 }