Fixing NPE in DateTimeWithPrecision.java NIHVIVO-1649
This commit is contained in:
parent
a3909c4432
commit
cce9d6a0d2
2 changed files with 107 additions and 14 deletions
|
@ -133,7 +133,7 @@ public class DateTimeWithPrecision extends BaseEditElement {
|
||||||
"'" + precisionUri + "' but could not convert to Precision object");
|
"'" + precisionUri + "' but could not convert to Precision object");
|
||||||
}
|
}
|
||||||
|
|
||||||
if( precisionUri == null || precisionUri.isEmpty() ){
|
if( precisionUri == null || precisionUri.isEmpty() || existingPrec == null){
|
||||||
map.put("existingPrecision", "");
|
map.put("existingPrecision", "");
|
||||||
|
|
||||||
/* no precision so there should also be no datetime */
|
/* no precision so there should also be no datetime */
|
||||||
|
@ -168,24 +168,35 @@ public class DateTimeWithPrecision extends BaseEditElement {
|
||||||
+ " but the date time is " + value);
|
+ " but the date time is " + value);
|
||||||
|
|
||||||
/* only put the values in the map for ones which are significant based on the precision */
|
/* only put the values in the map for ones which are significant based on the precision */
|
||||||
if( existingPrec.ordinal() >= VitroVocabulary.Precision.SECOND.ordinal() ){
|
if( existingPrec.ordinal() >= VitroVocabulary.Precision.SECOND.ordinal() )
|
||||||
map.put("second", Integer.toString(value.getSecondOfMinute() )) ;
|
map.put("second", Integer.toString(value.getSecondOfMinute() )) ;
|
||||||
}
|
else
|
||||||
if( existingPrec.ordinal() >= VitroVocabulary.Precision.MINUTE.ordinal() ){
|
map.put("second", "");
|
||||||
|
|
||||||
|
if( existingPrec.ordinal() >= VitroVocabulary.Precision.MINUTE.ordinal() )
|
||||||
map.put("minute", Integer.toString(value.getMinuteOfHour()) );
|
map.put("minute", Integer.toString(value.getMinuteOfHour()) );
|
||||||
}
|
else
|
||||||
if( existingPrec.ordinal() >= VitroVocabulary.Precision.HOUR.ordinal() ){
|
map.put("minute", "");
|
||||||
map.put("hour", Integer.toString(value.getHourOfDay()) );
|
|
||||||
}
|
if( existingPrec.ordinal() >= VitroVocabulary.Precision.HOUR.ordinal() )
|
||||||
if( existingPrec.ordinal() >= VitroVocabulary.Precision.DAY.ordinal() ){
|
map.put("hour", Integer.toString(value.getHourOfDay()) );
|
||||||
|
else
|
||||||
|
map.put("hour", "");
|
||||||
|
|
||||||
|
if( existingPrec.ordinal() >= VitroVocabulary.Precision.DAY.ordinal() )
|
||||||
map.put("day", Integer.toString(value.getDayOfMonth()) );
|
map.put("day", Integer.toString(value.getDayOfMonth()) );
|
||||||
}
|
else
|
||||||
if( existingPrec.ordinal() >= VitroVocabulary.Precision.MONTH.ordinal() ){
|
map.put("day", "");
|
||||||
|
|
||||||
|
if( existingPrec.ordinal() >= VitroVocabulary.Precision.MONTH.ordinal() )
|
||||||
map.put("month", Integer.toString(value.getMonthOfYear()));
|
map.put("month", Integer.toString(value.getMonthOfYear()));
|
||||||
}
|
else
|
||||||
if( existingPrec.ordinal() >= VitroVocabulary.Precision.YEAR.ordinal() ){
|
map.put("month", "");
|
||||||
|
|
||||||
|
if( existingPrec.ordinal() >= VitroVocabulary.Precision.YEAR.ordinal() )
|
||||||
map.put("year", Integer.toString(value.getYear()));
|
map.put("year", Integer.toString(value.getYear()));
|
||||||
}
|
else
|
||||||
|
map.put("year", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
return map;
|
return map;
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.edit.n3editing;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
|
||||||
|
import com.hp.hpl.jena.rdf.model.Literal;
|
||||||
|
|
||||||
|
public class DateTimeIntervalValidation implements N3Validator {
|
||||||
|
private static Log log = LogFactory.getLog(DateTimeIntervalValidation.class);
|
||||||
|
|
||||||
|
private String startFieldName;
|
||||||
|
private String endFieldName;
|
||||||
|
|
||||||
|
private String startValueName;
|
||||||
|
private String endValueName;
|
||||||
|
|
||||||
|
public DateTimeIntervalValidation(String startFieldName, String endFieldName){
|
||||||
|
this.startFieldName = startFieldName;
|
||||||
|
this.endFieldName = endFieldName;
|
||||||
|
startValueName = startFieldName + ".value";
|
||||||
|
endValueName = endFieldName + ".value";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> validate(EditConfiguration editConfig,
|
||||||
|
EditSubmission editSub) {
|
||||||
|
Map<String, Literal> existingLiterals = editConfig.getLiteralsInScope();
|
||||||
|
Literal existingStartYear = existingLiterals.get(startValueName);
|
||||||
|
Literal existingEndYear = existingLiterals.get(endValueName);
|
||||||
|
|
||||||
|
Map<String, Literal> literalsFromForm = editSub.getLiteralsFromForm();
|
||||||
|
Literal formStartYear = literalsFromForm.get(startValueName);
|
||||||
|
Literal formEndYear = literalsFromForm.get(endValueName);
|
||||||
|
|
||||||
|
Map<String, String> errors = new HashMap<String, String>();
|
||||||
|
|
||||||
|
if (formStartYear != null && formEndYear != null) {
|
||||||
|
errors.putAll(checkDateLiterals(formStartYear, formEndYear));
|
||||||
|
} else if (formStartYear != null && existingEndYear != null) {
|
||||||
|
errors.putAll(checkDateLiterals(formStartYear, existingEndYear));
|
||||||
|
} else if (existingStartYear != null && formEndYear != null) {
|
||||||
|
errors.putAll(checkDateLiterals(existingStartYear, formEndYear));
|
||||||
|
} else if (existingStartYear != null && existingEndYear != null) {
|
||||||
|
errors.putAll(checkDateLiterals(existingStartYear, existingEndYear));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errors.size() != 0)
|
||||||
|
return errors;
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, String> checkDateLiterals(Literal startLit, Literal endLit) {
|
||||||
|
Map<String, String> errors = new HashMap<String, String>();
|
||||||
|
try{
|
||||||
|
XSDDateTime startDate = (XSDDateTime)startLit.getValue();
|
||||||
|
XSDDateTime endDate = (XSDDateTime)endLit.getValue();
|
||||||
|
if( startDate != null && endDate!= null ){
|
||||||
|
Calendar startCal = startDate.asCalendar();
|
||||||
|
Calendar endCal = endDate.asCalendar();
|
||||||
|
|
||||||
|
if( endCal != null && ! endCal.after( startCal ) ){
|
||||||
|
errors.put(startFieldName, "Start year must be before end year");
|
||||||
|
errors.put(endFieldName, "End year must be after start year");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch(ClassCastException cce){
|
||||||
|
errors.put(startFieldName, "could not format star or end date");
|
||||||
|
errors.put(endFieldName, "could not format star or end date");
|
||||||
|
log.debug("could not format dates " + cce);
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue