refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Implementing library

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115288 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-06-10 10:42:39 +00:00
parent c0038b9efb
commit 80f9872aff
4 changed files with 42 additions and 71 deletions

View File

@ -5,6 +5,10 @@ package org.gcube.accounting.datamodel;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
@ -21,15 +25,15 @@ import java.util.UUID;
import org.gcube.accounting.datamodel.decorators.AggregatedField;
import org.gcube.accounting.datamodel.decorators.ComputedField;
import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.decorators.FieldAction;
import org.gcube.accounting.datamodel.decorators.FieldDecorator;
import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.deprecationmanagement.annotations.DeprecatedWarning;
import org.gcube.accounting.datamodel.deprecationmanagement.annotations.MoveToAggregatedUsageRecordId;
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult;
import org.gcube.accounting.datamodel.validations.validators.NotEmptyIfNotNullValidator;
import org.gcube.accounting.exception.InvalidValueException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -55,10 +59,10 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
@AggregatedField @ValidLong
protected static final String END_TIME = "endTime";
//@RequiredField @NotEmpty
//protected static final String RESOURCE_TYPE = "resourceType";
protected static final String USAGE_RECORD_TYPE = "resourceType";
@DeprecatedWarning
protected static final String RESOURCE_TYPE = "resourceType";
@RequiredField @NotEmpty
protected static final String USAGE_RECORD_TYPE = "UsageRecordType";
@RequiredField @NotEmpty
public static final String RESOURCE_SCOPE = "resourceScope";
@ -73,6 +77,22 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
@NotEmptyIfNotNull @MoveToAggregatedUsageRecordId
protected static final String AGGREGATED_ID = "aggregatedId";
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@FieldDecorator(managed=MoveToAggregatedUsageRecordIdAction.class)
protected @interface MoveToAggregatedUsageRecordId { }
protected class MoveToAggregatedUsageRecordIdAction implements FieldAction {
@Override
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
NotEmptyIfNotNullValidator neinnv = new NotEmptyIfNotNullValidator();
value = neinnv.validate(key, value, usageRecord);
usageRecord.setAggregatedUsageRecordId((String) value);
return value;
}
}
@RequiredField @ValidOperationResult
public static final String OPERATION_RESULT = "operationResult";
@ -308,14 +328,20 @@ public abstract class RawUsageRecord implements UsageRecord, Serializable {
@Override
@Deprecated
public String getResourceType(){
return getUsageRecordType();
return (String) this.resourceProperties.get(RESOURCE_TYPE);
}
/**
* {@inheritDoc}
*/
@Deprecated
public void setResourceType(String resourceType){}
public void setResourceType(String resourceType){
try {
setResourceProperty(RESOURCE_TYPE, resourceType);
}catch(InvalidValueException e){
logger.error("Unable to Set {}", RESOURCE_TYPE);
}
}
/**
* {@inheritDoc}

View File

@ -1,16 +0,0 @@
package org.gcube.accounting.datamodel.deprecationmanagement.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.FieldDecorator;
import org.gcube.accounting.datamodel.deprecationmanagement.validators.MoveToAggregatedUsageRecordIdAction;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@FieldDecorator(managed=MoveToAggregatedUsageRecordIdAction.class)
public @interface MoveToAggregatedUsageRecordId {
}

View File

@ -1,30 +0,0 @@
/**
*
*/
package org.gcube.accounting.datamodel.deprecationmanagement.validators;
import java.io.Serializable;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.decorators.FieldAction;
import org.gcube.accounting.datamodel.validations.validators.NotEmptyIfNotNullValidator;
import org.gcube.accounting.exception.InvalidValueException;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class MoveToAggregatedUsageRecordIdAction implements FieldAction {
/**
* {@inheritDoc}
*/
@Override
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
NotEmptyIfNotNullValidator neinnv = new NotEmptyIfNotNullValidator();
value = neinnv.validate(key, value, usageRecord);
usageRecord.setAggregatedUsageRecordId((String) value);
return value;
}
}

View File

@ -22,7 +22,6 @@ import org.gcube.accounting.datamodel.deprecationmanagement.annotations.MoveToOp
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult;
import org.gcube.accounting.exception.InvalidValueException;
/**
@ -36,7 +35,7 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord
*/
private static final long serialVersionUID = -8648691183939346858L;
public static enum JobStatus {
protected static enum JobStatus {
completed, failed
};
@ -50,28 +49,19 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord
public static final String JOB_START_TIME = "jobStartTime";
@RequiredField @ValidLong @CalculateWallDuration
public static final String JOB_END_TIME = "jobEndTime";
@ValidOperationResult @MoveToOperationResult
@MoveToOperationResult
protected static final String JOB_STATUS = "jobStatus";
@ValidInteger
public static final String VMS_USED = "vmsUsed";
@ComputedField @ValidLong @CalculateWallDuration
protected static final String WALL_DURATION = "wallDuration";
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@FieldDecorator(managed=CalculateWallDurationAction.class)
protected @interface CalculateWallDuration {
}
protected @interface CalculateWallDuration {}
protected class CalculateWallDurationAction implements FieldAction {
/**
* {@inheritDoc}
*/
@Override
public Serializable validate(String key, Serializable value, UsageRecord usageRecord) throws InvalidValueException {
try {
@ -87,7 +77,6 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord
}
return value;
}
}
@ -150,15 +139,17 @@ public class JobUsageRecord extends RawUsageRecord implements SingleUsageRecord
}
/*
@Deprecated
public JobStatus getJobStatus() {
protected JobStatus getJobStatus() {
return JobStatus.values()[((OperationResult) this.resourceProperties.get(OPERATION_RESULT)).ordinal()];
}
@Deprecated
public void setJobStatus(JobStatus jobStatus) throws InvalidValueException {
setResourceProperty(OPERATION_RESULT, OperationResult.values()[jobStatus.ordinal()]);
protected void setJobStatus(JobStatus jobStatus) throws InvalidValueException {
setResourceProperty(OPERATION_RESULT, jobStatus);
}
*/
public int getVmsUsed() {
return (Integer) this.resourceProperties.get(VMS_USED);