Skip to content
Snippets Groups Projects
Commit cfdf1ec2 authored by Stefan E. Funk's avatar Stefan E. Funk
Browse files

Logging with sysouts again... have to reconsider using Logger

parent 400c9815
No related branches found
No related tags found
No related merge requests found
package info.textgrid.middleware;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.Properties;
//import javax.swing.plaf.synth.SynthSpinnerUI;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
......@@ -16,28 +23,29 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import info.textgrid.middleware.common.TextGridMimetypes;
import info.textgrid.middleware.oaipmh.ListIdentifiersType;
import info.textgrid.middleware.oaipmh.RequestType;
import info.textgrid.middleware.oaipmh.ResumptionTokenType;
import info.textgrid.middleware.common.TextGridMimetypes;
/**
*
*/
public abstract class IdentifierListDelivererAbstract implements IdentifierListDelivererInterface {
private static Logger log = Logger.getLogger(IdentifierListDelivererAbstract.class.getName());
protected boolean textgrid;
protected boolean dariah;
private boolean foundItems;
private long resultSize;
private int searchResponseSize;
public String rangeField;
protected static Map<String, Integer> cursorCollector = new Hashtable<String, Integer>();
public ResumptionTokenType resTokenForResponse;
private static org.apache.commons.logging.Log log = LogFactory.getLog(RecordListDelivererAbstract.class);
/**
* @param textgrid
* @param dariah
......@@ -59,7 +67,7 @@ public abstract class IdentifierListDelivererAbstract implements IdentifierListD
return null;
}
/**
* @param from
* @param until
......@@ -68,47 +76,48 @@ public abstract class IdentifierListDelivererAbstract implements IdentifierListD
* @return
* @throws IOException
*/
public List<String> getUriListOfAllEditions(String from, String until, String set,
String resumptionToken) throws IOException {
/*
* InputStream input = null; try { input = new
* FileInputStream("/etc/dhrep/oaipmh/oaipmh.properties"); } catch (FileNotFoundException e) {
* // TODO Auto-generated catch block e.printStackTrace(); }
*
* Properties prop = new Properties(); prop.load(input);
*/
QueryBuilder query;
log.fine("FROM: " + from);
log.fine("TO: " + until);
QueryBuilder rangeQuery = QueryBuilders.rangeQuery("created").from(from).to(until);
QueryBuilder filterSandBox = QueryBuilders.matchPhraseQuery("nearlyPublished", "true");
if (set != null) {
String[] setParts = set.split(":");
String queryField = "";
String valueField = "";
if (setParts[0].equals(TGConstants.SET_FIELD_FOR_TEXTGRID)) {
queryField = TGConstants.PROJECT_ID_FOR_TEXTGRID;
valueField = setParts[1];
}
System.out.println("");
QueryBuilder matchQuery = QueryBuilders.matchPhraseQuery(queryField, valueField);
QueryBuilder boolQuery =
QueryBuilders.boolQuery().must(rangeQuery).must(matchQuery).mustNot(filterSandBox);
query = boolQuery;
} else {
query = QueryBuilders.boolQuery().must(rangeQuery).mustNot(filterSandBox);
public List<String> getUriListOfAllEditions(String from, String until, String set, String resumptionToken) throws IOException {
/* InputStream input = null;
try {
input = new FileInputStream("/etc/dhrep/oaipmh/oaipmh.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties prop = new Properties();
prop.load(input);*/
QueryBuilder query;
System.out.println("FROM: " + from);
System.out.println("TO: " + until);
QueryBuilder rangeQuery = QueryBuilders.rangeQuery("created").from(from).to(until);
QueryBuilder filterSandBox = QueryBuilders.matchPhraseQuery("nearlyPublished", "true");
if(set!=null) {
String[] setParts = set.split(":");
String queryField = "";
String valueField = "";
if (setParts[0].equals(TGConstants.SET_FIELD_FOR_TEXTGRID)) {
queryField = TGConstants.PROJECT_ID_FOR_TEXTGRID;
valueField = setParts[1];
}
System.out.println("");
QueryBuilder matchQuery = QueryBuilders.matchPhraseQuery(queryField, valueField);
QueryBuilder boolQuery = QueryBuilders.boolQuery().must(rangeQuery).must(matchQuery).mustNot(filterSandBox);
query = boolQuery;
}else {
query = QueryBuilders.boolQuery().must(rangeQuery).mustNot(filterSandBox);
}
return getConentFromESIndex(query, resumptionToken, set);
}
return getConentFromESIndex(query, resumptionToken, set);
}
/**
* @param query
* @param resumptionToken
......@@ -117,91 +126,92 @@ public abstract class IdentifierListDelivererAbstract implements IdentifierListD
* @throws IOException
*/
private List<String> getConentFromESIndex(QueryBuilder query, String resumptionToken,
String set) throws IOException {
List<String> uriList = new ArrayList<String>();
QueryBuilder recordFilter = QueryBuilders.boolQuery().must(query)
.must(QueryBuilders.matchPhraseQuery(TGConstants.FORMAT, TextGridMimetypes.EDITION));
SearchRequest searchRequest = new SearchRequest(OAI_ESClient.getEsIndex());
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
/*
* InputStream input = null; try { input = new
* FileInputStream("/etc/dhrep/oaipmh/oaipmh.properties"); } catch (FileNotFoundException e) {
* // TODO Auto-generated catch block e.printStackTrace(); }
*
* Properties prop = new Properties(); prop.load(input);
*/
searchSourceBuilder.query(recordFilter);
searchSourceBuilder.size(100);
searchRequest.source(searchSourceBuilder);
SearchResponse scrollResp = new SearchResponse();
if (resumptionToken != null) {
SearchScrollRequest scrollRequest = new SearchScrollRequest(resumptionToken);
scrollRequest.scroll(TimeValue.timeValueHours(24L));
try {
scrollResp = OAI_ESClient.getEsClient().scroll(scrollRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
searchRequest.source(searchSourceBuilder);
searchRequest.scroll(TimeValue.timeValueHours(24L));
try {
scrollResp = OAI_ESClient.getEsClient().search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String scrollID = scrollResp.getScrollId();
long completeListSize = scrollResp.getHits().totalHits;
setResultSize(completeListSize);
if (completeListSize > 0) {
setFoundItems(true);
int i = 0;
for (SearchHit hit : scrollResp.getHits().getHits()) {
i++;
if (hit != null && hit.getFields() != null) {
uriList.add(hit.getSourceAsMap().get(TGConstants.URI).toString());
}
}
if (resumptionToken != null
&& resTokenForResponse.getCursor().intValue() >= completeListSize) {
try {
cursorCollector.remove(resumptionToken);
} catch (NullPointerException couldNotRemove) {
log.info("Could net remove hash value: " + resumptionToken + " from hash map");
}
resTokenForResponse.setValue("");
} else {
resTokenForResponse = OAIPMHUtilities.getResumptionToken(
completeListSize, resumptionToken, cursorCollector, scrollID, this.searchResponseSize,
i);
}
} else {
setFoundItems(false);
}
return uriList;
String set) throws IOException {
List<String> uriList = new ArrayList<String>();
QueryBuilder recordFilter = QueryBuilders.boolQuery().must(query)
.must(QueryBuilders.matchPhraseQuery(TGConstants.FORMAT, TextGridMimetypes.EDITION));
SearchRequest searchRequest = new SearchRequest(OAI_ESClient.getEsIndex());
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
/* InputStream input = null;
try {
input = new FileInputStream("/etc/dhrep/oaipmh/oaipmh.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties prop = new Properties();
prop.load(input);*/
searchSourceBuilder.query(recordFilter);
searchSourceBuilder.size(100);
searchRequest.source(searchSourceBuilder);
SearchResponse scrollResp = new SearchResponse();
if (resumptionToken != null) {
SearchScrollRequest scrollRequest = new SearchScrollRequest(resumptionToken);
scrollRequest.scroll(TimeValue.timeValueHours(24L));
try {
scrollResp = OAI_ESClient.getEsClient().scroll(scrollRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
searchRequest.source(searchSourceBuilder);
searchRequest.scroll(TimeValue.timeValueHours(24L));
try {
scrollResp = OAI_ESClient.getEsClient().search(searchRequest,RequestOptions.DEFAULT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String scrollID = scrollResp.getScrollId();
long completeListSize = scrollResp.getHits().totalHits;
setResultSize(completeListSize);
if (completeListSize > 0) {
setFoundItems(true);
int i=0;
for (SearchHit hit : scrollResp.getHits().getHits()) {
i++;
if (hit != null && hit.getFields() != null) {
uriList.add(hit.getSourceAsMap().get(TGConstants.URI).toString());
}
}
if(resumptionToken!=null && resTokenForResponse.getCursor().intValue()>=completeListSize) {
try{
cursorCollector.remove(resumptionToken);
}catch(NullPointerException couldNotRemove) {
log.info("Could net remove hash value: " + resumptionToken + " from hash map");
}
resTokenForResponse.setValue("");
}else {
resTokenForResponse = OAIPMHUtilities.getResumptionToken(
completeListSize, resumptionToken, cursorCollector, scrollID, this.searchResponseSize, i);
}
}else {
setFoundItems(false);
}
return uriList;
}
/**
* @param request
* @return
......@@ -224,15 +234,15 @@ public abstract class IdentifierListDelivererAbstract implements IdentifierListD
// Check if resumptionToken is invalid or existing.
if (request.getResumptionToken() != null) {
boolean restokDCExisting = IdentifierListDelivererDC.cursorCollector != null
&& IdentifierListDelivererDC.cursorCollector.containsKey(request.getResumptionToken());
&& IdentifierListDelivererDC.cursorCollector.containsKey(request.getResumptionToken());
boolean restokIDIOMExisting = IdentifierListDelivererIDIOM.cursorCollector != null
&& IdentifierListDelivererDC.cursorCollector.containsKey(request.getResumptionToken());
&& IdentifierListDelivererDC.cursorCollector.containsKey(request.getResumptionToken());
boolean restok = cursorCollector != null
&& cursorCollector.containsKey(request.getResumptionToken());
if (!restokDCExisting && !restokIDIOMExisting && !restok) {
result.setError(TGConstants.OAI_BAD_RESUMPTION_TOKEN, "The value of the "
&& cursorCollector.containsKey(request.getResumptionToken());
if (!restokDCExisting && !restokIDIOMExisting && !restok) {
result.setError(TGConstants.OAI_BAD_RESUMPTION_TOKEN, "The value of the "
+ request.getResumptionToken() + " argument is invalid or expired.");
}
}
......@@ -253,36 +263,36 @@ public abstract class IdentifierListDelivererAbstract implements IdentifierListD
return result;
}
public boolean isFoundItems() {
return foundItems;
}
public void setFoundItems(boolean foundItems) {
this.foundItems = foundItems;
}
public long getResultSize() {
return resultSize;
}
public void setResultSize(long resultSize) {
this.resultSize = resultSize;
}
public ResumptionTokenType getResTokenForResponse() {
return resTokenForResponse;
}
public void setResTokenForResponse(ResumptionTokenType resTokenForResponse) {
this.resTokenForResponse = resTokenForResponse;
}
public String getRangeField() {
return rangeField;
}
public void setRangeField(String rangeField) {
this.rangeField = rangeField;
}
public boolean isFoundItems() {
return foundItems;
}
public void setFoundItems(boolean foundItems) {
this.foundItems = foundItems;
}
public long getResultSize() {
return resultSize;
}
public void setResultSize(long resultSize) {
this.resultSize = resultSize;
}
public ResumptionTokenType getResTokenForResponse() {
return resTokenForResponse;
}
public void setResTokenForResponse(ResumptionTokenType resTokenForResponse) {
this.resTokenForResponse = resTokenForResponse;
}
public String getRangeField() {
return rangeField;
}
public void setRangeField(String rangeField) {
this.rangeField = rangeField;
}
}
......@@ -4,8 +4,8 @@ import java.io.IOException;
import java.text.ParseException;
import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException;
import org.apache.commons.logging.LogFactory;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
......@@ -18,10 +18,10 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import info.textgrid.middleware.common.TextGridMimetypes;
import info.textgrid.middleware.oaipmh.HeaderType;
import info.textgrid.middleware.oaipmh.ListIdentifiersType;
import info.textgrid.middleware.oaipmh.ResumptionTokenType;
import info.textgrid.middleware.common.TextGridMimetypes;
/**
......@@ -36,8 +36,8 @@ import info.textgrid.middleware.oaipmh.ResumptionTokenType;
public class IdentifierListDelivererDC extends IdentifierListDelivererAbstract {
private static Logger log = Logger.getLogger(IdentifierListDelivererDC.class.getName());
private static org.apache.commons.logging.Log log =
LogFactory.getLog(IdentifierListDelivererDC.class);
public static Map<String, Integer> cursorCollector = new Hashtable<String, Integer>();
private String datestamp;
......@@ -163,22 +163,22 @@ public class IdentifierListDelivererDC extends IdentifierListDelivererAbstract {
long size = listFurtherValues.getHits().totalHits;
setResultSize(size);
log.fine(" ## hits --> " + size);
System.out.println(" ## hits --> " + size);
for (SearchHit hit : listFurtherValues.getHits().getHits()) {
i++;
log.fine(" ## hit --> " + hit.getId());
System.out.println(" ## hit --> " + hit.getId());
if (this.textgrid) {
// hit.getSourceAsMap().get(this.dateOfObjectCreation).toString();
String datestamp = hit.getSourceAsMap().get(this.dateOfObjectCreation).toString();
try {
datestamp = OAIPMHUtilities.convertDateFormat(datestamp).toXMLFormat();
} catch (ParseException e) {
log.severe(e.getMessage());
} catch (DatatypeConfigurationException e) {
log.severe(e.getMessage());
} catch (ParseException e1) {
log.debug(e1);
} catch (DatatypeConfigurationException e1) {
log.debug(e1);
}
String identifier = hit.getSourceAsMap().get(this.identifierField).toString();
lit = setListIdentifierHeader(datestamp, identifier, lit, set);
......@@ -190,10 +190,10 @@ public class IdentifierListDelivererDC extends IdentifierListDelivererAbstract {
try {
this.datestamp = hit.getSourceAsMap().get(this.dateOfObjectCreation).toString();
this.datestamp = OAIPMHUtilities.convertDateFormat(this.datestamp).toXMLFormat();
} catch (ParseException e) {
log.severe(e.getMessage());
} catch (DatatypeConfigurationException e) {
log.severe(e.getMessage());
} catch (ParseException e1) {
log.debug(e1);
} catch (DatatypeConfigurationException e1) {
log.debug(e1);
}
String identifier = hit.getSourceAsMap().get(this.identifierField).toString();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment