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  /*
17   * Created on 2005/07/30
18   */
19  package org.seasar.tuigwaa.model;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.seasar.tuigwaa.database.CriteriaFunctionFactory;
30  import org.seasar.tuigwaa.database.function.CriteriaExeFunction;
31  import org.seasar.tuigwaa.database.function.DaoMethod;
32  import org.seasar.tuigwaa.database.function.SearchExeFunction;
33  import org.seasar.tuigwaa.database.function.UpdateExeFunction;
34  import org.seasar.tuigwaa.database.function.aggregation.AggregationFunction;
35  import org.seasar.tuigwaa.database.function.aggregation.AvgProjectionFunction;
36  import org.seasar.tuigwaa.database.function.aggregation.GroupProjectionFunction;
37  import org.seasar.tuigwaa.database.function.aggregation.ProjectionFunction;
38  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
39  import org.seasar.tuigwaa.database.function.criteria.UnaryCriteriaFunction;
40  import org.seasar.tuigwaa.model.common.EntityDAO;
41  import org.seasar.tuigwaa.model.common.EntityDAOImpl;
42  import org.seasar.tuigwaa.model.common.EntityUtils;
43  import org.seasar.tuigwaa.model.core.TgwAttribute;
44  import org.seasar.tuigwaa.model.core.TgwEntity;
45  import org.seasar.tuigwaa.model.core.impl.DoubleAttribute;
46  import org.seasar.tuigwaa.model.core.impl.IntegerAttribute;
47  import org.seasar.tuigwaa.model.core.impl.TgwEntityImpl;
48  import org.seasar.tuigwaa.util.TgwPathResolver;
49  
50  import com.isenshi.util.extlib.DiconResource;
51  
52  /***
53   * @author nishioka
54   */
55  public class DAOServiceImpl implements DAOService {
56  
57  	private static final String[][] BASIC_FUNC_NAMES = {
58  			{ "orderListExe", EntityDAO.ORDERED_LIST },
59  			{ "loadExe", EntityDAO.LOAD }, { "listExe", EntityDAO.LIST },
60  			{ "deleteExe", EntityDAO.DELETE },
61  			{ "saveOrUpdateExe", EntityDAO.SAVEUPDATE },
62  			{ "injectCriteriaExe", EntityDAO.INJECT_CRITERIA },
63  			{ "deleteAllExe", EntityDAO.DELETEALL } };
64  
65  	private Map functionMap_ = new HashMap();
66  
67  	public DAOServiceImpl() {
68  	}
69  
70  	public void registerDaoMethod(TgwEntity entity, DaoMethod daoMethod) {
71  		FunctionSet set = getFunctionSet(entity);
72  		registerDaoMethod(entity.getDomainName(), set, daoMethod);
73  	}
74  
75  	public DaoMethod deregisterDaoMethod(TgwEntity entity, String funcName) {
76  		FunctionSet set = getFunctionSet(entity);
77  		return set.remove(funcName);
78  	}
79  
80  	public CriteriaExeFunction injectDataFilter(TgwEntity entity,
81  			String aggregationName, UnaryCriteriaFunction function) {
82  		Map map = getAggregationFunctionMap(entity);
83  		CriteriaExeFunction exe = (CriteriaExeFunction) map
84  				.get(aggregationName);
85  		List projectionList = exe.getProjectionList();
86  		CriteriaExeFunction newExe = CriteriaFunctionFactory
87  				.createCriteriaExeFunction(entity, "", exe
88  						.getUnaryCriteriaFunction());
89  		for (Iterator i = projectionList.iterator(); i.hasNext();) {
90  			newExe.addProjection((String) i.next());
91  		}
92  		newExe.setDataTableFilter(function);
93  		return newExe;
94  	}
95  
96  	public TgwEntity createProjectionEntity(TgwEntity orgEntity,
97  			String aggregationName) {
98  		Map aggMap = getAggregationFunctionMap(orgEntity);
99  		Map valueAggMap = getValueFunctionMap(orgEntity);
100 
101 		CriteriaExeFunction exe = (CriteriaExeFunction) aggMap
102 				.get(aggregationName);
103 		if (exe == null) {
104 			exe = (CriteriaExeFunction) valueAggMap.get(aggregationName);
105 		}
106 		return doCreateProjectionEntity(orgEntity, exe, aggregationName);
107 	}
108 
109 	public void loadFunctions(TgwEntity entity) {
110 		String path = TgwPathResolver.getDaoFilePath(entity.getDomainName(),
111 				entity.getName());
112 		DiconResource resource = loadDiconResource(path);
113 		if (resource == null) {
114 			return;
115 		}
116 
117 		FunctionSet functionSet = new FunctionSet();
118 
119 		Object[] daoMethods = resource.findComponents(DaoMethod.class);
120 		for (int i = 0; i < daoMethods.length; i++) {
121 			registerDaoMethod(entity.getDomainName(), functionSet,
122 					(DaoMethod) daoMethods[i]);
123 		}
124 
125 		synchronized (functionMap_) {
126 			functionMap_.put(EntityUtils.getKeys(entity), functionSet);
127 		}
128 	}
129 
130 	public void saveFunction(TgwEntity entity) {
131 		FunctionSet set = getFunctionSet(entity);
132 		saveFunction(entity.getDomainName(), entity.getName(), set);
133 	}
134 
135 	public void saveAllFunctions() {
136 		synchronized (functionMap_) {
137 			Iterator itr = functionMap_.keySet().iterator();
138 			while (itr.hasNext()) {
139 				String key = (String) itr.next();
140 				FunctionSet set = (FunctionSet) functionMap_.get(key);
141 				int index = key.indexOf(".");
142 				String domain = key.substring(0, index);
143 				String entityName = key.substring(index + 1, key.length());
144 				saveFunction(domain, entityName, set);
145 			}
146 		}
147 	}
148 
149 	public void resetAllFunctions() {
150 		functionMap_ = new HashMap();
151 	}
152 
153 	public Map getDataFilterFunctionMap(TgwEntity entity) {
154 		FunctionSet set = getFunctionSet(entity);
155 		return Collections.unmodifiableMap(set.getDataFilterFunctionMap());
156 	}
157 
158 	public Map getAggregationFunctionMap(TgwEntity entity) {
159 		FunctionSet set = getFunctionSet(entity);
160 		return Collections.unmodifiableMap(set.getAggregationExeFunctionMap());
161 	}
162 
163 	public Map getSearchFunctionMap(TgwEntity entity) {
164 		FunctionSet set = getFunctionSet(entity);
165 		return Collections.unmodifiableMap(set.getSearchExeFunctionMap());
166 	}
167 
168 	public Map getCustomFormFunctionMap(TgwEntity entity) {
169 		FunctionSet set = getFunctionSet(entity);
170 		return Collections.unmodifiableMap(set.getCustomFormFunctionMap());
171 	}
172 
173 	public Map getValueFunctionMap(TgwEntity entity) {
174 		FunctionSet set = getFunctionSet(entity);
175 		return Collections.unmodifiableMap(set.getValueExeFunctionMap());
176 	}
177 
178 	public EntityDAO getDAO(TgwEntity entity) {
179 		FunctionSet set = getFunctionSet(entity);
180 		Map funcMap = new HashMap();
181 		funcMap.putAll(set.getAllFunctionMap());
182 		for (int i = 0; i < BASIC_FUNC_NAMES.length; i++) {
183 			DaoMethod basicDao = CriteriaFunctionFactory.createBasicDaoMethod(
184 					entity, BASIC_FUNC_NAMES[i][0]);
185 			basicDao.setDomainName(entity.getDomainName());
186 			funcMap.put(BASIC_FUNC_NAMES[i][1], basicDao);
187 		}
188 		EntityDAO dao = new EntityDAOImpl(funcMap);
189 		return dao;
190 	}
191 
192 	public FunctionSet getFunctionSet(TgwEntity entity) {
193 		String fullPath = EntityUtils.getKeys(entity);
194 		FunctionSet set = null;
195 		synchronized (functionMap_) {
196 			set = (FunctionSet) functionMap_.get(fullPath);
197 			if (set == null) {
198 				set = new FunctionSet();
199 				functionMap_.put(fullPath, set);
200 			}
201 		}
202 		return set;
203 	}
204 
205 	// [Start] ------ Private Method ------
206 
207 	private TgwEntity doCreateProjectionEntity(TgwEntity entity,
208 			CriteriaExeFunction exe, String aggregationName) {
209 		TgwEntity newEntity = new TgwEntityImpl(entity.getDomainName(), entity
210 				.getName());
211 		newEntity.setAggregation(true);
212 		newEntity.setDisplayName(aggregationName);
213 
214 		UnaryCriteriaFunction topFunction = exe.getUnaryCriteriaFunction();
215 
216 		AggregationFunction aggFunction = getAggregationFunction(topFunction);
217 
218 		Iterator itr = aggFunction.getFunctionList().iterator();
219 
220 		for (int i = 0; itr.hasNext(); i++) {
221 			ProjectionFunction projection = (ProjectionFunction) itr.next();
222 			if (projection instanceof GroupProjectionFunction) {
223 				String fieldName = projection.getAlias();
224 				TgwAttribute field = entity.getField(fieldName);
225 				newEntity.addField(field);
226 			} else {
227 				// Virtual Number Field
228 				TgwAttribute iField = null;
229 				if (projection instanceof AvgProjectionFunction) {
230 					iField = new DoubleAttribute();
231 				} else {
232 					iField = new IntegerAttribute();
233 				}
234 				iField.setName(projection.getAlias());
235 				iField.setDisplayName((String) exe.getProjectionList().get(i));
236 				newEntity.addField(iField);
237 			}
238 		}
239 		return newEntity;
240 	}
241 
242 	private AggregationFunction getAggregationFunction(
243 			UnaryCriteriaFunction function) {
244 		if (function instanceof CriteriaListFunction) {
245 			List list = ((CriteriaListFunction) function).getFunctionList();
246 			for (Iterator i = list.iterator(); i.hasNext();) {
247 				UnaryCriteriaFunction aFunction = (UnaryCriteriaFunction) i
248 						.next();
249 				AggregationFunction aggFunction = getAggregationFunction(aFunction);
250 				if (aggFunction != null) {
251 					return aggFunction;
252 				}
253 			}
254 		} else if (function instanceof AggregationFunction) {
255 			return (AggregationFunction) function;
256 		}
257 		return null;
258 	}
259 
260 	private DiconResource loadDiconResource(String path) {
261 		DiconResource resource = new DiconResource();
262 		resource.setPath(path);
263 		resource.load();
264 		if (!resource.hasContainer()) {
265 			return null;
266 		}
267 		resource.bindRoot();
268 		resource.include("dicon/basedb.dicon");
269 		resource.include("dicon/logic.dicon");
270 		return resource;
271 	}
272 
273 	private void saveFunction(String domain, String entityName, FunctionSet set) {
274 		DiconResource resource = new DiconResource();
275 		Map options = new HashMap();
276 		Map aspects = new HashMap();
277 		aspects.put("evaluate", "basedb.requiredTx");
278 		options.put(DiconResource.OPTION_ASPECTS, aspects);
279 		resource.addComponentAll(set.getAllFunctionsExcludeCustomForm(),
280 				options);
281 
282 		Map options2 = new HashMap();
283 		Map aspects2 = new HashMap();
284 		List list = new ArrayList();
285 		list.add("tlogic.dataSave");
286 		list.add("basedb.requiredTx");
287 		aspects2.put("evaluate", list);
288 		options2.put(DiconResource.OPTION_ASPECTS, aspects2);
289 		resource.addComponentAll(set.getCustomFormFunctionMap().values(),
290 				options2);
291 
292 		resource.setPath(TgwPathResolver.getDaoFilePath(domain, entityName));
293 		resource.save();
294 	}
295 
296 	private void registerDaoMethod(String domainName, FunctionSet set,
297 			DaoMethod daoMethod) {
298 		if (daoMethod instanceof CriteriaExeFunction) {
299 			CriteriaExeFunction exe = (CriteriaExeFunction) daoMethod;
300 			if (exe.isValueFlag()) {
301 				set.addValueExeFunction(exe);
302 			} else if (exe.isProjection()) {
303 				set.addAggregationExeFunction(exe);
304 			} else {
305 				set.addDataFilterFunction(exe);
306 			}
307 		} else if (daoMethod instanceof SearchExeFunction) {
308 			SearchExeFunction exe = (SearchExeFunction) daoMethod;
309 			set.addSearchExeFunction(exe);
310 		} else if (daoMethod instanceof UpdateExeFunction) {
311 			UpdateExeFunction exe = (UpdateExeFunction) daoMethod;
312 			if (exe.getDtoFieldList() != null
313 					&& exe.getDtoFieldList().size() != 0) {
314 				set.addCustomForm(exe);
315 			} else {
316 				set.addCustomUpdateFunction(exe);
317 			}
318 		}
319 		daoMethod.setDomainName(domainName);
320 	}
321 
322 	// [Start] ------ Inner Class FunctionSet ------
323 
324 	public class FunctionSet {
325 
326 		private Map dataFilterFunctionMap_ = new HashMap();
327 
328 		private Map aggregationExeFunctionMap_ = new HashMap();
329 
330 		private Map searchExeFunctionMap = new HashMap();
331 
332 		private Map valueExeFunctionMap = new HashMap();
333 
334 		private Map customFormFunctionMap = new HashMap();
335 
336 		private Map customUpdateFunctionMap = new HashMap();
337 
338 		public DaoMethod remove(String funcName) {
339 			Object obj = null;
340 			if (obj == null) {
341 				obj = dataFilterFunctionMap_.remove(funcName);
342 			}
343 			if (obj == null) {
344 				obj = aggregationExeFunctionMap_.remove(funcName);
345 			}
346 			if (obj == null) {
347 				obj = searchExeFunctionMap.remove(funcName);
348 			}
349 			if (obj == null) {
350 				obj = valueExeFunctionMap.remove(funcName);
351 			}
352 			if (obj == null) {
353 				obj = customFormFunctionMap.remove(funcName);
354 			}
355 			if (obj == null) {
356 				obj = customUpdateFunctionMap.remove(funcName);
357 			}
358 			return (DaoMethod) obj;
359 		}
360 
361 		// data filter
362 
363 		public void addDataFilterFunction(CriteriaExeFunction function) {
364 			dataFilterFunctionMap_.put(function.getName(), function);
365 		}
366 
367 		public CriteriaExeFunction getDataFilterFunction(String name) {
368 			return (CriteriaExeFunction) dataFilterFunctionMap_.get(name);
369 		}
370 
371 		public Map getDataFilterFunctionMap() {
372 			return dataFilterFunctionMap_;
373 		}
374 
375 		// aggregation
376 
377 		public void addAggregationExeFunction(CriteriaExeFunction function) {
378 			aggregationExeFunctionMap_.put(function.getName(), function);
379 		}
380 
381 		public CriteriaExeFunction getAggregationExeFunction(String name) {
382 			return (CriteriaExeFunction) aggregationExeFunctionMap_.get(name);
383 		}
384 
385 		public Map getAggregationExeFunctionMap() {
386 			return aggregationExeFunctionMap_;
387 		}
388 
389 		// value exe
390 
391 		public void addValueExeFunction(CriteriaExeFunction function) {
392 			valueExeFunctionMap.put(function.getName(), function);
393 		}
394 
395 		public CriteriaExeFunction getValueExeFunction(String name) {
396 			return (CriteriaExeFunction) valueExeFunctionMap.get(name);
397 		}
398 
399 		public Map getValueExeFunctionMap() {
400 			return valueExeFunctionMap;
401 		}
402 
403 		// search exe
404 
405 		public void addSearchExeFunction(SearchExeFunction function) {
406 			searchExeFunctionMap.put(function.getName(), function);
407 		}
408 
409 		public Map getSearchExeFunctionMap() {
410 			return searchExeFunctionMap;
411 		}
412 
413 		// custom form
414 
415 		public void addCustomForm(UpdateExeFunction updateExe) {
416 			customFormFunctionMap.put(updateExe.getName(), updateExe);
417 		}
418 
419 		public Map getCustomFormFunctionMap() {
420 			return customFormFunctionMap;
421 		}
422 
423 		// custom update
424 
425 		public void addCustomUpdateFunction(UpdateExeFunction updateExe) {
426 			customUpdateFunctionMap.put(updateExe.getName(), updateExe);
427 		}
428 
429 		public Map getCustomUpdateFunctionMap() {
430 			return customUpdateFunctionMap;
431 		}
432 
433 		public Map getAllFunctionMap() {
434 			Map map = new HashMap();
435 			map.putAll(getDataFilterFunctionMap());
436 			map.putAll(getAggregationExeFunctionMap());
437 			map.putAll(getSearchExeFunctionMap());
438 			map.putAll(getValueExeFunctionMap());
439 			map.putAll(getCustomFormFunctionMap());
440 			map.putAll(getCustomUpdateFunctionMap());
441 			return map;
442 		}
443 
444 		public Map getAllFunctionMapExcludeCustomForm() {
445 			Map map = new HashMap();
446 			map.putAll(getDataFilterFunctionMap());
447 			map.putAll(getAggregationExeFunctionMap());
448 			map.putAll(getSearchExeFunctionMap());
449 			map.putAll(getValueExeFunctionMap());
450 			// map.putAll(getCustomFormFunctionMap());
451 			map.putAll(getCustomUpdateFunctionMap());
452 			return map;
453 		}
454 
455 		public Collection getAllFunctions() {
456 			return getAllFunctionMap().values();
457 		}
458 
459 		public Collection getAllFunctionsExcludeCustomForm() {
460 			return getAllFunctionMapExcludeCustomForm().values();
461 		}
462 	}
463 }