import java.util.concurrent.BlockingQueue import org.antlr.stringtemplate.StringTemplate import org.antlr.stringtemplate.StringTemplateGroup import org.antlr.stringtemplate.language.DefaultTemplateLexer import org.apache.commons.lang.StringEscapeUtils import eu.dnetlib.enabling.manager.msro.hope.groovy.RunnableGroovy import eu.dnetlib.hope.domains.common.* import eu.dnetlib.hope.domains.library.DateStm import eu.dnetlib.hope.domains.library.MarcEditionStm import eu.dnetlib.hope.domains.library.MarcPublication import eu.dnetlib.hope.domains.library.MarcRecord import eu.dnetlib.hope.domains.library.MarcRelative import eu.dnetlib.hope.domains.library.MarcTitle import eu.dnetlib.hope.domains.library.MarcTranslatedTitle import eu.dnetlib.miscutils.datetime.DateUtils class AMSABNonDigitalLibrary implements RunnableGroovy { def String templatePath = "/var/lib/hope-scripts/templates" def BlockingQueue resultsQueue, inputQueue; def String repositoryId; def String collectionID = "10796/9644DCBC-5187-493F-BB26-6F2C79E36A1B" def String collectionPID = 'http://hdl.handle.net/'+collectionID @Override public void run() { def StringTemplateGroup group = new StringTemplateGroup("libraryGroup", templatePath, DefaultTemplateLexer.class); def StringTemplate libraryTemplate = group.getInstanceOf("libraryTemplate"); def parser = new XmlParser(false, true) def input; def String collectionDate = DateUtils.now_ISO8601() def String marcCollection = createCollection(libraryTemplate, collectionDate) resultsQueue.put(marcCollection) def c = 1; while(!((input = this.inputQueue.take()).equals("END"))){ def Node xmlRoot = parser.parseText(input) def Node recordNode = xmlRoot.'**'.metadata.record[0] def String marcRecord = transformRecord(recordNode, libraryTemplate, collectionDate) if(marcRecord){ resultsQueue.put(marcRecord); c++; } //need to sleep for a while (10 are not enough) because otherwise //I got a ConcurrentModificationEception on the push result set on the side of the aggregator (as installed on node1.p.hope) //Please consider that we might not need this in the future at Amsterdam -- the aggregator will be faster! Thread.sleep(30) } libraryTemplate.reset() println c +" marc records enueued" this.resultsQueue.put("END") println "closedQueue" } def transformRecord = { Node record, StringTemplate template, String collectionDate -> template.reset() //identifiers: def String metadataId = record.local_identifier.text() def String metadataPID = record.HOPE_DU_landingPage?.text() def String repoObjId = (this.repositoryId+"::"+metadataId) //oai header def OAIHeader oaiheader = new OAIHeader(dateOfCollection:collectionDate, repositoryId:this.repositoryId, identifier:metadataId, objIdentifier:repoObjId, nonDigitalCollection:true) def src = record.HOPE_CP_name.text() def String mdLanguage = record.HOPE_DU_languageMetadata?.text() def MarcRecord marcRecord = new MarcRecord(descriptionLevel:record.HOPE_LU_descriptionLevel?.text(), localID:metadataId, pid:metadataPID, europeanaType:record.HOPE_DR_type?.text(), aggregator:"HOPE - Heritage of the people's Europe", landingPage:new LandingPage(href:metadataPID), providerName:src, providerLanguage:mdLanguage, isbn_020:StringEscapeUtils.escapeXml(record.isbn?.text()), issn_022:StringEscapeUtils.escapeXml(record.issn?.text()), bookNo_091:StringEscapeUtils.escapeXml(record.call_number?.text()), extents:record.extent*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, physDetails:record.illustrations*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, dimensions:record.dimensions*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, materialDesignations:record.accompanying_material*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, theabstracts:record.'abstract'*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, keywords:record.'keyword.contents'*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, personSubjects_600:record.'person.keyword.name'*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, corporateSubjects_610:record.'keyword.proper_name'*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, creatorPersons_100:record.'author.name'*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, creatorCorps_110:record.'corporate_author'*.text().collect{return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))}, temporalCoverages_648:record.'search_year'*.text(), spatialCoverages_651:record.'geographical_keyword'*.text().collect{return StringEscapeUtils.escapeXml(it) }, titles_245:[ new MarcTitle(main:new MarcTranslatedTitle(original:StringEscapeUtils.escapeXml(record.HOPE_DU_title?.text())), materialDesignation:StringEscapeUtils.escapeXml(record.material_type?.text())) ], editionStms_250:[ new MarcEditionStm(main:StringEscapeUtils.escapeXml(record.edition?.text()), responsibility:StringEscapeUtils.escapeXml(record.statement_of_responsibility?.text())) ], publication:new MarcPublication(publisher:StringEscapeUtils.escapeXml(record.publisher?.text()), placeOfPublication:StringEscapeUtils.escapeXml(record.place_of_publication?.text()), dateOfPublication:StringEscapeUtils.escapeXml(record.year_of_publication?.text()) ), language:mdLanguage, languages_041:record.'language_code'*.text(), notes:record.notes*.text().collect{ return new LanguagedValue(value:StringEscapeUtils.escapeXml(it))} ) //relatives def String parentID = record.HOPE_DU_isContainedBy?.text() if(parentID) marcRecord.setParentRecord(new MarcRelative(localID:parentID, pid:'http://hdl.handle.net/'+parentID)) else marcRecord.setParentRecord(new MarcRelative(localID:this.collectionID, pid:this.collectionPID)) template.setAttribute("oaiheader", oaiheader) template.setAttribute("marc", marcRecord) return template.toString(); } def createCollection = { StringTemplate template, String collectionDate -> template.reset() def OAIHeader oaiheader = new OAIHeader(dateOfCollection:collectionDate, repositoryId:this.repositoryId, identifier:this.collectionID, objIdentifier:repositoryId+"::"+collectionID, nonDigitalCollection:true) def String theAbstract = ''' The collection covers the broad field of Belgian social history from 1750 to the present, yet also includes many works on international topics. It includes not only documents from the past activities of Belgian social movements, but also research publications in the social history domain. ''' def MarcRecord marcCollection = new MarcRecord(pid:this.collectionPID, localID:this.collectionID, descriptionLevel:'collection', europeanaType:'TEXT',language:'Dutch', accessRights:'Europeana Rights Reserved Free Access', useRights:'Europeana Rights Reserved Free Access', europeanaRights:'http://www.europeana.eu/rights/rr-f/', leader:'', , aggregator:"HOPE - Heritage of the people's Europe", creationDate:new DateStm(date:'1750-2013', stm:'1750-2013'), accumulationDate:new DateStm(date:'1980-2013', stm:'1980-2013'), isbn_020:'', issn_022:'', providerName:'Amsab-Institute of Social History', providerLanguage:'Dutch', bookNo_091:'', extents:[ new LanguagedValue(value:'109909') ], physDetails:[], dimensions:[],materialDesignations:[ new LanguagedValue(value:'books, articles, serials') ], accruals:[], toc:'', theabstracts:[ new LanguagedValue(language:'eng', value:theAbstract) ], genre:'', keywords:[], creatorPersons_100:[], creatorCorps_110:[ 'Amsab-Institute of Social History' ], contributorMeetings_711:[], personSubjects_600:[], corporateSubjects_610:[], temporalCoverages_648:['1750-2013'], topicSubjects_650:[], spatialCoverages_651:['Belgium'], personContributors:[], corporateContributors:[], creatorMeetings_111:[], titles_245:[ new MarcTitle(main:new MarcTranslatedTitle(original:'Library Collection')) ], editionStms_250:[], repositories:[ 'Amsab-Institute of Social History' ], languages_041:[ 'Dutch', 'French', 'English', 'German', 'Italian' ], catalog:'OPAC available at http://opac.amsab.be') template.setAttribute("oaiheader", oaiheader) template.setAttribute("marc", marcCollection) return template.toString(); } @Override public void setResultsQueue(BlockingQueue queue) { this.resultsQueue = queue } @Override public void setInputQueue(BlockingQueue queue) { this.inputQueue = queue } @Override public void closeResultQueue() { this.resultsQueue.put("END") } @Override public void setRepositoryId(String repoId) { this.repositoryId = repoId } }