2010-10-01 21:52:51 +02:00
|
|
|
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;
|
|
|
|
|
2012-06-12 12:56:58 +02:00
|
|
|
import de.hsadmin.core.model.AbstractEntity;
|
2010-10-01 21:52:51 +02:00
|
|
|
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 {
|
|
|
|
|
2010-10-04 19:44:49 +02:00
|
|
|
protected abstract Class<? extends AbstractEntity> getEntityClass();
|
2010-10-01 21:52:51 +02:00
|
|
|
|
2015-09-11 17:26:42 +02:00
|
|
|
protected Class<? extends AbstractEntity> getAnnotatedEntityClass() {
|
|
|
|
return getEntityClass();
|
|
|
|
}
|
|
|
|
|
2013-04-29 20:01:09 +02:00
|
|
|
protected abstract void entity2map(Transaction tx, AbstractEntity entity, Map<String, Object> resultMap);
|
2014-12-04 16:03:37 +01:00
|
|
|
|
2013-04-29 20:01:09 +02:00
|
|
|
protected abstract void map2entity(Transaction tx, Map<String, Object> setParams, AbstractEntity entity) throws HSAdminException;
|
2010-10-01 21:52:51 +02:00
|
|
|
|
|
|
|
protected abstract void regularizeKeys(Map<String, String> whereParams);
|
2014-12-04 15:38:52 +01:00
|
|
|
|
2014-12-04 16:03:37 +01:00
|
|
|
|
2014-12-04 15:38:52 +01:00
|
|
|
public List<Map<String, Object>> search(String runAsUser, String ticket) throws HSAdminException {
|
|
|
|
return search(runAsUser, ticket, new HashMap<String, String>());
|
|
|
|
}
|
2014-12-04 16:03:37 +01:00
|
|
|
|
2011-10-28 16:08:18 +02:00
|
|
|
public List<Map<String, Object>> search(String runAsUser, String ticket,
|
2010-10-01 21:52:51 +02:00
|
|
|
Map<String, String> whereParams) throws HSAdminException {
|
2014-12-08 19:47:10 +01:00
|
|
|
final String user = runAsUser;
|
|
|
|
final Transaction transaction = new Transaction(user);
|
2010-10-01 21:52:51 +02:00
|
|
|
try {
|
2012-06-12 12:56:58 +02:00
|
|
|
if (transaction.login(user, ticket)) {
|
2014-12-08 19:47:10 +01:00
|
|
|
final ModuleInterface module = new GenericModuleImpl(transaction);
|
|
|
|
final UnixUser unixUser = transaction.getLoginUser();
|
|
|
|
final List<AbstractEntity> list = module.search(getEntityClass(),
|
2010-10-01 21:52:51 +02:00
|
|
|
buildQueryCondition(whereParams), null);
|
2010-10-04 19:44:49 +02:00
|
|
|
if (list == null) {
|
|
|
|
throw new HSAdminException("result list is null, runtime-error?");
|
|
|
|
}
|
2014-12-08 19:47:10 +01:00
|
|
|
final ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
2010-10-04 19:44:49 +02:00
|
|
|
for (AbstractEntity e : list) {
|
2014-12-08 19:47:10 +01:00
|
|
|
final HashMap<String, Object> entry = new HashMap<String, Object>();
|
2013-04-29 20:01:09 +02:00
|
|
|
entity2map(transaction, e, entry);
|
2010-10-01 21:52:51 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-28 16:08:18 +02:00
|
|
|
public Map<String, Object> add(String runAsUser, String ticket,
|
|
|
|
Map<String, Object> setParams) throws HSAdminException {
|
2014-12-08 19:47:10 +01:00
|
|
|
final String user = runAsUser;
|
|
|
|
final Transaction transaction = new Transaction(user);
|
2010-10-01 21:52:51 +02:00
|
|
|
try {
|
2012-06-12 12:56:58 +02:00
|
|
|
if (transaction.login(user, ticket)) {
|
2014-12-08 19:47:10 +01:00
|
|
|
final ModuleInterface module = new GenericModuleImpl(transaction);
|
|
|
|
final Constructor<? extends AbstractEntity> constructor =
|
2010-10-01 21:52:51 +02:00
|
|
|
getEntityClass().getConstructor();
|
2014-12-08 19:47:10 +01:00
|
|
|
final AbstractEntity entity = constructor.newInstance();
|
2013-01-21 13:42:07 +01:00
|
|
|
module.initialize(entity);
|
2013-04-29 20:01:09 +02:00
|
|
|
map2entity(transaction, setParams, entity);
|
2010-10-01 21:52:51 +02:00
|
|
|
transaction.beginTransaction();
|
2014-12-08 19:47:10 +01:00
|
|
|
final AbstractEntity insertedEntity = module.add(entity);
|
2010-10-01 21:52:51 +02:00
|
|
|
transaction.commitTransaction();
|
2014-12-08 19:47:10 +01:00
|
|
|
final HashMap<String, Object> entry = new HashMap<String, Object>();
|
2013-04-29 20:01:09 +02:00
|
|
|
entity2map(transaction, insertedEntity, entry);
|
2010-10-01 21:52:51 +02:00
|
|
|
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<String, String> whereParams) throws HSAdminException {
|
2014-12-08 19:47:10 +01:00
|
|
|
final String user = runAsUser;
|
|
|
|
final Transaction transaction = new Transaction(user);
|
2010-10-01 21:52:51 +02:00
|
|
|
try {
|
2012-06-12 12:56:58 +02:00
|
|
|
if (transaction.login(user, ticket)) {
|
2014-12-08 19:47:10 +01:00
|
|
|
final ModuleInterface module = new GenericModuleImpl(transaction);
|
|
|
|
final UnixUser unixUser = transaction.getLoginUser();
|
|
|
|
final String queryCondition = buildQueryCondition(whereParams);
|
2010-10-01 21:52:51 +02:00
|
|
|
if (queryCondition == null || queryCondition.length() == 0) {
|
|
|
|
throw new HSAdminException(
|
|
|
|
"better safe than sorry: no where parameter found");
|
|
|
|
}
|
2014-12-08 19:47:10 +01:00
|
|
|
final List<AbstractEntity> list = module.search(getEntityClass(),
|
2010-10-01 21:52:51 +02:00
|
|
|
queryCondition, null);
|
|
|
|
transaction.beginTransaction();
|
2010-10-04 19:44:49 +02:00
|
|
|
for (AbstractEntity e : list) {
|
2010-10-01 21:52:51 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-28 16:08:18 +02:00
|
|
|
public List<Map<String, Object>> update(String runAsUser, String ticket,
|
|
|
|
Map<String, Object> setParams, Map<String, String> whereParams)
|
2010-10-01 21:52:51 +02:00
|
|
|
throws HSAdminException {
|
2014-12-08 19:47:10 +01:00
|
|
|
final String user = runAsUser;
|
|
|
|
final Transaction transaction = new Transaction(user);
|
2010-10-01 21:52:51 +02:00
|
|
|
try {
|
2012-06-12 12:56:58 +02:00
|
|
|
if (transaction.login(user, ticket)) {
|
2014-12-08 19:47:10 +01:00
|
|
|
final ModuleInterface module = new GenericModuleImpl(transaction);
|
|
|
|
final UnixUser unixUser = transaction.getLoginUser();
|
|
|
|
final ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
|
|
|
final String queryCondition = buildQueryCondition(whereParams);
|
2010-10-01 21:52:51 +02:00
|
|
|
if (queryCondition == null || queryCondition.length() == 0) {
|
|
|
|
throw new HSAdminException(
|
|
|
|
"better safe than sorry: no where parameter found");
|
|
|
|
}
|
2014-12-08 19:47:10 +01:00
|
|
|
final List<AbstractEntity> list = module.search(getEntityClass(),
|
2010-10-05 21:42:07 +02:00
|
|
|
queryCondition, getOrderBy());
|
2010-10-01 21:52:51 +02:00
|
|
|
transaction.beginTransaction();
|
2010-10-04 19:44:49 +02:00
|
|
|
for (AbstractEntity update : list) {
|
2010-10-01 21:52:51 +02:00
|
|
|
if (update.isWriteAllowedFor(unixUser)) {
|
|
|
|
transaction.detach(update);
|
2013-04-29 20:01:09 +02:00
|
|
|
map2entity(transaction, setParams, update);
|
2010-10-01 21:52:51 +02:00
|
|
|
update = module.update(update);
|
2014-12-08 19:47:10 +01:00
|
|
|
final HashMap<String, Object> entry = new HashMap<String, Object>();
|
2013-04-29 20:01:09 +02:00
|
|
|
entity2map(transaction, update, entry);
|
2010-10-01 21:52:51 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 18:25:15 +02:00
|
|
|
protected String getOrderBy() {
|
2010-10-05 21:42:07 +02:00
|
|
|
return "ORDER BY obj.name ASC";
|
|
|
|
}
|
|
|
|
|
2010-10-01 21:52:51 +02:00
|
|
|
protected boolean assertNotNull(String string) {
|
|
|
|
return string != null && string.length() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean assertNotNull(Integer integ) {
|
|
|
|
return integ != null;
|
|
|
|
}
|
|
|
|
|
2011-03-25 17:47:54 +01:00
|
|
|
protected void replaceKey(Map<String, String> whereParams, String shortKey, String regularKey) {
|
|
|
|
if (whereParams.containsKey(shortKey)) {
|
2014-12-08 19:47:10 +01:00
|
|
|
final String value = whereParams.get(shortKey);
|
2011-03-25 17:47:54 +01:00
|
|
|
whereParams.remove(shortKey);
|
|
|
|
whereParams.put(regularKey, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean assertNotNull(Date aDate) {
|
|
|
|
return aDate != null;
|
|
|
|
}
|
|
|
|
|
2010-10-01 21:52:51 +02:00
|
|
|
private String buildQueryCondition(Map<String, String> whereParams) {
|
|
|
|
regularizeKeys(whereParams);
|
2014-12-08 19:47:10 +01:00
|
|
|
final StringBuffer cond = new StringBuffer();
|
|
|
|
final Iterator<String> keyIterator = whereParams.keySet().iterator();
|
2010-10-01 21:52:51 +02:00
|
|
|
while (keyIterator.hasNext()) {
|
|
|
|
if (cond.length() > 0) {
|
|
|
|
cond.append(" AND ");
|
|
|
|
}
|
2014-12-08 19:47:10 +01:00
|
|
|
final String field = keyIterator.next();
|
|
|
|
final String value = whereParams.get(field).replaceAll("'", "\'");
|
2010-10-01 21:52:51 +02:00
|
|
|
cond.append("obj.");
|
|
|
|
cond.append(field);
|
2010-12-17 18:39:53 +01:00
|
|
|
cond.append(" = ");
|
2014-12-08 19:47:10 +01:00
|
|
|
final boolean numeric = "id".equals(field);
|
2010-12-17 18:39:53 +01:00
|
|
|
if (!numeric) cond.append("'");
|
2010-10-01 21:52:51 +02:00
|
|
|
cond.append(value);
|
2010-12-17 18:39:53 +01:00
|
|
|
if (!numeric) cond.append("'");
|
2010-10-01 21:52:51 +02:00
|
|
|
}
|
|
|
|
return cond.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|