source: trunk/fmgVen/src/com/fmguler/ven/Ven.java @ 29

Last change on this file since 29 was 29, checked in by fmguler, 13 years ago

Refs #3 - Ven.list() is converted to the new format. Almost the same as Ven.get(). Added javadoc to Ven public methods. Added listing test, and made the sample domain object toString methods more readable.

Now, adding the criteria parameter to the operations is to be done.

File size: 6.9 KB
Line 
1/*
2 *  fmgVen - A Convention over Configuration Java ORM Tool
3 *  Copyright 2010 Fatih Mehmet Güler
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *  under the License.
17 */
18package com.fmguler.ven;
19
20import com.fmguler.ven.util.Convert;
21import java.util.HashMap;
22import java.util.List;
23import java.util.Map;
24import java.util.Set;
25import javax.sql.DataSource;
26import org.springframework.beans.BeanWrapper;
27import org.springframework.beans.BeanWrapperImpl;
28import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
29import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
30import org.springframework.jdbc.core.namedparam.SqlParameterSource;
31
32/**
33 * The main class for data access
34 * @author Fatih Mehmet Güler
35 */
36public class Ven {
37    private NamedParameterJdbcTemplate template;
38    private QueryGenerator generator;
39    private QueryMapper mapper;
40    private boolean debug = true;
41
42    public Ven() {
43        generator = new QueryGenerator();
44        mapper = new QueryMapper();
45    }
46
47    public int count() {
48        return 0;
49    }
50
51    /**
52     * Get the object with the specified id, of the specified objectClass type.
53     * <p>
54     * By default none of the associations will be retrieved.
55     * To include the object associations (retrieve the object graph) joins should be specified, e.g.
56     * <code>SomeObject.anotherObject</code>
57     *
58     * @param id the id of the object to be retrieved
59     * @param objectClass the class of the object to be retrieved
60     * @param joins the set of object graphs to be included with the object
61     * @return the retrieved object including specified associations
62     */
63    public Object get(int id, Class objectClass, Set joins) {
64        String query = generator.generateSelectQuery(objectClass, joins);
65        query += " where 1=1 and " + Convert.toDB(Convert.toSimpleName(objectClass.getName())) + ".id = :___id ";
66
67        Map paramMap = new HashMap();
68        paramMap.put("___id", new Integer(id));
69        if (debug) System.out.println("Ven - SQL: " + query);
70
71        List result = mapper.list(query, paramMap, objectClass);
72        if (result.isEmpty()) return null;
73        if (result.size() > 1) System.out.println("Ven - WARNING >> get(id) returns more than one row");
74        return result.get(0);
75    }
76
77    /**
78     * List the objects of the specified objectClass type.
79     * <p>
80     * By default none of the associations will be retrieved.
81     * To include the object associations (retrieve the object graph) joins should be specified, e.g.
82     * <code>SomeObject.anotherObject</code>
83     *
84     * @param objectClass the class of the objects to be retrieved
85     * @param joins the set of object graphs to be included with objects
86     * @return the list of objects including specified associations
87     */
88    public List list(Class objectClass, Set joins) {
89        String query = generator.generateSelectQuery(objectClass, joins);
90
91        Map paramMap = new HashMap();
92        if (debug) System.out.println("Ven - SQL: " + query);
93
94        List result = mapper.list(query, paramMap, objectClass);
95        return result;
96    }
97
98    /**
99     * Save the object. If it has a non null (or non zero) "id" property it will be updated.
100     * It will be inserted otherwise.
101     * <p>
102     * The object will be saved to a table with the same name as the object,
103     * The fields of object will be mapped to the table fields.
104     *
105     * @param object the object to be saved
106     */
107    public void save(Object object) {
108        String query = null;
109
110        if (isObjectNew(object)) {
111            //if this is a new object assign a new id first
112            generateId(object);
113            query = generator.generateInsertQuery(object);
114        } else {
115            query = generator.generateUpdateQuery(object);
116        }
117
118        //execute the insert/update query
119        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(object);
120        template.update(query, parameterSource);
121    }
122
123    /**
124     * Delete the the object with the specified id of the specified objectClass type
125     * @param id the id of the object to be deleted
126     * @param objectClass the class of the object to be deleted
127     */
128    public void delete(int id, Class objectClass) {
129        String query = generator.generateDeleteQuery(objectClass);
130        Map parameterMap = new HashMap();
131        parameterMap.put("id", new Integer(id));
132        template.update(query, parameterMap);
133    }
134
135    //--------------------------------------------------------------------------
136    //PRIVATE METHODS
137    //return true if the object id is zero or null false otherwise
138    private boolean isObjectNew(Object object) throws VenException {
139        BeanWrapper beanWrapper = new BeanWrapperImpl(object);
140        Object objectId = beanWrapper.getPropertyValue("id");
141        if (objectId == null) return true;
142        if (!(objectId instanceof Integer)) throw new VenException(VenException.EC_GENERATOR_OBJECT_ID_TYPE_INVALID);
143        return ((Integer)objectId).intValue() == 0;
144    }
145
146    //set new object id
147    private void generateId(Object object) {
148        Integer newObjectId = new Integer(template.queryForInt(generator.generateSequenceQuery(object), new HashMap()));
149        BeanWrapper beanWrapper = new BeanWrapperImpl(object);
150        beanWrapper.setPropertyValue("id", newObjectId);
151    }
152
153    //--------------------------------------------------------------------------
154    //SETTERS
155    /**
156     * Set the DataSource to be used to access to the database
157     */
158    public void setDataSource(DataSource dataSource) {
159        if (dataSource == null) throw new RuntimeException("fmgVen - DataSource cannot be null");
160        this.template = new NamedParameterJdbcTemplate(dataSource);
161        mapper.setDataSource(dataSource);
162    }
163
164    /**
165     * Add the domain packages that have corresponding database tables.
166     * <p>
167     * The objects in these packages are considered persistable.
168     * @param domainPackage the package of the entity classes.
169     * @return this instance to allow chaining.
170     */
171    public Ven addDomainPackage(String domainPackage) {
172        generator.addDomainPackage(domainPackage);
173        mapper.addDomainPackage(domainPackage);
174        return this;
175    }
176}
Note: See TracBrowser for help on using the repository browser.