4250: Geonetwork Resolver upgrade: it must return only "private" Metadata Ids for CKAN harversting

Task-Url: https://support.d4science.org/issues/4250

Updated 

 <csw:SearchResults numberOfRecordsMatched="XX" numberOfRecordsReturned="XX" elementSet="summary" nextRecord="XX">

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@129138 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-06-16 13:48:58 +00:00
parent ca90066456
commit 6afb574c7c
2 changed files with 90 additions and 6 deletions

View File

@ -7,8 +7,11 @@ package org.gcube.datatransfer.resolver.gis.util;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -19,10 +22,13 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
/** /**
@ -37,12 +43,11 @@ public class GetResponseRecordFilter {
/** /**
* Delete summary record. * Delete summary record.
* *
* @param doc * @param doc the doc
* the doc * @param identifier the identifier
* @param identifier * @return true, if successful
* the identifier
*/ */
private static void deleteSummaryRecord(Document doc, String identifier) { private static boolean deleteSummaryRecord(Document doc, String identifier) {
// <csw:SummaryRecord> list // <csw:SummaryRecord> list
NodeList nodes = doc.getElementsByTagName("csw:SummaryRecord"); NodeList nodes = doc.getElementsByTagName("csw:SummaryRecord");
@ -56,8 +61,10 @@ public class GetResponseRecordFilter {
if (idValue.equals(identifier)) { if (idValue.equals(identifier)) {
summaryRecord.getParentNode().removeChild(summaryRecord); summaryRecord.getParentNode().removeChild(summaryRecord);
logger.trace("Removed child " + idValue); logger.trace("Removed child " + idValue);
return true;
} }
} }
return false;
} }
@ -78,9 +85,36 @@ public class GetResponseRecordFilter {
dbf.setValidating(false); dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder(); DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(bis); Document doc = db.parse(bis);
int removed = 0;
for (String identifier : idsToRemove) { for (String identifier : idsToRemove) {
deleteSummaryRecord(doc, identifier); if(deleteSummaryRecord(doc, identifier))
removed++;
} }
logger.debug("Removed "+removed +" node/s");
NodeList nodeList = doc.getElementsByTagName("csw:SearchResults");
if(nodeList!=null && nodeList.item(0)!=null){
Node nd = nodeList.item(0);
// update staff attribute
NamedNodeMap attr = nd.getAttributes();
Node numberOfRecordsMatched = attr.getNamedItem("numberOfRecordsMatched");
Node numberOfRecordsReturned = attr.getNamedItem("numberOfRecordsReturned");
logger.trace("Old numberOfRecordsMatched: "+numberOfRecordsMatched.getTextContent());
logger.trace("Old numberOfRecordsReturned: "+numberOfRecordsReturned.getTextContent());
try{
int oldValueM = Integer.parseInt(numberOfRecordsMatched.getTextContent());
int oldValueR = Integer.parseInt(numberOfRecordsReturned.getTextContent());
int newValueM = oldValueM-removed;
int newValueR = oldValueR-removed;
logger.trace("Updating numberOfRecordsMatched at: "+newValueM);
logger.trace("Updating numberOfRecordsReturned at: "+newValueR);
numberOfRecordsMatched.setTextContent(newValueM+"");
numberOfRecordsReturned.setTextContent(newValueR+"");
}catch (Exception e) {
logger.warn("An error occurred during attribe numberOfRecordsMatched updating, skipping operation");
}
}
return documentToInputStream(doc); return documentToInputStream(doc);
} }
catch (Exception e) { catch (Exception e) {
@ -107,4 +141,25 @@ public class GetResponseRecordFilter {
return new ByteArrayInputStream(outputStream.toByteArray()); return new ByteArrayInputStream(outputStream.toByteArray());
// return out.toString(); // return out.toString();
} }
public static void main(String[] args) {
File file = new File("GetResponseRecords.xml");
List<String> idsToRemove = new ArrayList<String>();
idsToRemove.add("fao-species-map-hef");
idsToRemove.add("fao-species-map-hea");
idsToRemove.add("fao-species-map-crb");
idsToRemove.add("fao-fsa-map-41.1.2");
try {
InputStream is = GetResponseRecordFilter.removeSummaryIdsByListIds(new FileInputStream(file), idsToRemove);
System.out.println(IOUtils.toString(is));
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }

View File

@ -0,0 +1,29 @@
# Created by Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
# Set root category priority to ERROR and its only appender to LOGFILE.
log4j.rootCategory=ERROR, LOGFILE
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.MaxFileSize=25MB
log4j.appender.LOGFILE.MaxBackupIndex=10
log4j.appender.LOGFILE.File=logs/uri-resolver.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=[%-5p] %d %c - %m%n
#log4j.category.org.gcube=TRACE,A1
##GCube
log4j.category.org.gcube=ERROR, LOGFILE
log4j.additivity.org.gcube=false
##HomeLibrary
#log4j.logger.org.gcube.portlets.user.homelibrary=INFO, LOGFILE
###MongoLog
log4j.logger.com.mongodb=SEVERE, LOGFILE
##UriResolver
log4j.logger.org.gcube.datatransfer.resolver=DEBUG
##Storage
log4j.logger.org.gcube.contentmanager=INFO