2015-06-26 15:50:39 +02:00
|
|
|
package org.gcube.accounting.datamodel.validations.validators;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
2015-07-29 14:41:28 +02:00
|
|
|
import org.gcube.accounting.datamodel.basetypes.AbstractStorageUsageRecord.DataType;
|
2017-05-11 16:38:38 +02:00
|
|
|
|
2015-12-18 17:09:05 +01:00
|
|
|
import org.gcube.documentstore.exception.InvalidValueException;
|
2015-12-21 11:50:09 +01:00
|
|
|
import org.gcube.documentstore.records.Record;
|
|
|
|
import org.gcube.documentstore.records.implementation.FieldAction;
|
2015-06-26 15:50:39 +02:00
|
|
|
|
2016-10-12 14:15:28 +02:00
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2015-06-26 15:50:39 +02:00
|
|
|
public class ValidDataTypeValidator implements FieldAction {
|
|
|
|
|
|
|
|
private static final String ERROR = String.format("Not Instance of %s", DataType.class.getSimpleName());
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
@Override
|
2016-01-28 18:29:11 +01:00
|
|
|
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException {
|
2015-06-26 15:50:39 +02:00
|
|
|
if(value instanceof DataType){
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if(value instanceof String){
|
|
|
|
try{
|
|
|
|
DataType dataType = DataType.valueOf((String) value);
|
|
|
|
if(dataType !=null){
|
|
|
|
return dataType;
|
|
|
|
}
|
|
|
|
} 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){
|
|
|
|
return DataType.values()[(Integer) value];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(value instanceof Enum){
|
|
|
|
return DataType.values()[((Enum) value).ordinal()];
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch(Exception e){
|
2015-07-22 17:28:30 +02:00
|
|
|
throw new InvalidValueException(ERROR, e);
|
2015-06-26 15:50:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new InvalidValueException(ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|