hs.hsadmin/hsarback/src/de/hsadmin/remote/AbstractRemote.java

230 lines
7.4 KiB
Java
Raw Normal View History

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;
import de.hsadmin.core.model.AuthenticationException;
import de.hsadmin.core.model.AuthorisationException;
import de.hsadmin.core.model.Entity;
import de.hsadmin.core.model.GenericModuleImpl;
import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.model.ModuleInterface;
import de.hsadmin.core.model.TicketAuthentication;
import de.hsadmin.core.model.Transaction;
import de.hsadmin.mods.user.UnixUser;
public abstract class AbstractRemote implements IRemote {
private TicketAuthentication authentication;
public AbstractRemote() {
authentication = new TicketAuthentication();
}
protected abstract Class<? extends Entity> getEntityClass();
protected abstract void entity2map(Entity entity, Map<String, String> resultMap);
protected abstract void map2entity(Map<String, String> setParams, Entity entity);
protected abstract void regularizeKeys(Map<String, String> whereParams);
/* (non-Javadoc)
* @see de.hsadmin.remote.IRemote#search(java.lang.String, java.lang.String, java.util.Map)
*/
public List<Map<String, String>> search(String runAsUser, String ticket,
Map<String, String> whereParams) throws HSAdminException {
String user = runAsUser;
Transaction transaction = new Transaction(user);
try {
if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null;
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
List<Entity> list = module.search(getEntityClass(),
buildQueryCondition(whereParams), null);
ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
for (Entity e : list) {
HashMap<String, String> entry = new HashMap<String, String>();
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();
}
}
/* (non-Javadoc)
* @see de.hsadmin.remote.IRemote#add(java.lang.String, java.lang.String, java.util.Map)
*/
public Map<String, String> add(String runAsUser, String ticket,
Map<String, String> setParams) throws HSAdminException {
String user = runAsUser;
Transaction transaction = new Transaction(user);
try {
if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction);
Constructor<? extends Entity> constructor =
getEntityClass().getConstructor();
Entity entity = constructor.newInstance();
map2entity(setParams, entity);
transaction.beginTransaction();
Entity insertedEntity = module.add(entity);
transaction.commitTransaction();
HashMap<String, String> entry = new HashMap<String, String>();
entity2map(insertedEntity, entry);
return entry;
} else {
throw new AuthenticationException("authentication failed");
}
} catch (Exception e) {
throw new HSAdminException(e);
} finally {
transaction.close();
}
}
/* (non-Javadoc)
* @see de.hsadmin.remote.IRemote#delete(java.lang.String, java.lang.String, java.util.Map)
*/
public void delete(String runAsUser, String ticket,
Map<String, String> whereParams) throws HSAdminException {
String user = runAsUser;
Transaction transaction = new Transaction(user);
try {
if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null;
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) {
throw new HSAdminException(
"better safe than sorry: no where parameter found");
}
List<Entity> list = module.search(getEntityClass(),
queryCondition, null);
transaction.beginTransaction();
for (Entity 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();
}
}
/* (non-Javadoc)
* @see de.hsadmin.remote.IRemote#update(java.lang.String, java.lang.String, java.util.Map, java.util.Map)
*/
public List<Map<String, String>> update(String runAsUser, String ticket,
Map<String, String> setParams, Map<String, String> whereParams)
throws HSAdminException {
String user = runAsUser;
Transaction transaction = new Transaction(user);
try {
if (authentication.login(user, ticket)) {
ModuleInterface module = new GenericModuleImpl(transaction);
UnixUser unixUser = null;
unixUser = (UnixUser) module.findByString(UnixUser.class, user);
ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();
String queryCondition = buildQueryCondition(whereParams);
if (queryCondition == null || queryCondition.length() == 0) {
throw new HSAdminException(
"better safe than sorry: no where parameter found");
}
List<Entity> list = module.search(getEntityClass(),
queryCondition, "ORDER BY name ASC");
transaction.beginTransaction();
for (Entity update : list) {
if (update.isWriteAllowedFor(unixUser)) {
transaction.detach(update);
map2entity(setParams, update);
update = module.update(update);
HashMap<String, String> entry = new HashMap<String, String>();
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();
}
}
protected boolean assertNotNull(String string) {
return string != null && string.length() > 0;
}
protected boolean assertNotNull(Integer integ) {
return integ != null;
}
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);
cond.append(" = '");
cond.append(value);
cond.append("'");
}
return cond.toString();
}
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;
}
}