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 a4f4c87f6f94721eaa00b8c046cb84b79ecb19f8..545cf9ed04b30052a82f13640016a6e79cffbd2c 100644 --- a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java +++ b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHImpl.java @@ -52,6 +52,7 @@ public class OAIPMHImpl implements OAIPMHProducer { private RepIdentification rep; private RecordListDelivererDC recordListDC; private RecordListDelivererIDIOM recordListIDIOM; + private OpenAireRecord openAireRecord; private RecordDelivererDC recDelivDC; private RecordDelivererIDIOM recDelivIDIOM; @@ -81,13 +82,14 @@ public class OAIPMHImpl implements OAIPMHProducer { * @param identifierListIDIOM */ public OAIPMHImpl(RepIdentification rep, RecordDelivererDC recDelivDC, - RecordDelivererIDIOM recDelivIDIOM, RecordListDelivererDC recordListDC, + RecordDelivererIDIOM recDelivIDIOM, OpenAireRecord openAireRecord, RecordListDelivererDC recordListDC, RecordListDelivererIDIOM recordListIDIOM, MetadataFormatListDeliverer metadataFormatList, SetDeliverer setList, IdentifierListDelivererDC identifierList, IdentifierListDelivererIDIOM identifierListIDIOM) { this.rep = rep; this.recDelivDC = recDelivDC; this.recDelivIDIOM = recDelivIDIOM; + this.openAireRecord = openAireRecord; this.recordListDC = recordListDC; this.recordListIDIOM = recordListIDIOM; this.metadataFormatList = metadataFormatList; @@ -233,7 +235,7 @@ public class OAIPMHImpl implements OAIPMHProducer { if (request.getMetadataPrefix().equals(TGConstants.METADATA_IDIOM_PREFIX)) { recDeliv = this.recDelivIDIOM; } - + // Finally start the QUERY! GetRecordType getRecord = new GetRecordType(); @@ -678,7 +680,7 @@ public class OAIPMHImpl implements OAIPMHProducer { * @param theJAXBOAIElement * @return */ - private static String getStringFromJAXBOAIElement(final String theVerb, + public static String getStringFromJAXBOAIElement(final String theVerb, final JAXBElement<OAIPMHtype> theJAXBOAIElement) { String result = ""; diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHUtilities.java b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHUtilities.java index 309288f6ad2be4ea1236874034857b152a29bad9..abd3aeae0cca2b6ed9dde8c7e8643f0f494edbeb 100644 --- a/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHUtilities.java +++ b/oaipmh-core/src/main/java/info/textgrid/middleware/OAIPMHUtilities.java @@ -39,8 +39,11 @@ import org.json.JSONObject; import info.textgrid.clients.AuthClient; import info.textgrid.clients.tgauth.AuthClientException; import info.textgrid.middleware.oaipmh.GetRecordType; +import info.textgrid.middleware.oaipmh.HeaderType; import info.textgrid.middleware.oaipmh.ListMetadataFormatsType; import info.textgrid.middleware.oaipmh.MetadataFormatType; +import info.textgrid.middleware.oaipmh.MetadataType; +import info.textgrid.middleware.oaipmh.OAIPMHtype; import info.textgrid.middleware.oaipmh.RecordType; import info.textgrid.middleware.oaipmh.Resource; import info.textgrid.middleware.oaipmh.ResumptionTokenType; @@ -167,7 +170,7 @@ public class OAIPMHUtilities { outFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); String output2 = outFormatter.format(date); XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(output2); - + return xmlCal; } @@ -371,6 +374,13 @@ public class OAIPMHUtilities { return fieldResulst; } + /** + * + * @param idInElasticSearchIndex + * @param includes including the fields to fetch from the elastic search index + * @param excludes excluding the fields to fetch from the elastic search index + * @return + */ public static GetResponse getRcordByIDFromElasticSearch(String idInElasticSearchIndex, String[] includes, String[] excludes) { //Including and excluding the fields to fetch from the elastic search index @@ -406,4 +416,18 @@ public class OAIPMHUtilities { marshaller.marshal(resource, System.out); } + + public static GetRecordType getRecordType(Object metadata, HeaderType header) { + + MetadataType metadataType = new MetadataType(); + RecordType recordType = new RecordType(); + GetRecordType getRecordType = new GetRecordType(); + + metadataType.setAny(metadata); + recordType.setMetadata(metadataType); + recordType.setHeader(header); + getRecordType.setRecord(recordType); + + return getRecordType; + } } diff --git a/oaipmh-core/src/main/java/info/textgrid/middleware/OpenAireRecord.java b/oaipmh-core/src/main/java/info/textgrid/middleware/OpenAireRecord.java index 2beffac2cf8c17e32ce1e8a5946a3011232ea88d..2a0ef3504c3bb8b8b4bcac85076284b41d9590b1 100644 --- a/oaipmh-core/src/main/java/info/textgrid/middleware/OpenAireRecord.java +++ b/oaipmh-core/src/main/java/info/textgrid/middleware/OpenAireRecord.java @@ -1,23 +1,22 @@ package info.textgrid.middleware; -import java.io.IOException; import java.text.ParseException; -import java.util.ArrayList; -import java.util.List; + +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.datatype.DatatypeConfigurationException; import org.apache.commons.logging.LogFactory; -import org.elasticsearch.action.get.GetRequest; -import org.elasticsearch.action.get.GetResponse; -import org.elasticsearch.client.RequestOptions; import org.elasticsearch.common.Strings; -import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.json.JSONObject; import info.textgrid.middleware.oaipmh.ContributorType; import info.textgrid.middleware.oaipmh.DateType; import info.textgrid.middleware.oaipmh.GetRecordType; +import info.textgrid.middleware.oaipmh.HeaderType; +import info.textgrid.middleware.oaipmh.OAIPMHtype; +import info.textgrid.middleware.oaipmh.ObjectFactory; import info.textgrid.middleware.oaipmh.OpenaireDescriptionType; -import info.textgrid.middleware.oaipmh.RecordType; import info.textgrid.middleware.oaipmh.RelatedIdentifierType; import info.textgrid.middleware.oaipmh.RelationType; import info.textgrid.middleware.oaipmh.Resource; @@ -34,17 +33,13 @@ import info.textgrid.middleware.oaipmh.Resource.Dates.Date; import info.textgrid.middleware.oaipmh.Resource.Descriptions; import info.textgrid.middleware.oaipmh.Resource.Descriptions.Description; import info.textgrid.middleware.oaipmh.Resource.Formats; -import info.textgrid.middleware.oaipmh.Resource.GeoLocations.GeoLocation; import info.textgrid.middleware.oaipmh.Resource.Identifier; import info.textgrid.middleware.oaipmh.Resource.RelatedIdentifiers; import info.textgrid.middleware.oaipmh.Resource.RelatedIdentifiers.RelatedIdentifier; import info.textgrid.middleware.oaipmh.Resource.RightsList; import info.textgrid.middleware.oaipmh.Resource.RightsList.Rights; -import info.textgrid.middleware.oaipmh.Resource.Sizes; -import info.textgrid.middleware.oaipmh.Resource.Subjects.Subject; import info.textgrid.middleware.oaipmh.Resource.Titles; import info.textgrid.middleware.oaipmh.Resource.Titles.Title; -import info.textgrid.middleware.oaipmh.ResourceType; /** @@ -69,7 +64,7 @@ public class OpenAireRecord extends RecordDelivererAbstract { private JSONObject jsonObj = new JSONObject(); - private List < String > titleList = new ArrayList < String > (); + //private List < String > titleList = new ArrayList < String > (); //This variables contains the field requested in the ElasticSearch Index. The content will be given by a configuration file private String[] titleFields; @@ -90,17 +85,6 @@ public class OpenAireRecord extends RecordDelivererAbstract { //This variable contains the XML-content for a whole OpenAire file private Resource resource = new Resource(); - //The rescource contains several parts. This parts are given by the following files. Further explanation for the specific fields are - // given here: https://guidelines.openaire.eu/en/latest/data/index.html - - - - - - - - - /** * * @param textgrid @@ -114,46 +98,46 @@ public class OpenAireRecord extends RecordDelivererAbstract { * building the record XML object for the OAI-PMH response * @throws ParseException */ - public GetRecordType getRecordById(String idInElasticSearchIndex) throws ParseException { - - //Including and excluding the fields to fetch from the elastic search index - String[] includes = this.fields; - String[] excludes = Strings.EMPTY_ARRAY; - - //Setting the source context for fetching the fields from the elastic search index - FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes); - - //OAI-PMH recordy type - RecordType record = new RecordType(); - //OAI-PMH getRecord type - GetRecordType grt = new GetRecordType(); - - //Building the getRequest against the elastic search index - GetRequest getRequest = - new GetRequest(OAI_ESClient.getEsIndex(), OAI_ESClient.getEsType(), idInElasticSearchIndex) - .fetchSourceContext(fetchSourceContext); - - //Declaration of the result from the get-reuest - GetResponse esResultObject = null; - - try { - esResultObject = - OAI_ESClient.getEsClient().get(getRequest, RequestOptions.DEFAULT); - - } catch (IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); - log.error("Could not fetch the result of ElasticSearch"); + public GetRecordType getRecordById(String idInElasticSearchIndex) throws ParseException, DatatypeConfigurationException { + jsonObj = new JSONObject(OAIPMHUtilities.getRcordByIDFromElasticSearch(idInElasticSearchIndex, this.fields, Strings.EMPTY_ARRAY).getSource()); + + HeaderType header = new HeaderType(); + header.setDatestamp(OAIPMHUtilities.datestampAsString(OAIPMHUtilities.fieldLoader(jsonObj, TGConstants.CREATED))); + if(!idInElasticSearchIndex.startsWith(TGConstants.ITEM_IDENTIFIER_PREFIX)) { + header.setIdentifier(TGConstants.ITEM_IDENTIFIER_PREFIX + idInElasticSearchIndex); + }else { + header.setIdentifier(idInElasticSearchIndex); } - - - this.setOpenAireRecord(idInElasticSearchIndex); + + GetRecordType grt = OAIPMHUtilities.getRecordType(this.setOpenAireRecord(idInElasticSearchIndex), header); + return grt; } - private Resource setOpenAireRecord(String idInElasticSearchIndex) throws ParseException { - - jsonObj = new JSONObject(OAIPMHUtilities.getRcordByIDFromElasticSearch(idInElasticSearchIndex, this.fields, Strings.EMPTY_ARRAY).getSource()); + public JAXBElement<OAIPMHtype> getRecord(String idInElasticSearchIndex) throws ParseException, JAXBException, DatatypeConfigurationException { + ObjectFactory obf = new ObjectFactory(); + JAXBElement<OAIPMHtype> oaijaxb; + OAIPMHtype oai = new OAIPMHtype(); + HeaderType header = new HeaderType(); + + jsonObj = new JSONObject(OAIPMHUtilities.getRcordByIDFromElasticSearch(idInElasticSearchIndex, this.fields, Strings.EMPTY_ARRAY).getSource()); + + header.setDatestamp(OAIPMHUtilities.datestampAsString(OAIPMHUtilities.fieldLoader(jsonObj, TGConstants.CREATED))); + if(!idInElasticSearchIndex.startsWith(TGConstants.ITEM_IDENTIFIER_PREFIX)) { + header.setIdentifier(TGConstants.ITEM_IDENTIFIER_PREFIX + idInElasticSearchIndex); + }else { + header.setIdentifier(idInElasticSearchIndex); + } + + GetRecordType grt = OAIPMHUtilities.getRecordType(this.setOpenAireRecord(idInElasticSearchIndex), header); + oai.setGetRecord(grt); + oaijaxb = obf.createOAIPMH(oai); + oaijaxb.setValue(oai); + + return oaijaxb; + } + + private Resource setOpenAireRecord(String idInElasticSearchIndex) throws ParseException, DatatypeConfigurationException { resource.setTitles(this.addTitles()); resource.setPublisher(this.addPublisher()); @@ -347,14 +331,13 @@ public class OpenAireRecord extends RecordDelivererAbstract { } - private String addPublicationYear() { + private String addPublicationYear() throws ParseException, DatatypeConfigurationException { /** * Publication Year fields */ //TODO: fields from config file - //TODO: for each field - //TODO: just return the year - return OAIPMHUtilities.fieldLoader(jsonObj, "created"); + + return Integer.toString(OAIPMHUtilities.convertDateFormat(OAIPMHUtilities.fieldLoader(jsonObj, "created")).getYear()); } /** * @param formatToFilter diff --git a/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhTest.java b/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhTest.java index bf08c5f1959374f25bc0a8f9a4e559d9e8e78179..71db357a868b9f7f5c73f0fbc12df410943d083a 100644 --- a/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhTest.java +++ b/oaipmh-core/src/test/java/info/textgrid/middleware/OaiPmhTest.java @@ -229,7 +229,7 @@ public class OaiPmhTest { @Test //@Ignore - public void testGetRequestGetRecordOpenAire() throws ParseException, JAXBException { + public void testGetRequestGetRecordOpenAire() throws ParseException, JAXBException, DatatypeConfigurationException { System.out.println("Test for OpenAire get record"); OpenAireRecord oar = new OpenAireRecord(true, false); @@ -259,8 +259,8 @@ public class OaiPmhTest { oar.setRelationToWorkObject("edition.isEditionOf"); oar.setRelatedIdentifierFields(relatedIdentifierFields); - oar.getRecordById("24gv8.0"); - System.out.println("Identifier: " + oar.getResource().getIdentifier().getIdentifierType() + " " + oar.getResource().getIdentifier().getValue()); + //oar.getRecordById("24gv8.0"); + /*System.out.println("Identifier: " + oar.getResource().getIdentifier().getIdentifierType() + " " + oar.getResource().getIdentifier().getValue()); System.out.println("Creator: " + oar.getResource().getCreators().getCreator().get(0).getCreatorName() + " " + oar.getResource().getCreators().getCreator().get(0).getNameIdentifier().getValue()); System.out.println("Title: " + oar.getResource().getTitles().getTitle().get(0).getValue()); System.out.println("Publisher: " + oar.getResource().getPublisher()); @@ -307,10 +307,13 @@ public class OaiPmhTest { System.out.println(relatedIdentifier.getRelatedIdentifierType()); System.out.println(relatedIdentifier.getRelationType()); } - + */ + System.out.println("-----------------------------------\n"); - OAIPMHUtilities.marshal(oar.getResource()); - System.out.println(oar.getResource()); + //OAIPMHUtilities.marshal(oar.getResource()); + System.out.println(OAIPMHImpl.getStringFromJAXBOAIElement("GetRecord", oar.getRecord("24gv8.0"))); + + //OAIPMHUtilities.marshal(oar.getRecord("24gv8.0")); }