package de.hsadmin.remote; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import de.hsadmin.core.model.AbstractEntity; import de.hsadmin.core.model.AuthenticationException; import de.hsadmin.core.model.AuthorisationException; import de.hsadmin.core.model.GenericModuleImpl; import de.hsadmin.core.model.HSAdminException; import de.hsadmin.core.model.ModuleInterface; import de.hsadmin.core.model.Transaction; import de.hsadmin.mods.user.UnixUser; public abstract class AbstractRemote implements IRemote { protected abstract Class getEntityClass(); protected abstract void entity2map(Transaction tx, AbstractEntity entity, Map resultMap); protected abstract void map2entity(Transaction tx, Map setParams, AbstractEntity entity) throws HSAdminException; protected abstract void regularizeKeys(Map whereParams); public List> search(String runAsUser, String ticket) throws HSAdminException { return search(runAsUser, ticket, new HashMap()); } public List> search(String runAsUser, String ticket, Map whereParams) throws HSAdminException { final String user = runAsUser; final Transaction transaction = new Transaction(user); try { if (transaction.login(user, ticket)) { final ModuleInterface module = new GenericModuleImpl(transaction); final UnixUser unixUser = transaction.getLoginUser(); final List list = module.search(getEntityClass(), buildQueryCondition(whereParams), null); if (list == null) { throw new HSAdminException("result list is null, runtime-error?"); } final ArrayList> result = new ArrayList>(); for (AbstractEntity e : list) { final HashMap entry = new HashMap(); entity2map(transaction, e, entry); if (e.isReadAllowedFor(unixUser)) { result.add(entry); } } return result; } else { throw new AuthenticationException("authentication failed"); } } catch (SecurityException e) { throw new HSAdminException(e); } catch (IllegalArgumentException e) { throw new HSAdminException(e); } finally { transaction.close(); } } public Map add(String runAsUser, String ticket, Map setParams) throws HSAdminException { final String user = runAsUser; final Transaction transaction = new Transaction(user); try { if (transaction.login(user, ticket)) { final ModuleInterface module = new GenericModuleImpl(transaction); final Constructor constructor = getEntityClass().getConstructor(); final AbstractEntity entity = constructor.newInstance(); module.initialize(entity); map2entity(transaction, setParams, entity); transaction.beginTransaction(); final AbstractEntity insertedEntity = module.add(entity); transaction.commitTransaction(); final HashMap entry = new HashMap(); entity2map(transaction, insertedEntity, entry); return entry; } else { throw new AuthenticationException("authentication failed"); } } catch (Exception e) { throw new HSAdminException(e); } finally { transaction.close(); } } public void delete(String runAsUser, String ticket, Map whereParams) throws HSAdminException { final String user = runAsUser; final Transaction transaction = new Transaction(user); try { if (transaction.login(user, ticket)) { final ModuleInterface module = new GenericModuleImpl(transaction); final UnixUser unixUser = transaction.getLoginUser(); final String queryCondition = buildQueryCondition(whereParams); if (queryCondition == null || queryCondition.length() == 0) { throw new HSAdminException( "better safe than sorry: no where parameter found"); } final List list = module.search(getEntityClass(), queryCondition, null); transaction.beginTransaction(); for (AbstractEntity e : list) { if (e.isWriteAllowedFor(unixUser)) { module.delete(e); } else { throw new AuthorisationException(unixUser, "delete", e); } } transaction.commitTransaction(); } else { throw new AuthenticationException("authentication failed"); } } catch (SecurityException e) { throw new HSAdminException(e); } catch (IllegalArgumentException e) { throw new HSAdminException(e); } finally { transaction.close(); } } public List> update(String runAsUser, String ticket, Map setParams, Map whereParams) throws HSAdminException { final String user = runAsUser; final Transaction transaction = new Transaction(user); try { if (transaction.login(user, ticket)) { final ModuleInterface module = new GenericModuleImpl(transaction); final UnixUser unixUser = transaction.getLoginUser(); final ArrayList> result = new ArrayList>(); final String queryCondition = buildQueryCondition(whereParams); if (queryCondition == null || queryCondition.length() == 0) { throw new HSAdminException( "better safe than sorry: no where parameter found"); } final List list = module.search(getEntityClass(), queryCondition, getOrderBy()); transaction.beginTransaction(); for (AbstractEntity update : list) { if (update.isWriteAllowedFor(unixUser)) { transaction.detach(update); map2entity(transaction, setParams, update); update = module.update(update); final HashMap entry = new HashMap(); entity2map(transaction, update, entry); result.add(entry); } else { throw new AuthorisationException(unixUser, "update", update); } } transaction.commitTransaction(); return result; } else { throw new AuthenticationException("authentication failed"); } } catch (SecurityException e) { throw new HSAdminException(e); } catch (IllegalArgumentException e) { throw new HSAdminException(e); } finally { transaction.close(); } } protected String getOrderBy() { return "ORDER BY obj.name ASC"; } protected boolean assertNotNull(String string) { return string != null && string.length() > 0; } protected boolean assertNotNull(Integer integ) { return integ != null; } protected void replaceKey(Map whereParams, String shortKey, String regularKey) { if (whereParams.containsKey(shortKey)) { final String value = whereParams.get(shortKey); whereParams.remove(shortKey); whereParams.put(regularKey, value); } } protected boolean assertNotNull(Date aDate) { return aDate != null; } private String buildQueryCondition(Map whereParams) { regularizeKeys(whereParams); final StringBuffer cond = new StringBuffer(); final Iterator keyIterator = whereParams.keySet().iterator(); while (keyIterator.hasNext()) { if (cond.length() > 0) { cond.append(" AND "); } final String field = keyIterator.next(); final String value = whereParams.get(field).replaceAll("'", "\'"); cond.append("obj."); cond.append(field); cond.append(" = "); final boolean numeric = "id".equals(field); if (!numeric) cond.append("'"); cond.append(value); if (!numeric) cond.append("'"); } return cond.toString(); } }