refs #2437: Some records accounted on fallback are not recovered in the proper way in a special case
https://support.d4science.org/issues/2437 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@124668 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
d627816f70
commit
7bbaa4ed20
|
@ -22,6 +22,7 @@ import org.gcube.documentstore.records.AggregatedRecord;
|
|||
import org.gcube.documentstore.records.Record;
|
||||
import org.gcube.documentstore.records.implementation.validations.annotations.NotEmpty;
|
||||
import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong;
|
||||
import org.gcube.documentstore.records.implementation.validations.validators.ValidLongValidator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -38,13 +39,13 @@ public abstract class AbstractRecord implements Record {
|
|||
|
||||
private static Logger logger = LoggerFactory.getLogger(AbstractRecord.class);
|
||||
|
||||
@RequiredField @NotEmpty
|
||||
@NotEmpty
|
||||
protected static final String ID = Record.ID;
|
||||
|
||||
@RequiredField @ValidLong
|
||||
@ValidLong
|
||||
protected static final String CREATION_TIME = Record.CREATION_TIME;
|
||||
|
||||
@RequiredField @NotEmpty
|
||||
@NotEmpty
|
||||
protected static final String RECORD_TYPE = Record.RECORD_TYPE;
|
||||
|
||||
/** resource-specific properties */
|
||||
|
@ -58,11 +59,14 @@ public abstract class AbstractRecord implements Record {
|
|||
protected Set<String> aggregatedFields;
|
||||
|
||||
protected static Set<Field> getAllFields(Class<?> type) {
|
||||
Set<Field> fields = new HashSet<Field>();
|
||||
for (Class<?> c = type; c != null; c = c.getSuperclass())
|
||||
fields.addAll(Arrays.asList(c.getDeclaredFields()));
|
||||
return fields;
|
||||
}
|
||||
Set<Field> fields = new HashSet<Field>();
|
||||
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
|
||||
fields.addAll(Arrays.asList(c.getDeclaredFields()));
|
||||
fields.addAll(Arrays.asList(c.getFields()));
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
||||
protected void initializeValidation() {
|
||||
//logger.trace("Initializing Field Validators");
|
||||
|
@ -77,42 +81,52 @@ public abstract class AbstractRecord implements Record {
|
|||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
List<FieldAction> fieldValidators = new ArrayList<FieldAction>();
|
||||
validation.put(keyString, fieldValidators);
|
||||
|
||||
List<FieldAction> fieldComputations = new ArrayList<FieldAction>();
|
||||
computation.put(keyString, fieldComputations);
|
||||
|
||||
for (Annotation annotation : field.getAnnotations()){
|
||||
Class<? extends Annotation> annotationType = annotation.annotationType();
|
||||
if (annotationType.isAnnotationPresent(FieldDecorator.class)){
|
||||
Class<? extends FieldAction> managedClass = ((FieldDecorator)annotationType.getAnnotation(FieldDecorator.class)).action();
|
||||
FieldAction validator;
|
||||
try {
|
||||
validator = managedClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
logger.error("{} {}", keyString, annotation, e);
|
||||
continue;
|
||||
if(field.getAnnotations().length>0){
|
||||
|
||||
List<FieldAction> fieldValidators = validation.get(keyString);
|
||||
if(fieldValidators==null){
|
||||
fieldValidators = new ArrayList<FieldAction>();
|
||||
validation.put(keyString, fieldValidators);
|
||||
}
|
||||
|
||||
List<FieldAction> fieldComputations = computation.get(keyString);
|
||||
if(fieldComputations==null){
|
||||
fieldComputations = new ArrayList<FieldAction>();
|
||||
computation.put(keyString, fieldComputations);
|
||||
}
|
||||
|
||||
for (Annotation annotation : field.getAnnotations()){
|
||||
Class<? extends Annotation> annotationType = annotation.annotationType();
|
||||
if (annotationType.isAnnotationPresent(FieldDecorator.class)){
|
||||
Class<? extends FieldAction> managedClass = ((FieldDecorator)annotationType.getAnnotation(FieldDecorator.class)).action();
|
||||
FieldAction validator;
|
||||
try {
|
||||
validator = managedClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
logger.error("{} {}", keyString, annotation, e);
|
||||
continue;
|
||||
}
|
||||
fieldValidators.add(validator);
|
||||
}
|
||||
fieldValidators.add(validator);
|
||||
}
|
||||
if(annotationType.isAssignableFrom(RequiredField.class)){
|
||||
requiredFields.add(keyString);
|
||||
}
|
||||
if(annotationType.isAssignableFrom(AggregatedField.class)){
|
||||
aggregatedFields.add(keyString);
|
||||
}
|
||||
if(annotationType.isAssignableFrom(ComputedField.class)){
|
||||
computedFields.add(keyString);
|
||||
Class<? extends FieldAction> managedClass = ((ComputedField) annotation).action();
|
||||
FieldAction computeAction;
|
||||
try {
|
||||
computeAction = managedClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
logger.error("{} {}", keyString, annotation, e);
|
||||
continue;
|
||||
if(annotationType.isAssignableFrom(RequiredField.class)){
|
||||
requiredFields.add(keyString);
|
||||
}
|
||||
if(annotationType.isAssignableFrom(AggregatedField.class)){
|
||||
aggregatedFields.add(keyString);
|
||||
}
|
||||
if(annotationType.isAssignableFrom(ComputedField.class)){
|
||||
computedFields.add(keyString);
|
||||
Class<? extends FieldAction> managedClass = ((ComputedField) annotation).action();
|
||||
FieldAction computeAction;
|
||||
try {
|
||||
computeAction = managedClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
logger.error("{} {}", keyString, annotation, e);
|
||||
continue;
|
||||
}
|
||||
fieldComputations.add(computeAction);
|
||||
}
|
||||
fieldComputations.add(computeAction);
|
||||
}
|
||||
}
|
||||
field.setAccessible(defaultAccessibility);
|
||||
|
@ -217,8 +231,13 @@ public abstract class AbstractRecord implements Record {
|
|||
*/
|
||||
@Override
|
||||
public Calendar getCreationTime() {
|
||||
long millis = (Long) this.resourceProperties.get(CREATION_TIME);
|
||||
return timestampToCalendar(millis);
|
||||
Long millis = null;
|
||||
try {
|
||||
millis = (Long) new ValidLongValidator().validate(CREATION_TIME, this.resourceProperties.get(CREATION_TIME), null);
|
||||
return timestampToCalendar(millis);
|
||||
} catch (InvalidValueException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -379,7 +398,17 @@ public abstract class AbstractRecord implements Record {
|
|||
Map<String, Serializable> validated = new HashMap<String, Serializable>();
|
||||
for(String key : properties.keySet()){
|
||||
Serializable value = properties.get(key);
|
||||
|
||||
/* TODO Test Patch */
|
||||
Serializable checkedValue = validateField(key, value);
|
||||
if(checkedValue == null){
|
||||
validated.remove(key);
|
||||
}else{
|
||||
validated.put(key, checkedValue);
|
||||
}
|
||||
/* Restore if test patch is not good
|
||||
validated.put(key, validateField(key, value));
|
||||
*/
|
||||
}
|
||||
return validated;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package org.gcube.documentstore.records.implementation.validations.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.gcube.documentstore.records.implementation.FieldDecorator;
|
||||
import org.gcube.documentstore.records.implementation.validations.validators.ValidBooleanValidator;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@FieldDecorator(action=ValidBooleanValidator.class)
|
||||
public @interface ValidBoolean {
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package org.gcube.documentstore.records.implementation.validations.validators;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
import org.gcube.documentstore.records.Record;
|
||||
import org.gcube.documentstore.records.implementation.FieldAction;
|
||||
|
||||
|
||||
public class ValidBooleanValidator implements FieldAction {
|
||||
|
||||
private static final String ERROR = String.format("Not Instance of %s", Boolean.class.getSimpleName());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException {
|
||||
if(value instanceof Boolean){
|
||||
return value;
|
||||
}
|
||||
|
||||
try {
|
||||
if(value instanceof String){
|
||||
try{
|
||||
Boolean booleanObj = Boolean.valueOf((String) value);
|
||||
if(booleanObj !=null){
|
||||
return booleanObj;
|
||||
}
|
||||
} catch(Exception e){
|
||||
// Trying another way
|
||||
}
|
||||
|
||||
try{
|
||||
Integer integer = Integer.getInteger((String) value);
|
||||
if(integer!=null){
|
||||
value = integer;
|
||||
}
|
||||
} catch(Exception e){
|
||||
// Trying another way
|
||||
}
|
||||
}
|
||||
|
||||
if(value instanceof Integer){
|
||||
Boolean booleanObj = ((Integer) value) == 0 ? false : true;
|
||||
if(booleanObj !=null){
|
||||
return booleanObj;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}catch(Exception e){
|
||||
throw new InvalidValueException(ERROR, e);
|
||||
}
|
||||
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue