NIHVIVO-3352 improvements to validation in backend editing forms
This commit is contained in:
parent
777d10ba04
commit
8822dc51ff
23 changed files with 298 additions and 236 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.controller.edit;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -17,6 +19,8 @@ import edu.cornell.mannlib.vedit.controller.BaseEditController;
|
|||
import edu.cornell.mannlib.vedit.forwarder.PageForwarder;
|
||||
import edu.cornell.mannlib.vedit.forwarder.impl.UrlForwarder;
|
||||
import edu.cornell.mannlib.vedit.util.FormUtils;
|
||||
import edu.cornell.mannlib.vedit.validator.Validator;
|
||||
import edu.cornell.mannlib.vedit.validator.impl.RequiredFieldValidator;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.UseMiscellaneousAdminPages;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
|
||||
|
@ -75,7 +79,12 @@ public class ClassgroupRetryController extends BaseEditController {
|
|||
epo.setOriginalBean(vclassGroupForEditing);
|
||||
} else {
|
||||
vclassGroupForEditing = (VClassGroup) epo.getNewBean();
|
||||
}
|
||||
}
|
||||
|
||||
//validators
|
||||
List<Validator> validatorList = new ArrayList<Validator>();
|
||||
validatorList.add(new RequiredFieldValidator());
|
||||
epo.getValidatorMap().put("PublicName", validatorList);
|
||||
|
||||
//make a postinsert pageforwarder that will send us to a new class's fetch screen
|
||||
epo.setPostInsertPageForwarder(new VclassGroupInsertPageForwarder());
|
||||
|
|
|
@ -143,7 +143,7 @@ public class DatapropRetryController extends BaseEditController {
|
|||
if (objectForEditing.getDomainClassURI() != null) {
|
||||
VClass domain = vreq.getWebappDaoFactory().getVClassDao()
|
||||
.getVClassByURI(objectForEditing.getDomainClassURI());
|
||||
if (domain.isAnonymous()) {
|
||||
if (domain != null && domain.isAnonymous()) {
|
||||
domainOptionList.add(0, new Option(
|
||||
domain.getURI(),
|
||||
domain.getName(),
|
||||
|
|
|
@ -4,6 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.controller.edit;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -18,6 +20,8 @@ import edu.cornell.mannlib.vedit.controller.BaseEditController;
|
|||
import edu.cornell.mannlib.vedit.forwarder.PageForwarder;
|
||||
import edu.cornell.mannlib.vedit.forwarder.impl.UrlForwarder;
|
||||
import edu.cornell.mannlib.vedit.util.FormUtils;
|
||||
import edu.cornell.mannlib.vedit.validator.Validator;
|
||||
import edu.cornell.mannlib.vedit.validator.impl.RequiredFieldValidator;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.EditOntology;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
|
||||
|
@ -67,6 +71,11 @@ public class OntologyRetryController extends BaseEditController {
|
|||
action = "update";
|
||||
log.error("using newBean");
|
||||
}
|
||||
|
||||
//validators
|
||||
List<Validator> validatorList = new ArrayList<Validator>();
|
||||
validatorList.add(new RequiredFieldValidator());
|
||||
epo.getValidatorMap().put("URI", validatorList);
|
||||
|
||||
//make a simple mask for the class's id
|
||||
Object[] simpleMaskPair = new Object[2];
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.controller.edit;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -17,6 +19,8 @@ import edu.cornell.mannlib.vedit.controller.BaseEditController;
|
|||
import edu.cornell.mannlib.vedit.forwarder.PageForwarder;
|
||||
import edu.cornell.mannlib.vedit.forwarder.impl.UrlForwarder;
|
||||
import edu.cornell.mannlib.vedit.util.FormUtils;
|
||||
import edu.cornell.mannlib.vedit.validator.Validator;
|
||||
import edu.cornell.mannlib.vedit.validator.impl.RequiredFieldValidator;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.UseMiscellaneousAdminPages;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.PropertyGroup;
|
||||
|
@ -75,6 +79,11 @@ public class PropertyGroupRetryController extends BaseEditController {
|
|||
} else {
|
||||
propertyGroupForEditing = (PropertyGroup) epo.getNewBean();
|
||||
}
|
||||
|
||||
//validators
|
||||
List<Validator> validatorList = new ArrayList<Validator>();
|
||||
validatorList.add(new RequiredFieldValidator());
|
||||
epo.getValidatorMap().put("Name", validatorList);
|
||||
|
||||
//make a postinsert pageforwarder that will send us to a new class's fetch screen
|
||||
epo.setPostInsertPageForwarder(new PropertyGroupInsertPageForwarder());
|
||||
|
|
|
@ -27,6 +27,8 @@ import edu.cornell.mannlib.vedit.controller.BaseEditController;
|
|||
import edu.cornell.mannlib.vedit.forwarder.PageForwarder;
|
||||
import edu.cornell.mannlib.vedit.forwarder.impl.UrlForwarder;
|
||||
import edu.cornell.mannlib.vedit.util.FormUtils;
|
||||
import edu.cornell.mannlib.vedit.validator.Validator;
|
||||
import edu.cornell.mannlib.vedit.validator.impl.IntValidator;
|
||||
import edu.cornell.mannlib.vedit.validator.impl.XMLNameValidator;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.policy.bean.PropertyRestrictionListener;
|
||||
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
|
||||
|
@ -114,7 +116,10 @@ public class PropertyRetryController extends BaseEditController {
|
|||
List localNameInverseValidatorList = new ArrayList();
|
||||
localNameInverseValidatorList.add(new XMLNameValidator(true));
|
||||
epo.getValidatorMap().put("LocalName", localNameValidatorList);
|
||||
epo.getValidatorMap().put("LocalNameInverse", localNameInverseValidatorList);
|
||||
epo.getValidatorMap().put("LocalNameInverse", localNameInverseValidatorList);
|
||||
List<Validator> displayRankValidatorList = new ArrayList<Validator>();
|
||||
displayRankValidatorList.add(new IntValidator());
|
||||
epo.getValidatorMap().put("DisplayRank", displayRankValidatorList);
|
||||
|
||||
//set up any listeners
|
||||
List changeListenerList = new ArrayList();
|
||||
|
|
|
@ -215,7 +215,7 @@ public class VclassEditController extends BaseEditController {
|
|||
request.setAttribute("instantiable", instantiable);
|
||||
request.setAttribute("bodyJsp","/templates/edit/specific/classes_edit.jsp");
|
||||
request.setAttribute("title","Class Control Panel");
|
||||
request.setAttribute("css", "<link rel=\"stylesheet\" type=\"text/css\" href=\""+request.getAppBean().getThemeDir()+"css/edit.css\"/>");
|
||||
//request.setAttribute("css", "<link rel=\"stylesheet\" type=\"text/css\" href=\""+request.getAppBean().getThemeDir()+"css/edit.css\"/>");
|
||||
|
||||
try {
|
||||
rd.forward(request, response);
|
||||
|
|
|
@ -10,6 +10,7 @@ import java.util.LinkedList;
|
|||
import java.util.List;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
@ -176,9 +177,8 @@ public class VclassRetryController extends BaseEditController {
|
|||
try {
|
||||
rd.forward(request, response);
|
||||
} catch (Exception e) {
|
||||
log.error("VclassRetryController could not forward to view.");
|
||||
log.error(e.getMessage());
|
||||
log.error(e.getStackTrace());
|
||||
log.error("VclassRetryController could not forward to view.", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ public class AllClassGroupsListingController extends BaseEditController {
|
|||
results.add("XX");
|
||||
results.add("Group");
|
||||
results.add("display rank");
|
||||
results.add("XX");
|
||||
results.add("");
|
||||
results.add("XX");
|
||||
|
||||
if (groups != null) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue