473 lines
17 KiB
Java
473 lines
17 KiB
Java
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<ISGenericResource> getGenericResource(QueryString query)
|
|
{
|
|
return (List<ISGenericResource>) (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<GCUBEGenericResource> 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<ISGenericResource> genRes = new ArrayList<ISGenericResource>();
|
|
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<Pair> getAvailableGenericResourceNames() throws RemoteException {
|
|
List<Pair> pairs = new ArrayList<Pair>();
|
|
String xquery = "for $query in collection(\"/db/Profiles/GenericResource\")//Document/Data/child::*[local-name()='Profile']/Resource return <Result>{$query/ID}{$query/Profile/Name}</Result>"; // order by $query/Profile/Name
|
|
GCUBEGenericQuery queryMan;
|
|
try {
|
|
queryMan = client.getQuery(GCUBEGenericQuery.class);
|
|
queryMan.setExpression(xquery);
|
|
List<XMLResult> res = client.execute(queryMan, session.getScope());
|
|
String id, name;
|
|
for(XMLResult xml : res)
|
|
{
|
|
try
|
|
{
|
|
id = xml.evaluate("//ID/text()").get(0);
|
|
List<String> 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<ISGenericResource> 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<ISGenericResource> 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<ISGenericResource> 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<ISGenericResource>)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<String,Vector<String[]>> getAllXslts(String xsltType) {
|
|
HashMap<String,Vector<String[]>> schemas_xslts = new HashMap<String,Vector<String[]>>();
|
|
Vector<String[]> xslts = null;
|
|
|
|
|
|
|
|
List<String[]> 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<result.size(); i++) {
|
|
String fullName = result.get(i)[0];
|
|
String id = result.get(i)[1];
|
|
String tmp = fullName.substring(xsltType.length()+1,fullName.length());
|
|
String parts[] = tmp.split("_");
|
|
String schemaName = parts[0];
|
|
String xsltName = parts[1];
|
|
xslts = schemas_xslts.get(schemaName.replaceFirst("%5f", "_"));
|
|
if (xslts == null) {
|
|
xslts = new Vector<String[]>();
|
|
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<result.size(); i++) {
|
|
String fullName = result.get(i)[0];
|
|
System.out.println("fullname: " + fullName);
|
|
String id = result.get(i)[1];
|
|
System.out.println("id: " + id);
|
|
// String tmp = fullName.substring(xsltType.length()+1,fullName.length());
|
|
String tmp = fullName.substring(xsltType.length()+3,fullName.length());
|
|
System.out.println("tmp: " + tmp);
|
|
String parts[] = tmp.split("-\\|-");
|
|
String schemaName = parts[0];
|
|
for (int k = 0; k < parts.length; k++) {
|
|
System.out.println("part: " + parts[k]);
|
|
}
|
|
System.out.println("schemaName: " + schemaName);
|
|
String xsltName = parts[1];
|
|
System.out.println("xsltName: " + xsltName);
|
|
xslts = schemas_xslts.get(schemaName);
|
|
if (xslts == null) {
|
|
xslts = new Vector<String[]>();
|
|
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<String[]> 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>{$result/ID}{$result/Profile/Name}</Result>"));
|
|
|
|
List<XMLResult> result = client.execute(query, scope);
|
|
List<String[]> ret = new LinkedList<String[]>();
|
|
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;
|
|
}
|
|
|
|
|
|
}
|