Changeset 26 for trunk/fmgVen/src/com/fmguler/ven/Ven.java
- Timestamp:
- Jan 2, 2011, 8:51:21 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/fmgVen/src/com/fmguler/ven/Ven.java
r25 r26 18 18 package com.fmguler.ven; 19 19 20 import java.util.HashMap; 20 21 import java.util.List; 21 22 import javax.sql.DataSource; 23 import org.springframework.beans.BeanWrapper; 24 import org.springframework.beans.BeanWrapperImpl; 25 import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; 22 26 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 27 import org.springframework.jdbc.core.namedparam.SqlParameterSource; 23 28 24 29 /** … … 44 49 } 45 50 46 public Object get(int no, Class objectClass) {51 public Object get(int id, Class objectClass) { 47 52 return null; 48 53 } 49 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 */ 50 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); 51 78 } 52 79 53 public void delete(int no, Class objectClass) {80 public void delete(int id, Class objectClass) { 54 81 } 55 82 56 //SETTERS------------------------------------------------------------------- 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 57 103 public void setDataSource(DataSource dataSource) { 58 104 if (dataSource == null) throw new RuntimeException("fmgVen - DataSource cannot be null");
Note: See TracChangeset
for help on using the changeset viewer.