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

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

Refs #3 - Ven.save() is transferred to the new package. Save use case is completed. Convert.toDB() converts camel case object names to database names with underscores. Unchecked VenException is thrown by Ven methods. Liquibase changelog (test-db-changelog.xml) is changed to include only a simple object to test save operation. It creates a sample database table and inserts a sample row. On rollback it removes sample data. Using liquibase, Sample.java builds database, tests save operation and rolls back.

QueryGenerator generates insert and update queries for save and generates sequence query to assign ids to new objects. Ven calls query generator and runs the query using spring jdbc template.

Old codebase updated to run at Java 1.4

File size: 4.0 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 java.util.HashMap;
21import java.util.List;
22import javax.sql.DataSource;
23import org.springframework.beans.BeanWrapper;
24import org.springframework.beans.BeanWrapperImpl;
25import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
26import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
27import org.springframework.jdbc.core.namedparam.SqlParameterSource;
28
29/**
30 * The main class for data access
31 * @author Fatih Mehmet Güler
32 */
33public class Ven {
34    private NamedParameterJdbcTemplate template;
35    private QueryGenerator generator;
36    private QueryMapper mapper;
37
38    public Ven() {
39        generator = new QueryGenerator();
40        mapper = new QueryMapper();
41    }
42
43    public List list(Class objectClass) {
44        return null;
45    }
46
47    public int count() {
48        return 0;
49    }
50
51    public Object get(int id, Class objectClass) {
52        return null;
53    }
54
55    /**
56     * Save the object. If it has a "id" property it will be updated.
57     * It will be inserted otherwise.
58     * <p>
59     * The object will be saved to a table with the same name as the object,
60     * The fields of object will be mapped to the table fields.
61     *
62     * @param object the object to be saved
63     */
64    public void save(Object object) {
65        String query = null;
66
67        if (isObjectNew(object)) {
68            //if this is a new object assign a new id first
69            generateId(object);
70            query = generator.generateInsertQuery(object);
71        } else {
72            query = generator.generateUpdateQuery(object);
73        }
74
75        //execute the insert/update query
76        SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(object);
77        template.update(query, parameterSource);
78    }
79
80    public void delete(int id, Class objectClass) {
81    }
82
83    //--------------------------------------------------------------------------
84    //PRIVATE METHODS
85    //return true if the object id is zero or null false otherwise
86    private boolean isObjectNew(Object object) throws VenException {
87        BeanWrapper beanWrapper = new BeanWrapperImpl(object);
88        Object objectId = beanWrapper.getPropertyValue("id"); 
89        if (objectId == null) return true;
90        if (!(objectId instanceof Integer)) throw new VenException(VenException.EC_GENERATOR_OBJECT_ID_TYPE_INVALID);
91        return ((Integer)objectId).intValue() == 0;
92    }
93
94    //set new object id
95    private void generateId(Object object) {
96        Integer newObjectId = new Integer(template.queryForInt(generator.generateSequenceQuery(object), new HashMap()));
97        BeanWrapper beanWrapper = new BeanWrapperImpl(object);
98        beanWrapper.setPropertyValue("id", newObjectId);
99    }
100
101    //--------------------------------------------------------------------------
102    //SETTERS
103    public void setDataSource(DataSource dataSource) {
104        if (dataSource == null) throw new RuntimeException("fmgVen - DataSource cannot be null");
105        this.template = new NamedParameterJdbcTemplate(dataSource);
106    }
107
108    public Ven addDomainPackage(String domainPackage) {
109        generator.addDomainPackage(domainPackage);
110        mapper.addDomainPackage(domainPackage);
111        return this;
112    }
113}
Note: See TracBrowser for help on using the repository browser.