From f86c4952e13578865f5426b41f7fe99121eb5684 Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@hostsharing.net>
Date: Fri, 12 Aug 2011 17:58:20 +0200
Subject: [PATCH] etwas zurueck...

---
 hsarback/src/de/hsadmin/mods/db/DatabaseUser.java          |    7 ++-
 hsarback/src/de/hsadmin/mods/email/EMailAlias.java         |    7 ++-
 hsarback/src/de/hsadmin/mods/db/Database.java              |    7 ++-
 hsarback/src/de/hsadmin/core/model/AbstractEntity.java     |   34 +----------------
 hsarback/src/de/hsadmin/core/model/AbstractModuleImpl.java |   20 ++++++++-
 5 files changed, 31 insertions(+), 44 deletions(-)

diff --git a/hsarback/src/de/hsadmin/core/model/AbstractEntity.java b/hsarback/src/de/hsadmin/core/model/AbstractEntity.java
index 08fadbe..fac726d 100644
--- a/hsarback/src/de/hsadmin/core/model/AbstractEntity.java
+++ b/hsarback/src/de/hsadmin/core/model/AbstractEntity.java
@@ -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 @@
 		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 @@
 		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;
 	}
@@ -222,28 +215,5 @@
 	 * @return
 	 */
 	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);
-		}
-	}
 
 }
diff --git a/hsarback/src/de/hsadmin/core/model/AbstractModuleImpl.java b/hsarback/src/de/hsadmin/core/model/AbstractModuleImpl.java
index 279898b..3a03f08 100644
--- a/hsarback/src/de/hsadmin/core/model/AbstractModuleImpl.java
+++ b/hsarback/src/de/hsadmin/core/model/AbstractModuleImpl.java
@@ -108,9 +108,9 @@
 		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 @@
 			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;
+		}
+	}
 }
diff --git a/hsarback/src/de/hsadmin/mods/db/Database.java b/hsarback/src/de/hsadmin/mods/db/Database.java
index 8645401..5c62998 100644
--- a/hsarback/src/de/hsadmin/mods/db/Database.java
+++ b/hsarback/src/de/hsadmin/mods/db/Database.java
@@ -22,6 +22,7 @@
 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 @@
 				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");
diff --git a/hsarback/src/de/hsadmin/mods/db/DatabaseUser.java b/hsarback/src/de/hsadmin/mods/db/DatabaseUser.java
index 0b8dd1c..def55bb 100644
--- a/hsarback/src/de/hsadmin/mods/db/DatabaseUser.java
+++ b/hsarback/src/de/hsadmin/mods/db/DatabaseUser.java
@@ -23,6 +23,7 @@
 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 @@
 
 				// 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
diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAlias.java b/hsarback/src/de/hsadmin/mods/email/EMailAlias.java
index 5678a4e..87b60f7 100644
--- a/hsarback/src/de/hsadmin/mods/email/EMailAlias.java
+++ b/hsarback/src/de/hsadmin/mods/email/EMailAlias.java
@@ -17,6 +17,7 @@
 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 @@
 				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");

--
Gitblit v1.9.1