81 lines
2.2 KiB
Java
81 lines
2.2 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.accounting.datamodel;
|
|
|
|
import java.io.Serializable;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
import org.gcube.accounting.exception.NotAggregatableRecorsExceptions;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
*
|
|
*/
|
|
public abstract class AggregationStrategy<T extends B, B extends SingleUsageRecord> {
|
|
|
|
protected T t;
|
|
protected Set<String> aggregationField;
|
|
|
|
public AggregationStrategy(T t){
|
|
this.t = t;
|
|
this.aggregationField = new HashSet<String>();
|
|
this.aggregationField.add(BasicUsageRecord.USAGE_RECORD_TYPE);
|
|
this.aggregationField.add(BasicUsageRecord.SCOPE);
|
|
}
|
|
|
|
protected String commonFieldHash(B record) {
|
|
try {
|
|
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
|
|
|
|
String concatenation = "";
|
|
for(String field : aggregationField){
|
|
concatenation = concatenation + record.getResourceProperty(field).toString();
|
|
}
|
|
|
|
messageDigest.update(concatenation.getBytes());
|
|
return new String(messageDigest.digest());
|
|
}catch(NoSuchAlgorithmException e){
|
|
throw new UnsupportedOperationException(e.getCause());
|
|
}
|
|
}
|
|
|
|
protected boolean isAggregable(UsageRecord record) {
|
|
for(String field : aggregationField){
|
|
Serializable recordValue = record.getResourceProperty(field);
|
|
Serializable thisValue = t.getResourceProperty(field);
|
|
|
|
// TODO Check THIS
|
|
if(!recordValue.equals(thisValue)){
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public void aggregate(B record) throws NotAggregatableRecorsExceptions {
|
|
try{
|
|
if(isAggregable(record)){
|
|
throw new NotAggregatableRecorsExceptions("The Record provided as argument has different values for field wich must be common to be aggragatable");
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
AggregatedUsageRecord<T, B> aggregatedUsageRecord = ((AggregatedUsageRecord<T, B>) t);
|
|
aggregatedUsageRecord.aggregate(aggregatedUsageRecord.getAggregatedUsageRecord(record));
|
|
|
|
throw new NotAggregatableRecorsExceptions("");
|
|
}catch(NotAggregatableRecorsExceptions e){
|
|
throw e;
|
|
}catch(Exception ex){
|
|
throw new NotAggregatableRecorsExceptions(ex.getCause());
|
|
}
|
|
|
|
}
|
|
|
|
}
|