| 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 | |
|---|
| 20 | import com.fmguler.ven.util.Convert; |
|---|
| 21 | import java.util.HashMap; |
|---|
| 22 | import java.util.List; |
|---|
| 23 | import java.util.Map; |
|---|
| 24 | import java.util.Set; |
|---|
| 25 | import javax.sql.DataSource; |
|---|
| 26 | import org.springframework.beans.BeanWrapper; |
|---|
| 27 | import org.springframework.beans.BeanWrapperImpl; |
|---|
| 28 | import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; |
|---|
| 29 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
|---|
| 30 | import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * The main class for data access |
|---|
| 34 | * @author Fatih Mehmet Güler |
|---|
| 35 | */ |
|---|
| 36 | public 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 List list(Class objectClass) { |
|---|
| 48 | return null; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | public int count() { |
|---|
| 52 | return 0; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public Object get(int id, Class objectClass, Set joins) { |
|---|
| 56 | String query = generator.generateSelectQuery(objectClass, joins); |
|---|
| 57 | query += " where 1=1 and " + Convert.toDB(Convert.toSimpleName(objectClass.getName())) + ".id = :___id "; |
|---|
| 58 | |
|---|
| 59 | Map paramMap = new HashMap(); |
|---|
| 60 | paramMap.put("___id", new Integer(id)); |
|---|
| 61 | if (debug) System.out.println("Ven - SQL: " + query); |
|---|
| 62 | |
|---|
| 63 | List result = mapper.list(query, paramMap, objectClass); |
|---|
| 64 | if (result.isEmpty()) return null; |
|---|
| 65 | if (result.size() > 1) System.out.println("Ven - WARNING >> get(id) returns more than one row"); |
|---|
| 66 | return result.get(0); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Save the object. If it has a non null (or non zero) "id" property it will be updated. |
|---|
| 71 | * It will be inserted otherwise. |
|---|
| 72 | * <p> |
|---|
| 73 | * The object will be saved to a table with the same name as the object, |
|---|
| 74 | * The fields of object will be mapped to the table fields. |
|---|
| 75 | * |
|---|
| 76 | * @param object the object to be saved |
|---|
| 77 | */ |
|---|
| 78 | public void save(Object object) { |
|---|
| 79 | String query = null; |
|---|
| 80 | |
|---|
| 81 | if (isObjectNew(object)) { |
|---|
| 82 | //if this is a new object assign a new id first |
|---|
| 83 | generateId(object); |
|---|
| 84 | query = generator.generateInsertQuery(object); |
|---|
| 85 | } else { |
|---|
| 86 | query = generator.generateUpdateQuery(object); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | //execute the insert/update query |
|---|
| 90 | SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(object); |
|---|
| 91 | template.update(query, parameterSource); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Delete the the object with the specified id of the specified objectClass type |
|---|
| 96 | * @param id the id of the object to be deleted |
|---|
| 97 | * @param objectClass the class of the object to be deleted |
|---|
| 98 | */ |
|---|
| 99 | public void delete(int id, Class objectClass) { |
|---|
| 100 | String query = generator.generateDeleteQuery(objectClass); |
|---|
| 101 | Map parameterMap = new HashMap(); |
|---|
| 102 | parameterMap.put("id", new Integer(id)); |
|---|
| 103 | template.update(query, parameterMap); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | //-------------------------------------------------------------------------- |
|---|
| 107 | //PRIVATE METHODS |
|---|
| 108 | //return true if the object id is zero or null false otherwise |
|---|
| 109 | private boolean isObjectNew(Object object) throws VenException { |
|---|
| 110 | BeanWrapper beanWrapper = new BeanWrapperImpl(object); |
|---|
| 111 | Object objectId = beanWrapper.getPropertyValue("id"); |
|---|
| 112 | if (objectId == null) return true; |
|---|
| 113 | if (!(objectId instanceof Integer)) throw new VenException(VenException.EC_GENERATOR_OBJECT_ID_TYPE_INVALID); |
|---|
| 114 | return ((Integer)objectId).intValue() == 0; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | //set new object id |
|---|
| 118 | private void generateId(Object object) { |
|---|
| 119 | Integer newObjectId = new Integer(template.queryForInt(generator.generateSequenceQuery(object), new HashMap())); |
|---|
| 120 | BeanWrapper beanWrapper = new BeanWrapperImpl(object); |
|---|
| 121 | beanWrapper.setPropertyValue("id", newObjectId); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | //-------------------------------------------------------------------------- |
|---|
| 125 | //SETTERS |
|---|
| 126 | public void setDataSource(DataSource dataSource) { |
|---|
| 127 | if (dataSource == null) throw new RuntimeException("fmgVen - DataSource cannot be null"); |
|---|
| 128 | this.template = new NamedParameterJdbcTemplate(dataSource); |
|---|
| 129 | mapper.setDataSource(dataSource); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public Ven addDomainPackage(String domainPackage) { |
|---|
| 133 | generator.addDomainPackage(domainPackage); |
|---|
| 134 | mapper.addDomainPackage(domainPackage); |
|---|
| 135 | return this; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|