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
|
|
|
|
2011-10-28 16:08:18 +02:00
|
|
|
protected abstract void entity2map(AbstractEntity entity, Map<String, Object> resultMap);
|
2010-10-01 21:52:51 +02:00
|
|
|
|
2012-07-27 18:02:24 +02:00
|
|
|
protected abstract void map2entity(Map<String, Object> setParams, AbstractEntity entity) throws HSAdminException;
|
2010-10-01 21:52:51 +02:00
|
|
|
|
|
|
|
protected abstract void regularizeKeys(Map<String, String> whereParams);
|
|
|
|
|
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 {
|
|
|
|
String user = runAsUser;
|
|
|
|
Transaction transaction = new Transaction(user);
|
|
|
|
try {
|
2012-06-12 12:56:58 +02:00
|
|
|
if (transaction.login(user, ticket)) {
|
2010-10-01 21:52:51 +02:00
|
|
|
ModuleInterface module = new GenericModuleImpl(transaction);
|
2011-03-25 17:47:54 +01:00
|
|
|
UnixUser unixUser = transaction.getLoginUser();
|
2010-10-04 19:44:49 +02:00
|
|
|
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?");
|
|
|
|
}
|
2011-10-28 16:08:18 +02:00
|
|
|
ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
2010-10-04 19:44:49 +02:00
|
|
|
for (AbstractEntity e : list) {
|
2011-10-28 16:08:18 +02:00
|
|
|
HashMap<String, Object> entry = new HashMap<String, Object>();
|
2010-10-01 21:52:51 +02:00
|
|
|
entity2map(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-28 16:08:18 +02:00
|
|
|
public Map<String, Object> add(String runAsUser, String ticket,
|
|
|
|
Map<String, Object> setParams) throws HSAdminException {
|
2010-10-01 21:52:51 +02:00
|
|
|
String user = runAsUser;
|
|
|
|
Transaction transaction = new Transaction(user);
|
|
|
|
try {
|
2012-06-12 12:56:58 +02:00
|
|
|
if (transaction.login(user, ticket)) {
|
2010-10-01 21:52:51 +02:00
|
|
|
ModuleInterface module = new GenericModuleImpl(transaction);
|
2010-10-04 19:44:49 +02:00
|
|
|
Constructor<? extends AbstractEntity> constructor =
|
2010-10-01 21:52:51 +02:00
|
|
|
getEntityClass().getConstructor();
|
2010-10-04 19:44:49 +02:00
|
|
|
AbstractEntity entity = constructor.newInstance();
|
2013-01-21 13:42:07 +01:00
|
|
|
module.initialize(entity);
|
2010-10-01 21:52:51 +02:00
|
|
|
map2entity(setParams, entity);
|
|
|
|
transaction.beginTransaction();
|
2010-10-04 19:44:49 +02:00
|
|
|
AbstractEntity insertedEntity = module.add(entity);
|
2010-10-01 21:52:51 +02:00
|
|
|
transaction.commitTransaction();
|
2011-10-28 16:08:18 +02:00
|
|
|
HashMap<String, Object> entry = new HashMap<String, Object>();
|
2010-10-01 21:52:51 +02:00
|
|
|
entity2map(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<String, String> whereParams) throws HSAdminException {
|
|
|
|
String user = runAsUser;
|
|
|
|
Transaction transaction = new Transaction(user);
|
|
|
|
try {
|
2012-06-12 12:56:58 +02:00
|
|
|
if (transaction.login(user, ticket)) {
|
2010-10-01 21:52:51 +02:00
|
|
|
ModuleInterface module = new GenericModuleImpl(transaction);
|
2011-03-25 17:47:54 +01:00
|
|
|
UnixUser unixUser = transaction.getLoginUser();
|
2010-10-01 21:52:51 +02:00
|
|
|
String queryCondition = buildQueryCondition(whereParams);
|
|
|
|
if (queryCondition == null || queryCondition.length() == 0) {
|
|
|
|
throw new HSAdminException(
|
|
|
|
"better safe than sorry: no where parameter found");
|
|
|
|
}
|
2010-10-04 19:44:49 +02:00
|
|
|
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 {
|
|
|
|
String user = runAsUser;
|
|
|
|
Transaction transaction = new Transaction(user);
|
|
|
|
try {
|
2012-06-12 12:56:58 +02:00
|
|
|
if (transaction.login(user, ticket)) {
|
2010-10-01 21:52:51 +02:00
|
|
|
ModuleInterface module = new GenericModuleImpl(transaction);
|
2011-03-25 17:47:54 +01:00
|
|
|
UnixUser unixUser = transaction.getLoginUser();
|
2011-10-28 16:08:18 +02:00
|
|
|
ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
2010-10-01 21:52:51 +02:00
|
|
|
String queryCondition = buildQueryCondition(whereParams);
|
|
|
|
if (queryCondition == null || queryCondition.length() == 0) {
|
|
|
|
throw new HSAdminException(
|
|
|
|
"better safe than sorry: no where parameter found");
|
|
|
|
}
|
2010-10-04 19:44:49 +02:00
|
|
|
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);
|
|
|
|
map2entity(setParams, update);
|
|
|
|
update = module.update(update);
|
2011-10-28 16:08:18 +02:00
|
|
|
HashMap<String, Object> entry = new HashMap<String, Object>();
|
2010-10-01 21:52:51 +02:00
|
|
|
entity2map(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
String value = whereParams.get(shortKey);
|
|
|
|
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);
|
|
|
|
StringBuffer cond = new StringBuffer();
|
|
|
|
Iterator<String> keyIterator = whereParams.keySet().iterator();
|
|
|
|
while (keyIterator.hasNext()) {
|
|
|
|
if (cond.length() > 0) {
|
|
|
|
cond.append(" AND ");
|
|
|
|
}
|
|
|
|
String field = keyIterator.next();
|
|
|
|
String value = whereParams.get(field).replaceAll("'", "\'");
|
|
|
|
cond.append("obj.");
|
|
|
|
cond.append(field);
|
2010-12-17 18:39:53 +01:00
|
|
|
cond.append(" = ");
|
|
|
|
boolean numeric = "id".equals(field);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|