com.iih5.smartorm.model.Model.java Source code

Java tutorial

Introduction

Here is the source code for com.iih5.smartorm.model.Model.java

Source

package com.iih5.smartorm.model;
/*
 * Copyright 2016 xueyi (1581249005@qq.com)
 *
 * The SmartORM Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import com.alibaba.fastjson.JSON;
import com.iih5.smartorm.dialect.DefaultDialect;
import com.iih5.smartorm.kit.StringKit;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;

public abstract class Model<M extends Model> implements Serializable {
    private static final long serialVersionUID = -990334519496260591L;
    JdbcTemplate jdbc = null;
    //??
    private String table = "";
    private Map<String, Object> attrs = new HashMap<String, Object>();
    private Set<String> modifyFlag = new HashSet<String>();
    private Object[] NULL_PARA_ARRAY = new Object[] {};

    public Model() {
        this.table = StringKit.toTableNameByModel(this.getClass());
        this.jdbc = getJdbc();
    }

    public Model(String table) {
        this.table = table;
        this.jdbc = getJdbc();
    }

    /**
     * ?JDBC
     *
     * @return
     */
    private JdbcTemplate getJdbc() {
        return Db.getJdbcTemplate();
    }

    /**
     * ?
     *
     * @return
     */
    public Map<String, Object> getAttrs() {
        return attrs;
    }

    private Set<String> getModifyFlag() {
        return modifyFlag;
    }

    /**
     * ??
     *
     * @param dataSource
     * @return
     */
    public M use(String dataSource) {
        this.jdbc = Db.getJdbcTemplate(dataSource);
        return (M) this;
    }

    /**
     * Set attribute to model.
     *
     * @param attr  the attribute name of the model
     * @param value the value of the attribute
     * @return this model
     */
    public M set(String attr, Object value) {
        attrs.put(attr, value);
        getModifyFlag().add(attr); // Add modify flag, update() need this flag.
        return (M) this;
    }

    /**
     * @param attr
     * @param <T>
     * @return
     */
    public <T> T get(String attr) {
        return (T) (attrs.get(attr));
    }

    /**
     * @param attr
     * @return
     */
    public String getStr(String attr) {
        return (String) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public Integer getInt(String attr) {
        return (Integer) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public Long getLong(String attr) {
        return (Long) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public java.math.BigInteger getBigInteger(String attr) {
        return (java.math.BigInteger) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public Date getDate(String attr) {
        return (Date) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public java.sql.Time getTime(String attr) {
        return (java.sql.Time) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public java.sql.Timestamp getTimestamp(String attr) {
        return (java.sql.Timestamp) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public Double getDouble(String attr) {
        return (Double) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public Float getFloat(String attr) {
        return (Float) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public Boolean getBoolean(String attr) {
        return (Boolean) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public java.math.BigDecimal getBigDecimal(String attr) {
        return (java.math.BigDecimal) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public byte[] getBytes(String attr) {
        return (byte[]) attrs.get(attr);
    }

    /**
     * @param attr
     * @return
     */
    public Number getNumber(String attr) {
        return (Number) attrs.get(attr);
    }

    /**
     * ??
     *
     * @return ??
     */
    public boolean save() {
        try {
            Field[] attr = this.getClass().getFields();
            for (Field f : attr) {
                Object value = f.get(this);
                if (value != null) {
                    attrs.put(f.getName(), value);
                }
            }
            StringBuilder sql = new StringBuilder();
            List<Object> paras = new ArrayList<Object>();
            DefaultDialect.getDialect().forModelSave(table, attrs, sql, paras);
            if (jdbc.update(sql.toString(), paras.toArray()) < 0) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * ???
     *
     * @param conditions      conditions="userId=? and name=?"
     * @param conditionValues new Object[]{1000,'hill'};
     * @return true if delete succeed otherwise false
     */
    public boolean delete(String conditions, Object[] conditionValues) {
        if (conditionValues == null || conditionValues.length == 0) {
            return false;
        }
        String sql = DefaultDialect.getDialect().deleteByCondition(table, conditions);
        if (jdbc.update(sql, conditionValues) < 0) {
            return false;
        }
        return true;
    }

    public boolean delete(String conditions) {
        StringBuilder sql = new StringBuilder();
        sql.append("delete from ");
        sql.append(table);
        sql.append(" where ");
        sql.append(conditions);
        if (jdbc.update(sql.toString()) < 0) {
            return false;
        }
        return true;
    }

    public boolean deleteById(long id) {
        StringBuilder sql = new StringBuilder();
        sql.append("delete from ");
        sql.append(table);
        sql.append(" where id= ");
        sql.append(id);
        if (jdbc.update(sql.toString()) < 0) {
            return false;
        }
        return true;
    }

    public boolean deleteByIds(List list) {
        String st1 = list.toString();
        String arr = st1.substring(st1.indexOf("[") + 1, st1.indexOf("]"));
        StringBuilder sql = new StringBuilder();
        sql.append("delete from ");
        sql.append(table);
        sql.append(" where id in ");
        sql.append("(");
        sql.append(arr);
        sql.append(")");
        if (jdbc.update(sql.toString()) < 0) {
            return false;
        }
        return true;
    }

    /**
     * ???
     *
     * @param conditions      conditions="userId=? and name=?"
     * @param conditionValues new Object[]{1000,'hill'};
     * @return true if delete succeed otherwise false
     */
    public boolean update(String conditions, Object[] conditionValues) {
        try {
            Field[] attr = this.getClass().getFields();
            for (Field f : attr) {
                Object value = f.get(this);
                if (value != null) {
                    attrs.put(f.getName(), value);
                    modifyFlag.add(f.getName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (getModifyFlag().isEmpty()) {
            return false;
        }
        StringBuilder sql = new StringBuilder();
        DefaultDialect.getDialect().forModelUpdate(table, conditions, attrs, getModifyFlag(), sql);
        if (jdbc.update(sql.toString(), conditionValues) < 0) {
            return false;
        } else {
            return true;
        }
    }

    public boolean updateById(long id) {
        return update("id=?", new Object[] { id });
    }

    /**
     * 
     *
     * @param attr
     * @return this model
     */
    public M removeAttr(String attr) {
        attrs.remove(attr);
        getModifyFlag().remove(attr);
        return (M) this;
    }

    /**
     * 
     *
     * @param attrs
     * @return this model
     */
    public M removeAttr(String... attrs) {
        if (attrs != null)
            for (String a : attrs) {
                this.attrs.remove(a);
                this.getModifyFlag().remove(a);
            }
        return (M) this;
    }

    /**
     * 
     *
     * @return
     */
    public M clear() {
        attrs.clear();
        getModifyFlag().clear();
        return (M) this;
    }

    /**
     * @param columns        ?? columns="id,name,age"
     * @param conditions     conditions ? conditions="user_id=? and age=?"
     * @param conditionParas ??
     * @return Model
     * @
     */
    public M find(String columns, String conditions, Object[] conditionParas) {
        List<M> result = findList(columns, conditions, conditionParas);
        return result.size() > 0 ? result.get(0) : null;
    }

    /**
     * @param conditions     conditions ? conditions="user_id=? and age=?"
     * @param conditionParas ??
     * @return Model 1
     * @
     */
    public M find(String conditions, Object[] conditionParas) {
        List<M> result = findList(conditions, conditionParas);
        return result.size() > 0 ? result.get(0) : null;
    }

    /**
     * @param conditions conditions ? conditions="user_id=? and age=?"
     * @return Model
     * @
     */
    public M find(String conditions) {
        List<M> result = findList(conditions);
        return result.size() > 0 ? result.get(0) : null;
    }

    public M findById(long id) {
        return find("id=" + id);
    }

    //  private Set<String> columnMeta= new HashSet<String>();

    /**
     * Model
     *
     * @param columns        ?? columns="id,name,age"
     * @param conditions     ? conditions="user_id=? and age=?"
     * @param conditionParas ??
     * @param <T>
     * @return Model
     * @
     */
    <T> List<T> queryList(String columns, String conditions, Object[] conditionParas) {
        String sql = DefaultDialect.getDialect().forModelFindBy(table, columns, conditions);
        final Set<String> columnMeta = new HashSet<String>();
        return jdbc.query(sql, conditionParas, new RowMapper<T>() {
            public T mapRow(ResultSet rs, int rowNum) throws SQLException {
                try {
                    if (columnMeta.size() == 0) {
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String column = rs.getMetaData().getColumnLabel(i + 1);
                            columnMeta.add(column);
                        }
                    }
                    Model<?> mModel = getUsefulClass().newInstance();
                    Field[] fields = mModel.getClass().getFields();
                    if (fields.length > 0) {
                        for (Field f : fields) {
                            if (columnMeta.contains(f.getName())) {
                                f.set(mModel, rs.getObject(f.getName()));
                            }
                        }
                    } else {
                        ResultSetMetaData rsmd = rs.getMetaData();
                        int columnCount = rsmd.getColumnCount();
                        Map<String, Object> attrs = mModel.getAttrs();
                        for (int i = 1; i <= columnCount; i++) {
                            Object value = rs.getObject(i);
                            if (value != null) {
                                attrs.put(rsmd.getColumnLabel(i), value);
                            }
                        }
                    }
                    return (T) mModel;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    /**
     * Model
     *
     * @param columns        ?? columns="id,name,age"
     * @param conditions     ? conditions="user_id=? and age=?"
     * @param conditionParas ??
     * @return Model
     * @
     */
    public List<M> findList(String columns, String conditions, Object[] conditionParas) {
        return queryList(columns, conditions, conditionParas);
    }

    /**
     * Model
     *
     * @param conditions     ? conditions="user_id=? and age=?"
     * @param conditionParas ??
     * @return Model
     * @
     */
    public List<M> findList(String conditions, Object[] conditionParas) {
        return findList("*", conditions, conditionParas);
    }

    /**
     * @param conditions ? conditions="user_id=? and age=?"
     * @return Model
     * @
     */
    public List<M> findList(String conditions) {
        return findList(conditions, NULL_PARA_ARRAY);
    }

    /**
     * ?Map?(??attrs)
     *
     * @param sql
     * @return
     */
    public List<Map<String, Object>> findList(String sql, boolean isNotAttr) {
        return jdbc.queryForList(sql);
    }

    /**
     * ?Map?(??attrs)
     *
     * @param sql
     * @param paras
     * @return
     */
    public List<Map<String, Object>> findList(String sql, Object[] paras, boolean isNotAttr) {
        return jdbc.queryForList(sql, paras);
    }

    /**
     * (Boolean,Int,Number,Float,String...)
     *
     * @param column         ?? columns="id,name,age"
     * @param conditions     ? conditions="user_id=? and age=?"
     * @param conditionParas ??
     * @param classType      (Boolean,Int,Number,Float,String...)
     * @param <T>
     * @return 
     * @
     */
    public <T> List<T> findBasicObjectList(String column, String conditions, Object[] conditionParas,
            Class<T> classType) {
        String sql = DefaultDialect.getDialect().forModelFindBy(table, column, conditions);
        return jdbc.queryForList(sql, conditionParas, classType);
    }

    /**
     * (Boolean,Int,Number,Float,String...)
     *
     * @param column     ?? columns="id,name,age"
     * @param conditions ? conditions="user_id=? and age=?"
     * @param classType  (Boolean,Int,Number,Float,String...)
     * @return 
     * @
     */
    public <T> List<T> findBasicObjectList(String column, String conditions, Class<T> classType) {
        return findBasicObjectList(column, conditions, NULL_PARA_ARRAY, classType);
    }

    /**
     * (Boolean,Int,Number,Float,String...)
     *
     * @param column         ?? columns="id,name,age"
     * @param conditions     ? conditions="user_id=? and age=?"
     * @param conditionParas ??
     * @param classType      (Boolean,Int,Number,Float,String...)
     * @return 
     */
    public <T> T findBasicObject(String column, String conditions, Object[] conditionParas, Class<T> classType) {
        String sql = DefaultDialect.getDialect().forModelFindBy(table, column, conditions);
        return jdbc.queryForObject(sql, conditionParas, classType);
    }

    /**
     *  (Boolean,Int,Number,Float,String...)
     *
     * @param column     ?? columns="id,name,age"
     * @param conditions ? conditions="user_id=? and age=?"
     * @param classType  (Boolean,Int,Number,Float,String...)
     * @param <T>
     * @return 
     */
    public <T> T findBasicObject(String column, String conditions, Class<T> classType) {
        return (T) findBasicObject(column, conditions, NULL_PARA_ARRAY, classType);
    }

    /**
     * 
     *
     * @param pageNumber 
     * @param pageSize   ??
     * @param columns    ?? columns="id,name,age"
     * @param conditions ? conditions="user_id=? and age=?"
     * @param paras      ?
     * @return 
     * @
     */
    public Page<M> paginate(int pageNumber, int pageSize, String columns, String conditions, Object[] paras) {
        String sql = DefaultDialect.getDialect().forModelFindBy(table, columns, conditions);
        try {
            return (Page<M>) Db.paginate(this.getUsefulClass(), pageNumber, pageSize, sql, paras);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 
     *
     * @param pageNumber 
     * @param pageSize   ??
     * @param columns    ?? columns="id,name,age"
     * @param conditions ? conditions="user_id=? and age=?"
     * @param paras      ?
     * @param isMap      ?MAP? true MAP?(??attrs)
     * @return 
     * @
     */
    public Page<Map> paginate(int pageNumber, int pageSize, String columns, String conditions, Object[] paras,
            boolean isMap) {
        String sql = DefaultDialect.getDialect().forModelFindBy(table, columns, conditions);
        try {
            return (Page<Map>) Db.paginate(this.getUsefulClass(), pageNumber, pageSize, sql, paras, isMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 
     * @param pageNumber
     * @param pageSize
     * @param sql
     * @param paras
     * @return
     */
    public Page<M> paginateMultiple(int pageNumber, int pageSize, String sql, Object[] paras) {
        try {
            return (Page<M>) Db.paginateMultiple(this.getUsefulClass(), pageNumber, pageSize, sql, paras);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 
     * @param pageNumber
     * @param pageSize
     * @param sql
     * @param paras
     * @param isNotAttr ?MAP? true MAP?(??attrs)
     * @return
     */
    public Page<Map> paginateMultiple(int pageNumber, int pageSize, String sql, Object[] paras, boolean isNotAttr) {
        try {
            return Db.paginateMultiple(pageNumber, pageSize, sql, paras, isNotAttr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param o
     * @return
     */
    public boolean equals(Object o) {
        if (!(o instanceof Model))
            return false;
        if (getUsefulClass() != ((Model) o).getUsefulClass())
            return false;
        if (o == this)
            return true;
        return this.attrs.equals(((Model) o).attrs);
    }

    /**
     * @return
     */
    public int hashCode() {
        return (attrs == null ? 0 : attrs.hashCode()) ^ (getModifyFlag() == null ? 0 : getModifyFlag().hashCode());
    }

    /**
     * ?json
     *
     * @return json str
     */
    public String toString() {
        if (this.getClass().getFields().length > 0) {
            return JSON.toJSONString(this);
        }
        Map<String, Object> ddd = this.getAttrs();
        return JSON.toJSONString(this.getAttrs());
    }

    /**
     * @return
     */
    private Class<? extends Model> getUsefulClass() {
        Class c = getClass();
        return c.getName().indexOf("EnhancerByCGLIB") == -1 ? c : c.getSuperclass();
    }
}