Search in sources :

Example 6 with ConnectionFactoryException

use of jp.ossc.nimbus.service.connection.ConnectionFactoryException in project nimbus by nimbus-org.

the class MasterValidatorService method validate.

/**
 * 指定されたオブジェクトがマスタに含まれているかを検証する。<p>
 *
 * @param obj 検証対象のオブジェクト
 * @return 検証結果。検証成功の場合true
 * @exception ValidateException 検証に失敗した場合
 */
public boolean validate(Object obj) throws ValidateException {
    if (connectionFactory != null) {
        if (persistentManager != null) {
            Connection con = null;
            try {
                con = connectionFactory.getConnection();
                final List result = (List) persistentManager.loadQuery(con, query, obj, null);
                if (result.size() == 0) {
                    return false;
                }
                final Collection values = ((Map) result.get(0)).values();
                if (values.size() == 0) {
                    return false;
                }
                if (values.size() == 1) {
                    final Object value = values.iterator().next();
                    if (value instanceof Boolean) {
                        return ((Boolean) value).booleanValue();
                    } else if (value instanceof Number) {
                        return ((Number) value).intValue() != 0;
                    } else {
                        return true;
                    }
                } else {
                    return true;
                }
            } catch (ConnectionFactoryException e) {
                throw new ValidateException(e);
            } catch (PersistentException e) {
                throw new ValidateException(e);
            } finally {
                if (con != null) {
                    try {
                        con.close();
                    } catch (SQLException e) {
                    }
                }
            }
        } else {
            final RecordSet recset = templateRecordSet.cloneEmpty();
            Connection con = null;
            try {
                con = connectionFactory.getConnection();
                recset.setConnection(con);
                recset.setLogger(getLogger());
                for (int i = 0, imax = bindDataList.size(); i < imax; i++) {
                    final Property prop = (Property) bindDataList.get(i);
                    if (prop == null) {
                        recset.setBindData(i, obj);
                    } else {
                        recset.setBindData(i, prop.getProperty(obj));
                    }
                }
                return recset.search() != 0;
            } catch (ConnectionFactoryException e) {
                throw new ValidateException(e);
            } catch (NoSuchPropertyException e) {
                throw new ValidateException(e);
            } catch (InvocationTargetException e) {
                throw new ValidateException(e.getCause());
            } catch (SQLException e) {
                throw new ValidateException(e);
            } finally {
                if (con != null) {
                    try {
                        con.close();
                    } catch (SQLException e) {
                    }
                }
            }
        }
    } else {
        Map codeMaster = null;
        if (threadContext != null) {
            codeMaster = (Map) threadContext.get(codeMasterThreadContextKey);
        }
        if (codeMaster == null && codeMasterFinder != null) {
            codeMaster = codeMasterFinder.getCodeMasters();
        }
        if (codeMaster == null) {
            throw new ValidateException("CodeMaster is not found.");
        }
        final Object master = codeMaster.get(codeMasterName);
        if (master == null) {
            throw new ValidateException("Master '" + codeMasterName + "' is not found.");
        }
        if (master instanceof RecordSet) {
            final RecordSet recset = (RecordSet) master;
            if (searchCondition != null) {
                final Map params = new HashMap();
                final Iterator entries = bindDataMap.entrySet().iterator();
                try {
                    while (entries.hasNext()) {
                        final Map.Entry entry = (Map.Entry) entries.next();
                        final String key = (String) entry.getKey();
                        final Property prop = (Property) entry.getValue();
                        if (prop == null) {
                            params.put(key, obj);
                        } else {
                            params.put(key, prop.getProperty(obj));
                        }
                    }
                } catch (NoSuchPropertyException e) {
                    throw new ValidateException(e);
                } catch (InvocationTargetException e) {
                    throw new ValidateException(e.getCause());
                }
                try {
                    return recset.searchDynamicConditionReal(searchCondition, params).size() != 0;
                } catch (Exception e) {
                    throw new ValidateException(e);
                }
            } else {
                return recset.get(obj == null ? null : obj.toString()) != null;
            }
        } else if (master instanceof RecordList) {
            final RecordList recordList = (RecordList) master;
            if (searchCondition != null) {
                final Map params = new HashMap();
                final Iterator entries = bindDataMap.entrySet().iterator();
                try {
                    while (entries.hasNext()) {
                        final Map.Entry entry = (Map.Entry) entries.next();
                        final String key = (String) entry.getKey();
                        final Property prop = (Property) entry.getValue();
                        if (prop == null) {
                            params.put(key, obj);
                        } else {
                            params.put(key, prop.getProperty(obj));
                        }
                    }
                } catch (NoSuchPropertyException e) {
                    throw new ValidateException(e);
                } catch (InvocationTargetException e) {
                    throw new ValidateException(e.getCause());
                }
                try {
                    return recordList.realSearch(searchCondition, params).size() != 0;
                } catch (Exception e) {
                    throw new ValidateException(e);
                }
            } else {
                PropertySchema[] schemata = recordList.getRecordSchema().getPrimaryKeyPropertySchemata();
                if (schemata == null || schemata.length != 1) {
                    throw new ValidateException("Size of primary key property not equal 1.");
                }
                Record key = recordList.createRecord();
                key.setProperty(schemata[0].getName(), obj);
                return recordList.searchByPrimaryKey(key) != null;
            }
        } else {
            throw new ValidateException("Master '" + codeMasterName + "' is not supported type. type=" + master.getClass().getName());
        }
    }
}
Also used : ValidateException(jp.ossc.nimbus.util.validator.ValidateException) PersistentException(jp.ossc.nimbus.service.connection.PersistentException) ConnectionFactoryException(jp.ossc.nimbus.service.connection.ConnectionFactoryException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistentException(jp.ossc.nimbus.service.connection.PersistentException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConnectionFactoryException(jp.ossc.nimbus.service.connection.ConnectionFactoryException) ValidateException(jp.ossc.nimbus.util.validator.ValidateException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) Record(jp.ossc.nimbus.beans.dataset.Record) RecordSet(jp.ossc.nimbus.recset.RecordSet)

Example 7 with ConnectionFactoryException

use of jp.ossc.nimbus.service.connection.ConnectionFactoryException in project nimbus by nimbus-org.

the class DatabaseAuthenticateStoreService method create.

public void create(HttpServletRequest request, Object authenticatedInfo) throws AuthenticateStoreException {
    if (insertQuery == null) {
        return;
    }
    Connection con = null;
    try {
        con = connectionFactory.getConnection();
    } catch (ConnectionFactoryException e) {
        throw new AuthenticateStoreException(e);
    }
    try {
        boolean isExists = false;
        Object input = createInput(request, null, authenticatedInfo);
        if (selectQueryOnCreateUser != null) {
            List list = (List) persistentManager.loadQuery(con, selectQueryOnCreateUser, input, null);
            if (list.size() == 0 || (list.get(0) instanceof Number && ((Number) list.get(0)).intValue() <= 0) || (list.get(0) instanceof Boolean && !((Boolean) list.get(0)).booleanValue()) || (list.get(0) instanceof String && !((String) list.get(0)).equals("0"))) {
                throw new AuthenticateStoreException("Already exists." + authenticatedInfo);
            }
        }
        if (selectQueryOnFindUser != null) {
            List list = (List) persistentManager.loadQuery(con, selectQueryOnFindUser, input, null);
            isExists = list != null && list.size() != 0;
        }
        if (isExists) {
            if (isDeleteFindUser) {
                if (deleteQueryOnCreate != null) {
                    persistentManager.persistQuery(con, deleteQueryOnCreate, input);
                    persistentManager.persistQuery(con, insertQuery, input);
                }
            } else {
                if (updateQueryOnCreate != null) {
                    persistentManager.persistQuery(con, updateQueryOnCreate, input);
                }
            }
        } else {
            persistentManager.persistQuery(con, insertQuery, input);
        }
    } catch (PersistentException e) {
        throw new AuthenticateStoreException(e);
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
        }
    }
}
Also used : PersistentException(jp.ossc.nimbus.service.connection.PersistentException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) List(java.util.List) ConnectionFactoryException(jp.ossc.nimbus.service.connection.ConnectionFactoryException)

Example 8 with ConnectionFactoryException

use of jp.ossc.nimbus.service.connection.ConnectionFactoryException in project nimbus by nimbus-org.

the class DatabaseAuthenticateStoreService method activate.

public Object activate(HttpServletRequest request, Object authenticatedKey) throws AuthenticateStoreException {
    if (selectQueryOnFindUser == null) {
        return null;
    }
    Connection con = null;
    try {
        con = connectionFactory.getConnection();
    } catch (ConnectionFactoryException e) {
        throw new AuthenticateStoreException(e);
    }
    try {
        Object authenticatedInfo = null;
        if (selectQueryOnFindUser != null) {
            if (authenticatedInfoClass != null) {
                List list = (List) persistentManager.loadQuery(con, selectQueryOnFindUser, createInput(request, null, authenticatedKey), authenticatedInfoClass);
                if (list.size() == 0) {
                    return null;
                }
                authenticatedInfo = list.get(0);
            } else {
                if (authenticatedInfoTemplate instanceof DataSet) {
                    authenticatedInfo = ((DataSet) authenticatedInfoTemplate).cloneSchema();
                } else if (authenticatedInfoTemplate instanceof RecordList) {
                    authenticatedInfo = ((RecordList) authenticatedInfoTemplate).cloneSchema();
                } else if (authenticatedInfoTemplate instanceof Record) {
                    authenticatedInfo = ((Record) authenticatedInfoTemplate).cloneSchema();
                } else if (authenticatedInfoTemplate instanceof Cloneable) {
                    try {
                        authenticatedInfo = authenticatedInfoTemplate.getClass().getMethod("clone", (Class[]) null).invoke(authenticatedInfoTemplate, (Object[]) null);
                    } catch (NoSuchMethodException e) {
                        throw new AuthenticateStoreException(e);
                    } catch (IllegalAccessException e) {
                        throw new AuthenticateStoreException(e);
                    } catch (InvocationTargetException e) {
                        throw new AuthenticateStoreException(e);
                    }
                }
                authenticatedInfo = persistentManager.loadQuery(con, selectQueryOnFindUser, createInput(request, null, authenticatedKey), authenticatedInfo);
            }
        }
        if (authenticatedInfo != null && updateQueryOnActivate != null) {
            if (request.getSession(false) == null) {
                request.getSession(true);
            }
            persistentManager.persistQuery(con, updateQueryOnActivate, createInput(request, null, authenticatedInfo));
        }
        return authenticatedInfo;
    } catch (PersistentException e) {
        throw new AuthenticateStoreException(e);
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
        }
    }
}
Also used : PersistentException(jp.ossc.nimbus.service.connection.PersistentException) DataSet(jp.ossc.nimbus.beans.dataset.DataSet) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ConnectionFactoryException(jp.ossc.nimbus.service.connection.ConnectionFactoryException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) RecordList(jp.ossc.nimbus.beans.dataset.RecordList) List(java.util.List) Record(jp.ossc.nimbus.beans.dataset.Record)

Example 9 with ConnectionFactoryException

use of jp.ossc.nimbus.service.connection.ConnectionFactoryException in project nimbus by nimbus-org.

the class DatabaseScheduleStateManagerService method getState.

// ScheduleStateManagerのJavaDoc
public int getState(String name) {
    Connection con = null;
    PreparedStatement selectPs = null;
    ResultSet rs = null;
    try {
        con = connectionFactory.getConnection();
        selectPs = con.prepareStatement(scheduleStateSelectQuery);
        selectPs.setString(1, name);
        rs = selectPs.executeQuery();
        if (rs.next()) {
            int state = rs.getInt(1);
            if (rs.wasNull()) {
                state = STATE_UNKNOWN;
            }
            return state;
        } else {
            return STATE_UNKNOWN;
        }
    } catch (ConnectionFactoryException e) {
        getLogger().write(MSG_ID_00002, name, e);
        return STATE_UNKNOWN;
    } catch (SQLException e) {
        getLogger().write(MSG_ID_00002, name, e);
        return STATE_UNKNOWN;
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (selectPs != null) {
            try {
                selectPs.close();
            } catch (SQLException e) {
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ConnectionFactoryException(jp.ossc.nimbus.service.connection.ConnectionFactoryException)

Example 10 with ConnectionFactoryException

use of jp.ossc.nimbus.service.connection.ConnectionFactoryException in project nimbus by nimbus-org.

the class DatabaseScheduleStateManagerService method changeState.

// ScheduleStateManagerのJavaDoc
public void changeState(String name, int state) {
    Connection con = null;
    PreparedStatement selectPs = null;
    PreparedStatement updatePs = null;
    PreparedStatement insertPs = null;
    ResultSet rs = null;
    try {
        con = connectionFactory.getConnection();
        selectPs = con.prepareStatement(scheduleStateSelectQuery);
        selectPs.setString(1, name);
        rs = selectPs.executeQuery();
        if (rs.next()) {
            int oldState = rs.getInt(1);
            if (rs.wasNull()) {
                oldState = STATE_UNKNOWN;
            }
            if (state != oldState) {
                updatePs = con.prepareStatement(scheduleStateUpdateQuery);
                updatePs.setInt(1, state);
                updatePs.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
                updatePs.setString(3, name);
                updatePs.executeUpdate();
            }
        } else {
            insertPs = con.prepareStatement(scheduleStateInsertQuery);
            insertPs.setString(1, name);
            insertPs.setInt(2, state);
            insertPs.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
            insertPs.executeUpdate();
        }
    } catch (ConnectionFactoryException e) {
        getLogger().write(MSG_ID_00001, new Object[] { name, new Integer(state) }, e);
    } catch (SQLException e) {
        getLogger().write(MSG_ID_00001, new Object[] { name, new Integer(state) }, e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (selectPs != null) {
            try {
                selectPs.close();
            } catch (SQLException e) {
            }
        }
        if (updatePs != null) {
            try {
                updatePs.close();
            } catch (SQLException e) {
            }
        }
        if (insertPs != null) {
            try {
                insertPs.close();
            } catch (SQLException e) {
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) ConnectionFactoryException(jp.ossc.nimbus.service.connection.ConnectionFactoryException)

Aggregations

ConnectionFactoryException (jp.ossc.nimbus.service.connection.ConnectionFactoryException)10 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)7 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)3 List (java.util.List)3 RecordList (jp.ossc.nimbus.beans.dataset.RecordList)3 PersistentException (jp.ossc.nimbus.service.connection.PersistentException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Timestamp (java.sql.Timestamp)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Iterator (java.util.Iterator)2 Record (jp.ossc.nimbus.beans.dataset.Record)2 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DataSet (jp.ossc.nimbus.beans.dataset.DataSet)1