[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; |
---|
[27] | 22 | import java.util.Map; |
---|
[23] | 23 | import javax.sql.DataSource; |
---|
[26] | 24 | import org.springframework.beans.BeanWrapper; |
---|
| 25 | import org.springframework.beans.BeanWrapperImpl; |
---|
| 26 | import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; |
---|
[23] | 27 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
---|
[26] | 28 | import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
---|
[23] | 29 | |
---|
[22] | 30 | /** |
---|
| 31 | * The main class for data access |
---|
| 32 | * @author Fatih Mehmet Güler |
---|
| 33 | */ |
---|
| 34 | public class Ven { |
---|
[23] | 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 | |
---|
[25] | 44 | public List list(Class objectClass) { |
---|
[23] | 45 | return null; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | public int count() { |
---|
| 49 | return 0; |
---|
| 50 | } |
---|
| 51 | |
---|
[26] | 52 | public Object get(int id, Class objectClass) { |
---|
[23] | 53 | return null; |
---|
| 54 | } |
---|
| 55 | |
---|
[26] | 56 | /** |
---|
[27] | 57 | * Save the object. If it has a non null (or non zero) "id" property it will be updated. |
---|
[26] | 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 | */ |
---|
[23] | 65 | public void save(Object object) { |
---|
[26] | 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); |
---|
[23] | 79 | } |
---|
| 80 | |
---|
[27] | 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 | */ |
---|
[26] | 86 | public void delete(int id, Class objectClass) { |
---|
[27] | 87 | String query = generator.generateDeleteQuery(objectClass); |
---|
| 88 | Map parameterMap = new HashMap(); |
---|
| 89 | parameterMap.put("id", new Integer(id)); |
---|
| 90 | template.update(query, parameterMap); |
---|
[23] | 91 | } |
---|
| 92 | |
---|
[26] | 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); |
---|
[27] | 98 | Object objectId = beanWrapper.getPropertyValue("id"); |
---|
[26] | 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 |
---|
[23] | 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 | } |
---|
[22] | 123 | } |
---|