generisches Delete, Fehlerbehandlung

This commit is contained in:
Peter Hormanns 2010-12-17 17:39:53 +00:00
parent 93db501810
commit e62f1fdd50
11 changed files with 190 additions and 67 deletions

View File

@ -157,12 +157,14 @@ 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
public boolean isNew() {

View File

@ -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();
}

View File

@ -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<String, String> whereParams = new HashMap<String, String>();
Map<String, Object> whereParams = new HashMap<String, Object>();
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<String, Object>();
Object[] resArray = (Object[]) execute;
assertEquals(1, resArray.length);
Object res = resArray[0];
Map<String, Object> map = (Map<String, Object>) 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) {

View File

@ -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;

View File

@ -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;

View File

@ -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 {
public void setApplication(MainApplication app) throws HsarwebException {
this.application = app;
initTable();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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,6 +65,7 @@ public abstract class GenericModule implements Serializable {
return super.formatPropertyValue(rowId, colId, property);
}
};
try {
table.setWidth(100.0f, Sizeable.UNITS_PERCENTAGE);
table.setHeight(100.0f, Sizeable.UNITS_PERCENTAGE);
table.setSelectable(true);
@ -84,12 +85,15 @@ public abstract class GenericModule implements Serializable {
table.addContainerProperty("del", Button.class, null);
table.setColumnWidth("del", 16);
table.setColumnHeader("del", "");
} catch (Exception e) {
throw new HsarwebException(e);
}
}
private void loadTable() {
private void loadTable() throws HsarwebException {
table.removeAllItems();
try {
Object callSearch = remote.callSearch(getModuleConfig().getName(), new HashMap<String, String>());
Object callSearch = application.getRemote().callSearch(getModuleConfig().getName(), new HashMap<String, String>());
List<PropertyConfig> 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<String, String> paramHash = new HashMap<String, String>();
paramHash.put(findIdKey(), Long.toString(id));
application.getRemote().callDelete(getModuleConfig().getName(), paramHash);
loadTable();
}
private String findIdKey() {
List<PropertyConfig> 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;

View File

@ -9,4 +9,12 @@ public class HsarwebException extends Exception {
super(string, e);
}
public HsarwebException(Throwable e) {
super(e);
}
public HsarwebException(String string) {
super(string);
}
}

View File

@ -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<String, GenericModule> 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<String, GenericModule>();
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);
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());
try {
module.reload();
} catch (HsarwebException e) {
showSystemException(e);
}
}
public void showUserException(Exception e) {
getMainWindow().showNotification("Anwendungs-Fehler", "<br/ >" + e.getMessage(), Notification.TYPE_WARNING_MESSAGE);
}
public void showSystemException(Exception e) {
getMainWindow().showNotification("System-Fehler", "<br />Bitte informieren Sie den Support<br/ >" + e.getMessage(), Notification.TYPE_ERROR_MESSAGE);
}
}

View File

@ -18,13 +18,21 @@ public class Remote {
}
public Object callSearch(String module, Map<String, String> where) throws HsarwebException {
return xmlrpcCall(module, "search", where);
}
public void callDelete(String module, Map<String, String> where) throws HsarwebException {
xmlrpcCall(module, "delete", where);
}
private Object xmlrpcCall(String module, String operation, Map<String, String> 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);
}
@ -37,7 +45,7 @@ public class Remote {
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) {

View File

@ -1 +1,4 @@
applicationtitle=HSAdmin Web Application
delete=delete
confirmdelete=confirm delete
abort=abort

View File

@ -0,0 +1,4 @@
applicationtitle=HSAdmin Web Application
delete=löschen
confirmdelete=Diese Zeile löschen?
abort=abbrechen