merging r10046 to the trunk

This commit is contained in:
tworrall 2012-09-10 17:38:46 +00:00
parent 4bb5d83e17
commit c3ebf8eb49
2 changed files with 106 additions and 105 deletions

View file

@ -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; package edu.cornell.mannlib.vedit.util;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -27,29 +27,29 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
public class FormUtils { public class FormUtils {
protected static final Log log = LogFactory.getLog(FormUtils.class.getName()); protected static final Log log = LogFactory.getLog(FormUtils.class.getName());
protected static final int BASE_10 = 10; protected static final int BASE_10 = 10;
protected static final Class[] SUPPORTED_TYPES = { String.class, protected static final Class[] SUPPORTED_TYPES = { String.class,
int.class, int.class,
Integer.class, Integer.class,
boolean.class, boolean.class,
Date.class Date.class
}; };
protected static final List<Class> SUPPORTED_TYPE_LIST = Arrays protected static final List<Class> SUPPORTED_TYPE_LIST = Arrays
.asList(SUPPORTED_TYPES); .asList(SUPPORTED_TYPES);
/* this class needs to be reworked */ /* this class needs to be reworked */
public static void populateFormFromBean (Object bean, public static void populateFormFromBean (Object bean,
String action, String action,
FormObject foo) { FormObject foo) {
populateFormFromBean(bean,action,null,foo,new HashMap()); populateFormFromBean(bean,action,null,foo,new HashMap());
} }
public static void populateFormFromBean (Object bean, public static void populateFormFromBean (Object bean,
String action, String action,
FormObject foo, FormObject foo,
Map<String, String> badValuesHash) { Map<String, String> badValuesHash) {
populateFormFromBean(bean,action,null,foo,badValuesHash); populateFormFromBean(bean,action,null,foo,badValuesHash);
} }
@ -57,14 +57,14 @@ public class FormUtils {
/** /**
* Populates form objects with bean values * Populates form objects with bean values
*/ */
public static void populateFormFromBean (Object bean, public static void populateFormFromBean (Object bean,
String action, String action,
EditProcessObject epo, EditProcessObject epo,
FormObject foo, FormObject foo,
Map<String, String> BadValuesHash) { Map<String, String> BadValuesHash) {
Class beanClass = Class beanClass =
(epo != null && epo.getBeanClass() != null) (epo != null && epo.getBeanClass() != null)
? epo.getBeanClass() ? epo.getBeanClass()
: bean.getClass(); : bean.getClass();
Method[] meths = beanClass.getMethods(); Method[] meths = beanClass.getMethods();
@ -82,7 +82,7 @@ public class FormUtils {
//we only want people directly to type in ints, strings, and dates //we only want people directly to type in ints, strings, and dates
//of course, most of the ints are probably foreign keys anyway... //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()); 3,currMeth.getName().length());
//see if there's something in the bean using //see if there's something in the bean using
@ -91,7 +91,7 @@ public class FormUtils {
Class[] paramClass = new Class[1]; Class[] paramClass = new Class[1];
paramClass[0] = currMethType; paramClass[0] = currMethType;
try { try {
Method getter = beanClass.getMethod( Method getter = beanClass.getMethod(
"get" + elementName, (Class[]) null); "get" + elementName, (Class[]) null);
Object existingData = null; Object existingData = null;
try { try {
@ -104,14 +104,14 @@ public class FormUtils {
if (existingData instanceof String){ if (existingData instanceof String){
value += existingData; value += existingData;
} }
else if (!(existingData instanceof Integer else if (!(existingData instanceof Integer
&& (Integer)existingData < 0)) { && (Integer)existingData < 0)) {
value += existingData.toString(); value += existingData.toString();
} }
} }
String badValue = (String) BadValuesHash.get(elementName); String badValue = (String) BadValuesHash.get(elementName);
if (badValue != null) { if (badValue != null) {
value = badValue; value = badValue;
} }
foo.getValues().put(elementName, value); foo.getValues().put(elementName, value);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
@ -122,20 +122,20 @@ public class FormUtils {
} }
} }
public static List<Option> makeOptionListFromBeans (List beanList, public static List<Option> makeOptionListFromBeans (List beanList,
String valueField, String valueField,
String bodyField, String bodyField,
String selectedValue, String selectedValue,
String selectedBody) { String selectedBody) {
return makeOptionListFromBeans ( return makeOptionListFromBeans (
beanList, valueField, bodyField, selectedValue, selectedBody, true); beanList, valueField, bodyField, selectedValue, selectedBody, true);
} }
public static List<Option> makeOptionListFromBeans(List beanList, public static List<Option> makeOptionListFromBeans(List beanList,
String valueField, String valueField,
String bodyField, String bodyField,
String selectedValue, String selectedValue,
String selectedBody, String selectedBody,
boolean forceSelectedInclusion) { boolean forceSelectedInclusion) {
List<Option> optList = new LinkedList(); List<Option> optList = new LinkedList();
@ -152,11 +152,11 @@ public class FormUtils {
Method valueMeth = null; Method valueMeth = null;
Object valueObj = null; Object valueObj = null;
try { try {
valueMeth = bean.getClass().getMethod( valueMeth = bean.getClass().getMethod(
"get" + valueField, (Class[]) null); "get" + valueField, (Class[]) null);
valueObj = valueMeth.invoke(bean, (Object[]) null); valueObj = valueMeth.invoke(bean, (Object[]) null);
} catch (Exception e) { } catch (Exception e) {
log.warn("Could not find method get" + valueField + " on " + log.warn("Could not find method get" + valueField + " on " +
bean.getClass()); bean.getClass());
} }
@ -168,7 +168,7 @@ public class FormUtils {
Method bodyMeth = null; Method bodyMeth = null;
Object bodyObj = null; Object bodyObj = null;
try { try {
bodyMeth = bean.getClass().getMethod( bodyMeth = bean.getClass().getMethod(
"get" + bodyField, (Class[]) null); "get" + bodyField, (Class[]) null);
bodyObj = bodyMeth.invoke(bean, (Object[]) null); bodyObj = bodyMeth.invoke(bean, (Object[]) null);
} catch (Exception e) { } 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 // 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 // field to the first thing that happens to be in the select list
boolean skipThisStep = !forceSelectedInclusion; 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. // preserve it, as the bean was probably just instantiated.
// Should switch to a more robust way of handling inital bean values. // Should switch to a more robust way of handling inital bean values.
if (selectedValue == null) { 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) { String selectedVClassURI) {
List<Option> vclassOptionList = new LinkedList<Option>(); List<Option> vclassOptionList = new LinkedList<Option>();
for (VClass vclass : wadf.getVClassDao().getAllVclasses()) { for (VClass vclass : wadf.getVClassDao().getAllVclasses()) {
Option option = new Option(); Option option = new Option();
option.setValue(vclass.getURI()); option.setValue(vclass.getURI());
if ( (selectedVClassURI != null) if ( (selectedVClassURI != null)
&& (vclass.getURI() != null) && (vclass.getURI() != null)
&& (selectedVClassURI.equals(vclass.getURI())) ) { && (selectedVClassURI.equals(vclass.getURI())) ) {
option.setSelected(true); option.setSelected(true);
} }
String ontologyName = null; String ontologyPrefix = null;
if (vclass.getNamespace() != null) { if (vclass.getNamespace() != null) {
Ontology ont = wadf.getOntologyDao().getOntologyByURI( Ontology ont = wadf.getOntologyDao().getOntologyByURI(
vclass.getNamespace()); vclass.getNamespace());
if ( (ont != null) && (ont.getName() != null) ) { if ( (ont != null) && (ont.getPrefix() != null) ) {
ontologyName = ont.getName(); ontologyPrefix = ont.getPrefix();
} }
} }
StringBuffer classNameBuffer = new StringBuffer(); StringBuffer classNameBuffer = new StringBuffer();
if (ontologyPrefix != null) {
classNameBuffer.append(ontologyPrefix).append(":");
}
if (vclass.getName() != null) { if (vclass.getName() != null) {
classNameBuffer.append(vclass.getName()); classNameBuffer.append(vclass.getName());
} }
if (ontologyName != null) {
classNameBuffer.append(" (").append(ontologyName).append(")");
}
option.setBody(classNameBuffer.toString()); option.setBody(classNameBuffer.toString());
vclassOptionList.add(option); vclassOptionList.add(option);
} }
@ -269,42 +269,42 @@ public class FormUtils {
beanSet (newObj, field, value, null); beanSet (newObj, field, value, null);
} }
public static void beanSet(Object newObj, public static void beanSet(Object newObj,
String field, String field,
String value, String value,
EditProcessObject epo) { EditProcessObject epo) {
SimpleDateFormat standardDateFormat = new SimpleDateFormat( SimpleDateFormat standardDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"); "yyyy-MM-dd HH:mm:ss");
SimpleDateFormat minutesOnlyDateFormat = new SimpleDateFormat( SimpleDateFormat minutesOnlyDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm"); "yyyy-MM-dd HH:mm");
Class cls = Class cls =
(epo != null && epo.getBeanClass() != null) (epo != null && epo.getBeanClass() != null)
? epo.getBeanClass() ? epo.getBeanClass()
: newObj.getClass(); : newObj.getClass();
Class[] paramList = new Class[1]; Class[] paramList = new Class[1];
Method setterMethod = getSetterMethod(cls, field, SUPPORTED_TYPE_LIST); Method setterMethod = getSetterMethod(cls, field, SUPPORTED_TYPE_LIST);
if (setterMethod == null) { if (setterMethod == null) {
log.debug("Could not find method set" + field + " on " log.debug("Could not find method set" + field + " on "
+ cls.getName()); + cls.getName());
return; return;
} }
Class argumentType = setterMethod.getParameterTypes()[0]; Class argumentType = setterMethod.getParameterTypes()[0];
Object[] arglist = new Object[1]; Object[] arglist = new Object[1];
if (int.class.equals(argumentType) if (int.class.equals(argumentType)
|| Integer.class.equals(argumentType)) { || Integer.class.equals(argumentType)) {
arglist[0] = (int.class.equals(argumentType)) ? -1 : null; arglist[0] = (int.class.equals(argumentType)) ? -1 : null;
if (!value.isEmpty()) { if (!value.isEmpty()) {
int parsedInt = Integer.parseInt(value, BASE_10); int parsedInt = Integer.parseInt(value, BASE_10);
if (parsedInt < 0) { if (parsedInt < 0) {
throw new FormUtils.NegativeIntegerException(); throw new FormUtils.NegativeIntegerException();
} else { } else {
arglist[0] = parsedInt; arglist[0] = parsedInt;
} }
} }
} else if (Date.class.equals(argumentType)) { } else if (Date.class.equals(argumentType)) {
// this isn't so great ; should probably be in a validator // this isn't so great ; should probably be in a validator
if (value != null && value.length() > 0 && value.indexOf(":") < 1) { if (value != null && value.length() > 0 && value.indexOf(":") < 1) {
value += " 00:00:00"; value += " 00:00:00";
} }
if (value != null && value.length()>0) { if (value != null && value.length()>0) {
try { try {
@ -313,11 +313,11 @@ public class FormUtils {
try { try {
arglist[0] = minutesOnlyDateFormat.parse(value); arglist[0] = minutesOnlyDateFormat.parse(value);
} catch (ParseException q) { } catch (ParseException q) {
log.error(FormUtils.class.getName()+" could not parse" + log.error(FormUtils.class.getName()+" could not parse" +
value + " to a Date object."); value + " to a Date object.");
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Please enter a date/time in one of these " + "Please enter a date/time in one of these " +
"formats: '2007-07-07', '2007-07-07 07:07' " + "formats: '2007-07-07', '2007-07-07 07:07' " +
"or '2007-07-07 07:07:07'"); "or '2007-07-07 07:07:07'");
} }
} }
@ -335,24 +335,24 @@ public class FormUtils {
log.error(e,e); log.error(e,e);
} }
} }
private static Method getSetterMethod(Class beanClass, private static Method getSetterMethod(Class beanClass,
String fieldName, String fieldName,
List<Class> supportedTypes) { List<Class> supportedTypes) {
for (Class clazz : supportedTypes) { for (Class clazz : supportedTypes) {
try { try {
Class[] argList = new Class[1]; Class[] argList = new Class[1];
argList[0] = clazz; argList[0] = clazz;
return beanClass.getMethod("set" + fieldName, argList); return beanClass.getMethod("set" + fieldName, argList);
} catch (NoSuchMethodException nsme) { } catch (NoSuchMethodException nsme) {
// just try the next type // just try the next type
} }
} }
return null; 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 * key:value;key2:value2;key3:value, and puts the keys and values in a Map
* @param params * @param params
* @return * @return
@ -365,8 +365,8 @@ public class FormUtils {
beanParamMap.put(p[0],new String(Base64.decodeBase64(p[1].getBytes()))); beanParamMap.put(p[0],new String(Base64.decodeBase64(p[1].getBytes())));
} }
return beanParamMap; return beanParamMap;
} }
public static class NegativeIntegerException extends RuntimeException {} public static class NegativeIntegerException extends RuntimeException {}
} }

View file

@ -9,6 +9,7 @@ import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -71,6 +72,7 @@ public class IndividualTypeRetryController extends BaseEditController {
classNameMap.put(vc.getURI(),vc.getLocalNameWithPrefix()); classNameMap.put(vc.getURI(),vc.getLocalNameWithPrefix());
allClassURISet.add(vc.getURI()); allClassURISet.add(vc.getURI());
} }
allVClasses = null; allVClasses = null;
for (Iterator<VClass> indClassIt = ind.getVClasses(false).iterator(); indClassIt.hasNext(); ) { for (Iterator<VClass> indClassIt = ind.getVClasses(false).iterator(); indClassIt.hasNext(); ) {
@ -91,7 +93,6 @@ public class IndividualTypeRetryController extends BaseEditController {
foo.setOptionLists(optionMap); foo.setOptionLists(optionMap);
List<Option> typeOptionList = new ArrayList<Option>(); List<Option> typeOptionList = new ArrayList<Option>();
for (Iterator<String> classURIIt = classURIList.iterator(); classURIIt.hasNext();) { for (Iterator<String> classURIIt = classURIList.iterator(); classURIIt.hasNext();) {
String classURI = classURIIt.next(); String classURI = classURIIt.next();
Option opt = new Option(classURI,classNameMap.get(classURI)); Option opt = new Option(classURI,classNameMap.get(classURI));
@ -127,7 +128,7 @@ public class IndividualTypeRetryController extends BaseEditController {
private class OptionCollator implements Comparator { private class OptionCollator implements Comparator {
public int compare (Object o1, Object o2) { public int compare (Object o1, Object o2) {
Collator collator = Collator.getInstance(); 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(":")) );
} }
} }