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

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

Refs #3 - Ven.delete() is converted to the new format. Missing javadocs are entered.

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