diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java index a1c787f..01c52f4 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceBackendQuery.java @@ -16,6 +16,7 @@ import org.gcube.accounting.analytics.UsageValue; import org.gcube.accounting.analytics.exception.DuplicatedKeyFilterException; import org.gcube.accounting.analytics.exception.KeyException; import org.gcube.accounting.analytics.exception.ValueException; +import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.documentstore.records.AggregatedRecord; import org.json.JSONObject; @@ -26,6 +27,14 @@ public interface AccountingPersistenceBackendQuery { public static final int KEY_VALUES_LIMIT = 25; + public static String getScopeToQuery() { + String scope = AccountingPersistenceQueryFactory.getForcedScope().get(); + if(scope == null) { + scope = BasicUsageRecord.getContextFromToken(); + } + return scope; + } + public void prepareConnection( AccountingPersistenceBackendQueryConfiguration configuration) throws Exception; diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQueryFactory.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQueryFactory.java index 6271eb6..ab504e1 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQueryFactory.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQueryFactory.java @@ -3,14 +3,33 @@ */ package org.gcube.accounting.analytics.persistence; - /** * @author Luca Frosini (ISTI - CNR) * */ public class AccountingPersistenceQueryFactory { + private static final InheritableThreadLocal forcedScope = new InheritableThreadLocal() { + + @Override + protected String initialValue() { + return null; + } + + }; + + /** + * Used to force the query in a certain scope without changing the current effective scope. + * Please note that is responsibility of the AccountingPersistenceBackendQuery implementation + * use the scope to query. The facility method getScopeToQuery() has been also created + */ + public static InheritableThreadLocal getForcedScope() { + return forcedScope; + } + public static AccountingPersistenceQuery getInstance() { return AccountingPersistenceQuery.getInstance(); } + + }