From f002ef41f26a1b4141eb610da046b6d66c5079e4 Mon Sep 17 00:00:00 2001
From: Peter Hormanns < <peter.hormanns@jalin.de>
Date: Fri, 02 Nov 2012 14:05:41 +0100
Subject: [PATCH] refactorings on run-as feature

---
 hsarweb/src/de/hsadmin/web/AbstractModule.java  |   49 +++---------------------
 hsarweb/src/de/hsadmin/web/MainApplication.java |   19 +++++++++
 hsarweb/src/texts/main.properties               |    2 
 hsarweb/src/de/hsadmin/web/ItemsReader.java     |   25 ++++++++++++
 hsarweb/src/texts/main_de.properties            |    2 
 5 files changed, 53 insertions(+), 44 deletions(-)

diff --git a/hsarweb/src/de/hsadmin/web/AbstractModule.java b/hsarweb/src/de/hsadmin/web/AbstractModule.java
index 4afe9d7..83c4962 100644
--- a/hsarweb/src/de/hsadmin/web/AbstractModule.java
+++ b/hsarweb/src/de/hsadmin/web/AbstractModule.java
@@ -9,6 +9,7 @@
 import com.vaadin.terminal.Sizeable;
 import com.vaadin.terminal.ThemeResource;
 import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.Form;
 import com.vaadin.ui.HorizontalLayout;
@@ -16,7 +17,6 @@
 import com.vaadin.ui.Select;
 import com.vaadin.ui.VerticalLayout;
 import com.vaadin.ui.Window;
-import com.vaadin.ui.Button.ClickEvent;
 
 import de.hsadmin.web.config.ComponentFactory;
 import de.hsadmin.web.config.LocaleConfig;
@@ -55,15 +55,7 @@
 		if (this instanceof SearchAble || this instanceof InsertAble ||
 				!("USER".equals(application.getLoginUserRole()) || "NONE".equals(application.getLoginUserRole()))) {
 			HorizontalLayout toolbar = new HorizontalLayout();
-			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"));
-			}
+			createRunAsSelect(toolbar);
 			if (this instanceof InsertAble) {
 				Button btNew = new Button(moduleConfig.getLabel("new"));
 				ThemeResource icon = new ThemeResource("../runo/icons/16/document-add.png");
@@ -122,24 +114,15 @@
 		layout.setExpandRatio(component, 1.0f);
 	}
 
-	private void createRunAsSelect(HorizontalLayout toolbar, SearchForSelector... searchSelector) throws HsarwebException {
+	private void createRunAsSelect(HorizontalLayout toolbar) throws UnsupportedOperationException, HsarwebException {
 		selRunAs = new Select();
 		selRunAs.setWidth(100.0f, Sizeable.UNITS_PIXELS);
 		selRunAs.setImmediate(true);
-		selRunAs.setNewItemsAllowed(false);
+		selRunAs.setNewItemsAllowed(true);
 		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()));
-					}
-				}
-			}
+		for (Object item : application.readSelectRunAsItems()) {
+			selRunAs.addItem(item);
 		}
 		selRunAs.select(application.getRunAs());
 		selRunAs.setScrollToSelectedItem(true);
@@ -151,12 +134,9 @@
 				application.setRunAs(property.getValue().toString());
 			}
 		});
-		Label lbl = new Label("&nbsp;" + application.getLocaleConfig().getText("runas") + "&nbsp;", Label.CONTENT_XHTML);
-		lbl.setSizeFull();
-		toolbar.addComponent(lbl);
+		selRunAs.setDescription(application.getLocaleConfig().getText("runas"));
 		toolbar.addComponent(selRunAs);
 	}
-
 
 	public void setApplication(MainApplication app) throws HsarwebException {
 		application = app;
@@ -180,20 +160,5 @@
 
 	@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;
-		}
-	}
 
 }
diff --git a/hsarweb/src/de/hsadmin/web/ItemsReader.java b/hsarweb/src/de/hsadmin/web/ItemsReader.java
new file mode 100644
index 0000000..95e22f2
--- /dev/null
+++ b/hsarweb/src/de/hsadmin/web/ItemsReader.java
@@ -0,0 +1,25 @@
+package de.hsadmin.web;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ItemsReader {
+	
+	public static List<Object>  readItemList(MainApplication app, String module, String property) throws HsarwebException {
+		final List<Object> itemsList = new ArrayList<Object>();
+		Object custListObj = app.getRemote().callSearch(module, new HashMap<String, String>());
+		if (custListObj instanceof Object[]) {
+			Object[] custList = (Object[]) custListObj;
+			for (Object custObj : custList) {
+				if (custObj instanceof Map<?, ?>) {
+					Map<?, ?> custHash = (Map<?, ?>)custObj;
+					itemsList.add(custHash.get(property));
+				}
+			}
+		}
+		return itemsList;
+	}
+
+}
\ No newline at end of file
diff --git a/hsarweb/src/de/hsadmin/web/MainApplication.java b/hsarweb/src/de/hsadmin/web/MainApplication.java
index a2ef55e..fc81e35 100644
--- a/hsarweb/src/de/hsadmin/web/MainApplication.java
+++ b/hsarweb/src/de/hsadmin/web/MainApplication.java
@@ -1,6 +1,8 @@
 package de.hsadmin.web;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
@@ -50,6 +52,7 @@
 	private String runAs = null;
 	private TabSheet tabSheet;
 	private Window mainWindow;
+	private List<Object> runAsList = null;
 
 
 	@Override
@@ -224,4 +227,20 @@
 		}
 	}
 
+	public List<Object> readSelectRunAsItems() throws HsarwebException {
+		if (runAsList == null) {
+			runAsList = new ArrayList<Object>();
+			if ("HOSTMASTER".equals(loginUserRole)) {
+				runAsList = ItemsReader.readItemList(this, "member", "membercode");
+			}
+			if ("CUSTOMER".equals(loginUserRole)) {
+				runAsList = ItemsReader.readItemList(this, "pac", "name");
+			}
+			if (loginUserRole.startsWith("PAC")) {
+				runAsList = ItemsReader.readItemList(this, "user", "name");
+			}
+		}
+		return runAsList;
+	}
+
 }
diff --git a/hsarweb/src/texts/main.properties b/hsarweb/src/texts/main.properties
index a257ae8..c393470 100644
--- a/hsarweb/src/texts/main.properties
+++ b/hsarweb/src/texts/main.properties
@@ -5,7 +5,7 @@
 save=save
 abort=abort
 new=new
-runas=run as
+runas=<strong>select</strong><br />change user perspective
 modules.NONE=de.hsadmin.web.HomeModule
 modules.USER=de.hsadmin.web.HomeModule
 modules.DOM_ADMIN=de.hsadmin.web.DomainReadonlyModule,de.hsadmin.web.EMailAddressModule,de.hsadmin.web.HomeModule
diff --git a/hsarweb/src/texts/main_de.properties b/hsarweb/src/texts/main_de.properties
index 47285fd..a1cff87 100644
--- a/hsarweb/src/texts/main_de.properties
+++ b/hsarweb/src/texts/main_de.properties
@@ -5,4 +5,4 @@
 save=speichern
 abort=abbrechen
 new=Eintrag anlegen
-runas=als Benutzer
+runas=<strong>Auswahl/Eingabe:</strong><br />zur Ansicht eines anderen<br />Benutzers wechseln

--
Gitblit v1.9.3