From e62f1fdd50708039943d3fdffa8e6639e706dd88 Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Fri, 17 Dec 2010 17:39:53 +0000 Subject: [PATCH] generisches Delete, Fehlerbehandlung --- .../de/hsadmin/mods/email/EMailAddress.java | 10 +- .../src/de/hsadmin/remote/AbstractRemote.java | 6 +- .../de/hsadmin/remote/EMailAddressTest.java | 20 ++- .../de/hsadmin/remote/EMailAliasTest.java | 2 +- .../test/de/hsadmin/remote/UnixUserTest.java | 2 +- hsarweb/src/de/hsadmin/web/GenericModule.java | 131 ++++++++++++------ .../src/de/hsadmin/web/HsarwebException.java | 8 ++ .../src/de/hsadmin/web/MainApplication.java | 53 +++++-- hsarweb/src/de/hsadmin/web/Remote.java | 16 ++- hsarweb/src/texts/main.properties | 5 +- hsarweb/src/texts/main_de.properties | 4 + 11 files changed, 190 insertions(+), 67 deletions(-) create mode 100644 hsarweb/src/texts/main_de.properties diff --git a/hsarback/src/de/hsadmin/mods/email/EMailAddress.java b/hsarback/src/de/hsadmin/mods/email/EMailAddress.java index 31da21f..4531256 100644 --- a/hsarback/src/de/hsadmin/mods/email/EMailAddress.java +++ b/hsarback/src/de/hsadmin/mods/email/EMailAddress.java @@ -157,11 +157,13 @@ public class EMailAddress extends AbstractEntity implements Serializable { } public String toString() { - if (localpart != null && target != null) - return super.toString() + "{ id=" + id + "; address=" + localpart - + subdomain + "; target=" + target + " }"; - else + if (localpart != null && target != null && domain != null) { + String local = super.toString() + "{ id=" + id + "; address=" + localpart + "@"; + if (subdomain != null) { local += "." + subdomain; } + return local + domain + "; target=" + target + " }"; + } else { return super.toString(); + } } @Override diff --git a/hsarback/src/de/hsadmin/remote/AbstractRemote.java b/hsarback/src/de/hsadmin/remote/AbstractRemote.java index 721ee08..8223740 100644 --- a/hsarback/src/de/hsadmin/remote/AbstractRemote.java +++ b/hsarback/src/de/hsadmin/remote/AbstractRemote.java @@ -202,9 +202,11 @@ public abstract class AbstractRemote implements IRemote { String value = whereParams.get(field).replaceAll("'", "\'"); cond.append("obj."); cond.append(field); - cond.append(" = '"); + cond.append(" = "); + boolean numeric = "id".equals(field); + if (!numeric) cond.append("'"); cond.append(value); - cond.append("'"); + if (!numeric) cond.append("'"); } return cond.toString(); } diff --git a/hsarback/test/de/hsadmin/remote/EMailAddressTest.java b/hsarback/test/de/hsadmin/remote/EMailAddressTest.java index 7eb1d32..f11fd8a 100644 --- a/hsarback/test/de/hsadmin/remote/EMailAddressTest.java +++ b/hsarback/test/de/hsadmin/remote/EMailAddressTest.java @@ -44,7 +44,7 @@ public class EMailAddressTest { try { Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; - assertEquals(262, result.length); + assertEquals(253, result.length); for (Object o : result) { if (o instanceof Map) { Map row = (Map) o; @@ -191,13 +191,29 @@ public class EMailAddressTest { } assertEquals(count + 1, getObjectCount()); count = getObjectCount(); - Map whereParams = new HashMap(); + Map whereParams = new HashMap(); whereParams.put("localpart", "f6n"); whereParams.put("domain", "jalin.de"); params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { + Object execute = client.execute(MODULE + ".search", params); + assertTrue(execute instanceof Object[]); + whereParams = new HashMap(); + Object[] resArray = (Object[]) execute; + assertEquals(1, resArray.length); + Object res = resArray[0]; + Map map = (Map) res; + Object idVal = map.get("id"); + whereParams.put("id", idVal); + } catch (XmlRpcException e) { + fail(e.getMessage()); + } + try { + params = new Object[] { user, + cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), + whereParams }; Object execute = client.execute(MODULE + ".delete", params); assertNull(execute); } catch (XmlRpcException e) { diff --git a/hsarback/test/de/hsadmin/remote/EMailAliasTest.java b/hsarback/test/de/hsadmin/remote/EMailAliasTest.java index 9fc610f..48cfa95 100644 --- a/hsarback/test/de/hsadmin/remote/EMailAliasTest.java +++ b/hsarback/test/de/hsadmin/remote/EMailAliasTest.java @@ -44,7 +44,7 @@ public class EMailAliasTest { try { Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; - assertEquals(262, result.length); + assertEquals(3, result.length); for (Object o : result) { if (o instanceof Map) { Map row = (Map) o; diff --git a/hsarback/test/de/hsadmin/remote/UnixUserTest.java b/hsarback/test/de/hsadmin/remote/UnixUserTest.java index c9ffcee..f9dfdda 100644 --- a/hsarback/test/de/hsadmin/remote/UnixUserTest.java +++ b/hsarback/test/de/hsadmin/remote/UnixUserTest.java @@ -43,7 +43,7 @@ public class UnixUserTest { try { Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; - assertEquals(23, result.length); + assertEquals(22, result.length); for (Object o : result) { if (o instanceof Map) { Map row = (Map) o; diff --git a/hsarweb/src/de/hsadmin/web/GenericModule.java b/hsarweb/src/de/hsadmin/web/GenericModule.java index f2dcd86..8301035 100644 --- a/hsarweb/src/de/hsadmin/web/GenericModule.java +++ b/hsarweb/src/de/hsadmin/web/GenericModule.java @@ -14,10 +14,15 @@ import com.vaadin.terminal.Sizeable; import com.vaadin.terminal.ThemeResource; import com.vaadin.ui.Button; import com.vaadin.ui.Component; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; import com.vaadin.ui.Table; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.Window; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.themes.BaseTheme; +import de.hsadmin.web.config.LocaleConfig; import de.hsadmin.web.config.ModuleConfig; import de.hsadmin.web.config.PropertyConfig; @@ -27,16 +32,11 @@ public abstract class GenericModule implements Serializable { private static final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); private Table table; - private Remote remote; + private MainApplication application; - public void setRemote(Remote remote) { - this.remote = remote; - try { - initTable(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + public void setApplication(MainApplication app) throws HsarwebException { + this.application = app; + initTable(); } public abstract ModuleConfig getModuleConfig(); @@ -45,11 +45,11 @@ public abstract class GenericModule implements Serializable { return table; } - public void reload() { + public void reload() throws HsarwebException { loadTable(); } - private void initTable() throws IllegalAccessException { + private void initTable() throws HsarwebException { table = new Table() { private static final long serialVersionUID = 35127658139420917L; @Override @@ -65,31 +65,35 @@ public abstract class GenericModule implements Serializable { return super.formatPropertyValue(rowId, colId, property); } }; - table.setWidth(100.0f, Sizeable.UNITS_PERCENTAGE); - table.setHeight(100.0f, Sizeable.UNITS_PERCENTAGE); - table.setSelectable(true); - table.setImmediate(true); - table.setColumnCollapsingAllowed(true); - table.setColumnReorderingAllowed(true); - for (PropertyConfig prop : getModuleConfig().getPropertyList()) { - table.addContainerProperty(prop.getId(), prop.getType(), prop.getDefaultValue()); - table.setColumnHeader(prop.getId(), prop.getLabel()); - if (prop.isHidden()) { - table.setColumnCollapsed(prop.getId(), true); + try { + table.setWidth(100.0f, Sizeable.UNITS_PERCENTAGE); + table.setHeight(100.0f, Sizeable.UNITS_PERCENTAGE); + table.setSelectable(true); + table.setImmediate(true); + table.setColumnCollapsingAllowed(true); + table.setColumnReorderingAllowed(true); + for (PropertyConfig prop : getModuleConfig().getPropertyList()) { + table.addContainerProperty(prop.getId(), prop.getType(), prop.getDefaultValue()); + table.setColumnHeader(prop.getId(), prop.getLabel()); + if (prop.isHidden()) { + table.setColumnCollapsed(prop.getId(), true); + } } + table.addContainerProperty("edit", Button.class, null); + table.setColumnWidth("edit", 16); + table.setColumnHeader("edit", ""); + table.addContainerProperty("del", Button.class, null); + table.setColumnWidth("del", 16); + table.setColumnHeader("del", ""); + } catch (Exception e) { + throw new HsarwebException(e); } - table.addContainerProperty("edit", Button.class, null); - table.setColumnWidth("edit", 16); - table.setColumnHeader("edit", ""); - table.addContainerProperty("del", Button.class, null); - table.setColumnWidth("del", 16); - table.setColumnHeader("del", ""); } - private void loadTable() { + private void loadTable() throws HsarwebException { table.removeAllItems(); try { - Object callSearch = remote.callSearch(getModuleConfig().getName(), new HashMap()); + Object callSearch = application.getRemote().callSearch(getModuleConfig().getName(), new HashMap()); List propertyList = getModuleConfig().getPropertyList(); if (callSearch instanceof Object[]) { for (Object row : ((Object[])callSearch)) { @@ -133,14 +137,29 @@ public abstract class GenericModule implements Serializable { table.sort(); } } catch (UnsupportedOperationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (HsarwebException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new HsarwebException(e); } } + private void deleteRow(long id) throws HsarwebException { + Map paramHash = new HashMap(); + paramHash.put(findIdKey(), Long.toString(id)); + application.getRemote().callDelete(getModuleConfig().getName(), paramHash); + loadTable(); + } + + private String findIdKey() { + List propertyList = getModuleConfig().getPropertyList(); + String idKey = null; + for (PropertyConfig propConf : propertyList) { + if (propConf.isIdent()) { + idKey = propConf.getId(); + return idKey; + } + } + return idKey; + } + private Button createEditButton(long id) { ThemeResource icon = new ThemeResource("../runo/icons/16/document-txt.png"); Button button = new Button(); @@ -151,7 +170,7 @@ public abstract class GenericModule implements Serializable { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { - loadTable(); +// loadTable(); System.out.println("Data: " + event.getButton().getData()); } }); @@ -160,16 +179,50 @@ public abstract class GenericModule implements Serializable { private Button createDeleteButton(long id) { ThemeResource icon = new ThemeResource("../runo/icons/16/document-delete.png"); - Button button = new Button(); + final Button button = new Button(); button.setIcon(icon); button.setData(id); button.setStyleName(BaseTheme.BUTTON_LINK); button.addListener(new Button.ClickListener() { private static final long serialVersionUID = 1L; + private Window childWindow; @Override public void buttonClick(ClickEvent event) { - loadTable(); - System.out.println("Data: " + event.getButton().getData()); + LocaleConfig localeConfig = application.getLocaleConfig(); + childWindow = new Window(getModuleConfig().getLabel("moduletitle") + " " + localeConfig.getText("delete")); + childWindow.setWidth(320.0f, Sizeable.UNITS_PIXELS); + VerticalLayout vLayout = new VerticalLayout(); + vLayout.setMargin(true); + vLayout.setSpacing(true); + vLayout.addComponent(new Label(localeConfig.getText("confirmdelete"))); + HorizontalLayout hLayout = new HorizontalLayout(); + Button btDeleteRow = new Button(localeConfig.getText("delete")); + btDeleteRow.addListener(new Button.ClickListener() { + private static final long serialVersionUID = 1L; + @Override + public void buttonClick(ClickEvent event) { + application.getMainWindow().removeWindow(childWindow); + try { + deleteRow((Long) button.getData()); + } catch (HsarwebException e) { + application.showUserException(e); + } + } + }); + Button btAbort = new Button(localeConfig.getText("abort")); + btAbort.addListener(new Button.ClickListener() { + private static final long serialVersionUID = 1L; + @Override + public void buttonClick(ClickEvent event) { + application.getMainWindow().removeWindow(childWindow); + } + }); + hLayout.addComponent(btDeleteRow); + hLayout.addComponent(btAbort); + vLayout.addComponent(hLayout); + childWindow.setContent(vLayout); + childWindow.setModal(true); + application.getMainWindow().addWindow(childWindow); } }); return button; diff --git a/hsarweb/src/de/hsadmin/web/HsarwebException.java b/hsarweb/src/de/hsadmin/web/HsarwebException.java index 0fb59ea..2f69413 100644 --- a/hsarweb/src/de/hsadmin/web/HsarwebException.java +++ b/hsarweb/src/de/hsadmin/web/HsarwebException.java @@ -9,4 +9,12 @@ public class HsarwebException extends Exception { super(string, e); } + public HsarwebException(Throwable e) { + super(e); + } + + public HsarwebException(String string) { + super(string); + } + } diff --git a/hsarweb/src/de/hsadmin/web/MainApplication.java b/hsarweb/src/de/hsadmin/web/MainApplication.java index 07d7875..ec41cbb 100644 --- a/hsarweb/src/de/hsadmin/web/MainApplication.java +++ b/hsarweb/src/de/hsadmin/web/MainApplication.java @@ -15,6 +15,7 @@ import org.jasig.cas.client.validation.Assertion; import com.vaadin.Application; import com.vaadin.terminal.Sizeable; +import com.vaadin.terminal.Terminal; import com.vaadin.terminal.ThemeResource; import com.vaadin.terminal.gwt.server.HttpServletRequestListener; import com.vaadin.ui.Component; @@ -22,6 +23,7 @@ import com.vaadin.ui.TabSheet; import com.vaadin.ui.Window; import com.vaadin.ui.TabSheet.SelectedTabChangeEvent; import com.vaadin.ui.TabSheet.Tab; +import com.vaadin.ui.Window.Notification; import de.hsadmin.web.config.LocaleConfig; import de.hsadmin.web.config.ModuleConfig; @@ -37,6 +39,7 @@ public class MainApplication extends Application implements HttpServletRequestLi private Remote remote; private Map modules; + @Override public void init() { localeConfig = new LocaleConfig(Locale.getDefault(), "main"); @@ -44,14 +47,14 @@ public class MainApplication extends Application implements HttpServletRequestLi Window mainWindow = new Window(localeConfig.getText("applicationtitle")); TabSheet tabs = new TabSheet(); tabs.setWidth(100.0f, Sizeable.UNITS_PERCENTAGE); - tabs.setHeight(680.0f, Sizeable.UNITS_PIXELS); + tabs.setHeight(200.0f, Sizeable.UNITS_PERCENTAGE); String modulesParamString = getContextParam("hsarmodules"); modules = new HashMap(); GenericModule firstModule = null; for (String className : modulesParamString.split(",")) { try { GenericModule module = (GenericModule) Class.forName(className).newInstance(); - module.setRemote(remote); + module.setApplication(this); if (firstModule == null) { firstModule = module; } @@ -59,21 +62,25 @@ public class MainApplication extends Application implements HttpServletRequestLi String label = moduleConfig.getLabel("moduletitle"); modules.put(label, module); tabs.addTab(module.getComponent(), label, new ThemeResource(moduleConfig.getLabel("moduleicon"))); - } catch (InstantiationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + } catch (Exception e) { + showSystemException(e); } } tabs.addListener(this); mainWindow.addComponent(tabs); setMainWindow(mainWindow); - firstModule.reload(); + setErrorHandler(new Terminal.ErrorListener() { + private static final long serialVersionUID = 1L; + @Override + public void terminalError(Terminal.ErrorEvent event) { + event.getThrowable().printStackTrace(); + } + }); + try { + firstModule.reload(); + } catch (HsarwebException e) { + showSystemException(e); + } } public String getProxyTicket() { @@ -88,6 +95,14 @@ public class MainApplication extends Application implements HttpServletRequestLi return userPrincipal.getName(); } + public Remote getRemote() { + return remote; + } + + public LocaleConfig getLocaleConfig() { + return localeConfig; + } + @Override public void onRequestStart(HttpServletRequest request, HttpServletResponse response) { @@ -108,7 +123,19 @@ public class MainApplication extends Application implements HttpServletRequestLi Component selectedTab = tabSheet.getSelectedTab(); Tab tab = tabSheet.getTab(selectedTab); GenericModule module = modules.get(tab.getCaption()); - module.reload(); + try { + module.reload(); + } catch (HsarwebException e) { + showSystemException(e); + } + } + + public void showUserException(Exception e) { + getMainWindow().showNotification("Anwendungs-Fehler", "
" + e.getMessage(), Notification.TYPE_WARNING_MESSAGE); + } + + public void showSystemException(Exception e) { + getMainWindow().showNotification("System-Fehler", "
Bitte informieren Sie den Support
" + e.getMessage(), Notification.TYPE_ERROR_MESSAGE); } } diff --git a/hsarweb/src/de/hsadmin/web/Remote.java b/hsarweb/src/de/hsadmin/web/Remote.java index d4649b0..ffddb12 100644 --- a/hsarweb/src/de/hsadmin/web/Remote.java +++ b/hsarweb/src/de/hsadmin/web/Remote.java @@ -18,26 +18,34 @@ public class Remote { } public Object callSearch(String module, Map where) throws HsarwebException { + return xmlrpcCall(module, "search", where); + } + + public void callDelete(String module, Map where) throws HsarwebException { + xmlrpcCall(module, "delete", where); + } + + private Object xmlrpcCall(String module, String operation, Map where) throws HsarwebException { Object[] params = new Object[3]; params[0] = app.getLogin(); params[1] = app.getProxyTicket(); params[2] = where; Object res; try { - res = getClient().execute(module + ".search", params); + res = getClient().execute(module + "." + operation, params); } catch (XmlRpcException e) { throw new HsarwebException("error in remote server call", e); } - return res; + return res; } - + private XmlRpcClient getClient() throws HsarwebException { if (client == null) { XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); try { String xmlrpcURL = app.getContextParam("xmlrpcURL"); config.setServerURL(new URL(xmlrpcURL)); - config.setEnabledForExceptions(true); + config.setEnabledForExtensions(true); client = new XmlRpcClient(); client.setConfig(config); } catch (MalformedURLException e) { diff --git a/hsarweb/src/texts/main.properties b/hsarweb/src/texts/main.properties index 0ccf7c2..e0e17f9 100644 --- a/hsarweb/src/texts/main.properties +++ b/hsarweb/src/texts/main.properties @@ -1 +1,4 @@ -applicationtitle=HSAdmin Web Application \ No newline at end of file +applicationtitle=HSAdmin Web Application +delete=delete +confirmdelete=confirm delete +abort=abort \ No newline at end of file diff --git a/hsarweb/src/texts/main_de.properties b/hsarweb/src/texts/main_de.properties new file mode 100644 index 0000000..219cacd --- /dev/null +++ b/hsarweb/src/texts/main_de.properties @@ -0,0 +1,4 @@ +applicationtitle=HSAdmin Web Application +delete=löschen +confirmdelete=Diese Zeile löschen? +abort=abbrechen \ No newline at end of file