Changeset 26 for trunk/fmgVen/src/com


Ignore:
Timestamp:
Jan 2, 2011, 8:51:21 PM (14 years ago)
Author:
fmguler
Message:

Refs #3 - Ven.save() is transferred to the new package. Save use case is completed. Convert.toDB() converts camel case object names to database names with underscores. Unchecked VenException is thrown by Ven methods. Liquibase changelog (test-db-changelog.xml) is changed to include only a simple object to test save operation. It creates a sample database table and inserts a sample row. On rollback it removes sample data. Using liquibase, Sample.java builds database, tests save operation and rolls back.

QueryGenerator generates insert and update queries for save and generates sequence query to assign ids to new objects. Ven calls query generator and runs the query using spring jdbc template.

Old codebase updated to run at Java 1.4

Location:
trunk/fmgVen/src/com/fmguler/ven
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/fmgVen/src/com/fmguler/ven/QueryGenerator.java

    r23 r26  
    1818package com.fmguler.ven; 
    1919 
     20import com.fmguler.ven.util.Convert; 
     21import java.beans.PropertyDescriptor; 
     22import java.util.Date; 
    2023import java.util.HashSet; 
    2124import java.util.Set; 
     25import org.springframework.beans.BeanWrapper; 
     26import org.springframework.beans.BeanWrapperImpl; 
    2227 
    2328/** 
     
    2732public class QueryGenerator { 
    2833    private Set domainPackages; 
     34    private Set dbClasses; 
    2935 
    3036    public QueryGenerator() { 
    3137        domainPackages = new HashSet(); 
     38        dbClasses = new HashSet(); 
     39        //the predefined database classes; 
     40        this.dbClasses.add(Integer.class); 
     41        this.dbClasses.add(String.class); 
     42        this.dbClasses.add(Date.class); 
     43        this.dbClasses.add(Double.class); 
     44        this.dbClasses.add(Boolean.class); 
    3245    } 
    3346 
     
    4053    } 
    4154 
    42     public String generateUpdateQuery() { 
    43         return null; 
     55    public String generateInsertQuery(Object object) throws VenException { 
     56        BeanWrapper wr = new BeanWrapperImpl(object); 
     57        String objectName = Convert.toSimpleName(object.getClass().getName()); 
     58        String tableName = Convert.toDB(objectName); 
     59        PropertyDescriptor[] pdArr = wr.getPropertyDescriptors(); 
     60 
     61        //generate insert query 
     62        StringBuffer query = new StringBuffer("insert into " + tableName + "("); 
     63        StringBuffer values = new StringBuffer(" values("); 
     64        for (int i = 0; i < pdArr.length; i++) { 
     65            Class fieldClass = pdArr[i].getPropertyType(); //field class 
     66            String columnName = Convert.toDB(pdArr[i].getName()); //column name 
     67            String fieldName = pdArr[i].getName(); //field name 
     68            //if (fieldName.equals("id")) continue; //remove if it does not break the sequence 
     69            if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc) 
     70                query.append(columnName); 
     71                query.append(","); 
     72                values.append(":").append(fieldName); 
     73                values.append(","); 
     74            } 
     75            if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { //object 
     76                query.append(Convert.toDB(fieldName)).append("_id"); 
     77                query.append(","); 
     78                values.append(":").append(fieldName).append(".id"); 
     79                values.append(","); 
     80            } 
     81        } 
     82        query.deleteCharAt(query.length() - 1); 
     83        query.append(")"); 
     84        values.deleteCharAt(values.length() - 1); 
     85        values.append(");"); 
     86        query.append(values); 
     87 
     88        return query.toString(); 
    4489    } 
    4590 
    46     //SETTERS------------------------------------------------------------------- 
     91    /** 
     92     * Generates insert/update query 
     93     * @return the insert update SQL query 
     94     */ 
     95    public String generateUpdateQuery(Object object) throws VenException { 
     96        BeanWrapper wr = new BeanWrapperImpl(object); 
     97        String objectName = Convert.toSimpleName(object.getClass().getName()); 
     98        String tableName = Convert.toDB(objectName); 
     99        PropertyDescriptor[] pdArr = wr.getPropertyDescriptors(); 
     100 
     101        StringBuffer query = new StringBuffer("update " + tableName + " set "); 
     102        for (int i = 0; i < pdArr.length; i++) { 
     103            Class fieldClass = pdArr[i].getPropertyType(); //field class 
     104            String columnName = Convert.toDB(pdArr[i].getName()); //column name 
     105            String fieldName = pdArr[i].getName(); //field name 
     106            if (dbClasses.contains(fieldClass)) { //direct database field (Integer,String,Date, etc) 
     107                query.append(columnName).append("=:").append(fieldName); 
     108                query.append(","); 
     109            } 
     110            if (fieldClass.getPackage() != null && domainPackages.contains(fieldClass.getPackage().getName())) { //object 
     111                query.append(columnName).append("_id=:").append(fieldName).append(".id"); 
     112                query.append(","); 
     113            } 
     114        } 
     115        query.deleteCharAt(query.length() - 1); 
     116        query.append(" where id = :id ;"); //TODO: remove the last comma 
     117        return query.toString(); 
     118    } 
     119 
     120    public String generateSequenceQuery(Object object) throws VenException { 
     121        String objectName = Convert.toSimpleName(object.getClass().getName()); 
     122        String tableName = Convert.toDB(objectName); 
     123        return "select nextval('" + tableName + "_id_seq');"; 
     124    } 
     125 
     126    //-------------------------------------------------------------------------- 
     127    //SETTERS 
    47128    public void addDomainPackage(String domainPackage) { 
    48129        domainPackages.add(domainPackage); 
  • trunk/fmgVen/src/com/fmguler/ven/Ven.java

    r25 r26  
    1818package com.fmguler.ven; 
    1919 
     20import java.util.HashMap; 
    2021import java.util.List; 
    2122import javax.sql.DataSource; 
     23import org.springframework.beans.BeanWrapper; 
     24import org.springframework.beans.BeanWrapperImpl; 
     25import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; 
    2226import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 
     27import org.springframework.jdbc.core.namedparam.SqlParameterSource; 
    2328 
    2429/** 
     
    4449    } 
    4550 
    46     public Object get(int no, Class objectClass) { 
     51    public Object get(int id, Class objectClass) { 
    4752        return null; 
    4853    } 
    4954 
     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     */ 
    5064    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); 
    5178    } 
    5279 
    53     public void delete(int no, Class objectClass) { 
     80    public void delete(int id, Class objectClass) { 
    5481    } 
    5582 
    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 
    57103    public void setDataSource(DataSource dataSource) { 
    58104        if (dataSource == null) throw new RuntimeException("fmgVen - DataSource cannot be null"); 
Note: See TracChangeset for help on using the changeset viewer.