2015-05-28 13:19:25 +02:00
/ * *
*
* /
package org.gcube.accounting.datamodel ;
import java.io.Serializable ;
import java.lang.annotation.Annotation ;
2015-06-10 12:42:39 +02:00
import java.lang.annotation.ElementType ;
import java.lang.annotation.Retention ;
import java.lang.annotation.RetentionPolicy ;
import java.lang.annotation.Target ;
2015-06-09 15:53:21 +02:00
import java.lang.reflect.Constructor ;
2015-05-28 13:19:25 +02:00
import java.lang.reflect.Field ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Calendar ;
import java.util.Date ;
import java.util.HashMap ;
2015-06-09 14:54:14 +02:00
import java.util.HashSet ;
2015-05-28 13:19:25 +02:00
import java.util.List ;
import java.util.Map ;
import java.util.Map.Entry ;
import java.util.Set ;
import java.util.UUID ;
2015-06-08 17:30:08 +02:00
import org.gcube.accounting.datamodel.decorators.AggregatedField ;
2015-06-09 17:36:38 +02:00
import org.gcube.accounting.datamodel.decorators.ComputedField ;
import org.gcube.accounting.datamodel.decorators.FieldAction ;
import org.gcube.accounting.datamodel.decorators.FieldDecorator ;
2015-06-10 12:42:39 +02:00
import org.gcube.accounting.datamodel.decorators.RequiredField ;
2015-06-09 10:41:32 +02:00
import org.gcube.accounting.datamodel.deprecationmanagement.annotations.DeprecatedWarning ;
2015-06-08 17:19:09 +02:00
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 ;
2015-06-10 12:42:39 +02:00
import org.gcube.accounting.datamodel.validations.validators.NotEmptyIfNotNullValidator ;
2015-05-28 13:19:25 +02:00
import org.gcube.accounting.exception.InvalidValueException ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
* @author Luca Frosini ( ISTI - CNR ) http : //www.lucafrosini.com/
*
* /
2015-06-09 14:03:59 +02:00
public abstract class RawUsageRecord implements UsageRecord , Serializable {
2015-05-28 13:19:25 +02:00
private static Logger logger = LoggerFactory . getLogger ( RawUsageRecord . class ) ;
2015-06-08 17:30:08 +02:00
@RequiredField @NotEmpty
2015-05-28 13:19:25 +02:00
public static final String ID = " id " ;
2015-06-08 17:30:08 +02:00
@RequiredField @NotEmpty
2015-05-28 13:19:25 +02:00
public static final String CREATOR_ID = " creatorId " ;
2015-06-08 17:30:08 +02:00
@RequiredField @NotEmpty
2015-05-28 13:19:25 +02:00
public static final String CONSUMER_ID = " consumerId " ;
2015-06-08 17:30:08 +02:00
@RequiredField @ValidLong
2015-05-28 13:19:25 +02:00
public static final String CREATION_TIME = " creationTime " ;
2015-06-08 17:30:08 +02:00
@AggregatedField @ValidLong
2015-06-04 18:52:29 +02:00
protected static final String START_TIME = " startTime " ;
2015-06-08 17:30:08 +02:00
@AggregatedField @ValidLong
2015-06-04 18:52:29 +02:00
protected static final String END_TIME = " endTime " ;
2015-06-09 15:53:21 +02:00
2015-06-10 12:42:39 +02:00
@DeprecatedWarning
protected static final String RESOURCE_TYPE = " resourceType " ;
@RequiredField @NotEmpty
protected static final String USAGE_RECORD_TYPE = " UsageRecordType " ;
2015-06-09 15:53:21 +02:00
2015-06-08 17:30:08 +02:00
@RequiredField @NotEmpty
2015-05-28 13:19:25 +02:00
public static final String RESOURCE_SCOPE = " resourceScope " ;
2015-06-09 10:41:32 +02:00
@DeprecatedWarning @NotEmpty
2015-06-05 15:55:22 +02:00
protected static final String RESOURCE_OWNER = " resourceOwner " ;
2015-06-09 15:53:21 +02:00
@AggregatedField @NotEmptyIfNotNull
protected static final String AGGREGATED = " aggregated " ;
2015-06-08 17:30:08 +02:00
@AggregatedField @NotEmptyIfNotNull
2015-06-05 09:47:48 +02:00
protected static final String AGGREGATED_USAGE_RECORD_ID = " aggregatedUsageRecordId " ;
2015-06-09 10:41:32 +02:00
@NotEmptyIfNotNull @MoveToAggregatedUsageRecordId
protected static final String AGGREGATED_ID = " aggregatedId " ;
2015-06-10 12:42:39 +02:00
@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 ;
}
}
2015-06-08 17:30:08 +02:00
@RequiredField @ValidOperationResult
2015-06-04 18:52:29 +02:00
public static final String OPERATION_RESULT = " operationResult " ;
2015-05-28 13:19:25 +02:00
/ * *
* Generated Serial Version UID
* /
private static final long serialVersionUID = - 2060728578456796388L ;
2015-06-09 14:54:14 +02:00
/** resource-specific properties */
protected Map < String , Serializable > resourceProperties ;
2015-05-28 13:19:25 +02:00
2015-06-09 17:36:38 +02:00
protected Map < String , List < FieldAction > > validation ;
2015-06-09 14:54:14 +02:00
protected Set < String > requiredFields ;
2015-06-09 17:36:38 +02:00
protected Set < String > aggregatedFields ;
protected Set < String > computedFields ;
2015-05-28 13:19:25 +02:00
protected void initializeValidation ( ) {
logger . debug ( " Initializing Field Validators " ) ;
List < Field > fields = Arrays . asList ( this . getClass ( ) . getDeclaredFields ( ) ) ;
for ( Field field : fields ) {
String keyString ;
try {
keyString = ( String ) field . get ( null ) ;
} catch ( Exception e ) {
continue ;
}
2015-06-09 17:36:38 +02:00
List < FieldAction > fieldValidators = new ArrayList < FieldAction > ( ) ;
2015-05-28 13:19:25 +02:00
validation . put ( keyString , fieldValidators ) ;
for ( Annotation annotation : field . getAnnotations ( ) ) {
2015-06-09 17:36:38 +02:00
if ( annotation . annotationType ( ) . isAnnotationPresent ( FieldDecorator . class ) ) {
Class < ? extends FieldAction > managedClass = ( ( FieldDecorator ) annotation . annotationType ( ) . getAnnotation ( FieldDecorator . class ) ) . managed ( ) ;
FieldAction validator ;
2015-05-28 13:19:25 +02:00
try {
validator = managedClass . newInstance ( ) ;
} catch ( InstantiationException | IllegalAccessException e ) {
continue ;
}
fieldValidators . add ( validator ) ;
}
2015-06-09 17:36:38 +02:00
if ( annotation . annotationType ( ) . isAssignableFrom ( RequiredField . class ) ) {
2015-06-09 14:54:14 +02:00
requiredFields . add ( keyString ) ;
}
2015-06-09 17:36:38 +02:00
if ( annotation . annotationType ( ) . isAssignableFrom ( AggregatedField . class ) ) {
aggregatedFields . add ( keyString ) ;
}
if ( annotation . annotationType ( ) . isAssignableFrom ( ComputedField . class ) ) {
computedFields . add ( keyString ) ;
}
2015-05-28 13:19:25 +02:00
}
}
}
2015-06-09 14:54:14 +02:00
/ * *
* Initialize variable
* /
private void init ( ) {
2015-06-09 17:36:38 +02:00
this . validation = new HashMap < String , List < FieldAction > > ( ) ;
2015-06-09 14:54:14 +02:00
this . requiredFields = new HashSet < String > ( ) ;
2015-05-28 13:19:25 +02:00
initializeValidation ( ) ;
}
2015-06-09 15:53:21 +02:00
public RawUsageRecord ( ) {
2015-06-09 14:54:14 +02:00
init ( ) ;
this . resourceProperties = new HashMap < String , Serializable > ( ) ;
this . resourceProperties . put ( ID , UUID . randomUUID ( ) . toString ( ) ) ;
2015-06-09 15:53:21 +02:00
this . resourceProperties . put ( USAGE_RECORD_TYPE , this . getClass ( ) . getSimpleName ( ) ) ;
2015-06-09 14:54:14 +02:00
Calendar calendar = Calendar . getInstance ( ) ;
this . resourceProperties . put ( CREATION_TIME , calendar . getTimeInMillis ( ) ) ;
}
2015-06-09 15:53:21 +02:00
public RawUsageRecord ( Map < String , Serializable > properties ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
init ( ) ;
setResourceProperties ( properties ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
public String getId ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( ID ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
public void setId ( String id ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( ID , id ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
public String getCreatorId ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( CREATOR_ID ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
public void setCreatorId ( String creatorId ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( CREATOR_ID , creatorId ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
public String getConsumerId ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( CONSUMER_ID ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
public void setConsumerId ( String consumerId ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( CONSUMER_ID , consumerId ) ;
2015-05-28 13:19:25 +02:00
}
protected Calendar timestampStringToCalendar ( long millis ) {
Calendar calendar = Calendar . getInstance ( ) ;
calendar . setTimeInMillis ( millis ) ;
return calendar ;
}
/ * *
* { @inheritDoc }
* /
@Override
public Calendar getCreationTime ( ) {
2015-06-09 14:54:14 +02:00
long millis = ( Long ) this . resourceProperties . get ( CREATION_TIME ) ;
2015-05-28 13:19:25 +02:00
return timestampStringToCalendar ( millis ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
public void setCreationTime ( Calendar creationTime ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( CREATION_TIME , creationTime . getTimeInMillis ( ) ) ;
2015-05-28 13:19:25 +02:00
}
2015-06-04 18:52:29 +02:00
@Override
@Deprecated
public Date getCreateTime ( ) {
2015-06-09 14:54:14 +02:00
long millis = ( Long ) this . resourceProperties . get ( CREATION_TIME ) ;
2015-06-04 18:52:29 +02:00
return timestampStringToCalendar ( millis ) . getTime ( ) ;
}
2015-05-28 13:19:25 +02:00
/ * *
* { @inheritDoc }
* /
@Override
@Deprecated
public void setCreateTime ( Date createTime ) throws InvalidValueException {
Calendar calendar = Calendar . getInstance ( ) ;
calendar . setTime ( createTime ) ;
setCreationTime ( calendar ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
public Calendar getStartTime ( ) {
2015-06-09 14:54:14 +02:00
long millis = ( Long ) this . resourceProperties . get ( START_TIME ) ;
2015-05-28 13:19:25 +02:00
return timestampStringToCalendar ( millis ) ;
}
/ * *
2015-06-04 18:52:29 +02:00
* Set the left end of the time interval covered by this { # UsageRecord }
* @param startTime Start Time
* @throws InvalidValueException
2015-05-28 13:19:25 +02:00
* /
2015-06-04 18:52:29 +02:00
protected void setStartTime ( Calendar startTime ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( START_TIME , startTime . getTimeInMillis ( ) ) ;
2015-05-28 13:19:25 +02:00
}
2015-06-04 18:52:29 +02:00
/ * *
2015-05-28 13:19:25 +02:00
* { @inheritDoc }
2015-06-04 18:52:29 +02:00
* /
2015-05-28 13:19:25 +02:00
@Override
@Deprecated
public void setStartTime ( Date startTime ) throws InvalidValueException {
Calendar calendar = Calendar . getInstance ( ) ;
calendar . setTime ( startTime ) ;
setStartTime ( calendar ) ;
}
2015-06-04 18:52:29 +02:00
* /
2015-05-28 13:19:25 +02:00
/ * *
* { @inheritDoc }
* /
@Override
public Calendar getEndTime ( ) {
2015-06-09 14:54:14 +02:00
long millis = ( Long ) this . resourceProperties . get ( END_TIME ) ;
2015-05-28 13:19:25 +02:00
return timestampStringToCalendar ( millis ) ;
}
/ * *
2015-06-04 18:52:29 +02:00
* Set the right end of the time interval covered by this { # UsageRecord }
* @param endTime End Time
* @throws InvalidValueException
2015-05-28 13:19:25 +02:00
* /
2015-06-04 18:52:29 +02:00
protected void setEndTime ( Calendar endTime ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( END_TIME , endTime . getTimeInMillis ( ) ) ;
2015-05-28 13:19:25 +02:00
}
2015-06-04 18:52:29 +02:00
/ * *
2015-05-28 13:19:25 +02:00
* { @inheritDoc }
2015-06-04 18:52:29 +02:00
* /
2015-05-28 13:19:25 +02:00
@Override
@Deprecated
public void setEndTime ( Date endTime ) throws InvalidValueException {
Calendar calendar = Calendar . getInstance ( ) ;
calendar . setTime ( endTime ) ;
setEndTime ( calendar ) ;
}
2015-06-04 18:52:29 +02:00
* /
2015-05-28 13:19:25 +02:00
2015-06-09 14:54:14 +02:00
protected String getUsageRecordType ( ) {
2015-06-04 18:52:29 +02:00
//return (String) this.resourceSpecificProperties.get(RESOURCE_TYPE);
return this . getClass ( ) . getSimpleName ( ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
@Deprecated
public String getResourceType ( ) {
2015-06-10 12:42:39 +02:00
return ( String ) this . resourceProperties . get ( RESOURCE_TYPE ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Deprecated
2015-06-10 12:42:39 +02:00
public void setResourceType ( String resourceType ) {
try {
setResourceProperty ( RESOURCE_TYPE , resourceType ) ;
} catch ( InvalidValueException e ) {
logger . error ( " Unable to Set {} " , RESOURCE_TYPE ) ;
}
}
2015-05-28 13:19:25 +02:00
/ * *
* { @inheritDoc }
* /
@Override
public String getResourceScope ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( RESOURCE_SCOPE ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
public void setResourceScope ( String scope ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( RESOURCE_SCOPE , scope ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
2015-06-04 18:52:29 +02:00
@Deprecated
2015-05-28 13:19:25 +02:00
public String getResourceOwner ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( RESOURCE_OWNER ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
2015-06-04 18:52:29 +02:00
@Deprecated
2015-05-28 13:19:25 +02:00
public void setResourceOwner ( String owner ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( RESOURCE_OWNER , owner ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
2015-06-04 18:52:29 +02:00
@Deprecated
2015-05-28 13:19:25 +02:00
public String getAggregatedId ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( AGGREGATED_ID ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
2015-06-04 18:52:29 +02:00
@Override @
Deprecated
2015-05-28 13:19:25 +02:00
public void setAggregatedId ( String aggregatedId ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( AGGREGATED_ID , aggregatedId ) ;
2015-05-28 13:19:25 +02:00
}
2015-06-09 10:41:32 +02:00
/ * *
* { @inheritDoc }
* /
@Override
2015-06-05 09:47:48 +02:00
public String getAggregatedUsageRecordId ( ) {
2015-06-09 14:54:14 +02:00
return ( String ) this . resourceProperties . get ( AGGREGATED_USAGE_RECORD_ID ) ;
2015-06-05 09:47:48 +02:00
}
2015-06-09 10:41:32 +02:00
/ * *
* { @inheritDoc }
* /
@Override
2015-06-05 09:47:48 +02:00
public void setAggregatedUsageRecordId ( String aggregatedId ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( AGGREGATED_USAGE_RECORD_ID , aggregatedId ) ;
2015-06-05 09:47:48 +02:00
}
2015-05-28 13:19:25 +02:00
/ * *
* { @inheritDoc }
* /
@Override
2015-06-09 14:54:14 +02:00
@Deprecated
2015-05-28 13:19:25 +02:00
public Map < String , Serializable > getResourceSpecificProperties ( ) {
2015-06-09 14:54:14 +02:00
return getResourceProperties ( ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
2015-06-09 14:54:14 +02:00
@Deprecated
2015-05-28 13:19:25 +02:00
public void setResourceSpecificProperties ( Map < String , Serializable > properties ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperties ( properties ) ;
2015-05-28 13:19:25 +02:00
}
2015-06-09 14:54:14 +02:00
2015-05-28 13:19:25 +02:00
/ * *
* { @inheritDoc }
* /
@Override
2015-06-09 14:54:14 +02:00
public Map < String , Serializable > getResourceProperties ( ) {
return this . resourceProperties ;
}
/ * *
* { @inheritDoc }
* /
@Override
public void setResourceProperties ( Map < String , Serializable > properties ) throws InvalidValueException {
validateProperties ( properties ) ;
this . resourceProperties = new HashMap < String , Serializable > ( properties ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
@Deprecated
2015-05-28 13:19:25 +02:00
public Serializable getResourceSpecificProperty ( String key ) {
2015-06-09 14:54:14 +02:00
return getResourceProperty ( key ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
2015-06-09 14:54:14 +02:00
@Deprecated
2015-05-28 13:19:25 +02:00
public void setResourceSpecificProperty ( String key , Serializable value ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( key , value ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
public Serializable getResourceProperty ( String key ) {
return this . resourceProperties . get ( key ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
public void setResourceProperty ( String key , Serializable value ) throws InvalidValueException {
2015-06-05 15:55:22 +02:00
Serializable checkedValue = validateField ( key , value ) ;
2015-06-09 14:54:14 +02:00
if ( checkedValue = = null ) {
this . resourceProperties . remove ( key ) ;
} else {
this . resourceProperties . put ( key , checkedValue ) ;
}
2015-05-28 13:19:25 +02:00
}
2015-06-05 15:55:22 +02:00
protected Serializable validateField ( String key , Serializable serializable ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
if ( key = = null ) {
throw new InvalidValueException ( " The key of property to set cannot be null " ) ;
}
2015-06-05 15:55:22 +02:00
Serializable checkedValue = serializable ;
2015-06-09 17:36:38 +02:00
List < FieldAction > fieldValidators = validation . get ( key ) ;
2015-05-28 13:19:25 +02:00
if ( fieldValidators ! = null ) {
2015-06-09 17:36:38 +02:00
for ( FieldAction fieldValidator : fieldValidators ) {
2015-06-08 18:20:02 +02:00
checkedValue = fieldValidator . validate ( key , checkedValue , this ) ;
2015-06-09 17:36:38 +02:00
/ *
* Check here if the key is for a ComputedField or for an
* AggregatedField
* /
2015-05-28 13:19:25 +02:00
}
}
2015-06-05 15:55:22 +02:00
return checkedValue ;
2015-05-28 13:19:25 +02:00
}
2015-06-09 14:54:14 +02:00
protected void validateProperties ( Map < String , Serializable > properties ) throws InvalidValueException {
2015-05-28 13:19:25 +02:00
for ( String key : properties . keySet ( ) ) {
Serializable serializable = properties . get ( key ) ;
validateField ( key , serializable ) ;
}
}
/ * *
* { @inheritDoc }
* /
@Override
2015-06-09 14:54:14 +02:00
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 Usage Record does not contain the following required propert%s %s " , pluralManagement , notPresentProperties . toString ( ) ) ) ;
}
2015-05-28 13:19:25 +02:00
}
@Override
public String toString ( ) {
2015-06-09 14:54:14 +02:00
return resourceProperties . toString ( ) ;
2015-05-28 13:19:25 +02:00
}
/ * *
* { @inheritDoc }
* /
@Override
@Deprecated
public String getFullyQualifiedConsumerId ( ) {
return getConsumerId ( ) ;
}
/ * *
* { @inheritDoc }
* /
@Override
@Deprecated
public void setFullyQualifiedConsumerId ( String fqcid ) { }
2015-06-04 18:52:29 +02:00
/ * *
* { @inheritDoc }
* /
@Override
public OperationResult getOperationResult ( ) {
2015-06-09 14:54:14 +02:00
return OperationResult . valueOf ( ( String ) this . resourceProperties . get ( OPERATION_RESULT ) ) ;
2015-06-04 18:52:29 +02:00
}
/ * *
* { @inheritDoc }
2015-06-05 09:47:48 +02:00
* @throws InvalidValueException
2015-06-04 18:52:29 +02:00
* /
@Override
2015-06-05 09:47:48 +02:00
public void setOperationResult ( OperationResult operationResult ) throws InvalidValueException {
2015-06-09 14:54:14 +02:00
setResourceProperty ( OPERATION_RESULT , operationResult ) ;
2015-06-04 18:52:29 +02:00
}
2015-05-28 13:19:25 +02:00
/ * *
* Compare this UsageRecord instance with the one provided as argument
* @param usageRecord the Usage 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 UsageRecord provided as parameter does
* not contains all parameters in this instance , - 1 is returned .
* /
@Override
public int compareTo ( UsageRecord usageRecord ) {
2015-06-09 14:54:14 +02:00
Set < Entry < String , Serializable > > thisSet = this . resourceProperties . entrySet ( ) ;
Set < Entry < String , Serializable > > usageRecordSet = usageRecord . getResourceProperties ( ) . entrySet ( ) ;
2015-05-28 13:19:25 +02:00
if ( thisSet . size ( ) ! = usageRecordSet . size ( ) ) {
return thisSet . size ( ) - usageRecordSet . size ( ) ;
}
if ( usageRecordSet . containsAll ( thisSet ) ) {
return 0 ;
}
return 1 ;
}
2015-06-04 18:52:29 +02:00
2015-06-09 15:53:21 +02:00
@SuppressWarnings ( " unchecked " )
protected static Class < ? extends UsageRecord > getClass ( String usageRecordName , boolean aggregated ) throws ClassNotFoundException {
Class < ? extends UsageRecord > clz = null ;
Class < ? extends UsageRecord > utilityClass = org . gcube . accounting . datamodel . implementations . JobUsageRecord . class ;
if ( aggregated ) {
utilityClass = org . gcube . accounting . datamodel . implementations . aggregated . JobUsageRecord . class ;
}
String classCanonicalName = utilityClass . getCanonicalName ( ) ;
classCanonicalName . replace ( utilityClass . getSimpleName ( ) , usageRecordName ) ;
try {
clz = ( Class < ? extends UsageRecord > ) Class . forName ( classCanonicalName ) ;
} catch ( ClassNotFoundException e ) {
logger . error ( " Unable to retrieve class {} " , classCanonicalName ) ;
throw e ;
}
return clz ;
}
2015-06-04 18:52:29 +02:00
/ * *
* This method use the resourceType value contained in the Map to instance
* the right UsageRecord class and return it . If the type implementation
* does not exist or the validation of one or more field validation fails
* an exception is thrown
* @param usageRecordMap
* @return the instance of the UsageRecord class .
* @throws Exception if fails
* /
public static UsageRecord getUsageRecord ( Map < String , Serializable > usageRecordMap ) throws Exception {
2015-06-09 15:53:21 +02:00
String className = ( String ) usageRecordMap . get ( USAGE_RECORD_TYPE ) ;
2015-06-09 17:36:38 +02:00
boolean aggregated = false ;
try {
aggregated = ( Boolean ) usageRecordMap . get ( AGGREGATED ) ;
} catch ( Exception e ) { }
2015-06-09 15:53:21 +02:00
Class < ? extends UsageRecord > clz = getClass ( className , aggregated ) ;
logger . debug ( " Trying to instantiate {} " , clz . getClass ( ) . getSimpleName ( ) ) ;
@SuppressWarnings ( " rawtypes " )
Class [ ] usageRecordArgTypes = { Map . class } ;
Constructor < ? extends UsageRecord > usageRecordConstructor = clz . getDeclaredConstructor ( usageRecordArgTypes ) ;
Object [ ] usageRecordArguments = { usageRecordMap } ;
2015-06-09 17:36:38 +02:00
UsageRecord usageRecord = usageRecordConstructor . newInstance ( usageRecordArguments ) ;
logger . debug ( " Created Usage Record : {} " , usageRecord ) ;
return usageRecord ;
2015-06-04 18:52:29 +02:00
}
2015-05-28 13:19:25 +02:00
}