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

89 lines
3.1 KiB
Java

package org.gcube.application.framework.core.cache.factories;
import java.util.ArrayList;
import java.util.List;
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.application.framework.core.vremanagement.model.ISGenericResource;
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 net.sf.ehcache.constructs.blocking.CacheEntryFactory;
/**
* @author Valia Tsagkalidou
*
*/
public class GenericResourceCacheEntryFactory implements CacheEntryFactory {
static ISClient client = null;
/**
* Constructor to initialize the ISClient
*/
public GenericResourceCacheEntryFactory() {
super();
if(client == null)
{
try {
client = GHNContext.getImplementation(ISClient.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param key a QueryString representing pairs of keys and values.
* @return a list containing the Generic Resources that correspond to the query
*/
public List<ISGenericResource> createEntry(Object key) throws Exception {
QueryString querySt = (QueryString) key;
GCUBEScope scope = GCUBEScope.getScope(querySt.get(CacheEntryConstants.dl));
GCUBEGenericResourceQuery query = client.getQuery(GCUBEGenericResourceQuery.class);
System.out.println("\n\nquery: " + querySt.toString());
if(querySt.containsKey(CacheEntryConstants.id))
{
query.addAtomicConditions(new AtomicCondition("/ID", querySt.get(CacheEntryConstants.id)));
}
else if(querySt.containsKey(CacheEntryConstants.name) && querySt.get(CacheEntryConstants.name).equals(SessionConstants.ScenarioSchemaInfo))
{
query.addAtomicConditions(new AtomicCondition("/Profile/Name", querySt.get(CacheEntryConstants.name)));
query.addAtomicConditions(new AtomicCondition("/Profile/Body/DL/attribute::name", querySt.get(CacheEntryConstants.dl)));
}
else if(querySt.containsKey(CacheEntryConstants.name))
{
query.addAtomicConditions(new AtomicCondition("/Profile/Name", querySt.get(CacheEntryConstants.name)));
}
try{
List<GCUBEGenericResource> result = client.execute(query, scope);
List<ISGenericResource> res = new ArrayList<ISGenericResource>();
System.out.println("size of results: " + result.size());
for(GCUBEGenericResource resource : result)
{
ISGenericResource genResource = new ISGenericResource(resource.getID(), resource.getName(), resource.getDescription(), resource.getBody());
res.add(genResource);
}
return res;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
}