Adding a reference implementation
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@121995 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
aadec044b3
commit
e502cfbae6
|
@ -0,0 +1,402 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.documentstore.records.implementation;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
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.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractRecord implements Record {
|
||||
|
||||
/**
|
||||
* Generated Serial Version UID
|
||||
*/
|
||||
private static final long serialVersionUID = -2060728578456796388L;
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AbstractRecord.class);
|
||||
|
||||
@RequiredField @NotEmpty
|
||||
private static final String ID = Record.ID;
|
||||
|
||||
@RequiredField @ValidLong
|
||||
private static final String CREATION_TIME = Record.CREATION_TIME;
|
||||
|
||||
@RequiredField @NotEmpty
|
||||
private static final String RECORD_TYPE = Record.RECORD_TYPE;
|
||||
|
||||
/** resource-specific properties */
|
||||
protected Map<String, Comparable<? extends Serializable>> resourceProperties;
|
||||
|
||||
protected Map<String, List<FieldAction>> validation;
|
||||
|
||||
protected Set<String> requiredFields;
|
||||
protected Set<String> computedFields;
|
||||
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;
|
||||
}
|
||||
|
||||
protected void initializeValidation() {
|
||||
//logger.trace("Initializing Field Validators");
|
||||
Set<Field> fields = getAllFields(this.getClass());
|
||||
|
||||
for(Field field : fields){
|
||||
boolean defaultAccessibility = field.isAccessible();
|
||||
field.setAccessible(true);
|
||||
String keyString;
|
||||
try {
|
||||
keyString = (String) field.get(null);
|
||||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
List<FieldAction> fieldValidators = new ArrayList<FieldAction>();
|
||||
validation.put(keyString, fieldValidators);
|
||||
for (Annotation annotation : field.getAnnotations()){
|
||||
if (annotation.annotationType().isAnnotationPresent(FieldDecorator.class)){
|
||||
Class<? extends FieldAction> managedClass = ((FieldDecorator)annotation.annotationType().getAnnotation(FieldDecorator.class)).action();
|
||||
FieldAction validator;
|
||||
try {
|
||||
validator = managedClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
logger.error("{} {}", keyString, annotation, e);
|
||||
continue;
|
||||
}
|
||||
fieldValidators.add(validator);
|
||||
}
|
||||
if(annotation.annotationType().isAssignableFrom(RequiredField.class)){
|
||||
requiredFields.add(keyString);
|
||||
}
|
||||
if(annotation.annotationType().isAssignableFrom(AggregatedField.class)){
|
||||
aggregatedFields.add(keyString);
|
||||
}
|
||||
if(annotation.annotationType().isAssignableFrom(ComputedField.class)){
|
||||
computedFields.add(keyString);
|
||||
}
|
||||
}
|
||||
field.setAccessible(defaultAccessibility);
|
||||
}
|
||||
/*
|
||||
logger.trace("Required Fields {}", requiredFields);
|
||||
logger.trace("Aggregated Fields {}", aggregatedFields);
|
||||
logger.trace("Computed Fields {}", computedFields);
|
||||
*/
|
||||
}
|
||||
|
||||
protected void cleanExtraFields(){
|
||||
Set<String> neededFields = this.requiredFields;
|
||||
neededFields.addAll(this.aggregatedFields);
|
||||
|
||||
Set<String> keysToRemove = new HashSet<String>();
|
||||
Set<String> propertyKeys = this.resourceProperties.keySet();
|
||||
for(String propertyName : propertyKeys){
|
||||
if(!neededFields.contains(propertyName)){
|
||||
keysToRemove.add(propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
for(String keyToRemove : keysToRemove){
|
||||
this.resourceProperties.remove(keyToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize variable
|
||||
*/
|
||||
private void init() {
|
||||
this.validation = new HashMap<String, List<FieldAction>>();
|
||||
this.requiredFields = new HashSet<String>();
|
||||
this.aggregatedFields = new HashSet<String>();
|
||||
this.computedFields = new HashSet<String>();
|
||||
this.resourceProperties = new HashMap<String, Comparable<? extends Serializable>>();
|
||||
initializeValidation();
|
||||
}
|
||||
|
||||
public AbstractRecord(){
|
||||
init();
|
||||
this.resourceProperties.put(ID, UUID.randomUUID().toString());
|
||||
this.setRecordType();
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
this.resourceProperties.put(CREATION_TIME, calendar.getTimeInMillis());
|
||||
}
|
||||
|
||||
public AbstractRecord(Map<String, Comparable<? extends Serializable>> properties) throws InvalidValueException {
|
||||
init();
|
||||
setResourceProperties(properties);
|
||||
if(this instanceof AggregatedRecord){
|
||||
this.resourceProperties.put(AggregatedRecord.AGGREGATED, true);
|
||||
cleanExtraFields();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Set<String> getRequiredFields() {
|
||||
return requiredFields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRecordType() {
|
||||
return (String) this.resourceProperties.get(RECORD_TYPE);
|
||||
}
|
||||
|
||||
protected abstract String giveMeRecordType();
|
||||
|
||||
protected void setRecordType(){
|
||||
this.resourceProperties.put(RECORD_TYPE, this.giveMeRecordType());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String getId() {
|
||||
return (String) this.resourceProperties.get(ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void setId(String id) throws InvalidValueException {
|
||||
setResourceProperty(ID, id);
|
||||
}
|
||||
|
||||
protected Calendar timestampStringToCalendar(long millis){
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeInMillis(millis);
|
||||
return calendar;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Calendar getCreationTime() {
|
||||
long millis = (Long) this.resourceProperties.get(CREATION_TIME);
|
||||
return timestampStringToCalendar(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void setCreationTime(Calendar creationTime) throws InvalidValueException {
|
||||
setResourceProperty(CREATION_TIME, creationTime.getTimeInMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Comparable<? extends Serializable>> getResourceProperties() {
|
||||
return new HashMap<String, Comparable<? extends Serializable>>(this.resourceProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void setResourceProperties(Map<String, Comparable<? extends Serializable>> properties) throws InvalidValueException {
|
||||
Map<String, Comparable<? extends Serializable>> validated = validateProperties(properties);
|
||||
this.resourceProperties = new HashMap<String, Comparable<? extends Serializable>>(validated);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Comparable<? extends Serializable> getResourceProperty(String key) {
|
||||
return this.resourceProperties.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void setResourceProperty(String key, Comparable<? extends Serializable> value) throws InvalidValueException {
|
||||
Comparable<? extends Serializable> checkedValue = validateField(key, value);
|
||||
if(checkedValue == null){
|
||||
this.resourceProperties.remove(key);
|
||||
}else{
|
||||
this.resourceProperties.put(key, checkedValue);
|
||||
}
|
||||
}
|
||||
|
||||
// AGGREGATION
|
||||
/* --------------------------------------- */
|
||||
|
||||
private static final String START_TIME = AggregatedRecord.START_TIME;
|
||||
private static final String END_TIME = AggregatedRecord.END_TIME;
|
||||
private static final String OPERATION_COUNT = AggregatedRecord.OPERATION_COUNT;
|
||||
|
||||
/**
|
||||
* Return the left end of the time interval covered by this {#UsageRecord}
|
||||
* @return Start Time
|
||||
*/
|
||||
protected long getStartTimeInMillis() {
|
||||
return (Long) this.resourceProperties.get(START_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the left end of the time interval covered by this {#UsageRecord}
|
||||
* @return Start Time
|
||||
*/
|
||||
protected Calendar getStartTimeAsCalendar() {
|
||||
long millis = getStartTimeInMillis();
|
||||
return timestampStringToCalendar(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the left end of the time interval covered by this {#UsageRecord}
|
||||
* @param startTime Start Time
|
||||
* @throws InvalidValueException
|
||||
*/
|
||||
protected void setStartTime(Calendar startTime) throws InvalidValueException {
|
||||
setResourceProperty(START_TIME, startTime.getTimeInMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the right end of the time interval covered by this {#UsageRecord}
|
||||
* @return End Time
|
||||
*/
|
||||
protected long getEndTimeInMillis() {
|
||||
return (Long) this.resourceProperties.get(END_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the right end of the time interval covered by this {#UsageRecord}
|
||||
* @return End Time
|
||||
*/
|
||||
protected Calendar getEndTimeAsCalendar() {
|
||||
long millis = getEndTimeInMillis();
|
||||
return timestampStringToCalendar(millis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the right end of the time interval covered by this {#UsageRecord}
|
||||
* @param endTime End Time
|
||||
* @throws InvalidValueException
|
||||
*/
|
||||
protected void setEndTime(Calendar endTime) throws InvalidValueException {
|
||||
setResourceProperty(END_TIME, endTime.getTimeInMillis());
|
||||
}
|
||||
|
||||
protected int getOperationCount() {
|
||||
return (Integer) this.resourceProperties.get(OPERATION_COUNT);
|
||||
}
|
||||
|
||||
protected void setOperationCount(int operationCount) throws InvalidValueException {
|
||||
setResourceProperty(OPERATION_COUNT, operationCount);
|
||||
}
|
||||
|
||||
protected Comparable<? extends Serializable> validateField(String key, Comparable<? extends Serializable> value) throws InvalidValueException {
|
||||
if(key == null){
|
||||
throw new InvalidValueException("The key of property to set cannot be null");
|
||||
}
|
||||
Comparable<? extends Serializable> checkedValue = value;
|
||||
List<FieldAction> fieldValidators = validation.get(key);
|
||||
if(fieldValidators!=null){
|
||||
for(FieldAction fieldValidator : fieldValidators){
|
||||
if(aggregatedFields.contains(key)){
|
||||
// TODO
|
||||
}
|
||||
if(computedFields.contains(key)){
|
||||
logger.debug("{} is a computed field. To be calculated all the required fields to calcutalate it MUST be set. In any case the provided value will be ignored.", key);
|
||||
}
|
||||
try {
|
||||
checkedValue = fieldValidator.validate(key, checkedValue, this);
|
||||
} catch (InvalidValueException e) {
|
||||
logger.error(String.format("The provided value %s is NOT valid for field with key %s.", checkedValue.toString(), key));
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return checkedValue;
|
||||
}
|
||||
|
||||
protected Map<String, Comparable<? extends Serializable>> validateProperties(Map<String, Comparable<? extends Serializable>> properties) throws InvalidValueException{
|
||||
Map<String, Comparable<? extends Serializable>> validated = new HashMap<String, Comparable<? extends Serializable>>();
|
||||
for(String key : properties.keySet()){
|
||||
Comparable<? extends Serializable> value = properties.get(key);
|
||||
validated.put(key, validateField(key, value));
|
||||
}
|
||||
return validated;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void validate() throws InvalidValueException {
|
||||
validateProperties(this.resourceProperties);
|
||||
Set<String> notPresentProperties = new HashSet<String>();
|
||||
for(String key : this.requiredFields){
|
||||
if(!this.resourceProperties.containsKey(key)){
|
||||
notPresentProperties.add(key);
|
||||
}
|
||||
}
|
||||
if(!notPresentProperties.isEmpty()){
|
||||
String pluralManagement = notPresentProperties.size() == 1 ? "y" : "ies";
|
||||
throw new InvalidValueException(String.format("The Record does not contain the following required propert%s %s", pluralManagement, notPresentProperties.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return resourceProperties.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare this Record instance with the one provided as argument
|
||||
* @param record the Record to compare
|
||||
* @return 0 is and only if the UsageRecord provided as parameter
|
||||
* contains all and ONLY the parameters contained in this instance.
|
||||
* If the number of parameters differs, the methods return the difference
|
||||
* between the number of parameter in this instance and the ones in the
|
||||
* UsageRecord provided as parameter.
|
||||
* If the size is the same but the Record provided as parameter does
|
||||
* not contains all parameters in this instance, -1 is returned.
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Record record) {
|
||||
Set<Entry<String, Comparable<? extends Serializable>>> thisSet = this.resourceProperties.entrySet();
|
||||
Set<Entry<String, Comparable<? extends Serializable>>> usageRecordSet = record.getResourceProperties().entrySet();
|
||||
if(thisSet.size() != usageRecordSet.size()){
|
||||
return thisSet.size() - usageRecordSet.size();
|
||||
}
|
||||
if(usageRecordSet.containsAll(thisSet)){
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package org.gcube.documentstore.records.implementation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* This annotations indicates that the field is related to an aggregation
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AggregatedField {
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package org.gcube.documentstore.records.implementation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* This annotations indicates that the field is calculated using the
|
||||
* value of other field in the instance
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ComputedField {
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.documentstore.records.implementation;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
import org.gcube.documentstore.records.Record;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*/
|
||||
public interface FieldAction {
|
||||
|
||||
/**
|
||||
* Validate (and eventually convert) the value of the property identified by
|
||||
* the key.
|
||||
* @param key The key of the property
|
||||
* @param value The value to be validated (and eventually converted) of the
|
||||
* property
|
||||
* @param record the record the property is attached
|
||||
* @return the validated (and eventually converted) value of the property
|
||||
* @throws InvalidValueException if the validation or the eventual
|
||||
* conversion fails
|
||||
*/
|
||||
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException;
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.documentstore.records.implementation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
@Target(ElementType.ANNOTATION_TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
public @interface FieldDecorator {
|
||||
|
||||
Class<? extends FieldAction> action();
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package org.gcube.documentstore.records.implementation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface RequiredField {
|
||||
|
||||
}
|
|
@ -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.NotEmptyValidator;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@FieldDecorator(action=NotEmptyValidator.class)
|
||||
public @interface NotEmpty {
|
||||
|
||||
}
|
|
@ -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.NotEmptyIfNotNullValidator;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@FieldDecorator(action=NotEmptyIfNotNullValidator.class)
|
||||
public @interface NotEmptyIfNotNull {
|
||||
|
||||
}
|
|
@ -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.NotNullValidator;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@FieldDecorator(action=NotNullValidator.class)
|
||||
public @interface NotNull {
|
||||
|
||||
}
|
|
@ -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.ValidIntegerValidator;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@FieldDecorator(action=ValidIntegerValidator.class)
|
||||
public @interface ValidInteger {
|
||||
|
||||
}
|
|
@ -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.ValidLongValidator;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@FieldDecorator(action=ValidLongValidator.class)
|
||||
public @interface ValidLong {
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package org.gcube.documentstore.records.implementation.validations.validators;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
public class NotEmptyIfNotNullValidator extends NotEmptyValidator {
|
||||
|
||||
protected boolean isValid(Serializable toValidate) {
|
||||
if (toValidate == null) return true;
|
||||
if (toValidate.getClass().isArray() ){
|
||||
return ((Object[])toValidate).length>0;
|
||||
}else if ( toValidate instanceof Iterable<?>){
|
||||
return ((Iterable<?>) toValidate).iterator().hasNext();
|
||||
} else if (toValidate instanceof Map<?,?>){
|
||||
return ((Map<?,?>) toValidate).size()>0;
|
||||
} else if (toValidate instanceof String ){
|
||||
return !((String)toValidate).isEmpty();
|
||||
} else return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package org.gcube.documentstore.records.implementation.validations.validators;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.documentstore.exception.InvalidValueException;
|
||||
import org.gcube.documentstore.records.Record;
|
||||
import org.gcube.documentstore.records.implementation.FieldAction;
|
||||
|
||||
public class NotEmptyValidator implements FieldAction {
|
||||
|
||||
private static final String ERROR = "Is Empty";
|
||||
|
||||
protected boolean isValid(Serializable toValidate) {
|
||||
if (toValidate == null) return false;
|
||||
if (toValidate.getClass().isArray() ){
|
||||
return ((Object[])toValidate).length>0;
|
||||
}else if ( toValidate instanceof Iterable<?>){
|
||||
return ((Iterable<?>) toValidate).iterator().hasNext();
|
||||
} else if (toValidate instanceof Map<?,?>){
|
||||
return ((Map<?,?>) toValidate).size()>0;
|
||||
} else if (toValidate instanceof String ){
|
||||
return !((String)toValidate).isEmpty();
|
||||
} else return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException {
|
||||
try{
|
||||
if(isValid((Serializable) value)){
|
||||
return value;
|
||||
}
|
||||
}catch(Exception e){
|
||||
throw new InvalidValueException(ERROR, e);
|
||||
}
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
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 NotNullValidator implements FieldAction {
|
||||
|
||||
private static final String ERROR = "Is Null";
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException {
|
||||
if(value!=null){
|
||||
return value;
|
||||
}
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
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 ValidIntegerValidator implements FieldAction {
|
||||
|
||||
private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException {
|
||||
if(value instanceof Integer){
|
||||
return value;
|
||||
}
|
||||
Integer integerObj = Integer.valueOf((String) value);
|
||||
if(integerObj!=null){
|
||||
return integerObj;
|
||||
}
|
||||
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
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 ValidLongValidator implements FieldAction {
|
||||
|
||||
private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName());
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException {
|
||||
if(value instanceof Long){
|
||||
return value;
|
||||
}
|
||||
try {
|
||||
Long longObj = Long.valueOf((String) value);
|
||||
if(longObj!=null){
|
||||
return longObj;
|
||||
}
|
||||
}catch (Exception e) {
|
||||
throw new InvalidValueException(ERROR, e);
|
||||
}
|
||||
throw new InvalidValueException(ERROR);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue