Fixed jackson serialization

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@153095 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-09-14 12:21:34 +00:00
parent 160a1c2cc8
commit 07dfa4248f
5 changed files with 75 additions and 62 deletions

View File

@ -10,6 +10,11 @@ import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
import org.gcube.documentstore.records.implementation.AggregatedField;
import org.gcube.documentstore.records.implementation.RequiredField;
import org.gcube.documentstore.records.implementation.validations.annotations.ValidBoolean;
import org.gcube.documentstore.records.implementation.validations.annotations.ValidInteger;
import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
@ -20,13 +25,13 @@ public interface AggregatedRecord<A extends AggregatedRecord<A,R>, R extends Rec
/**
* KEY : Indicate that this {@link Record} is an aggregation
*/
@RequiredField @AggregatedField
@RequiredField @AggregatedField @ValidBoolean
public static final String AGGREGATED = "aggregated";
/**
* KEY : Indicate The Number of {@link AggregatedRecord}
*/
@RequiredField @AggregatedField
@RequiredField @AggregatedField @ValidInteger
public static final String OPERATION_COUNT = "operationCount";
/**
@ -34,7 +39,7 @@ public interface AggregatedRecord<A extends AggregatedRecord<A,R>, R extends Rec
* {@link AggregatedRecord}. The value will be recorded in UTC milliseconds
* from the epoch.
*/
@RequiredField @AggregatedField
@RequiredField @AggregatedField @ValidLong
public static final String START_TIME = "startTime";
/**
@ -42,7 +47,7 @@ public interface AggregatedRecord<A extends AggregatedRecord<A,R>, R extends Rec
* {@link AggregatedRecord}. The value will be recorded in UTC milliseconds
* from the epoch.
*/
@RequiredField @AggregatedField
@RequiredField @AggregatedField @ValidLong
public static final String END_TIME = "endTime";
/**
@ -50,28 +55,36 @@ public interface AggregatedRecord<A extends AggregatedRecord<A,R>, R extends Rec
* The returned Set MUST be a copy of the internal representation.
* Any modification to the returned Set MUST not affect the object
*/
@JsonIgnore
public Set<String> getAggregatedFields();
public int getOperationCount();
public void setOperationCount(int operationCount) throws InvalidValueException;
@JsonIgnore
public Calendar getStartTime();
@JsonIgnore
public void setStartTime(Calendar startTime) throws InvalidValueException;
@JsonIgnore
public Calendar getEndTime();
@JsonIgnore
public void setEndTime(Calendar endTime) throws InvalidValueException;
public A aggregate(A record) throws NotAggregatableRecordsExceptions;
public A aggregate(R record) throws NotAggregatableRecordsExceptions;
@JsonIgnore
public boolean isAggregable(A record) throws NotAggregatableRecordsExceptions;
@JsonIgnore
public boolean isAggregable(R record) throws NotAggregatableRecordsExceptions;
@JsonIgnore
public Class<R> getAggregable();
}

View File

@ -69,7 +69,6 @@ public interface Record extends Comparable<Record>, Serializable {
* Return the {@link Record} Type
* @return {@link Record} Type
*/
@JsonIgnore
public String getRecordType();
@ -77,7 +76,6 @@ public interface Record extends Comparable<Record>, Serializable {
* Return the unique id for this {@link Record}
* @return {@link Record} Unique ID
*/
@JsonIgnore
public String getId();
/**
@ -87,7 +85,6 @@ public interface Record extends Comparable<Record>, Serializable {
* @param id Unique ID
* @throws InvalidValueException
*/
@JsonIgnore
public void setId(String id) throws InvalidValueException;
/**
@ -119,6 +116,7 @@ public interface Record extends Comparable<Record>, Serializable {
/**
* Set all resource-specific properties, replacing existing ones
*/
@JsonIgnore
public void setResourceProperties(Map<String, ? extends Serializable> resourceSpecificProperties) throws InvalidValueException;
/**
@ -126,6 +124,7 @@ public interface Record extends Comparable<Record>, Serializable {
* @param key the key of the requested property
* @return the value of the given resource property
*/
@JsonIgnore
public Serializable getResourceProperty(String key);
/**
@ -143,6 +142,7 @@ public interface Record extends Comparable<Record>, Serializable {
* This API is intended for intern use only.
* @param key the key of the requested property to remove
*/
@JsonIgnore
public void removeResourceProperty(String key);
/**

View File

@ -21,7 +21,6 @@ import java.util.UUID;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.records.AggregatedRecord;
import org.gcube.documentstore.records.CustomMapDeserializer;
import org.gcube.documentstore.records.Record;
import org.gcube.documentstore.records.implementation.validations.annotations.NotEmpty;
import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong;
@ -29,11 +28,8 @@ import org.gcube.documentstore.records.implementation.validations.validators.Val
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
/**
* @author Luca Frosini (ISTI - CNR)
@ -54,19 +50,12 @@ public abstract class AbstractRecord implements Record {
protected static final String CREATION_TIME = Record.CREATION_TIME;
/** resource-specific properties */
@JsonDeserialize(using = CustomMapDeserializer.class)
@JsonIgnoreProperties(ignoreUnknown = true)
protected Map<String, Serializable> resourceProperties;
protected Map<String, List<FieldAction>> validation;
protected Map<String, List<FieldAction>> computation;
@JsonIgnore
protected Set<String> requiredFields;
@JsonIgnore
protected Set<String> computedFields;
@JsonIgnore
protected Set<String> aggregatedFields;
protected static Set<Field> getAllFields(Class<?> type) {
@ -188,7 +177,6 @@ public abstract class AbstractRecord implements Record {
}
@JsonIgnoreProperties(ignoreUnknown = true)
public AbstractRecord(){
init();
this.resourceProperties.put(ID, UUID.randomUUID().toString());
@ -197,7 +185,6 @@ public abstract class AbstractRecord implements Record {
}
@JsonIgnoreProperties(ignoreUnknown = true)
public AbstractRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException {
init();
setResourceProperties(properties);
@ -211,6 +198,7 @@ public abstract class AbstractRecord implements Record {
* {@inheritDoc}
*/
@Override
@JsonIgnore
public Set<String> getRequiredFields() {
return new HashSet<String>(requiredFields);
}
@ -219,6 +207,7 @@ public abstract class AbstractRecord implements Record {
* {@inheritDoc}
*/
@Override
@JsonIgnore
public Set<String> getComputedFields() {
return new HashSet<String>(computedFields);
}
@ -226,6 +215,7 @@ public abstract class AbstractRecord implements Record {
/**
* {@inheritDoc}
*/
@JsonIgnore
public Set<String> getAggregatedFields() {
return new HashSet<String>(aggregatedFields);
}
@ -277,8 +267,6 @@ public abstract class AbstractRecord implements Record {
/**
* {@inheritDoc}
*/
//insert here for discovery auto
//@JsonAnyGetter
@Override
public Map<String, Serializable> getResourceProperties() {
return new HashMap<String, Serializable>(this.resourceProperties);
@ -287,9 +275,6 @@ public abstract class AbstractRecord implements Record {
/**
* {@inheritDoc}
*/
@JsonAnySetter
@Override
public void setResourceProperties(Map<String, ? extends Serializable> properties) throws InvalidValueException {
Map<String, ? extends Serializable> validated = validateProperties(properties);
@ -299,7 +284,6 @@ public abstract class AbstractRecord implements Record {
/**
* {@inheritDoc}
*/
@JsonAnyGetter
@Override
public Serializable getResourceProperty(String key) {
return this.resourceProperties.get(key);
@ -313,7 +297,6 @@ public abstract class AbstractRecord implements Record {
/**
* {@inheritDoc}
*/
@JsonAnySetter
@Override
public void setResourceProperty(String key, Serializable value) throws InvalidValueException {
Serializable checkedValue = validateField(key, value);
@ -339,6 +322,23 @@ public abstract class AbstractRecord implements Record {
setResourceProperty(AggregatedRecord.END_TIME, endTime.getTimeInMillis());
}
/**
* Return the right end of the time interval covered by this Record
* @return End Time
*/
protected long getEndTimeInMillis() {
return (Long) this.resourceProperties.get(AggregatedRecord.END_TIME);
}
/**
* Return the right end of the time interval covered by this Record
* @return End Time
*/
protected Calendar getEndTimeAsCalendar() {
long millis = getEndTimeInMillis();
return timestampToCalendar(millis);
}
protected int getOperationCount() {
return (Integer) this.resourceProperties.get(AggregatedRecord.OPERATION_COUNT);
}
@ -373,42 +373,25 @@ public abstract class AbstractRecord implements Record {
setResourceProperty(AggregatedRecord.START_TIME, startTime.getTimeInMillis());
}
/**
* Return the right end of the time interval covered by this Record
* @return End Time
*/
protected long getEndTimeInMillis() {
return (Long) this.resourceProperties.get(AggregatedRecord.END_TIME);
}
//Introduce for to serialize Java Object
/**
* Set the boolean aggregate by this Record
* @param Boolean aggregate
* @throws InvalidValueException
*/
protected void setAggregate(Boolean aggregate) throws InvalidValueException {
protected void setAggregated(Boolean aggregate) throws InvalidValueException {
setResourceProperty(AggregatedRecord.AGGREGATED, aggregate);
}
/**
* Return the boolean aggregate by this Record
* @return Boolean Aggregate
*/
protected Boolean getAggregate() {
return (Boolean) this.resourceProperties.get(AggregatedRecord.AGGREGATED);
protected Boolean isAggregated() {
Boolean bool = (Boolean) this.resourceProperties.get(AggregatedRecord.AGGREGATED);
if(bool==null){
return false;
}
return bool;
}
//End Introduce for to serialize Java Object
/**
* Return the right end of the time interval covered by this Record
* @return End Time
*/
protected Calendar getEndTimeAsCalendar() {
long millis = getEndTimeInMillis();
return timestampToCalendar(millis);
}
protected Serializable validateField(String key, Serializable value) throws InvalidValueException {
if(key == null){
@ -458,7 +441,6 @@ public abstract class AbstractRecord implements Record {
}
}
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()){

View File

@ -18,13 +18,24 @@ public class ValidIntegerValidator implements FieldAction {
*/
@Override
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException {
if(value instanceof Integer){
return value;
}
Integer integerObj = Integer.valueOf((String) value);
if(integerObj!=null){
return integerObj;
}
try {
Integer integerObj = Integer.valueOf((String) value);
if(integerObj!=null){
return integerObj;
}
}catch (Exception e) {}
try {
Double doubleObj = Double.valueOf((String) value);
if(doubleObj!=null){
return doubleObj.intValue();
}
}catch (Exception e) {}
throw new InvalidValueException(ERROR);
}

View File

@ -21,14 +21,21 @@ public class ValidLongValidator implements FieldAction {
if(value instanceof Long){
return value;
}
try {
Long longObj = Long.valueOf((String) value);
if(longObj!=null){
return longObj;
return longObj.longValue();
}
}catch (Exception e) {
throw new InvalidValueException(ERROR, e);
}
}catch (Exception e) {}
try {
Double doubleObj = Double.valueOf((String) value);
if(doubleObj!=null){
return doubleObj.longValue();
}
}catch (Exception e) {}
throw new InvalidValueException(ERROR);
}