etwas zurueck...

This commit is contained in:
Peter Hormanns 2011-08-12 15:58:20 +00:00
parent 888b109f9e
commit f86c4952e1
5 changed files with 31 additions and 44 deletions

View File

@ -1,10 +1,5 @@
package de.hsadmin.core.model;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.persistence.EntityManager;
import de.hsadmin.mods.user.UnixUser;
@ -93,8 +88,7 @@ public abstract class AbstractEntity {
try {
method = entityClass.getDeclaredMethod("restriction");
} catch (SecurityException e) {
throw new RuntimeException(
"unexpected SecurityException in restriction()");
throw new RuntimeException("unexpected SecurityException in restriction()");
} catch (NoSuchMethodException e) {
// don't grant any access
return "FALSE";
@ -105,8 +99,7 @@ public abstract class AbstractEntity {
try {
restriction = (String) method.invoke(null);
} catch (Exception e) {
throw new RuntimeException(
"unexpected Exception in Entity-restriction() implementation");
throw new RuntimeException("unexpected Exception in Entity-restriction() implementation");
}
return restriction;
}
@ -223,27 +216,4 @@ public abstract class AbstractEntity {
*/
public abstract UnixUser owningUser(EntityManager em);
/**
* Clonable::clone did result in javasisst errors (duplicate method) since
* JBoss 5.1.
* This is just for testing, it does not use the proper class loader!
*/
public <T extends AbstractEntity> T replicate() {
try {
ByteArrayOutputStream baOut = new ByteArrayOutputStream();
ObjectOutputStream oOut = new ObjectOutputStream(baOut);
oOut.writeObject(this);
byte[] buffer = baOut.toByteArray();
ByteArrayInputStream baIn = new ByteArrayInputStream(buffer);
ObjectInputStream oIn = new ObjectInputStream(baIn);
@SuppressWarnings("unchecked")
T clone = (T) oIn.readObject();
return clone;
} catch (Exception exc) {
throw new RuntimeException(exc);
}
}
}

View File

@ -108,9 +108,9 @@ public abstract class AbstractModuleImpl implements ModuleInterface {
EntityManager entityManager = transaction.getEntityManager();
entityManager.clear();
Query query = entityManager.createQuery(queryString);
query.setParameter("loginUser", loginUser);
query.setParameter("loginUserName", loginUser.getName());
query.setParameter("loginUserPac", loginUser.getPac());
setQueryParameter(query, queryString, "loginUser", loginUser);
setQueryParameter(query, queryString, "loginUserName", loginUser.getName());
setQueryParameter(query, queryString, "loginUserPac", loginUser.getPac());
try {
List<?> res = query.getResultList();
List<AbstractEntity> ret = new LinkedList<AbstractEntity>();
@ -209,4 +209,18 @@ public abstract class AbstractModuleImpl implements ModuleInterface {
condition = restriction;
return condition;
}
public static void setQueryParameter(Query query, String queryString,
String argName, Object argValue) {
int argLen = argName.length();
int iMax = queryString.length();
int i = 0;
while ((i = queryString.indexOf(argName, i)) >= 0) {
if ((i + argLen) >= iMax || queryString.charAt(i + argLen) < 'A') {
query.setParameter(argName, argValue);
break;
}
++i;
}
}
}

View File

@ -22,6 +22,7 @@ import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.AbstractModuleImpl;
import de.hsadmin.core.model.FieldValidation;
import de.hsadmin.core.model.SearchFilter;
import de.hsadmin.mods.pac.Pac;
@ -91,9 +92,9 @@ public abstract class Database extends AbstractEntity implements Serializable {
String queryString = "SELECT obj FROM " + entityAnnot.name() + " obj WHERE "
+ Pac.createQueryFromStringKey(pacName);
Query query = em.createQuery(queryString);
query.setParameter("loginUser", loginUser);
query.setParameter("loginUserName", loginUser.getName());
query.setParameter("loginUserPac", loginUser.getPac());
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUser", loginUser);
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUserName", loginUser.getName());
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUserPac", loginUser.getPac());
pac = (Pac) query.getSingleResult();
} catch (NoResultException exc) {
throw new SecurityException("packet '" + pacName + "' not found or access denied");

View File

@ -23,6 +23,7 @@ import javax.persistence.Table;
import javax.persistence.Transient;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.AbstractModuleImpl;
import de.hsadmin.core.model.FieldValidation;
import de.hsadmin.mods.pac.Pac;
import de.hsadmin.mods.user.UnixUser;
@ -86,9 +87,9 @@ public abstract class DatabaseUser extends AbstractEntity implements Serializabl
// set parameters
Query query = em.createQuery(queryString);
query.setParameter("loginUser", loginUser);
query.setParameter("loginUserName", loginUser.getName());
query.setParameter("loginUserPac", loginUser.getPac());
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUser", loginUser);
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUserName", loginUser.getName());
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUserPac", loginUser.getPac());
pac = (Pac) query.getSingleResult();
} catch (NoResultException exc) {
throw new SecurityException("packet '" + pacName

View File

@ -17,6 +17,7 @@ import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.AbstractModuleImpl;
import de.hsadmin.core.model.EntityInfo;
import de.hsadmin.core.model.FieldValidation;
import de.hsadmin.core.model.ModuleImpl;
@ -79,9 +80,9 @@ public class EMailAlias extends AbstractEntity implements Serializable {
Entity entityAnnot = Pac.class.getAnnotation(Entity.class);
String queryString = "SELECT obj FROM " + entityAnnot.name() + " obj WHERE " + Pac.createQueryFromStringKey(pacName);
Query query = em.createQuery(queryString);
query.setParameter("loginUser", loginUser);
query.setParameter("loginUserName", loginUser.getName());
query.setParameter("loginUserPac", loginUser.getPac());
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUser", loginUser);
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUserName", loginUser.getName());
AbstractModuleImpl.setQueryParameter(query, queryString, "loginUserPac", loginUser.getPac());
pac = (Pac) query.getSingleResult();
} catch (NoResultException exc) {
throw new SecurityException("packet '" + pacName + "' not found or access denied");