From f3b725d0d8b5f618ac5577a0349b148768ef7be2 Mon Sep 17 00:00:00 2001
From: Purodha <bli00@hostsharing.net>
Date: Thu, 03 Jan 2013 20:55:19 +0100
Subject: [PATCH] Module added: moduleprop - per module list of used fields taken from annotations (begun)

---
 hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties |    1 
 hsarback/src/de/hsadmin/remote/ModulePropertiesRemote.java        |  108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+), 0 deletions(-)

diff --git a/hsarback/src/de/hsadmin/remote/ModulePropertiesRemote.java b/hsarback/src/de/hsadmin/remote/ModulePropertiesRemote.java
new file mode 100644
index 0000000..5aed461
--- /dev/null
+++ b/hsarback/src/de/hsadmin/remote/ModulePropertiesRemote.java
@@ -0,0 +1,108 @@
+package de.hsadmin.remote;
+
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import de.hsadmin.core.model.AbstractEntity;
+import de.hsadmin.core.model.AnnFieldIO;
+import de.hsadmin.core.model.AuthenticationException;
+import de.hsadmin.core.model.HSAdminException;
+import de.hsadmin.core.model.ReadWriteAccess;
+import de.hsadmin.core.model.Transaction;
+
+public class ModulePropertiesRemote implements IRemote {
+
+	@Override
+	public List<Map<String, Object>> search(String runAsUser, String ticket,
+			Map<String, String> whereParams) throws HSAdminException {
+		String user = runAsUser;
+		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
+		Transaction transaction = new Transaction(user);
+		try {
+			if (transaction.login(user, ticket)) {
+				InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("org/apache/xmlrpc/webserver/XmlRpcServlet.properties");
+				Properties properties = new Properties();
+				properties.load(resourceAsStream);
+				
+				for (Object moduleKey : properties.entrySet()) {
+					Class<?> remoteClass = Class.forName(properties.getProperty((String) moduleKey));
+					Object newInstance = remoteClass.newInstance();
+					if (newInstance instanceof AbstractRemote) {
+						Map<String,Object> modMap = new HashMap<String, Object>();
+						modMap.put("module", moduleKey);
+						AbstractRemote remote = (AbstractRemote) newInstance;
+						Class<? extends AbstractEntity> entityClass = remote.getEntityClass();
+						ArrayList<Map<String, Object>> propertiesList = new ArrayList<Map<String,Object>>();
+						modMap.put("properties", propertiesList);
+//						AnnFieldIO fieldIO = entityClass.getAnnotation(AnnFieldIO.class);
+						for (Field f: entityClass.getDeclaredFields()) {
+							HashMap<String, Object> propertyProperties = new HashMap<String, Object>();
+							propertiesList.add(propertyProperties);
+							AnnFieldIO fieldIO = f.getAnnotation(AnnFieldIO.class);
+							String fieldName = f.getName();
+							propertyProperties.put("property", fieldName);
+							String regExp = fieldIO.validation();
+							propertyProperties.put("validation", regExp);
+							ReadWriteAccess rw = fieldIO.rw();
+							propertyProperties.put("rw", rw.name());
+							String[] referredProps = fieldIO.referredProps();
+							propertyProperties.put("referredProps", referredProps);							
+							Class<?> declaringClass = f.getDeclaringClass();
+							for(String s : referredProps){					
+								String fullPropertyName = fieldName+"."+s;			
+								propertiesList.add(createReferredPropertyInfo(fullPropertyName, declaringClass, fullPropertyName));
+							}
+						}											
+						result.add(modMap);
+					}
+				}
+				return result;
+			} else {
+				throw new AuthenticationException("authentication failed");
+			}
+		} catch (Exception e) {
+			throw new HSAdminException(e);
+		} finally {
+			transaction.close();
+		}
+	}
+
+	private Map<String, Object> createReferredPropertyInfo(String fieldName, Class<?> declaringClass,
+			String propertyName) throws SecurityException, NoSuchFieldException {
+		int indexOfDot = propertyName.indexOf('.');
+		if (indexOfDot>=0) {
+			String prefix = propertyName.substring(0, indexOfDot);
+			String remainingPart = propertyName.substring(indexOfDot + 1);
+			return createReferredPropertyInfo(fieldName, declaringClass.getDeclaredField(prefix).getDeclaringClass(), remainingPart);
+		} else {
+			Map<String, Object> result = new HashMap<String, Object>();
+			result.put("property", fieldName);
+			return result;
+		}
+	}
+
+	@Override
+	public Map<String, Object> add(String runAsUser, String ticket,
+			Map<String, Object> setParams) throws HSAdminException {
+		throw new HSAdminException("not implemented");
+	}
+
+	@Override
+	public List<Map<String, Object>> update(String runAsUser, String ticket,
+			Map<String, Object> setParams, Map<String, String> whereParams)
+			throws HSAdminException {
+		throw new HSAdminException("not implemented");
+	}
+
+	@Override
+	public void delete(String runAsUser, String ticket,
+			Map<String, String> whereParams) throws HSAdminException {
+		throw new HSAdminException("not implemented");
+	}
+
+}
diff --git a/hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties b/hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties
index 7cdc32f..6cc440a 100644
--- a/hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties
+++ b/hsarback/src/org/apache/xmlrpc/webserver/XmlRpcServlet.properties
@@ -10,3 +10,4 @@
 postgresqluser=de.hsadmin.remote.PgsqlUserRemote
 mysqldb=de.hsadmin.remote.MysqlDbRemote
 postgresqldb=de.hsadmin.remote.PgsqlDbRemote
+moduleprop=de.hsadmin.remote.ModulePropertiesRemote

--
Gitblit v1.9.0-SNAPSHOT