diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/IdentifierListDeliverer.java b/oaipmh-core/src/main/java/info/textgrid/middleware/IdentifierListDeliverer.java index 8eef691f46eb83d99d10026ed75555b5379ef21c..dedd073e1416206e3294dbfe885b33dec586b6bb 100644 --- a/oaipmh-core/src/main/java/info/textgrid/middleware/IdentifierListDeliverer.java +++ b/oaipmh-core/src/main/java/info/textgrid/middleware/IdentifierListDeliverer.java @@ -55,10 +55,6 @@ public class IdentifierListDeliverer { private static Map<String, Integer> cursorCollector = new Hashtable<String, Integer >(); private int searchResponseSize; - - ResumptionTokenType resTokenForResponse = new ResumptionTokenType(); - - /** * In OAIPMH a ListIdentifiers request is answered by responding the @@ -196,28 +192,13 @@ public class IdentifierListDeliverer { } - int cursor; - if(resumptionToken != null && cursorCollector.containsKey(resumptionToken)){ - cursor = cursorCollector.get(resumptionToken).intValue() + i; - resTokenForResponse.setCursor(BigInteger.valueOf((long) cursor)); - cursorCollector.put(listFurtherValues.getScrollId(), cursor); - - }else { - resTokenForResponse.setCursor(BigInteger.valueOf(searchResponseSize)); - cursorCollector.put(listFurtherValues.getScrollId(), searchResponseSize); - cursor = 0; - } - - if(cursor < listFurtherValues.getHits().getTotalHits()) { - resTokenForResponse.setValue(resumptionToken); - } - - if(cursor >= listFurtherValues.getHits().getTotalHits()) { - resTokenForResponse.setValue(""); - } - - resTokenForResponse.setCompleteListSize(BigInteger.valueOf(listFurtherValues.getHits().getTotalHits())); - lit.setResumptionToken(resTokenForResponse); + // Check the need for a resumption token! + ResumptionTokenType resTokenForResponse = OAIPMHUtillities.getResumptionToken( + listFurtherValues.getHits().getTotalHits(), resumptionToken, cursorCollector, + listFurtherValues.getScrollId(), this.searchResponseSize, i); + if (resTokenForResponse != null) { + lit.setResumptionToken(resTokenForResponse); + } return listFurtherValues; } diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHUtillities.java b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHUtillities.java index 0eb441cc934c4c74282f0ed83f94703a702ebe4b..9489e1bac75d063c361eb69fb4a597cac319efde 100644 --- a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHUtillities.java +++ b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHUtillities.java @@ -1,13 +1,15 @@ package info.textgrid.middleware; - import info.textgrid.middleware.oaipmh.ListMetadataFormatsType; import info.textgrid.middleware.oaipmh.MetadataFormatType; +import info.textgrid.middleware.oaipmh.ResumptionTokenType; +import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; +import java.util.Map; import java.util.TimeZone; import javax.xml.datatype.DatatypeConfigurationException; @@ -15,7 +17,8 @@ import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; /** - * OAIPMHUtillities is responsible to implement all necessary functions to implement the arguments of a request + * OAIPMHUtillities is responsible to implement all necessary functions to + * implement the arguments of a request * * @author Maximilian Brodhun: SUB * @version 1.0 @@ -24,92 +27,168 @@ import javax.xml.datatype.XMLGregorianCalendar; */ public class OAIPMHUtillities { - + /** - * Since textGrid just supports dc the only returned MetadataFormat is dublinCore + * Since textGrid just supports dc the only returned MetadataFormat is + * dublinCore + * * @return */ - - public MetadataFormatType setMetadataFormats(){ + public MetadataFormatType setMetadataFormats() { + MetadataFormatType tgDublinCore = new MetadataFormatType(); - - tgDublinCore.setMetadataNamespace("http://www.openarchives.org/OAI/2.0/oai_dc/"); + tgDublinCore + .setMetadataNamespace("http://www.openarchives.org/OAI/2.0/oai_dc/"); tgDublinCore.setMetadataPrefix("oai:dc"); - tgDublinCore.setSchema("http://www.openarchives.org/OAI/2.0/oai_dc.xsd"); - + tgDublinCore + .setSchema("http://www.openarchives.org/OAI/2.0/oai_dc.xsd"); + return tgDublinCore; } - + /** * producing the list of all metadata formats + * * @return tgRepMetadaFormats */ - - public ListMetadataFormatsType setMetadataFormatList(){ - - ListMetadataFormatsType tgRepMetadataFormats = new ListMetadataFormatsType(); + public ListMetadataFormatsType setMetadataFormatList() { + ListMetadataFormatsType tgRepMetadataFormats = new ListMetadataFormatsType(); MetadataFormatType tgDublinCore = setMetadataFormats(); - - tgRepMetadataFormats.getMetadataFormat().add(tgDublinCore ); - + tgRepMetadataFormats.getMetadataFormat().add(tgDublinCore); + return tgRepMetadataFormats; } - + /** * calculating the current date and give it back as XMLGregorianCalendar + * * @return now * @throws DatatypeConfigurationException */ - - public static XMLGregorianCalendar getXMLGregorianCalendarNow() throws DatatypeConfigurationException{ + public static XMLGregorianCalendar getXMLGregorianCalendarNow() + throws DatatypeConfigurationException { + GregorianCalendar gregorianCalendar = new GregorianCalendar(); - DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); - XMLGregorianCalendar now = - datatypeFactory.newXMLGregorianCalendar(gregorianCalendar); - return now; + DatatypeFactory datatypeFactory = DatatypeFactory.newInstance(); + XMLGregorianCalendar now = datatypeFactory + .newXMLGregorianCalendar(gregorianCalendar); + + return now; } - + /** - * Converting a given string representing a date value into the date format required for OAIPMH - * and give it back as XMLGregorianCalendar - * + * Converting a given string representing a date value into the date format + * required for OAIPMH and give it back as XMLGregorianCalendar + * * @param originalDateTimeString * @return xmlCal * @throws ParseException * @throws DatatypeConfigurationException */ - public static XMLGregorianCalendar convertDateFormat(String originalDateTimeString) throws ParseException, DatatypeConfigurationException{ - - SimpleDateFormat tgItemTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S"); + public static XMLGregorianCalendar convertDateFormat( + String originalDateTimeString) throws ParseException, + DatatypeConfigurationException { + + SimpleDateFormat tgItemTime = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss.S"); Date date = tgItemTime.parse(originalDateTimeString); - SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + SimpleDateFormat outFormatter = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss'Z'"); outFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); String output2 = outFormatter.format(date); - XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(output2); - + XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance() + .newXMLGregorianCalendar(output2); + return xmlCal; } - + /** - * Converting a given string representing a date value into the date format required for OAIPMH - * and give it back as string + * Converting a given string representing a date value into the date format + * required for OAIPMH and give it back as string * * @param originalDateTimeString - * @return dateOutputAsString + * @return dateOutputAsString * @throws ParseException */ - - public static String datestampAsString(String originalDateTimeString) throws ParseException{ - - SimpleDateFormat tgItemTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S"); + public static String datestampAsString(String originalDateTimeString) + throws ParseException { + + SimpleDateFormat tgItemTime = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss.S"); Date date = tgItemTime.parse(originalDateTimeString); - SimpleDateFormat outFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + SimpleDateFormat outFormatter = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss'Z'"); outFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); String dateOutputAsString = outFormatter.format(date); - + return dateOutputAsString; } - - + + /** + * <p> + * Returns a resumptionToken for a search, if necessary. + * </p> + * + * TODO Check if we really must put all these references in! + * + * @param completeListSize + * @param resumptionToken + * @param cursorCollector + * @param scrollID + * @param searchResponseSize + * @param i + * @return A resumption token if applicable, null otherwise. + */ + public static ResumptionTokenType getResumptionToken( + final long completeListSize, final String resumptionToken, + Map<String, Integer> cursorCollector, final String scrollID, + final int searchResponseSize, final int i) { + + int cursor; + + // Three cases here: + // 1. Complete list size is > 100 and a token already is existing: we + // need to check hash map! + // 2. Complete list size is > 100 and we have no token: we do need one! + // 3. Complete list size is <= 100 (we do not need a token! do nothing!) + if (completeListSize > 100) { + ResumptionTokenType resTokenForResponse = new ResumptionTokenType(); + + if (resumptionToken != null + && cursorCollector.containsKey(resumptionToken)) { + cursor = cursorCollector.get(resumptionToken).intValue() + i; + resTokenForResponse + .setCursor(BigInteger.valueOf((long) cursor)); + cursorCollector.put(scrollID, cursor); + } else { + resTokenForResponse.setCursor(BigInteger + .valueOf(searchResponseSize)); + cursorCollector.put(scrollID, searchResponseSize); + cursor = 0; + } + + // Set resumption token string if cursor is less then complete list + // size: More objects can be delivered! + if (cursor < completeListSize) { + resTokenForResponse.setValue(scrollID); + } + + // Remove resumption token string if cursor is bigger then or equal + // complete list size: This is the last response, no more objects + // are available! + if (cursor >= completeListSize) { + resTokenForResponse.setValue(""); + } + + // Set complete list size. + resTokenForResponse.setCompleteListSize(BigInteger + .valueOf(completeListSize)); + + return resTokenForResponse; + } + + return null; + } + } diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/RecordListDeliverer.java b/oaipmh-core/src/main/java/info/textgrid/middleware/RecordListDeliverer.java index 9766491daa436f5e68a9e7ea35c306db10aa6d90..6a11b764ed19fa9fbcc4ade70040c5381d0a7df6 100644 --- a/oaipmh-core/src/main/java/info/textgrid/middleware/RecordListDeliverer.java +++ b/oaipmh-core/src/main/java/info/textgrid/middleware/RecordListDeliverer.java @@ -107,12 +107,10 @@ public class RecordListDeliverer { private String modifiedValue; private static final int lifeTimeResToken = 60000; - public static Map<String, Integer> cursorCollector = new Hashtable<String, Integer >(); + static Map<String, Integer> cursorCollector = new Hashtable<String, Integer >(); private int searchResponseSize; - - ResumptionTokenType resTokenForResponse = new ResumptionTokenType(); - + /** * @param oaiEsClient */ @@ -165,7 +163,7 @@ public class RecordListDeliverer { */ public void fetchFields(QueryBuilder query, ListRecordsType recordList, String resumptionToken, String set) throws UnsupportedEncodingException { - + // System.out.println("RESTOKEN: "+ resumptionToken); //SearchResponse getRecordList = null; String scrollID = ""; @@ -199,11 +197,11 @@ public class RecordListDeliverer { //System.out.println("QUERY: " + scrollResp); scrollID = scrollResp.getScrollId(); - long completeListeSize = scrollResp.getHits().totalHits(); + long completeListSize = scrollResp.getHits().totalHits(); // System.out.println("COMPLETELISTESIZE: " + completeListeSize); - if (completeListeSize > 0) { + if (completeListSize > 0) { setFoundItems(true); // System.out.println("ÃœBER IF"); @@ -268,39 +266,13 @@ public class RecordListDeliverer { // System.out.println("BLABLABLABLA"); - int cursor; - // Three cases here: - // 1. Complete list size is > 100 and a token already is existing: we need to check hash map! - // 2. Complete list size is > 100 and we have no token: we do need one! - // 3. Complete list size is <= 100 (we do not need a token! do nothing here!) - if (completeListeSize > 100) { - if (resumptionToken != null && cursorCollector.containsKey(resumptionToken)){ - cursor = cursorCollector.get(resumptionToken).intValue() + i; - resTokenForResponse.setCursor(BigInteger.valueOf((long) cursor)); - cursorCollector.put(scrollID, cursor); - //cursorCollector.remove(resumptionToken); - } else { - resTokenForResponse.setCursor(BigInteger.valueOf(searchResponseSize)); - cursorCollector.put(scrollID, searchResponseSize); - cursor = 0; - } - - // Set resumption token string if cursor is less then complete list size: - // More objects can be delivered! - if(cursor < scrollResp.getHits().getTotalHits()) { - resTokenForResponse.setValue(scrollID); - } - - // Remove resumption token string if cursor is bigger then or equal complete list size: - // This is the last response, no more objects are available! - if(cursor >= scrollResp.getHits().getTotalHits()) { - resTokenForResponse.setValue(""); - } - - resTokenForResponse.setCompleteListSize(BigInteger.valueOf(scrollResp.getHits().getTotalHits())); + // Check the need for a resumption token! + ResumptionTokenType resTokenForResponse = OAIPMHUtillities.getResumptionToken( + completeListSize, resumptionToken, cursorCollector, scrollID, searchResponseSize, i); + if (resTokenForResponse != null) { recordList.setResumptionToken(resTokenForResponse); } - + } else { setFoundItems(false); }