2015-06-10 17:36:50 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.accounting.datamodel;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.Map;
|
2016-09-19 12:53:57 +02:00
|
|
|
import java.util.SortedSet;
|
2015-06-10 17:36:50 +02:00
|
|
|
|
2015-12-21 11:50:09 +01:00
|
|
|
import org.gcube.accounting.datamodel.backwardcompatibility.MoveToRecordType;
|
2015-06-10 17:36:50 +02:00
|
|
|
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationResult;
|
2016-02-29 15:16:54 +01:00
|
|
|
import org.gcube.accounting.datamodel.validations.validators.ValidOperationResultValidator;
|
2016-07-25 17:27:51 +02:00
|
|
|
import org.gcube.common.authorization.client.Constants;
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2016-09-08 17:21:05 +02:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2015-12-18 17:09:05 +01:00
|
|
|
import org.gcube.documentstore.exception.InvalidValueException;
|
2016-03-22 14:38:04 +01:00
|
|
|
import org.gcube.documentstore.records.Record;
|
2015-12-21 11:50:09 +01:00
|
|
|
import org.gcube.documentstore.records.implementation.AbstractRecord;
|
|
|
|
import org.gcube.documentstore.records.implementation.RequiredField;
|
|
|
|
import org.gcube.documentstore.records.implementation.validations.annotations.NotEmpty;
|
2015-06-10 17:36:50 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-10 17:36:50 +02:00
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
|
|
*
|
|
|
|
*/
|
2015-12-21 11:50:09 +01:00
|
|
|
public abstract class BasicUsageRecord extends AbstractRecord implements UsageRecord {
|
2015-06-10 17:36:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generated Serial Version UID
|
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = -2060728578456796388L;
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-10 17:36:50 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(BasicUsageRecord.class);
|
2015-12-18 17:09:05 +01:00
|
|
|
|
2016-03-22 14:38:04 +01:00
|
|
|
/**
|
|
|
|
* Moved to {@link Record#RECORD_TYPE}. This filed should not be set
|
|
|
|
* explicitly because it is automatically calculated
|
|
|
|
*/
|
2015-12-21 11:50:09 +01:00
|
|
|
@Deprecated @MoveToRecordType
|
2016-03-22 14:38:04 +01:00
|
|
|
public static final String USAGE_RECORD_TYPE = "usageRecordType";
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-23 17:47:38 +02:00
|
|
|
@RequiredField @NotEmpty
|
2015-12-21 11:50:09 +01:00
|
|
|
public static final String CONSUMER_ID = UsageRecord.CONSUMER_ID;
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-23 17:47:38 +02:00
|
|
|
@RequiredField @NotEmpty
|
2015-12-21 11:50:09 +01:00
|
|
|
public static final String SCOPE = UsageRecord.SCOPE;
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-23 17:47:38 +02:00
|
|
|
@RequiredField @ValidOperationResult
|
2015-12-21 11:50:09 +01:00
|
|
|
public static final String OPERATION_RESULT = UsageRecord.OPERATION_RESULT;
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2016-02-09 18:46:23 +01:00
|
|
|
public static String getScopeFromToken(){
|
2016-09-08 17:21:05 +02:00
|
|
|
|
|
|
|
String scope =ScopeProvider.instance.get();
|
|
|
|
if (scope==null){
|
|
|
|
String token = SecurityTokenProvider.instance.get();
|
|
|
|
|
|
|
|
AuthorizationEntry authorizationEntry;
|
|
|
|
try {
|
|
|
|
authorizationEntry = Constants.authorizationService().get(token);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
scope = authorizationEntry.getContext();
|
2016-02-09 18:46:23 +01:00
|
|
|
}
|
|
|
|
return scope;
|
|
|
|
}
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-10 17:36:50 +02:00
|
|
|
/**
|
|
|
|
* Initialize variable
|
|
|
|
*/
|
2015-12-21 11:50:09 +01:00
|
|
|
protected void init() {
|
|
|
|
super.init();
|
|
|
|
// Backward compatibility
|
2015-07-20 15:45:27 +02:00
|
|
|
try {
|
2016-07-25 17:27:51 +02:00
|
|
|
this.setScope(getScopeFromToken());
|
2015-07-20 17:02:43 +02:00
|
|
|
} catch(Exception e) {
|
2015-07-20 15:45:27 +02:00
|
|
|
logger.warn("Unable to automaticcally set the scope using scope provider. The record will not be valid if the scope will not be explicitly set.");
|
|
|
|
}
|
2015-06-10 17:36:50 +02:00
|
|
|
}
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-10 17:36:50 +02:00
|
|
|
public BasicUsageRecord(){
|
2015-12-21 11:50:09 +01:00
|
|
|
super();
|
2015-06-10 17:36:50 +02:00
|
|
|
}
|
|
|
|
|
2016-01-28 18:29:11 +01:00
|
|
|
public BasicUsageRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException {
|
2015-12-21 12:00:46 +01:00
|
|
|
super(properties);
|
2015-06-10 17:36:50 +02:00
|
|
|
}
|
2016-09-19 12:53:57 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SortedSet<String> getQuerableKeys()
|
|
|
|
throws Exception {
|
|
|
|
SortedSet<String> properties = super.getQuerableKeys();
|
|
|
|
|
|
|
|
properties.remove(UsageRecord.SCOPE);
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
}
|
2015-06-10 17:36:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getConsumerId() {
|
|
|
|
return (String) this.resourceProperties.get(CONSUMER_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setConsumerId(String consumerId) throws InvalidValueException {
|
|
|
|
setResourceProperty(CONSUMER_ID, consumerId);
|
|
|
|
}
|
2016-09-08 17:21:05 +02:00
|
|
|
|
|
|
|
|
2015-06-26 15:50:39 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getScope() {
|
|
|
|
return (String) this.resourceProperties.get(SCOPE);
|
|
|
|
}
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-26 15:50:39 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setScope(String scope) throws InvalidValueException {
|
|
|
|
setResourceProperty(SCOPE, scope);
|
|
|
|
}
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-10 17:36:50 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public OperationResult getOperationResult(){
|
2016-02-29 15:16:54 +01:00
|
|
|
try {
|
|
|
|
return (OperationResult) new ValidOperationResultValidator().
|
|
|
|
validate(OPERATION_RESULT, this.resourceProperties.get(OPERATION_RESULT), null);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-06-10 17:36:50 +02:00
|
|
|
}
|
2016-09-08 17:21:05 +02:00
|
|
|
|
2015-06-10 17:36:50 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* @throws InvalidValueException
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setOperationResult(OperationResult operationResult) throws InvalidValueException {
|
|
|
|
setResourceProperty(OPERATION_RESULT, operationResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|