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

138 lines
5.3 KiB
Java

package org.gcube.application.framework.core.cache.factories;
import java.util.ArrayList;
import java.util.HashMap;
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.core.contexts.GCUBEContext;
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 org.gcube.common.core.utils.logging.GCUBELog;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
/**
* @author Valia Tsagkalidou
*
*/
public class GenericResourceCacheEntryFactory implements CacheEntryFactory {
static ISClient client = null;
/** Object logger. */
protected final GCUBELog logger = new GCUBELog(this);
/**
* Constructor to initialize the ISClient
*/
public GenericResourceCacheEntryFactory() {
super();
if(client == null)
{
try {
client = GHNContext.getImplementation(ISClient.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());
GCUBEScope scope = GCUBEScope.getScope(querySt.get(CacheEntryConstants.vre));
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.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.addAtomicConditions(new AtomicCondition("/Profile/Name", querySt.get(CacheEntryConstants.name)));
}
else if(querySt.containsKey(CacheEntryConstants.vreResource))
{
String secondaryType = new String();
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("//SecondaryType", secondaryType), new AtomicCondition("//Body/Scope", scope.toString()));
}
try{
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)) {
System.out.println("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();
System.out.println("Number of scopes for ScenarioSchemaInfo: " + scopes.values().size());
System.out.println(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);
}
System.out.println("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;
}
}
}