merging r10046 to the trunk
This commit is contained in:
parent
4bb5d83e17
commit
c3ebf8eb49
2 changed files with 106 additions and 105 deletions
|
@ -1,11 +1,11 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vedit.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
@ -27,29 +27,29 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
|||
|
||||
public class FormUtils {
|
||||
|
||||
protected static final Log log = LogFactory.getLog(FormUtils.class.getName());
|
||||
protected static final int BASE_10 = 10;
|
||||
protected static final Class[] SUPPORTED_TYPES = { String.class,
|
||||
int.class,
|
||||
Integer.class,
|
||||
boolean.class,
|
||||
Date.class
|
||||
};
|
||||
|
||||
protected static final List<Class> SUPPORTED_TYPE_LIST = Arrays
|
||||
protected static final Log log = LogFactory.getLog(FormUtils.class.getName());
|
||||
protected static final int BASE_10 = 10;
|
||||
protected static final Class[] SUPPORTED_TYPES = { String.class,
|
||||
int.class,
|
||||
Integer.class,
|
||||
boolean.class,
|
||||
Date.class
|
||||
};
|
||||
|
||||
protected static final List<Class> SUPPORTED_TYPE_LIST = Arrays
|
||||
.asList(SUPPORTED_TYPES);
|
||||
|
||||
/* this class needs to be reworked */
|
||||
|
||||
public static void populateFormFromBean (Object bean,
|
||||
String action,
|
||||
public static void populateFormFromBean (Object bean,
|
||||
String action,
|
||||
FormObject foo) {
|
||||
populateFormFromBean(bean,action,null,foo,new HashMap());
|
||||
}
|
||||
|
||||
public static void populateFormFromBean (Object bean,
|
||||
String action,
|
||||
FormObject foo,
|
||||
public static void populateFormFromBean (Object bean,
|
||||
String action,
|
||||
FormObject foo,
|
||||
Map<String, String> badValuesHash) {
|
||||
populateFormFromBean(bean,action,null,foo,badValuesHash);
|
||||
}
|
||||
|
@ -57,14 +57,14 @@ public class FormUtils {
|
|||
/**
|
||||
* Populates form objects with bean values
|
||||
*/
|
||||
public static void populateFormFromBean (Object bean,
|
||||
String action,
|
||||
EditProcessObject epo,
|
||||
FormObject foo,
|
||||
public static void populateFormFromBean (Object bean,
|
||||
String action,
|
||||
EditProcessObject epo,
|
||||
FormObject foo,
|
||||
Map<String, String> BadValuesHash) {
|
||||
Class beanClass =
|
||||
(epo != null && epo.getBeanClass() != null)
|
||||
? epo.getBeanClass()
|
||||
Class beanClass =
|
||||
(epo != null && epo.getBeanClass() != null)
|
||||
? epo.getBeanClass()
|
||||
: bean.getClass();
|
||||
|
||||
Method[] meths = beanClass.getMethods();
|
||||
|
@ -82,7 +82,7 @@ public class FormUtils {
|
|||
//we only want people directly to type in ints, strings, and dates
|
||||
//of course, most of the ints are probably foreign keys anyway...
|
||||
|
||||
String elementName = currMeth.getName().substring(
|
||||
String elementName = currMeth.getName().substring(
|
||||
3,currMeth.getName().length());
|
||||
|
||||
//see if there's something in the bean using
|
||||
|
@ -91,7 +91,7 @@ public class FormUtils {
|
|||
Class[] paramClass = new Class[1];
|
||||
paramClass[0] = currMethType;
|
||||
try {
|
||||
Method getter = beanClass.getMethod(
|
||||
Method getter = beanClass.getMethod(
|
||||
"get" + elementName, (Class[]) null);
|
||||
Object existingData = null;
|
||||
try {
|
||||
|
@ -104,14 +104,14 @@ public class FormUtils {
|
|||
if (existingData instanceof String){
|
||||
value += existingData;
|
||||
}
|
||||
else if (!(existingData instanceof Integer
|
||||
else if (!(existingData instanceof Integer
|
||||
&& (Integer)existingData < 0)) {
|
||||
value += existingData.toString();
|
||||
}
|
||||
}
|
||||
String badValue = (String) BadValuesHash.get(elementName);
|
||||
if (badValue != null) {
|
||||
value = badValue;
|
||||
value = badValue;
|
||||
}
|
||||
foo.getValues().put(elementName, value);
|
||||
} catch (NoSuchMethodException e) {
|
||||
|
@ -122,20 +122,20 @@ public class FormUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static List<Option> makeOptionListFromBeans (List beanList,
|
||||
String valueField,
|
||||
String bodyField,
|
||||
String selectedValue,
|
||||
public static List<Option> makeOptionListFromBeans (List beanList,
|
||||
String valueField,
|
||||
String bodyField,
|
||||
String selectedValue,
|
||||
String selectedBody) {
|
||||
return makeOptionListFromBeans (
|
||||
return makeOptionListFromBeans (
|
||||
beanList, valueField, bodyField, selectedValue, selectedBody, true);
|
||||
}
|
||||
|
||||
public static List<Option> makeOptionListFromBeans(List beanList,
|
||||
String valueField,
|
||||
String bodyField,
|
||||
String selectedValue,
|
||||
String selectedBody,
|
||||
public static List<Option> makeOptionListFromBeans(List beanList,
|
||||
String valueField,
|
||||
String bodyField,
|
||||
String selectedValue,
|
||||
String selectedBody,
|
||||
boolean forceSelectedInclusion) {
|
||||
List<Option> optList = new LinkedList();
|
||||
|
||||
|
@ -152,11 +152,11 @@ public class FormUtils {
|
|||
Method valueMeth = null;
|
||||
Object valueObj = null;
|
||||
try {
|
||||
valueMeth = bean.getClass().getMethod(
|
||||
valueMeth = bean.getClass().getMethod(
|
||||
"get" + valueField, (Class[]) null);
|
||||
valueObj = valueMeth.invoke(bean, (Object[]) null);
|
||||
} catch (Exception e) {
|
||||
log.warn("Could not find method get" + valueField + " on " +
|
||||
log.warn("Could not find method get" + valueField + " on " +
|
||||
bean.getClass());
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,7 @@ public class FormUtils {
|
|||
Method bodyMeth = null;
|
||||
Object bodyObj = null;
|
||||
try {
|
||||
bodyMeth = bean.getClass().getMethod(
|
||||
bodyMeth = bean.getClass().getMethod(
|
||||
"get" + bodyField, (Class[]) null);
|
||||
bodyObj = bodyMeth.invoke(bean, (Object[]) null);
|
||||
} catch (Exception e) {
|
||||
|
@ -201,11 +201,11 @@ public class FormUtils {
|
|||
|
||||
}
|
||||
|
||||
// if the list of beans doesn't include the selected value/body,
|
||||
// if the list of beans doesn't include the selected value/body,
|
||||
// insert it anyway so we don't inadvertently change the value of the
|
||||
// field to the first thing that happens to be in the select list
|
||||
boolean skipThisStep = !forceSelectedInclusion;
|
||||
// For now, if the value is a negative integer, we won't try to
|
||||
// For now, if the value is a negative integer, we won't try to
|
||||
// preserve it, as the bean was probably just instantiated.
|
||||
// Should switch to a more robust way of handling inital bean values.
|
||||
if (selectedValue == null) {
|
||||
|
@ -233,32 +233,32 @@ public class FormUtils {
|
|||
|
||||
}
|
||||
|
||||
public static List<Option> makeVClassOptionList(WebappDaoFactory wadf,
|
||||
public static List<Option> makeVClassOptionList(WebappDaoFactory wadf,
|
||||
String selectedVClassURI) {
|
||||
List<Option> vclassOptionList = new LinkedList<Option>();
|
||||
for (VClass vclass : wadf.getVClassDao().getAllVclasses()) {
|
||||
Option option = new Option();
|
||||
option.setValue(vclass.getURI());
|
||||
if ( (selectedVClassURI != null)
|
||||
&& (vclass.getURI() != null)
|
||||
if ( (selectedVClassURI != null)
|
||||
&& (vclass.getURI() != null)
|
||||
&& (selectedVClassURI.equals(vclass.getURI())) ) {
|
||||
option.setSelected(true);
|
||||
}
|
||||
String ontologyName = null;
|
||||
String ontologyPrefix = null;
|
||||
if (vclass.getNamespace() != null) {
|
||||
Ontology ont = wadf.getOntologyDao().getOntologyByURI(
|
||||
Ontology ont = wadf.getOntologyDao().getOntologyByURI(
|
||||
vclass.getNamespace());
|
||||
if ( (ont != null) && (ont.getName() != null) ) {
|
||||
ontologyName = ont.getName();
|
||||
if ( (ont != null) && (ont.getPrefix() != null) ) {
|
||||
ontologyPrefix = ont.getPrefix();
|
||||
}
|
||||
}
|
||||
StringBuffer classNameBuffer = new StringBuffer();
|
||||
if (ontologyPrefix != null) {
|
||||
classNameBuffer.append(ontologyPrefix).append(":");
|
||||
}
|
||||
if (vclass.getName() != null) {
|
||||
classNameBuffer.append(vclass.getName());
|
||||
}
|
||||
if (ontologyName != null) {
|
||||
classNameBuffer.append(" (").append(ontologyName).append(")");
|
||||
}
|
||||
option.setBody(classNameBuffer.toString());
|
||||
vclassOptionList.add(option);
|
||||
}
|
||||
|
@ -269,42 +269,42 @@ public class FormUtils {
|
|||
beanSet (newObj, field, value, null);
|
||||
}
|
||||
|
||||
public static void beanSet(Object newObj,
|
||||
String field,
|
||||
String value,
|
||||
public static void beanSet(Object newObj,
|
||||
String field,
|
||||
String value,
|
||||
EditProcessObject epo) {
|
||||
SimpleDateFormat standardDateFormat = new SimpleDateFormat(
|
||||
SimpleDateFormat standardDateFormat = new SimpleDateFormat(
|
||||
"yyyy-MM-dd HH:mm:ss");
|
||||
SimpleDateFormat minutesOnlyDateFormat = new SimpleDateFormat(
|
||||
SimpleDateFormat minutesOnlyDateFormat = new SimpleDateFormat(
|
||||
"yyyy-MM-dd HH:mm");
|
||||
Class cls =
|
||||
(epo != null && epo.getBeanClass() != null)
|
||||
? epo.getBeanClass()
|
||||
Class cls =
|
||||
(epo != null && epo.getBeanClass() != null)
|
||||
? epo.getBeanClass()
|
||||
: newObj.getClass();
|
||||
Class[] paramList = new Class[1];
|
||||
Method setterMethod = getSetterMethod(cls, field, SUPPORTED_TYPE_LIST);
|
||||
if (setterMethod == null) {
|
||||
log.debug("Could not find method set" + field + " on "
|
||||
+ cls.getName());
|
||||
return;
|
||||
}
|
||||
Class[] paramList = new Class[1];
|
||||
Method setterMethod = getSetterMethod(cls, field, SUPPORTED_TYPE_LIST);
|
||||
if (setterMethod == null) {
|
||||
log.debug("Could not find method set" + field + " on "
|
||||
+ cls.getName());
|
||||
return;
|
||||
}
|
||||
Class argumentType = setterMethod.getParameterTypes()[0];
|
||||
Object[] arglist = new Object[1];
|
||||
if (int.class.equals(argumentType)
|
||||
if (int.class.equals(argumentType)
|
||||
|| Integer.class.equals(argumentType)) {
|
||||
arglist[0] = (int.class.equals(argumentType)) ? -1 : null;
|
||||
if (!value.isEmpty()) {
|
||||
int parsedInt = Integer.parseInt(value, BASE_10);
|
||||
if (parsedInt < 0) {
|
||||
throw new FormUtils.NegativeIntegerException();
|
||||
} else {
|
||||
arglist[0] = parsedInt;
|
||||
}
|
||||
arglist[0] = (int.class.equals(argumentType)) ? -1 : null;
|
||||
if (!value.isEmpty()) {
|
||||
int parsedInt = Integer.parseInt(value, BASE_10);
|
||||
if (parsedInt < 0) {
|
||||
throw new FormUtils.NegativeIntegerException();
|
||||
} else {
|
||||
arglist[0] = parsedInt;
|
||||
}
|
||||
}
|
||||
} else if (Date.class.equals(argumentType)) {
|
||||
// this isn't so great ; should probably be in a validator
|
||||
if (value != null && value.length() > 0 && value.indexOf(":") < 1) {
|
||||
value += " 00:00:00";
|
||||
} else if (Date.class.equals(argumentType)) {
|
||||
// this isn't so great ; should probably be in a validator
|
||||
if (value != null && value.length() > 0 && value.indexOf(":") < 1) {
|
||||
value += " 00:00:00";
|
||||
}
|
||||
if (value != null && value.length()>0) {
|
||||
try {
|
||||
|
@ -313,11 +313,11 @@ public class FormUtils {
|
|||
try {
|
||||
arglist[0] = minutesOnlyDateFormat.parse(value);
|
||||
} catch (ParseException q) {
|
||||
log.error(FormUtils.class.getName()+" could not parse" +
|
||||
log.error(FormUtils.class.getName()+" could not parse" +
|
||||
value + " to a Date object.");
|
||||
throw new IllegalArgumentException(
|
||||
"Please enter a date/time in one of these " +
|
||||
"formats: '2007-07-07', '2007-07-07 07:07' " +
|
||||
throw new IllegalArgumentException(
|
||||
"Please enter a date/time in one of these " +
|
||||
"formats: '2007-07-07', '2007-07-07 07:07' " +
|
||||
"or '2007-07-07 07:07:07'");
|
||||
}
|
||||
}
|
||||
|
@ -335,24 +335,24 @@ public class FormUtils {
|
|||
log.error(e,e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Method getSetterMethod(Class beanClass,
|
||||
String fieldName,
|
||||
List<Class> supportedTypes) {
|
||||
for (Class clazz : supportedTypes) {
|
||||
try {
|
||||
Class[] argList = new Class[1];
|
||||
argList[0] = clazz;
|
||||
return beanClass.getMethod("set" + fieldName, argList);
|
||||
} catch (NoSuchMethodException nsme) {
|
||||
// just try the next type
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
private static Method getSetterMethod(Class beanClass,
|
||||
String fieldName,
|
||||
List<Class> supportedTypes) {
|
||||
for (Class clazz : supportedTypes) {
|
||||
try {
|
||||
Class[] argList = new Class[1];
|
||||
argList[0] = clazz;
|
||||
return beanClass.getMethod("set" + fieldName, argList);
|
||||
} catch (NoSuchMethodException nsme) {
|
||||
// just try the next type
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a Base-64-encoded String of format
|
||||
* Decodes a Base-64-encoded String of format
|
||||
* key:value;key2:value2;key3:value, and puts the keys and values in a Map
|
||||
* @param params
|
||||
* @return
|
||||
|
@ -365,8 +365,8 @@ public class FormUtils {
|
|||
beanParamMap.put(p[0],new String(Base64.decodeBase64(p[1].getBytes())));
|
||||
}
|
||||
return beanParamMap;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class NegativeIntegerException extends RuntimeException {}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.util.Comparator;
|
|||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -71,6 +72,7 @@ public class IndividualTypeRetryController extends BaseEditController {
|
|||
classNameMap.put(vc.getURI(),vc.getLocalNameWithPrefix());
|
||||
allClassURISet.add(vc.getURI());
|
||||
}
|
||||
|
||||
allVClasses = null;
|
||||
|
||||
for (Iterator<VClass> indClassIt = ind.getVClasses(false).iterator(); indClassIt.hasNext(); ) {
|
||||
|
@ -91,7 +93,6 @@ public class IndividualTypeRetryController extends BaseEditController {
|
|||
foo.setOptionLists(optionMap);
|
||||
|
||||
List<Option> typeOptionList = new ArrayList<Option>();
|
||||
|
||||
for (Iterator<String> classURIIt = classURIList.iterator(); classURIIt.hasNext();) {
|
||||
String classURI = classURIIt.next();
|
||||
Option opt = new Option(classURI,classNameMap.get(classURI));
|
||||
|
@ -127,7 +128,7 @@ public class IndividualTypeRetryController extends BaseEditController {
|
|||
private class OptionCollator implements Comparator {
|
||||
public int compare (Object o1, Object o2) {
|
||||
Collator collator = Collator.getInstance();
|
||||
return collator.compare( ((Option)o1).getBody() , ((Option)o2).getBody() );
|
||||
return collator.compare( ((Option)o1).getBody().toString().substring(((Option)o1).getBody().toString().indexOf(":")) , ((Option)o2).getBody().toString().substring(((Option)o2).getBody().toString().indexOf(":")) );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue