Search in sources :

Example 6 with ActiveRecordException

use of com.jfinal.plugin.activerecord.ActiveRecordException in project jfinal by jfinal.

the class Injector method injectModel.

@SuppressWarnings("unchecked")
public static <T> T injectModel(Class<T> modelClass, String modelName, HttpServletRequest request, boolean skipConvertError) {
    Object temp = createInstance(modelClass);
    if (temp instanceof Model == false) {
        throw new IllegalArgumentException("getModel only support class of Model, using getBean for other class.");
    }
    Model<?> model = (Model<?>) temp;
    Table table = TableMapping.me().getTable(model.getClass());
    if (table == null) {
        throw new ActiveRecordException("The Table mapping of model: " + modelClass.getName() + " not exists or the ActiveRecordPlugin not start.");
    }
    String modelNameAndDot = StrKit.notBlank(modelName) ? modelName + "." : null;
    Map<String, String[]> parasMap = request.getParameterMap();
    TypeConverter converter = TypeConverter.me();
    // 以及支持界面的 attrName有误时可以感知并抛出异常避免出错
    for (Entry<String, String[]> entry : parasMap.entrySet()) {
        String paraName = entry.getKey();
        String attrName;
        if (modelNameAndDot != null) {
            if (paraName.startsWith(modelNameAndDot)) {
                attrName = paraName.substring(modelNameAndDot.length());
            } else {
                continue;
            }
        } else {
            attrName = paraName;
        }
        Class<?> colType = table.getColumnType(attrName);
        if (colType == null) {
            if (skipConvertError) {
                continue;
            } else {
                throw new ActiveRecordException("The model attribute " + attrName + " is not exists.");
            }
        }
        try {
            String[] paraValueArray = entry.getValue();
            String paraValue = (paraValueArray != null && paraValueArray.length > 0) ? paraValueArray[0] : null;
            Object value = paraValue != null ? converter.convert(colType, paraValue) : null;
            model.set(attrName, value);
        } catch (Exception e) {
            if (skipConvertError == false) {
                throw new RuntimeException("Can not convert parameter: " + paraName, e);
            }
        }
    }
    return (T) model;
}
Also used : Table(com.jfinal.plugin.activerecord.Table) ActiveRecordException(com.jfinal.plugin.activerecord.ActiveRecordException) ActiveRecordException(com.jfinal.plugin.activerecord.ActiveRecordException) TypeConverter(com.jfinal.core.converter.TypeConverter) Model(com.jfinal.plugin.activerecord.Model)

Example 7 with ActiveRecordException

use of com.jfinal.plugin.activerecord.ActiveRecordException in project jfinal by jfinal.

the class Tx method intercept.

public void intercept(Invocation inv) {
    Config config = getConfigWithTxConfig(inv);
    if (config == null)
        config = DbKit.getConfig();
    Connection conn = config.getThreadLocalConnection();
    if (conn != null) {
        // Nested transaction support
        try {
            if (conn.getTransactionIsolation() < getTransactionLevel(config))
                conn.setTransactionIsolation(getTransactionLevel(config));
            inv.invoke();
            return;
        } catch (SQLException e) {
            throw new ActiveRecordException(e);
        }
    }
    Boolean autoCommit = null;
    try {
        conn = config.getConnection();
        autoCommit = conn.getAutoCommit();
        config.setThreadLocalConnection(conn);
        // conn.setTransactionIsolation(transactionLevel);
        conn.setTransactionIsolation(getTransactionLevel(config));
        conn.setAutoCommit(false);
        inv.invoke();
        conn.commit();
    } catch (NestedTransactionHelpException e) {
        if (conn != null)
            try {
                conn.rollback();
            } catch (Exception e1) {
                LogKit.error(e1.getMessage(), e1);
            }
        LogKit.logNothing(e);
    } catch (Throwable t) {
        if (conn != null)
            try {
                conn.rollback();
            } catch (Exception e1) {
                LogKit.error(e1.getMessage(), e1);
            }
        // 支持在 controller 中 try catch 的 catch 块中使用 render(...) 并 throw e,实现灵活控制 render
        if (inv.isActionInvocation() && inv.getController().getRender() != null) {
            LogKit.error(t.getMessage(), t);
        } else {
            throw t instanceof RuntimeException ? (RuntimeException) t : new ActiveRecordException(t);
        }
    } finally {
        try {
            if (conn != null) {
                if (autoCommit != null)
                    conn.setAutoCommit(autoCommit);
                conn.close();
            }
        } catch (Throwable t) {
            // can not throw exception here, otherwise the more important exception in previous catch block can not be thrown
            LogKit.error(t.getMessage(), t);
        } finally {
            // prevent memory leak
            config.removeThreadLocalConnection();
        }
    }
}
Also used : NestedTransactionHelpException(com.jfinal.plugin.activerecord.NestedTransactionHelpException) SQLException(java.sql.SQLException) Config(com.jfinal.plugin.activerecord.Config) Connection(java.sql.Connection) ActiveRecordException(com.jfinal.plugin.activerecord.ActiveRecordException) SQLException(java.sql.SQLException) ActiveRecordException(com.jfinal.plugin.activerecord.ActiveRecordException) NestedTransactionHelpException(com.jfinal.plugin.activerecord.NestedTransactionHelpException)

Aggregations

ActiveRecordException (com.jfinal.plugin.activerecord.ActiveRecordException)7 Before (com.jfinal.aop.Before)5 Record (com.jfinal.plugin.activerecord.Record)2 ToolString (com.hxkj.common.util.ToolString)1 SysRoleMenu (com.hxkj.system.model.SysRoleMenu)1 SysUserRole (com.hxkj.system.model.SysUserRole)1 TypeConverter (com.jfinal.core.converter.TypeConverter)1 Config (com.jfinal.plugin.activerecord.Config)1 Model (com.jfinal.plugin.activerecord.Model)1 NestedTransactionHelpException (com.jfinal.plugin.activerecord.NestedTransactionHelpException)1 Table (com.jfinal.plugin.activerecord.Table)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1