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.common;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.seasar.tuigwaa.database.DataTable;
24  import org.seasar.tuigwaa.database.function.DaoMethod;
25  import org.seasar.tuigwaa.database.function.criteria.CriteriaListFunction;
26  import org.seasar.tuigwaa.database.function.criteria.EqCriteriaFunction;
27  
28  
29  public class EntityDAOImpl implements EntityDAO {
30  
31  	private Map funcMap_ = Collections.synchronizedMap(new HashMap());
32  
33  	public EntityDAOImpl(Map funcMap) {
34  		funcMap_.putAll(funcMap);
35  	}
36  
37  	public DaoMethod getMethod(String methodName) {
38  		return (DaoMethod) funcMap_.get(methodName);
39  	}
40  
41  	public Object load(Long id) {
42  		return getMethod(LOAD).evaluate(id);
43  	}
44  
45  	public Object loadByValue(String attrName, Object value) {
46  		DaoMethod method = getMethod(INJECT_CRITERIA);
47  		EqCriteriaFunction eq = new EqCriteriaFunction(attrName, value);
48  		DataTable dataTable = (DataTable) method.evaluate(eq);
49  		if (dataTable.getRowSize() > 0) {
50  			return dataTable.getRowObject(0);
51  		} else {
52  			return null;
53  		}
54  	}
55  
56  	public void delete(Long id) {
57  		getMethod(DELETE).evaluate(id);
58  	}
59  
60  	public void deleteAll() {
61  		getMethod(DELETEALL).evaluate();
62  	}
63  
64  	public void saveOrUpdate(Object obj) {
65  		getMethod(SAVEUPDATE).evaluate(obj);
66  	}
67  
68  	public DataTable list() {
69  		return (DataTable) getMethod(LIST).evaluate();
70  	}
71  
72  	public DataTable find(Map filterMap) {
73  		CriteriaListFunction list = new CriteriaListFunction();
74  		for(Iterator i = filterMap.keySet().iterator();i.hasNext();){
75  			String key = (String)i.next();
76  			Object value = filterMap.get(key);
77  			list.addFunction(new EqCriteriaFunction(key, value));
78  		}
79  		return (DataTable)getMethod(INJECT_CRITERIA).evaluate(list);
80  	}
81  	
82  	public Object getValue(String name) {
83  		return getMethod(name).evaluate();
84  	}
85  }