From f97acf975c989e59f004724f428f9b0a55a2085f Mon Sep 17 00:00:00 2001
From: Peter Hormanns < <peter.hormanns@jalin.de>
Date: Wed, 31 Oct 2012 11:01:48 +0100
Subject: [PATCH] run-as selector depends on user-role

---
 hsarweb/src/de/hsadmin/web/AbstractModule.java |  102 +++++++++++++++++++++++++++-----------------------
 1 files changed, 55 insertions(+), 47 deletions(-)

diff --git a/hsarweb/src/de/hsadmin/web/AbstractModule.java b/hsarweb/src/de/hsadmin/web/AbstractModule.java
index 537f414..61b746d 100644
--- a/hsarweb/src/de/hsadmin/web/AbstractModule.java
+++ b/hsarweb/src/de/hsadmin/web/AbstractModule.java
@@ -54,49 +54,14 @@
 		if (this instanceof SearchAble || this instanceof InsertAble ||
 				!("USER".equals(application.getLoginUserRole()) || "NONE".equals(application.getLoginUserRole()))) {
 			HorizontalLayout toolbar = new HorizontalLayout();
-			if (!("USER".equals(application.getLoginUserRole()) || "NONE".equals(application.getLoginUserRole()))) {
-				selRunAs = new Select();
-				selRunAs.setWidth(100.0f, Sizeable.UNITS_PIXELS);
-				selRunAs.setImmediate(true);
-				selRunAs.setNewItemsAllowed(false);
-				selRunAs.setNullSelectionAllowed(false);
-				if (!application.getLoginUserRole().startsWith("PAC")) {
-//					if (application.getLoginUserRole().startsWith("PAC")) {
-//						
-//					}
-					selRunAs.addItem(application.getLogin());
-					Object custListObj = application.getRemote().callSearch("member", new HashMap<String, String>());
-					if (custListObj instanceof Object[]) {
-						Object[] custList = (Object[]) custListObj;
-						for (Object custObj : custList) {
-							if (custObj instanceof Map<?, ?>) {
-								Map<?, ?> custHash = (Map<?, ?>)custObj;
-								selRunAs.addItem(custHash.get("membercode"));
-							}
-						}
-					}
-				}
-				Object pacListObj = application.getRemote().callSearch("pac", new HashMap<String, String>());
-				if (pacListObj instanceof Object[]) {
-					Object[] pacList = (Object[]) pacListObj;
-					for (Object pacObj : pacList) {
-						if (pacObj instanceof Map<?, ?>) {
-							Map<?, ?> pacHash = (Map<?, ?>)pacObj;
-							selRunAs.addItem(pacHash.get("name"));
-						}
-					}
-				}
-				selRunAs.select(application.getRunAs());
-				selRunAs.setScrollToSelectedItem(true);
-				selRunAs.addListener(new Property.ValueChangeListener() {
-					private static final long serialVersionUID = 1L;
-					@Override
-					public void valueChange(ValueChangeEvent event) {
-						Property property = event.getProperty();
-						application.setRunAs(property.getValue().toString());
-					}
-				});
-				toolbar.addComponent(selRunAs);
+			if ("HOSTMASTER".equals(application.getLoginUserRole())) {
+				createRunAsSelect(toolbar, new SearchForSelector("member", "membercode"), new SearchForSelector("pac", "name"));
+			}
+			if ("CUSTOMER".equals(application.getLoginUserRole())) {
+				createRunAsSelect(toolbar, new SearchForSelector("pac", "name"));
+			}
+			if (application.getLoginUserRole().startsWith("PAC")) {
+				createRunAsSelect(toolbar, new SearchForSelector("user", "name"));
 			}
 			if (this instanceof InsertAble) {
 				Button btNew = new Button(moduleConfig.getLabel("new"));
@@ -150,14 +115,42 @@
 				});
 				toolbar.addComponent(btNew);
 			}
-//			if (this instanceof SearchAble) {
-//				Button btSearch = new Button("search");
-//				toolbar.addComponent(btSearch);
-//			}
 			layout.addComponent(toolbar);
 		}
 		layout.addComponent(component);
 		layout.setExpandRatio(component, 1.0f);
+	}
+
+	private void createRunAsSelect(HorizontalLayout toolbar, SearchForSelector... searchSelector) throws HsarwebException {
+		selRunAs = new Select();
+		selRunAs.setWidth(100.0f, Sizeable.UNITS_PIXELS);
+		selRunAs.setImmediate(true);
+		selRunAs.setNewItemsAllowed(false);
+		selRunAs.setNullSelectionAllowed(false);
+		selRunAs.addItem(application.getLogin());
+		for (SearchForSelector sel : searchSelector) {
+			Object custListObj = application.getRemote().callSearch(sel.getModuleName(), new HashMap<String, String>());
+			if (custListObj instanceof Object[]) {
+				Object[] custList = (Object[]) custListObj;
+				for (Object custObj : custList) {
+					if (custObj instanceof Map<?, ?>) {
+						Map<?, ?> custHash = (Map<?, ?>)custObj;
+						selRunAs.addItem(custHash.get(sel.getPropertyName()));
+					}
+				}
+			}
+		}
+		selRunAs.select(application.getRunAs());
+		selRunAs.setScrollToSelectedItem(true);
+		selRunAs.addListener(new Property.ValueChangeListener() {
+			private static final long serialVersionUID = 1L;
+			@Override
+			public void valueChange(ValueChangeEvent event) {
+				Property property = event.getProperty();
+				application.setRunAs(property.getValue().toString());
+			}
+		});
+		toolbar.addComponent(selRunAs);
 	}
 
 
@@ -184,4 +177,19 @@
 	@Override
 	public abstract ModuleConfig getModuleConfig();
 
+	private class SearchForSelector {
+		private final String moduleName;
+		private final String propertyName; 
+		public SearchForSelector(String module, String property) {
+			moduleName = module;
+			propertyName = property;
+		}
+		public String getModuleName() {
+			return moduleName;
+		}
+		public String getPropertyName() {
+			return propertyName;
+		}
+	}
+
 }

--
Gitblit v1.9.0-SNAPSHOT