NIHVIVO-3352 improvements to validation in backend editing forms

This commit is contained in:
brianjlowe 2011-11-23 17:11:27 +00:00
parent 777d10ba04
commit 8822dc51ff
23 changed files with 298 additions and 236 deletions

View file

@ -16,7 +16,8 @@ import java.lang.reflect.InvocationTargetException;
import java.io.IOException;
/**
* This controller exists only so we can request different edit form controllers without having to have entries in web.xml for each.
* This controller exists only so we can request different edit form controllers
* without having to have entries in web.xml for each.
* @author bjl23
*
*/
@ -24,54 +25,62 @@ public class EditFrontController extends VitroHttpServlet {
private static final Log log = LogFactory.getLog(EditFrontController.class.getName());
private static final String CONTROLLER_PKG = "edu.cornell.mannlib.vitro.webapp.controller.edit";
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
String controllerName = request.getParameter("controller")+"RetryController";
if (controllerName==null || controllerName.length()==0) {
log.error("doPost() found no controller parameter");
}
Class controller = null;
Object controllerInstance = null;
try {
controller = Class.forName(CONTROLLER_PKG+"."+controllerName);
try {
controllerInstance = controller.getConstructor((Class[]) null).newInstance((Object[]) null);
((HttpServlet)controllerInstance).init(getServletConfig());
} catch (Exception e) {
log.error("doPost() could not instantiate specific controller "+controllerName);
}
} catch (ClassNotFoundException e){
log.error("doPost() could not find controller "+CONTROLLER_PKG+"."+controllerName);
}
Class[] args = new Class[2];
args[0] = HttpServletRequest.class;
args[1] = HttpServletResponse.class;
try {
Method meth = controller.getDeclaredMethod("doGet",args);
Object[] methArgs = new Object[2];
methArgs[0] = request;
methArgs[1] = response;
try {
meth.invoke(controllerInstance,methArgs);
} catch (IllegalAccessException e) {
log.error("doPost() encountered IllegalAccessException on invoking "+controllerName);
} catch (InvocationTargetException e) {
log.error("doPost() encountered InvocationTargetException on invoking "+controllerName);
log.debug(e.getTargetException().getMessage());
e.printStackTrace();
}
} catch (NoSuchMethodException e){
log.error("could not find doPost() method in "+controllerName);
}
} catch (Exception e) {
e.printStackTrace();
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String controllerName = request.getParameter("controller")+"RetryController";
if (controllerName==null || controllerName.length()==0) {
log.error("doPost() found no controller parameter");
}
Class controller = null;
Object controllerInstance = null;
try {
controller = Class.forName(CONTROLLER_PKG+"."+controllerName);
try {
controllerInstance = controller.getConstructor(
(Class[]) null).newInstance((Object[]) null);
((HttpServlet)controllerInstance).init(getServletConfig());
} catch (Exception e) {
String errMsg = "doPost() could not instantiate specific " +
"controller " + controllerName;
log.error(errMsg, e);
throw new RuntimeException(errMsg, e);
}
} catch (ClassNotFoundException e){
String errMsg = "doPost() could not find controller " +
CONTROLLER_PKG + "." + controllerName;
log.error(errMsg);
throw new RuntimeException(errMsg);
}
Class[] args = new Class[2];
args[0] = HttpServletRequest.class;
args[1] = HttpServletResponse.class;
try {
Method meth = controller.getDeclaredMethod("doGet",args);
Object[] methArgs = new Object[2];
methArgs[0] = request;
methArgs[1] = response;
try {
meth.invoke(controllerInstance,methArgs);
} catch (IllegalAccessException e) {
String errMsg = "doPost() encountered IllegalAccessException " +
"while invoking " + controllerName;
log.error(errMsg, e);
throw new RuntimeException(errMsg, e);
} catch (InvocationTargetException e) {
String errMsg = "doPost() encountered InvocationTargetException " +
"while invoking " + controllerName;
log.error(errMsg, e);
throw new RuntimeException(errMsg, e);
}
} catch (NoSuchMethodException e){
log.error("could not find doPost() method in " + controllerName);
throw new RuntimeException("could not find doPost() method in " +
controllerName);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
doPost(request,response);
}

View file

@ -108,6 +108,7 @@ public class OperationController extends BaseEditController {
//if validation failed, go back to the form controller
if (!valid){
epo.setAttribute("globalErrorMsg", "Please correct errors highlighted below.");
retry(request, response, epo);
return;
}

View file

@ -349,73 +349,6 @@ public class FormUtils {
}
}
return null;
}
/**
* Takes a bean and uses all of its setter methods to set null values
* @return
*/
public static Object nullBean(Object bean){
Class cls = bean.getClass();
Method[] meths = cls.getMethods();
for (int i=0; i<meths.length; ++i){
Method meth = meths[i];
if (meth.getName().indexOf("set")==0){
try{
meth.invoke(bean,(Object[]) null);
} catch (Exception e) {
log.error ("unable to use " + meth.getName() +
" to set null.");
}
}
}
return bean;
}
/**
* Takes any nonnull values from an overlay bean and sets them on a base bean
* @param base
* @param overlay
* @return overlaid bean
*/
public static Object overlayBean (Object base, Object overlay) throws IllegalArgumentException {
Class baseCls = base.getClass();
Class overlayCls = overlay.getClass();
if (overlayCls != baseCls)
throw new IllegalArgumentException("overlayBean requires two objects of the same type");
Method[] meths = overlayCls.getMethods();
for (int i=0; i<meths.length; ++i){
Method meth = meths[i];
String methName = meth.getName();
if (methName.indexOf("get")==0){
try {
Object overlayObj = meth.invoke(overlay,(Object[]) null);
if (overlayObj != null) {
String setterName = "set"+methName.substring(3,methName.length());
Class setterArgClass = null;
if (overlayObj instanceof Integer)
setterArgClass = int.class;
else
setterArgClass = overlayObj.getClass();
Class[] setterArgClasses = new Class[1];
setterArgClasses[0] = setterArgClass;
try {
Method setterMeth = baseCls.getMethod(setterName,setterArgClasses);
Object[] setterObjs = new Object[1];
setterObjs[0] = overlayObj;
setterMeth.invoke(base,setterObjs);
} catch (NoSuchMethodException e) {
log.error("could not find setter method "+setterName);
}
}
} catch (Exception e) {
log.error("could not invoke getter method "+methName);
}
}
}
return base;
}
/**

View file

@ -46,7 +46,9 @@ public class IntValidator implements Validator {
return vo;
}
public IntValidator(){}
public IntValidator (int minVal, int maxVal){
this.minVal = minVal;
this.maxVal = maxVal;