aslcore/src/main/java/org/gcube/application/framework/core/cache/factories/GenericResourceCacheEntryFa...

199 lines
8.2 KiB
Java

package org.gcube.application.framework.core.cache.factories;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gcube.application.framework.core.genericresources.model.ISGenericResource;
import org.gcube.application.framework.core.util.CacheEntryConstants;
import org.gcube.application.framework.core.util.QueryString;
import org.gcube.application.framework.core.util.SessionConstants;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.ScopeGroup;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.gcube.common.core.contexts.GHNContext;
//import org.gcube.common.core.informationsystem.client.AtomicCondition;
//import org.gcube.common.core.informationsystem.client.ISClient;
//import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericResourceQuery;
//import org.gcube.common.core.resources.GCUBEGenericResource;
//import org.gcube.common.core.scope.GCUBEScope;
//import org.gcube.common.core.scope.GCUBEScope.Type;
import static org.gcube.resources.discovery.icclient.ICFactory.*;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
/**
* @author Valia Tsagkalidou
*
*/
public class GenericResourceCacheEntryFactory implements CacheEntryFactory {
// static ISClient client = null;
static DiscoveryClient<GenericResource> client = null;
/** The logger. */
private static final Logger logger = LoggerFactory.getLogger(GenericResourceCacheEntryFactory.class);
/**
* Constructor to initialize the ISClient
*/
public GenericResourceCacheEntryFactory() {
super();
if(client == null)
{
try {
// client = GHNContext.getImplementation(ISClient.class);
client = clientFor(GenericResource.class);
} catch (Exception e) {
logger.error("",e);
}
}
}
/**
* @param key a QueryString representing pairs of keys and values: needed keys are "vre" and one of "id" or "name"
* @return a list containing the Generic Resources that correspond to the query
*/
public List<ISGenericResource> createEntry(Object key) throws Exception {
QueryString querySt = (QueryString) key;
logger.info("query: " + querySt.toString());
String scope = ScopeProvider.instance.get();
// GCUBEScope scope = GCUBEScope.getScope(querySt.get(CacheEntryConstants.vre));
SimpleQuery query = queryFor(GenericResource.class);
// GCUBEGenericResourceQuery query = client.getQuery(GCUBEGenericResourceQuery.class);
logger.info("In generic resources cache entry factory");
if(querySt.containsKey(CacheEntryConstants.id))
{ //Retrieving generic resources based on their ID
query.addCondition("$resource/ID eq '"+querySt.get(CacheEntryConstants.id)+"'");
// query.addAtomicConditions(new AtomicCondition("/ID", querySt.get(CacheEntryConstants.id)));
}
// else if(querySt.containsKey(CacheEntryConstants.name) && querySt.get(CacheEntryConstants.name).equals(SessionConstants.ScenarioSchemaInfo))
// { //Retrieving the generic resource that represents the static search configuration
// query.addAtomicConditions(new AtomicCondition("/Profile/Name", querySt.get(CacheEntryConstants.name)));
// query.addAtomicConditions(new AtomicCondition("/Profile/Body/DL/attribute::name", querySt.get(CacheEntryConstants.vre)));
// }
else if(querySt.containsKey(CacheEntryConstants.name))
{ //Retrieving generic resources based on their name
query.addCondition("$resource/Profile/Name eq '"+querySt.get(CacheEntryConstants.name)+"'");
// query.addAtomicConditions(new AtomicCondition("/Profile/Name", querySt.get(CacheEntryConstants.name)));
}
else if(querySt.containsKey(CacheEntryConstants.vreResource))
{
String secondaryType = new String();
ScopeBean sb = new ScopeBean(ScopeProvider.instance.get());
if(sb.is(ScopeBean.Type.VRE))
secondaryType = ScopeBean.Type.VRE.name();
else if (sb.is(ScopeBean.Type.VO))
secondaryType = ScopeBean.Type.VO.name();
else
secondaryType = ScopeBean.Type.INFRASTRUCTURE.name();
query.addCondition("$resource/Profile/SecondaryType eq '"+secondaryType+"'")
.addCondition("$resource/Scopes eq '"+scope+"'");
// if (scope.getType() == Type.VRE)
// secondaryType = GCUBEGenericResource.SECONDARYTYPE_VRE;
// else if (scope.getType() == Type.VO)
// secondaryType = GCUBEGenericResource.SECONDARYTYPE_VO;
// else
// secondaryType = GCUBEGenericResource.SECONDARYTYPE_INFRASTRUCTURE;
// query.addAtomicConditions(new AtomicCondition("/Profile/SecondaryType", secondaryType), new AtomicCondition("//Body/Scope", scope.toString()));
}
try{
List<GenericResource> results = client.submit(query);
if (querySt.containsKey(CacheEntryConstants.name) && querySt.get(CacheEntryConstants.name).equals(SessionConstants.ScenarioSchemaInfo)) {
logger.info("The number of generic Resources for ScenarioSchemaInfo returned is: " + results.size());
List<GenericResource> newResult = new ArrayList<GenericResource>();
for (GenericResource resource : results) {
ScopeGroup<String> scopes = resource.scopes();
logger.info("Number of scopes for ScenarioSchemaInfo: " + scopes.size());
String [] sc = (String[]) scopes.toArray();
logger.info(sc.toString());
if(scopes.contains(scope))
newResult.add(resource);
}
if (newResult.size() > 1) {
GenericResource voResource = null;
for (GenericResource resource : newResult) {
if(resource.scopes().size()==1){
voResource = resource;
break;
}
}
newResult.clear();
newResult.add(voResource);
}
logger.info("Number of genericResources for ScenarioSchemaInfo left after the pruning" + newResult.size());
results = newResult;
}
logger.debug("size of results: " + results.size());
List<ISGenericResource> res = new ArrayList<ISGenericResource>();
for(GenericResource resource : results)
{
ISGenericResource genResource = new ISGenericResource(resource.id(), resource.profile().name(),resource.profile().description(),resource.profile().body().toString(),resource.profile().type());
res.add(genResource);
}
return res;
}catch (Exception e) {
logger.error("",e);
return null;
}
// List<GCUBEGenericResource> result = client.execute(query, scope);
// List<ISGenericResource> res = new ArrayList<ISGenericResource>();
// if (querySt.containsKey(CacheEntryConstants.name) && querySt.get(CacheEntryConstants.name).equals(SessionConstants.ScenarioSchemaInfo)) {
// logger.info("The number of generic Resources for ScenarioSchemaInfo returned is: " + result.size());
// List<GCUBEGenericResource> newResult = new ArrayList<GCUBEGenericResource>();
// for (GCUBEGenericResource resource : result) {
// Map<String, GCUBEScope> scopes = resource.getScopes();
// logger.info("Number of scopes for ScenarioSchemaInfo: " + scopes.values().size());
// logger.info(scopes.values().toString());
// if (scopes.containsValue(scope)) {
// newResult.add(resource);
// }
// }
//
//
// if (newResult.size() > 1) {
// GCUBEGenericResource voResource = null;
// for (GCUBEGenericResource resource:newResult) {
// if (resource.getScopes().size() == 1) {
// voResource = resource;
// break;
// }
// }
// newResult.clear();
// newResult.add(voResource);
// }
// logger.info("Number of genericResources for ScenarioSchemaInfo left after the pruning" + newResult.size());
// result = newResult;
// }
// logger.debug("size of results: " + result.size());
// for(GCUBEGenericResource resource : result)
// {
// ISGenericResource genResource = new ISGenericResource(resource.getID(), resource.getName(), resource.getDescription(), resource.getBody(), resource.getSecondaryType());
// res.add(genResource);
// }
// return res;
// }catch (Exception e) {
// logger.error("",e);
// return null;
// }
}
}