Fixing internals

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@122625 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-01-28 17:26:01 +00:00
parent 1e8d5d6c0c
commit a7936a47ae
10 changed files with 94 additions and 45 deletions

View File

@ -84,19 +84,19 @@ public interface Record extends Comparable<Record>, Serializable {
* not affect the object
* @return a Map containing the properties
*/
public Map<String, Comparable<? extends Serializable>> getResourceProperties();
public Map<String, Serializable> getResourceProperties();
/**
* Set all resource-specific properties, replacing existing ones
*/
public void setResourceProperties(Map<String, Comparable<? extends Serializable>> resourceSpecificProperties) throws InvalidValueException;
public void setResourceProperties(Map<String, ? extends Serializable> resourceSpecificProperties) throws InvalidValueException;
/**
* Return the value of the given resource property.
* @param key the key of the requested property
* @return the value of the given resource property
*/
public Comparable<? extends Serializable> getResourceProperty(String key);
public Serializable getResourceProperty(String key);
/**
* Set the value of the given resource property.
@ -105,8 +105,7 @@ public interface Record extends Comparable<Record>, Serializable {
* @param key the key of the requested property
* @param value the value of the given resource property
*/
public void setResourceProperty(String key, Comparable<? extends Serializable> value) throws InvalidValueException;
public void setResourceProperty(String key, Serializable value) throws InvalidValueException;
/**
* Validate the Resource Record.

View File

@ -131,7 +131,7 @@ public abstract class RecordUtility {
* }
*/
protected static Map<String, Comparable<? extends Serializable>> getMapFromString(String serializedMap){
protected static Map<String, ? extends Serializable> getMapFromString(String serializedMap){
/* Checking line sanity */
if(!serializedMap.startsWith(LINE_FREFIX) && !serializedMap.endsWith(LINE_SUFFIX)){
return null;
@ -141,7 +141,7 @@ public abstract class RecordUtility {
serializedMap = serializedMap.replace(LINE_FREFIX, "");
serializedMap = serializedMap.replace(LINE_SUFFIX, "");
Map<String, Comparable<? extends Serializable>> map = new HashMap<String, Comparable<? extends Serializable>>();
Map<String, Serializable> map = new HashMap<String,Serializable>();
String[] pairs = serializedMap.split(KEY_VALUE_PAIR_SEPARATOR);
for (int i=0;i<pairs.length;i++) {
@ -150,7 +150,7 @@ public abstract class RecordUtility {
String[] keyValue = pair.split(KEY_VALUE_LINKER);
String key = keyValue[0].trim();
Comparable<? extends Serializable> value = keyValue[1].trim();
Serializable value = keyValue[1].trim();
map.put(key, value);
}
@ -165,7 +165,7 @@ public abstract class RecordUtility {
* @throws Exception if deserialization fails
*/
public static Record getRecord(String serializedMap) throws Exception {
Map<String,Comparable<? extends Serializable>> map = getMapFromString(serializedMap);
Map<String,? extends Serializable> map = getMapFromString(serializedMap);
return getRecord(map);
}
@ -175,7 +175,7 @@ public abstract class RecordUtility {
* @return the Record
* @throws Exception if deserialization fails
*/
public static Record getRecord(Map<String, Comparable<? extends Serializable>> recordMap) throws Exception {
public static Record getRecord(Map<String, ? extends Serializable> recordMap) throws Exception {
String className = (String) recordMap.get(Record.RECORD_TYPE);
boolean aggregated = false;

View File

@ -3,6 +3,7 @@
*/
package org.gcube.documentstore.records.aggregation;
import java.io.Serializable;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
@ -82,12 +83,20 @@ public class AggregationUtility<T extends AggregatedRecord<T,?>> {
@SuppressWarnings("unchecked")
public boolean isAggregable(T record) {
for(String field : aggregationFields){
@SuppressWarnings("rawtypes")
Comparable recordValue = record.getResourceProperty(field);
@SuppressWarnings("rawtypes")
Comparable thisValue = t.getResourceProperty(field);
if(recordValue.compareTo(thisValue)!=0){
return false;
Serializable recordValue = record.getResourceProperty(field);
Serializable thisValue = t.getResourceProperty(field);
if(recordValue instanceof Comparable && thisValue instanceof Comparable){
@SuppressWarnings("rawtypes")
Comparable recordValueComparable = (Comparable) recordValue;
@SuppressWarnings("rawtypes")
Comparable thisValueComparable = (Comparable) thisValue;
if(recordValueComparable.compareTo(thisValueComparable)!=0){
return false;
}
}else{
if(recordValue.hashCode()!=this.hashCode()){
return false;
}
}
}
return true;

View File

@ -48,9 +48,10 @@ public abstract class AbstractRecord implements Record {
protected static final String RECORD_TYPE = Record.RECORD_TYPE;
/** resource-specific properties */
protected Map<String, Comparable<? extends Serializable>> resourceProperties;
protected Map<String, Serializable> resourceProperties;
protected Map<String, List<FieldAction>> validation;
protected Map<String, List<FieldAction>> computation;
protected Set<String> requiredFields;
protected Set<String> computedFields;
@ -78,9 +79,14 @@ public abstract class AbstractRecord implements Record {
}
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()){
if (annotation.annotationType().isAnnotationPresent(FieldDecorator.class)){
Class<? extends FieldAction> managedClass = ((FieldDecorator)annotation.annotationType().getAnnotation(FieldDecorator.class)).action();
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();
@ -90,14 +96,23 @@ public abstract class AbstractRecord implements Record {
}
fieldValidators.add(validator);
}
if(annotation.annotationType().isAssignableFrom(RequiredField.class)){
if(annotationType.isAssignableFrom(RequiredField.class)){
requiredFields.add(keyString);
}
if(annotation.annotationType().isAssignableFrom(AggregatedField.class)){
if(annotationType.isAssignableFrom(AggregatedField.class)){
aggregatedFields.add(keyString);
}
if(annotation.annotationType().isAssignableFrom(ComputedField.class)){
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);
}
}
field.setAccessible(defaultAccessibility);
@ -131,10 +146,11 @@ public abstract class AbstractRecord implements Record {
*/
protected void init() {
this.validation = new HashMap<String, List<FieldAction>>();
this.computation = 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>>();
this.resourceProperties = new HashMap<String, Serializable>();
initializeValidation();
}
@ -146,7 +162,7 @@ public abstract class AbstractRecord implements Record {
this.resourceProperties.put(CREATION_TIME, calendar.getTimeInMillis());
}
public AbstractRecord(Map<String, Comparable<? extends Serializable>> properties) throws InvalidValueException {
public AbstractRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException {
init();
setResourceProperties(properties);
if(this instanceof AggregatedRecord){
@ -217,24 +233,24 @@ public abstract class AbstractRecord implements Record {
* {@inheritDoc}
*/
@Override
public Map<String, Comparable<? extends Serializable>> getResourceProperties() {
return new HashMap<String, Comparable<? extends Serializable>>(this.resourceProperties);
public Map<String, Serializable> getResourceProperties() {
return new HashMap<String, 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);
public void setResourceProperties(Map<String, ? extends Serializable> properties) throws InvalidValueException {
Map<String, ? extends Serializable> validated = validateProperties(properties);
this.resourceProperties = new HashMap<String, Serializable>(validated);
}
/**
* {@inheritDoc}
*/
@Override
public Comparable<? extends Serializable> getResourceProperty(String key) {
public Serializable getResourceProperty(String key) {
return this.resourceProperties.get(key);
}
@ -242,8 +258,8 @@ public abstract class AbstractRecord implements Record {
* {@inheritDoc}
*/
@Override
public void setResourceProperty(String key, Comparable<? extends Serializable> value) throws InvalidValueException {
Comparable<? extends Serializable> checkedValue = validateField(key, value);
public void setResourceProperty(String key, Serializable value) throws InvalidValueException {
Serializable checkedValue = validateField(key, value);
if(checkedValue == null){
this.resourceProperties.remove(key);
}else{
@ -314,11 +330,11 @@ public abstract class AbstractRecord implements Record {
return timestampToCalendar(millis);
}
protected Comparable<? extends Serializable> validateField(String key, Comparable<? extends Serializable> value) throws InvalidValueException {
protected Serializable validateField(String key, Serializable value) throws InvalidValueException {
if(key == null){
throw new InvalidValueException("The key of property to set cannot be null");
}
Comparable<? extends Serializable> checkedValue = value;
Serializable checkedValue = value;
List<FieldAction> fieldValidators = validation.get(key);
if(fieldValidators!=null){
for(FieldAction fieldValidator : fieldValidators){
@ -339,10 +355,30 @@ public abstract class AbstractRecord implements Record {
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>>();
protected void computeField(String key) throws InvalidValueException {
if(key == null){
throw new InvalidValueException("The key of property to set cannot be null");
}
Serializable computedValue = null;
List<FieldAction> fieldComputations = computation.get(key);
if(fieldComputations!=null){
for(FieldAction fieldValidator : fieldComputations){
try {
computedValue = fieldValidator.validate(key, null, this);
this.resourceProperties.put(key, computedValue);
} catch (InvalidValueException e) {
logger.error(String.format("Unable to calculate the field with key %s", key));
throw e;
}
}
}
}
protected Map<String, ? extends Serializable> validateProperties(Map<String, ? extends Serializable> properties) throws InvalidValueException{
Map<String, Serializable> validated = new HashMap<String, Serializable>();
for(String key : properties.keySet()){
Comparable<? extends Serializable> value = properties.get(key);
Serializable value = properties.get(key);
validated.put(key, validateField(key, value));
}
return validated;
@ -353,6 +389,10 @@ public abstract class AbstractRecord implements Record {
*/
@Override
public void validate() throws InvalidValueException {
for(String key : this.computedFields){
computeField(key);
}
validateProperties(this.resourceProperties);
Set<String> notPresentProperties = new HashSet<String>();
for(String key : this.requiredFields){
@ -384,8 +424,8 @@ public abstract class AbstractRecord implements Record {
*/
@Override
public int compareTo(Record record) {
Set<Entry<String, Comparable<? extends Serializable>>> thisSet = this.resourceProperties.entrySet();
Set<Entry<String, Comparable<? extends Serializable>>> recordSet = record.getResourceProperties().entrySet();
Set<Entry<String, Serializable>> thisSet = this.resourceProperties.entrySet();
Set<Entry<String, Serializable>> recordSet = record.getResourceProperties().entrySet();
if(thisSet.size() != recordSet.size()){
return thisSet.size() - recordSet.size();
}

View File

@ -13,5 +13,6 @@ import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ComputedField {
Class<? extends FieldAction> action();
}

View File

@ -24,6 +24,6 @@ public interface FieldAction {
* @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;
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException;
}

View File

@ -28,7 +28,7 @@ public class NotEmptyValidator implements FieldAction {
* {@inheritDoc}
*/
@Override
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException {
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException {
try{
if(isValid((Serializable) value)){
return value;

View File

@ -14,7 +14,7 @@ public class NotNullValidator implements FieldAction {
* {@inheritDoc}
*/
@Override
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException {
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException {
if(value!=null){
return value;
}

View File

@ -15,7 +15,7 @@ public class ValidIntegerValidator implements FieldAction {
* {@inheritDoc}
*/
@Override
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException {
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException {
if(value instanceof Integer){
return value;
}

View File

@ -15,7 +15,7 @@ public class ValidLongValidator implements FieldAction {
* {@inheritDoc}
*/
@Override
public Comparable<? extends Serializable> validate(String key, Comparable<? extends Serializable> value, Record record) throws InvalidValueException {
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException {
if(value instanceof Long){
return value;
}