ItEr20S04ArquitecturaServidorItEr19S04: Adding automatic handling of OptimistLockingFailureException.
This commit is contained in:
parent
8bfa9f2e8d
commit
8f0aa042e4
3 changed files with 93 additions and 0 deletions
|
|
@ -0,0 +1,46 @@
|
|||
package org.navalplanner.web.common;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
import org.zkoss.zk.ui.Component;
|
||||
import org.zkoss.zk.ui.Executions;
|
||||
import org.zkoss.zk.ui.util.GenericForwardComposer;
|
||||
|
||||
public class ConcurrentModificationController extends GenericForwardComposer {
|
||||
|
||||
private static final Log LOG = LogFactory
|
||||
.getLog(ConcurrentModificationController.class);
|
||||
private String backURL;
|
||||
|
||||
@Override
|
||||
public void doAfterCompose(Component comp) throws Exception {
|
||||
super.doAfterCompose(comp);
|
||||
backURL = getBackURL();
|
||||
}
|
||||
|
||||
public static void showException(
|
||||
OptimisticLockingFailureException exception, String backURL) {
|
||||
LOG
|
||||
.error(
|
||||
"an OptimistLockingFailureException caused a disruption to an user",
|
||||
exception);
|
||||
Executions.sendRedirect("/common/concurrent_modification.zul?back="
|
||||
+ backURL);
|
||||
}
|
||||
|
||||
private static String getBackURL() {
|
||||
return getRequest().getParameter("back");
|
||||
}
|
||||
|
||||
private static HttpServletRequest getRequest() {
|
||||
return (HttpServletRequest) Executions.getCurrent().getNativeRequest();
|
||||
}
|
||||
|
||||
public void onClick$continue() {
|
||||
Executions.sendRedirect(backURL);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package org.navalplanner.web.common;
|
||||
|
||||
import org.navalplanner.web.common.ExceptionCatcherProxy.IExceptionHandler;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
|
||||
public class ConcurrentModificationDetector {
|
||||
|
||||
public static <T> T detectConcurrentModification(Class<T> interfaceClass,
|
||||
T model, final String backURL) {
|
||||
IExceptionHandler<OptimisticLockingFailureException> handler = createHandler(backURL);
|
||||
return ExceptionCatcherProxy.doCatchFor(interfaceClass).when(
|
||||
OptimisticLockingFailureException.class, handler)
|
||||
.applyTo(model);
|
||||
}
|
||||
|
||||
private static IExceptionHandler<OptimisticLockingFailureException> createHandler(
|
||||
final String backURL) {
|
||||
return new IExceptionHandler<OptimisticLockingFailureException>() {
|
||||
|
||||
@Override
|
||||
public void onException(OptimisticLockingFailureException exception) {
|
||||
ConcurrentModificationController.showException(exception,
|
||||
backURL);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?init class="org.zkoss.zk.ui.util.Composition" arg0="/common/layout/template_v02.zul"?>
|
||||
<?link rel="stylesheet" type="text/css" href="/common/css/navalpro_v01.css"?>
|
||||
<?link rel="stylesheet" type="text/css" href="/common/css/navalpro_zk.css"?>
|
||||
<zk>
|
||||
|
||||
<window self="@{define(content)}" >
|
||||
<!--caption>
|
||||
Erro ${requestScope['javax.servlet.error.status_code']}
|
||||
</caption-->
|
||||
<vbox apply="org.navalplanner.web.common.ConcurrentModificationController"
|
||||
sclass="errorbox">
|
||||
Another user has modified the same data, so the operation couldn't be completed.
|
||||
|
||||
<hbox style="margin-left:auto; margin-right:auto">
|
||||
<button id="continue" label="Continue" />
|
||||
</hbox>
|
||||
</vbox>
|
||||
</window>
|
||||
</zk>
|
||||
Loading…
Add table
Reference in a new issue