Search in sources :

Example 1 with ValidateException

use of jp.ossc.nimbus.util.validator.ValidateException 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)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Record (jp.ossc.nimbus.beans.dataset.Record)1 RecordList (jp.ossc.nimbus.beans.dataset.RecordList)1 RecordSet (jp.ossc.nimbus.recset.RecordSet)1 ConnectionFactoryException (jp.ossc.nimbus.service.connection.ConnectionFactoryException)1 PersistentException (jp.ossc.nimbus.service.connection.PersistentException)1 ValidateException (jp.ossc.nimbus.util.validator.ValidateException)1