use of jp.ossc.nimbus.service.connection.PersistentException 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());
}
}
}
use of jp.ossc.nimbus.service.connection.PersistentException 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) {
}
}
}
use of jp.ossc.nimbus.service.connection.PersistentException 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) {
}
}
}
Aggregations