From 1c6bbc9ecec89ed26b61d147af471cb1db8fa43a Mon Sep 17 00:00:00 2001 From: Rena Tsantouli Date: Fri, 1 Jul 2011 14:46:25 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerCore@41087 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../framework/core/util/GenericResource.java | 472 ++++++++++++++++++ 1 file changed, 472 insertions(+) create mode 100644 src/org/gcube/application/framework/core/util/GenericResource.java diff --git a/src/org/gcube/application/framework/core/util/GenericResource.java b/src/org/gcube/application/framework/core/util/GenericResource.java new file mode 100644 index 0000000..161117b --- /dev/null +++ b/src/org/gcube/application/framework/core/util/GenericResource.java @@ -0,0 +1,472 @@ +package org.gcube.application.framework.core.util; + +import java.io.StringReader; +import java.rmi.RemoteException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.gcube.application.framework.core.GenericResourceInfoI; +import org.gcube.application.framework.core.cache.CachesManager; +import org.gcube.application.framework.core.genericresources.model.ISGenericResource; +import org.gcube.application.framework.core.security.PortalSecurityManager; +import org.gcube.application.framework.core.session.ASLSession; +import org.gcube.application.framework.core.session.SessionManager; +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.GHNContext; +import org.gcube.common.core.informationsystem.client.AtomicCondition; +import org.gcube.common.core.informationsystem.client.ISClient; +import org.gcube.common.core.informationsystem.client.QueryParameter; +import org.gcube.common.core.informationsystem.client.XMLResult; +import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery; +import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericResourceQuery; +import org.gcube.common.core.informationsystem.publisher.ISPublisher; +import org.gcube.common.core.informationsystem.publisher.ISPublisherException; +import org.gcube.common.core.resources.GCUBEGenericResource; +import org.gcube.common.core.scope.GCUBEScope; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; + +/** + * @author Valia Tsaqgkalidou (NKUA) + */ +public class GenericResource implements GenericResourceInfoI{ + + /** + * A static IS publisher in order to communicate with IS + */ + protected static ISPublisher publisher = null; + protected static ISClient client = null; + + /** + * The D4Science session to be used + */ + ASLSession session; + + /** + * Constructs a GenericReosurce object + * @param extrenalSessionID the external session id which is used for the D4ScienceSession (usually the session.getId() of the HttpSession or PortletSession) + * @param username the username of the user that called this constructor + */ + public GenericResource(String extrenalSessionID, String username) + { + session = SessionManager.getInstance().getASLSession(extrenalSessionID, username); + try { + publisher = GHNContext.getImplementation(ISPublisher.class); + } catch (Exception e) { + e.printStackTrace(); + } + if(client == null) + { + try { + client = GHNContext.getImplementation(ISClient.class); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + client = null; + } + } + } + + + + /** + * Constructs a GenericReosurce object + * @param session the D4Science session to be used for retrieving information needed + */ + public GenericResource(ASLSession session) { + super(); + this.session = session; + try { + publisher = GHNContext.getImplementation(ISPublisher.class); + } catch (Exception e) { + e.printStackTrace(); + } + if(client == null) + { + try { + client = GHNContext.getImplementation(ISClient.class); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + client = null; + } + } + } + + + + /** + * @return the name of the active VRE + */ + protected String getDLName() + { + //SOS + //return session.getOriginalScopeName(); + return session.getOriginalScopeName(); + } + + /** + * @param query the query to retrieve generic resources from cache + * @return a list of generic resources + */ + protected List getGenericResource(QueryString query) + { + return (List) (CachesManager.getInstance().getGenericResourceCache().get(query).getValue()); + } + + + /** + * Creates a new generic resource + * @param genericResource the new generic resource + * @return the ID of the newly created resource + * @throws RemoteException when an error has occurred while communicating with IS + */ + public String createGenericResource(ISGenericResource genericResource) + throws RemoteException { + try { + GCUBEGenericResource gCubeRes = GHNContext.getImplementation(GCUBEGenericResource.class); + gCubeRes.setID(""); + gCubeRes.setName(genericResource.getName()); + gCubeRes.setDescription(genericResource.getDescription()); + gCubeRes.setBody(genericResource.getBody()); + gCubeRes.setSecondaryType(genericResource.getSecondaryType()); + gCubeRes.addScope(session.getScope()); + + String Gr = publisher.registerGCUBEResource(gCubeRes, session.getScope(), new PortalSecurityManager(session)); + Document doc = parseXMLFileToDOM(Gr); + XPath xpath = XPathFactory.newInstance().newXPath(); + try { + return (String) xpath.evaluate("/Resource/ID/text()", doc, XPathConstants.STRING); + } catch (XPathExpressionException e) { + e.printStackTrace(); + throw new RemoteException(); + } + } catch (Exception e) { + e.printStackTrace(); + throw new RemoteException(); + } + } + + + /** + * Updates a generic resource based on it's ID + * @param genericResource the generic resource to be updated + * @throws RemoteException when an error has occurred while communicating with IS + */ + public void updateGenericResourceByID(ISGenericResource genericResource) + throws RemoteException { + try { + + //GCUBEGenericResource gCubeRes = GHNContext.getImplementation(GCUBEGenericResource.class); + + System.out.println("In updateGenericResourceByID!"); + GCUBEGenericResourceQuery query = client.getQuery(GCUBEGenericResourceQuery.class); + query.addAtomicConditions(new AtomicCondition("/ID", genericResource.getId())); + List result = client.execute(query, session.getScope()); + + if (result == null || result.size() == 0) { + System.out.println("Error in updateGenericResourceByID: The generic resources list is null"); + return; + } + + + + GCUBEGenericResource gCubeRes = result.get(0); + + + // gCubeRes.setID(genericResource.getId()); + gCubeRes.setName(genericResource.getName()); + gCubeRes.setDescription(genericResource.getDescription()); + gCubeRes.setBody(genericResource.getBody()); + gCubeRes.setSecondaryType(genericResource.getSecondaryType()); + // gCubeRes.addScope(session.getScope()); + + try { + //SOS!!!! uncomment this and erase the two lines bellow + //publisher.updateGCUBEResource(gCubeRes, session.getScope(), new PortalSecurityManager(session)); + + String resultIS = publisher.registerGCUBEResource(gCubeRes, session.getScope(), new PortalSecurityManager(session)); + System.out.println ("ISPublisher returned: " + resultIS); + }catch (Exception e) { + e.printStackTrace(); + } + + + + List genRes = new ArrayList(); + genRes.add(genericResource); + QueryString query1 = new QueryString(); + query1.addParameter(CacheEntryConstants.id, genericResource.getId()); + query1.addParameter(CacheEntryConstants.vre, session.getOriginalScopeName()); + CachesManager.getInstance().getGenericResourceCache().put(new net.sf.ehcache.Element(query1, genRes)); + + query1.clear(); + query1.put(CacheEntryConstants.name, genericResource.getName().trim()); + query1.addParameter(CacheEntryConstants.vre, session.getOriginalScopeName()); + if(CachesManager.getInstance().getGenericResourceCache().isElementInMemory(query1) || CachesManager.getInstance().getGenericResourceCache().isElementOnDisk(query1)) + { + CachesManager.getInstance().getGenericResourceCache().get(query1).setTimeToLive(-1); + } + else { + System.out.println("The generic resource was not found"); + } + + + + } catch (Exception e) { + e.printStackTrace(); + throw new RemoteException(); + } + } + + + /** + * @return a list containing pairs of (name, id) of the available generic resources + * @throws RemoteException when an error has occurred while communicating with IS + */ + public List getAvailableGenericResourceNames() throws RemoteException { + List pairs = new ArrayList(); + String xquery = "for $query in collection(\"/db/Profiles/GenericResource\")//Document/Data/child::*[local-name()='Profile']/Resource return {$query/ID}{$query/Profile/Name}"; // order by $query/Profile/Name + GCUBEGenericQuery queryMan; + try { + queryMan = client.getQuery(GCUBEGenericQuery.class); + queryMan.setExpression(xquery); + List res = client.execute(queryMan, session.getScope()); + String id, name; + for(XMLResult xml : res) + { + try + { + id = xml.evaluate("//ID/text()").get(0); + List var = xml.evaluate("//Name/text()"); + if(var.size() > 0) + name = var.get(0); + else + name = ""; + System.out.println(id + " " + name); + pairs.add(new Pair(name, id)); + } + catch(Exception e) + { + + } + } + return pairs; + } catch (Exception e) { + e.printStackTrace(); + throw new RemoteException(); + } + } + + + /** + * @param id the id of the generic resource + * @return a list containing the corresponding generic resources + * @throws RemoteException when an error has occurred while communicating with IS + */ + public List getGenericResourceByID(String id) + throws RemoteException { + QueryString query = new QueryString(); + query.put(CacheEntryConstants.id, id); + query.put(CacheEntryConstants.vre, getDLName()); + return getGenericResource(query); + } + + + /** + * @param name the name of the generic resource + * @return a list containing the generic resources that have as name the given + * @throws RemoteException when an error has occurred while communicating with IS + */ + public List getGenericResourceByName(String name) + throws RemoteException { + QueryString query = new QueryString(); + query.put(CacheEntryConstants.name, name); + query.put(CacheEntryConstants.vre, getDLName()); + return getGenericResource(query); + } + + + /** + * @return a list containing the generic resources that describe which collections are part of the active VRE as well as their hierarchical structure (the name of this generic resource is "ScenarioCollectionInfo") + * @throws RemoteException when an error has occurred while communicating with IS + */ + public List getGenericResourceForScenario() + throws RemoteException { + QueryString query = new QueryString(); + query.put(CacheEntryConstants.name, SessionConstants.ScenarioSchemaInfo); + query.put(CacheEntryConstants.vre, getDLName()); + return getGenericResource(query); + } + + /** + * Removes an existing generic resource + * @param genericResource the generic resource to be removed + * @throws RemoteException when an error has occurred while communicating with IS + */ + public void removeGenericResource(ISGenericResource genericResource) + throws RemoteException { + try { + publisher.removeGCUBEResource(genericResource.getId(), GCUBEGenericResource.TYPE, session.getScope(), new PortalSecurityManager(session)); + + QueryString query = new QueryString(); + query.addParameter(CacheEntryConstants.id, genericResource.getId()); + query.addParameter(CacheEntryConstants.vre, session.getOriginalScopeName()); + if(CachesManager.getInstance().getGenericResourceCache().isElementInMemory(query) || CachesManager.getInstance().getGenericResourceCache().isElementOnDisk(query)) + { + genericResource = ((List)CachesManager.getInstance().getGenericResourceCache().get(query).getValue()).get(0); + CachesManager.getInstance().getGenericResourceCache().get(query).setTimeToLive(0); + } + + query.clear(); + query.put(CacheEntryConstants.name, genericResource.getName()); + query.addParameter(CacheEntryConstants.vre, session.getOriginalScopeName()); + if(CachesManager.getInstance().getGenericResourceCache().isElementInMemory(query) || CachesManager.getInstance().getGenericResourceCache().isElementOnDisk(query)) + { + CachesManager.getInstance().getGenericResourceCache().get(query).setTimeToLive(0); + } + } catch (ISPublisherException e) { + e.printStackTrace(); + throw new RemoteException(); + } + + } + + + /** + * Parses the xml and returns it as DOM object + * @param XMLdoc the xml as a string + * @return xml parsed as a Document + */ + static Document parseXMLFileToDOM(String XMLdoc) { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; + try { + builder = dbFactory.newDocumentBuilder(); + Document doc = builder.parse(new InputSource(new StringReader(XMLdoc))); + return doc; + } catch (Exception e) { + return null; + } + } + + + + /** + * + * @param xsltType Presentation or Metadata. The type of the xslt + * @return A vector which contains all the generic resources, xslts of this type. The xslts are sorted by the schema. + */ + public HashMap> getAllXslts(String xsltType) { + HashMap> schemas_xslts = new HashMap>(); + Vector xslts = null; + + + + List result = null; + try { + result = retrieveGenericResourcesFromNameParts(xsltType, session.getScope()); + } catch (Exception e) { + e.printStackTrace(); + } + + if (result == null) { + return null; + } + /* if (xsltType.equals("MetadataXSLT")) { + for (int i=0; i(); + schemas_xslts.put(schemaName.replaceFirst("%5f", "_"), xslts); + } + + // add the xslt name and id + String s[] = new String[2]; + s[0] = xsltName; + s[1] = id; + xslts.add(s); + } + return schemas_xslts; + }*/ + // else { + // Presentation xslts + System.out.println("Inside getAllXslts"); + System.out.println("The type is: " + xsltType); + for (int i=0; i(); + schemas_xslts.put(schemaName, xslts); + } + + // add the xslt name and id + String s[] = new String[2]; + s[0] = xsltName; + s[1] = id; + xslts.add(s); + } + return schemas_xslts; + // } + } + + private static List retrieveGenericResourcesFromNameParts(String nameParts, GCUBEScope scope) throws Exception { + + String condition = "contains($result/Profile/Name/string(), '" + nameParts + "')"; + + GCUBEGenericQuery query = client.getQuery("GCUBEResourceQuery"); + query.addParameters(new QueryParameter("FILTER", condition), + new QueryParameter("TYPE", GCUBEGenericResource.TYPE), + new QueryParameter("RESULT", "{$result/ID}{$result/Profile/Name}")); + + List result = client.execute(query, scope); + List ret = new LinkedList(); + if (result==null || result.size()==0) + throw new Exception("No generic resources were found."); + + for(XMLResult r : result){ + String[] nameAndID = { r.evaluate("//Name/text()").get(0), r.evaluate("//ID/text()").get(0) }; + System.out.println("Inside retrieveGenericResourcesFromNameParts name: " + nameAndID[0]); + System.out.println("Inside retrieveGenericResourcesFromNameParts id: " + nameAndID[1]); + ret.add(nameAndID); + } + return ret; + } + + +}