From 250d615d219326ce97e0fa8a04b0760102ea0ee4 Mon Sep 17 00:00:00 2001
From: mbrodhu <behnert-brodhun@sub.uni-goettingen.de>
Date: Sat, 14 Nov 2020 20:22:38 +0100
Subject: [PATCH] enable get alluris with elasticsearch instaed of triplestore

---
 .../info/textgrid/middleware/OAIPMHImpl.java  |  2 +
 .../middleware/RecordListDelivererIDIOM.java  | 89 ++++++++++++++++++-
 2 files changed, 89 insertions(+), 2 deletions(-)

diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java
index 030e4abf..bb82e4a4 100644
--- a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java
+++ b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java
@@ -418,9 +418,11 @@ public class OAIPMHImpl implements OAIPMHProducer {
             && RecordListDelivererDC.cursorCollector.containsKey(request.getResumptionToken());
         boolean restokIDIOMExisting = RecordListDelivererIDIOM.cursorCollector != null
             && RecordListDelivererIDIOM.cursorCollector.containsKey(request.getResumptionToken());
+        System.out.println("OAIIMPL: " + request.getResumptionToken());
         if (restokDCExisting) {
           recListDeliv = this.recordListDC;
         } else if (restokIDIOMExisting) {
+        	System.out.println("DUMDIDUM");
           recListDeliv = this.recordListIDIOM;
         }
       }
diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/RecordListDelivererIDIOM.java b/oaipmh-core/src/main/java/info/textgrid/middleware/RecordListDelivererIDIOM.java
index 3f9453d9..20213d67 100644
--- a/oaipmh-core/src/main/java/info/textgrid/middleware/RecordListDelivererIDIOM.java
+++ b/oaipmh-core/src/main/java/info/textgrid/middleware/RecordListDelivererIDIOM.java
@@ -6,6 +6,12 @@ import java.util.Map;
 import java.util.UUID;
 import org.apache.commons.logging.LogFactory;
 import org.classicmayan.tools.Queries;
+import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.search.SearchHit;
+
 import info.textgrid.middleware.oaipmh.HeaderType;
 import info.textgrid.middleware.oaipmh.ListRecordsType;
 import info.textgrid.middleware.oaipmh.MetadataType;
@@ -21,12 +27,14 @@ public class RecordListDelivererIDIOM extends RecordListDelivererAbstract {
   static Map<String, Integer> cursorCollector = new Hashtable<String, Integer>();
 
   private OAI_ESClient oaiEsClient;
+  private boolean foundItems;
 
   /**
    * @param textgrid
    * @param dariah
    */
   public RecordListDelivererIDIOM(boolean textgrid, boolean dariah) {
+	
     super(textgrid, dariah);
   }
 
@@ -53,7 +61,7 @@ public class RecordListDelivererIDIOM extends RecordListDelivererAbstract {
    * @see info.textgrid.middleware.RecordListDelivererInterface#getRecords(java.lang.String,
    * java.lang.String, java.lang.String, java.lang.String)
    */
-  @Override
+ /* @Override
   public ListRecordsType getRecords(final String from, final String to, final String set,
       final String resumptionToken) {
 
@@ -80,6 +88,7 @@ public class RecordListDelivererIDIOM extends RecordListDelivererAbstract {
 
     for (String artefactURI : artefactURIs) {
       RecordDelivererIDIOM idiomRecord = new RecordDelivererIDIOM(true, false);
+
       recordList.getRecord().add(idiomRecord.getRecordById(artefactURI).getRecord());
       i++;
     }
@@ -93,8 +102,76 @@ public class RecordListDelivererIDIOM extends RecordListDelivererAbstract {
     }
 
     return recordList;
-  }
+  }*/
 
+  @Override
+  public ListRecordsType getRecords(final String from, final String to, final String set,
+	      final String resumptionToken) {
+      System.out.println("BUFF");
+	  ListRecordsType recordList = new ListRecordsType();
+	  
+	  QueryBuilder recordFilterForClassicMayan;
+	  QueryBuilder rangeQuery;
+	  Queries queries = new Queries();
+	  List<String> artefactURIs = queries.getArtefactList();;
+	  
+	  rangeQuery = QueryBuilders.rangeQuery("created").from(from).to(to);
+	  recordFilterForClassicMayan = QueryBuilders.boolQuery().must(rangeQuery)
+			  .must(QueryBuilders.matchPhraseQuery("project.id", "TGPR-0e926f53-1aba-d415-ecf6-539edcd8a318"))
+			  .must(QueryBuilders.matchPhraseQuery("format", "text/tg.inputform+rdf+xml"))
+			  .must(QueryBuilders.matchPhraseQuery("notes", "ARTEFACT"));
+	  
+	  SearchResponse scrollResp;
+	  
+	  if (resumptionToken == null) {
+		  
+	      scrollResp = OAI_ESClient.getOaiESClient()
+	              .prepareSearch("textgrid-nonpublic")
+	              .setScroll(new TimeValue(60000))
+	              .setTypes(OAI_ESClient.getEsType())
+	              .addFields("lastModified", "textgridUri", "project.id")
+	              .setQuery(recordFilterForClassicMayan)
+	              .setSize(30)
+	              .execute()
+	              .actionGet();
+	  }else {
+		  System.out.println("resumptionToken for res: " + resumptionToken);
+	      scrollResp = OAI_ESClient.getOaiESClient().prepareSearchScroll(resumptionToken)
+	              .setScroll(new TimeValue(60000)).execute().actionGet();
+	  }
+	  
+	  String scrollID = scrollResp.getScrollId();
+	  
+	  long completeListSize = scrollResp.getHits().totalHits();
+	    //System.out.println(completeListSize);
+	    //System.out.println(completeListSize + " || " + artefactURIs.size());
+	    if (completeListSize > 0) {
+	        setFoundItems(true);
+	        int i = 0;
+	        //System.out.println("inside ");
+	        for (SearchHit hit : scrollResp.getHits().getHits()) {
+	          i++;
+	          String textgridURI = hit.getFields().get("textgridUri").getValue().toString().replace(".0", "");
+	          if(artefactURIs.contains(textgridURI)) {
+	        	  RecordDelivererIDIOM idiomRecord = new RecordDelivererIDIOM(true, false);
+	        	  recordList.getRecord().add(idiomRecord.getRecordById(textgridURI).getRecord());
+	          }
+	        }
+
+		   // Check the need for a resumption token!
+	       System.out.println("resumptionToken: " + resumptionToken);
+		   ResumptionTokenType resTokenForResponse = OAIPMHUtilities.getResumptionToken(
+		          completeListSize, resumptionToken, cursorCollector, scrollID, 30, i);
+		   if (resTokenForResponse != null) {
+			   recordList.setResumptionToken(resTokenForResponse);
+		   }
+	    }else {
+	      setFoundItems(false);
+	    }
+	    
+	 return recordList;
+  }
+  
   /*
    * (non-Javadoc)
    * 
@@ -120,4 +197,12 @@ public class RecordListDelivererIDIOM extends RecordListDelivererAbstract {
     this.oaiEsClient = oaiEsClient;
   }
 
+public boolean isFoundItems() {
+	return foundItems;
+}
+
+public void setFoundItems(boolean foundItems) {
+	this.foundItems = foundItems;
+}
+
 }
-- 
GitLab