refs #200: Create accouting-lib library
https://support.d4science.org/issues/200 Fixing validators git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115255 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
4db4ebba0a
commit
0b1b2db21d
|
@ -394,7 +394,7 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
|
||||||
List<ValidatorAction> fieldValidators = validation.get(key);
|
List<ValidatorAction> fieldValidators = validation.get(key);
|
||||||
if(fieldValidators!=null){
|
if(fieldValidators!=null){
|
||||||
for(ValidatorAction fieldValidator : fieldValidators){
|
for(ValidatorAction fieldValidator : fieldValidators){
|
||||||
checkedValue = fieldValidator.validate(checkedValue);
|
checkedValue = fieldValidator.validate(key, checkedValue, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return checkedValue;
|
return checkedValue;
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.accounting.datamodel.decorators;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.UsageRecord;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
||||||
*/
|
|
||||||
public interface AggregatedAction {
|
|
||||||
|
|
||||||
public Serializable manage(String key, Serializable manage, UsageRecord usageRecord);
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.accounting.datamodel.decorators;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.UsageRecord;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
||||||
*/
|
|
||||||
public interface DeprecatedAction {
|
|
||||||
|
|
||||||
public Serializable manage(String key, Serializable manage, UsageRecord usageRecord);
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
package org.gcube.accounting.datamodel.decorators;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
@Target(ElementType.FIELD)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Inherited
|
|
||||||
public @interface DeprecatedField {
|
|
||||||
|
|
||||||
Class<? extends DeprecatedAction> action();
|
|
||||||
|
|
||||||
}
|
|
|
@ -5,19 +5,25 @@ package org.gcube.accounting.datamodel.decorators;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||||
*/
|
*/
|
||||||
public interface ValidatorAction {
|
public interface ValidatorAction {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate (and convert if needed) the provided property
|
* Validate (and eventually convert) the value of the property identified by
|
||||||
* @param toValidate the property to validate
|
* the key.
|
||||||
* @return the validated (and converted property)
|
* @param key The key of the property
|
||||||
* @throws InvalidValueException if the validation (or conversion) fails
|
* @param value The value to be validated (and eventually converted) of the
|
||||||
|
* property
|
||||||
|
* @param usageRecord the Usage 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 Serializable validate(Serializable toValidate) throws InvalidValueException;
|
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,11 @@ import java.util.Calendar;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.DeprecatedField;
|
|
||||||
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
|
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
|
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
|
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
|
||||||
import org.gcube.accounting.datamodel.validations.validators.ConvertToOperationResult;
|
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,7 +40,7 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord
|
||||||
@RequiredField @ValidLong
|
@RequiredField @ValidLong
|
||||||
public static final String JOB_END_TIME = "jobEndTime";
|
public static final String JOB_END_TIME = "jobEndTime";
|
||||||
|
|
||||||
@DeprecatedField(action=ConvertToOperationResult.class) @NotEmpty //TODO
|
@ValidOperationResult
|
||||||
protected static final String JOB_STATUS = "jobStatus";
|
protected static final String JOB_STATUS = "jobStatus";
|
||||||
|
|
||||||
@ValidInteger
|
@ValidInteger
|
||||||
|
|
|
@ -5,11 +5,10 @@ package org.gcube.accounting.datamodel.implementations;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.DeprecatedField;
|
|
||||||
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
||||||
|
import org.gcube.accounting.datamodel.validations.annotations.DeprecatedWarning;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
|
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull;
|
import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull;
|
||||||
import org.gcube.accounting.datamodel.validations.validators.WarningAlert;
|
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,7 +22,7 @@ public class PortletUsageRecord extends RawUsageRecord implements SingleUsageRec
|
||||||
*/
|
*/
|
||||||
private static final long serialVersionUID = 8339324883678974869L;
|
private static final long serialVersionUID = 8339324883678974869L;
|
||||||
|
|
||||||
@DeprecatedField(action=WarningAlert.class) @NotEmptyIfNotNull
|
@DeprecatedWarning @NotEmptyIfNotNull
|
||||||
protected static final String USER_ID = "userId";
|
protected static final String USER_ID = "userId";
|
||||||
|
|
||||||
@RequiredField @NotEmpty
|
@RequiredField @NotEmpty
|
||||||
|
|
|
@ -5,12 +5,11 @@ package org.gcube.accounting.datamodel.implementations;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.DeprecatedField;
|
|
||||||
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
||||||
|
import org.gcube.accounting.datamodel.validations.annotations.DeprecatedWarning;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
|
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.ValidIP;
|
import org.gcube.accounting.datamodel.validations.annotations.ValidIP;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
|
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
|
||||||
import org.gcube.accounting.datamodel.validations.validators.WarningAlert;
|
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,7 +31,7 @@ public class ServiceUsageRecord extends RawUsageRecord implements SingleUsageRec
|
||||||
public static final String REF_HOST = "refHost";
|
public static final String REF_HOST = "refHost";
|
||||||
@RequiredField @NotEmpty
|
@RequiredField @NotEmpty
|
||||||
public static final String REF_VM = "refVM";
|
public static final String REF_VM = "refVM";
|
||||||
@DeprecatedField(action=WarningAlert.class) @NotEmpty
|
@DeprecatedWarning @NotEmpty
|
||||||
public static final String DOMAIN = "domain";
|
public static final String DOMAIN = "domain";
|
||||||
@ValidInteger
|
@ValidInteger
|
||||||
protected static final String INVOCATION_COUNT = "invocationCount";
|
protected static final String INVOCATION_COUNT = "invocationCount";
|
||||||
|
|
|
@ -7,13 +7,12 @@ import java.util.Calendar;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.RawUsageRecord;
|
import org.gcube.accounting.datamodel.RawUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.DeprecatedField;
|
|
||||||
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
import org.gcube.accounting.datamodel.decorators.RequiredField;
|
||||||
|
import org.gcube.accounting.datamodel.validations.annotations.DeprecatedWarning;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
|
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
|
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
|
||||||
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
|
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
|
||||||
import org.gcube.accounting.datamodel.validations.validators.ConvertToOperationResult;
|
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult;
|
||||||
import org.gcube.accounting.datamodel.validations.validators.WarningAlert;
|
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,7 +27,7 @@ public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord
|
||||||
private static final long serialVersionUID = -2208425042550641240L;
|
private static final long serialVersionUID = -2208425042550641240L;
|
||||||
|
|
||||||
public static enum USAGE_PHASE {
|
public static enum USAGE_PHASE {
|
||||||
completed, failed
|
completed, failed
|
||||||
};
|
};
|
||||||
|
|
||||||
@RequiredField @NotEmpty
|
@RequiredField @NotEmpty
|
||||||
|
@ -37,13 +36,13 @@ public class TaskUsageRecord extends RawUsageRecord implements SingleUsageRecord
|
||||||
public static final String REF_HOST = "refHost";
|
public static final String REF_HOST = "refHost";
|
||||||
@RequiredField @NotEmpty
|
@RequiredField @NotEmpty
|
||||||
public static final String REF_VM = "refVM";
|
public static final String REF_VM = "refVM";
|
||||||
@DeprecatedField(action=WarningAlert.class)
|
@DeprecatedWarning
|
||||||
protected static final String DOMAIN = "domain";
|
protected static final String DOMAIN = "domain";
|
||||||
@RequiredField @ValidLong
|
@RequiredField @ValidLong
|
||||||
public static final String USAGE_START_TIME = "usageStartTime";
|
public static final String USAGE_START_TIME = "usageStartTime";
|
||||||
@RequiredField @ValidLong
|
@RequiredField @ValidLong
|
||||||
public static final String USAGE_END_TIME = "usageEndTime";
|
public static final String USAGE_END_TIME = "usageEndTime";
|
||||||
@DeprecatedField(action=ConvertToOperationResult.class) @NotEmpty
|
@ValidOperationResult
|
||||||
public static final String USAGE_PHASE = "usagePhase";
|
public static final String USAGE_PHASE = "usagePhase";
|
||||||
@ValidInteger
|
@ValidInteger
|
||||||
public static final String INPUT_FILES_NUMBER = "inputFilesNumber";
|
public static final String INPUT_FILES_NUMBER = "inputFilesNumber";
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package org.gcube.accounting.datamodel.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.accounting.datamodel.decorators.ValidatorField;
|
||||||
|
import org.gcube.accounting.datamodel.validations.validators.DeprecatedWarningValidator;
|
||||||
|
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@ValidatorField(managed=DeprecatedWarningValidator.class)
|
||||||
|
public @interface DeprecatedWarning {
|
||||||
|
|
||||||
|
}
|
|
@ -1,26 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.accounting.datamodel.validations.validators;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.UsageRecord;
|
|
||||||
import org.gcube.accounting.datamodel.decorators.DeprecatedAction;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ConvertToOperationResult implements DeprecatedAction {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Serializable manage(String key, Serializable manage, UsageRecord usageRecord) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.accounting.datamodel.validations.validators;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
|
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
||||||
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DeprecatedWarningValidator implements ValidatorAction {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(DeprecatedWarningValidator.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
|
||||||
|
logger.warn("The field {} is deprecated for {}. Anyway the field will be included in the document",
|
||||||
|
key, usageRecord.getClass().getSimpleName());
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package org.gcube.accounting.datamodel.validations.validators;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
|
@ -23,11 +24,14 @@ public class NotEmptyValidator implements ValidatorAction{
|
||||||
} else return true;
|
} else return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
|
||||||
try{
|
try{
|
||||||
if(isValid(toValidate)){
|
if(isValid(value)){
|
||||||
return toValidate;
|
return value;
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
throw new InvalidValueException(ERROR, e.getCause());
|
throw new InvalidValueException(ERROR, e.getCause());
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.gcube.accounting.datamodel.validations.validators;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
|
@ -9,10 +10,13 @@ public class NotNullValidator implements ValidatorAction {
|
||||||
|
|
||||||
private static final String ERROR = "Is Null";
|
private static final String ERROR = "Is Null";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
|
||||||
if(toValidate!=null){
|
if(value!=null){
|
||||||
return toValidate;
|
return value;
|
||||||
}
|
}
|
||||||
throw new InvalidValueException(ERROR);
|
throw new InvalidValueException(ERROR);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.io.Serializable;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -42,11 +43,14 @@ public class ValidIPValidator implements ValidatorAction{
|
||||||
return ipV6Matcher.matches();
|
return ipV6Matcher.matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
|
||||||
try {
|
try {
|
||||||
if(isIpAddress((String) toValidate)){
|
if(isIpAddress((String) value)){
|
||||||
return (String) toValidate;
|
return (String) value;
|
||||||
}
|
}
|
||||||
}catch (Exception e) {
|
}catch (Exception e) {
|
||||||
throw new InvalidValueException(ERROR, e.getCause());
|
throw new InvalidValueException(ERROR, e.getCause());
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.gcube.accounting.datamodel.validations.validators;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
|
@ -10,12 +11,15 @@ public class ValidIntegerValidator implements ValidatorAction {
|
||||||
|
|
||||||
private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName());
|
private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
|
||||||
if(toValidate instanceof Integer){
|
if(value instanceof Integer){
|
||||||
return toValidate;
|
return value;
|
||||||
}
|
}
|
||||||
Integer integerObj = Integer.getInteger((String) toValidate);
|
Integer integerObj = Integer.getInteger((String) value);
|
||||||
if(integerObj!=null){
|
if(integerObj!=null){
|
||||||
return integerObj;
|
return integerObj;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.gcube.accounting.datamodel.validations.validators;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
|
||||||
|
@ -10,13 +11,16 @@ public class ValidLongValidator implements ValidatorAction {
|
||||||
|
|
||||||
private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName());
|
private static final String ERROR = String.format("Not Instance of %s", Integer.class.getSimpleName());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
|
||||||
if(toValidate instanceof Long){
|
if(value instanceof Long){
|
||||||
return toValidate;
|
return value;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Long longObj = Long.getLong((String) toValidate);
|
Long longObj = Long.getLong((String) value);
|
||||||
if(longObj!=null){
|
if(longObj!=null){
|
||||||
return longObj;
|
return longObj;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package org.gcube.accounting.datamodel.validations.validators;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
||||||
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
import org.gcube.accounting.datamodel.decorators.ValidatorAction;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
@ -10,16 +11,20 @@ public class ValidOperationResultValidator implements ValidatorAction {
|
||||||
|
|
||||||
private static final String ERROR = String.format("Not Instance of %s", OperationResult.class.getSimpleName());
|
private static final String ERROR = String.format("Not Instance of %s", OperationResult.class.getSimpleName());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
@Override
|
@Override
|
||||||
public Serializable validate(Serializable toValidate) throws InvalidValueException {
|
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
|
||||||
if(toValidate instanceof OperationResult){
|
if(value instanceof OperationResult){
|
||||||
return toValidate;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if(toValidate instanceof String){
|
if(value instanceof String){
|
||||||
try{
|
try{
|
||||||
OperationResult operationResult = OperationResult.valueOf((String) toValidate);
|
OperationResult operationResult = OperationResult.valueOf((String) value);
|
||||||
if(operationResult !=null){
|
if(operationResult !=null){
|
||||||
return operationResult;
|
return operationResult;
|
||||||
}
|
}
|
||||||
|
@ -28,17 +33,21 @@ public class ValidOperationResultValidator implements ValidatorAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
Integer integer = Integer.getInteger((String) toValidate);
|
Integer integer = Integer.getInteger((String) value);
|
||||||
if(integer!=null){
|
if(integer!=null){
|
||||||
toValidate = integer;
|
value = integer;
|
||||||
}
|
}
|
||||||
} catch(Exception e){
|
} catch(Exception e){
|
||||||
// Trying another way
|
// Trying another way
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(toValidate instanceof Integer){
|
if(value instanceof Integer){
|
||||||
return OperationResult.values()[(Integer) toValidate];
|
return OperationResult.values()[(Integer) value];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(value instanceof Enum){
|
||||||
|
return OperationResult.values()[((Enum) value).ordinal()];
|
||||||
}
|
}
|
||||||
|
|
||||||
}catch(Exception e){
|
}catch(Exception e){
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.accounting.datamodel.validations.validators;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.UsageRecord;
|
|
||||||
import org.gcube.accounting.datamodel.decorators.DeprecatedAction;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class WarningAlert implements DeprecatedAction {
|
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(WarningAlert.class);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Serializable manage(String key, Serializable manage, UsageRecord usageRecord) {
|
|
||||||
logger.warn("The field {} is deprecated for the {}. Anyway the field will be included in the document",
|
|
||||||
key, usageRecord.getClass().getSimpleName());
|
|
||||||
return manage;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package org.gcube.accounting.datamodel.validators;
|
package org.gcube.accounting.datamodel.validations.validators;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.validations.validators.ValidLongValidator;
|
import org.gcube.accounting.datamodel.validations.validators.ValidLongValidator;
|
||||||
import org.gcube.accounting.exception.InvalidValueException;
|
import org.gcube.accounting.exception.InvalidValueException;
|
||||||
|
@ -17,20 +17,20 @@ public class ValidTimeValidatorTest {
|
||||||
public void testPrimitiveLong() throws InvalidValueException{
|
public void testPrimitiveLong() throws InvalidValueException{
|
||||||
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
||||||
long myLong = 4;
|
long myLong = 4;
|
||||||
validTimeValidator.validate(myLong);
|
validTimeValidator.validate(null, myLong, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testClassLong() throws InvalidValueException{
|
public void testClassLong() throws InvalidValueException{
|
||||||
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
||||||
Long myLong = new Long(4);
|
Long myLong = new Long(4);
|
||||||
validTimeValidator.validate(myLong);
|
validTimeValidator.validate(null, myLong, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=InvalidValueException.class)
|
@Test(expected=InvalidValueException.class)
|
||||||
public void testWrongValue() throws InvalidValueException {
|
public void testWrongValue() throws InvalidValueException {
|
||||||
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
ValidLongValidator validTimeValidator = new ValidLongValidator();
|
||||||
validTimeValidator.validate("test");
|
validTimeValidator.validate(null, "test", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue