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

124 lines
4.3 KiB
Java

package org.gcube.application.framework.core.cache.factories;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xpath.XPathAPI;
import org.gcube.application.framework.core.cache.CachesManager;
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.application.framework.core.commons.model.SearchField;
import org.gcube.common.core.utils.logging.GCUBELog;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
/**
* @author Valia Tsagkalidou (NKUA)
*
*/
public class SchemaInfoCacheEntryFactory implements CacheEntryFactory {
/** Object logger. */
protected final GCUBELog logger = new GCUBELog(this);
/**
* Document factory instance
*/
public static final DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
/**
* @param key a String representing the VRE name.
* @return a HashMap that contains the Searchable Fields for each metadata schema
*/
public Object createEntry(Object key) throws Exception {
HashMap<String, List<SearchField>> SchemaHashMap = new HashMap<String, List<SearchField>>();
QueryString query = new QueryString();
query.addParameter(CacheEntryConstants.vre, key.toString());
query.addParameter(CacheEntryConstants.name, SessionConstants.MetadataSchemaInfo);
List<ISGenericResource> genericResources = ((List<ISGenericResource>)CachesManager.getInstance().getGenericResourceCache().get(query).getValue());
String schemaInfo = "<Records>";
for(ISGenericResource res : genericResources)
schemaInfo += "<Record>" + res.getBody() + "</Record>";
schemaInfo += "</Records>";
retrieveMetadataSchemaInfo(schemaInfo, SchemaHashMap);
return SchemaHashMap;
}
/**
* Retrieves the metadata schema info from DIS
*
* @param metaSchemata a serialized xml document containing the schemata together with their searchble fields
* @param SchemaHashMap a HashMap that contains the Searchable Fields for each metadata schema to be filled in
*/
protected void retrieveMetadataSchemaInfo(String metaSchemata, HashMap<String, List<SearchField>> SchemaHashMap) {
if(metaSchemata == null || metaSchemata.trim().equals(""))
return;
try {
InputSource metadata_in = new InputSource(new StringReader(metaSchemata));
Document doc = dfactory.newDocumentBuilder().parse(metadata_in);
NodeList res = doc.getElementsByTagName("Record");
int n = res.getLength();
int i;
for (int k = 0; k < n; k++)
{
//for each schema:
String schema = null;
List<SearchField> metaFields = new ArrayList<SearchField>();
schema = res.item(k).getChildNodes().item(0).getNodeName(); // schema name
System.out.println("schema=" + schema);
NodeList MetaRes = XPathAPI.selectNodeList(res.item(k), "//"+schema+"/option");
int mrLength = MetaRes.getLength();
for (i = 0; i < mrLength; i++)
{ // For each option (criterion):
try
{
NodeList res2 = MetaRes.item(i).getChildNodes();
int j, m;
m = res2.getLength();
SearchField option = new SearchField();
for (j = 0; j < m; j++)
{
String name = res2.item(j).getNodeName();
if (name.equals("option-name"))
{
option.name = res2.item(j).getFirstChild().getNodeValue();
}
else if (name.equals("option-value"))
{
option.value = res2.item(j).getFirstChild().getNodeValue();
}
else if (name.equals("option-type"))
{
option.type = res2.item(j).getFirstChild().getNodeValue();
}
else if (name.equals("option-sort"))
{
option.sort = res2.item(j).getFirstChild().getNodeValue();
}
}
metaFields.add(option);
}
catch (Exception e) {
e.printStackTrace();
}
}
SchemaHashMap.put(schema, metaFields);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}