[22] | 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 | */ |
---|
| 18 | package com.fmguler.ven; |
---|
| 19 | |
---|
[26] | 20 | import java.util.HashMap; |
---|
[23] | 21 | import java.util.List; |
---|
| 22 | import javax.sql.DataSource; |
---|
[26] | 23 | import org.springframework.beans.BeanWrapper; |
---|
| 24 | import org.springframework.beans.BeanWrapperImpl; |
---|
| 25 | import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; |
---|
[23] | 26 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
---|
[26] | 27 | import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
---|
[23] | 28 | |
---|
[22] | 29 | /** |
---|
| 30 | * The main class for data access |
---|
| 31 | * @author Fatih Mehmet Güler |
---|
| 32 | */ |
---|
| 33 | public class Ven { |
---|
[23] | 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 | |
---|
[25] | 43 | public List list(Class objectClass) { |
---|
[23] | 44 | return null; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | public int count() { |
---|
| 48 | return 0; |
---|
| 49 | } |
---|
| 50 | |
---|
[26] | 51 | public Object get(int id, Class objectClass) { |
---|
[23] | 52 | return null; |
---|
| 53 | } |
---|
| 54 | |
---|
[26] | 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 | */ |
---|
[23] | 64 | public void save(Object object) { |
---|
[26] | 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); |
---|
[23] | 78 | } |
---|
| 79 | |
---|
[26] | 80 | public void delete(int id, Class objectClass) { |
---|
[23] | 81 | } |
---|
| 82 | |
---|
[26] | 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 |
---|
[23] | 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 | } |
---|
[22] | 113 | } |
---|