import java.util.concurrent.BlockingQueue import org.antlr.stringtemplate.StringTemplate import org.antlr.stringtemplate.StringTemplateGroup import org.antlr.stringtemplate.language.DefaultTemplateLexer import eu.dnetlib.enabling.manager.msro.hope.groovy.RunnableGroovy import eu.dnetlib.hope.domains.common.LanguagedValue import eu.dnetlib.hope.domains.common.OAIHeader import eu.dnetlib.hope.domains.generic.DCMetadata import eu.dnetlib.hope.domains.generic.DCObject import eu.dnetlib.hope.domains.generic.DCResource import eu.dnetlib.miscutils.datetime.DateUtils /** * DNetGroovy class for TA Suomen. * @author alessia * */ class TASuomenDC implements RunnableGroovy { def String templatePath = "/var/lib/hope-scripts/templates" //def TALidoUtils utils = new TALidoUtils() def BlockingQueue resultsQueue, inputQueue; def String repositoryId; def String collectionID = "YAM100202" def String dataProvider = "Työväen Arkisto" /** * Driver namespace for elements in the OAI header. */ def dri = new groovy.xml.Namespace("http://www.driver-repository.eu/namespace/dri", 'dri') def oai = new groovy.xml.Namespace("http://www.openarchives.org/OAI/2.0/", 'oai') def generateDCCollection = {StringTemplate genericTemplate -> def OAIHeader oaiheader = new OAIHeader() oaiheader.setDateOfCollection(DateUtils.now_ISO8601()) oaiheader.setIdentifier(this.collectionID) oaiheader.setObjIdentifier(this.repositoryId+'::'+this.collectionID) oaiheader.setRepositoryId(this.repositoryId) oaiheader.setSetSpec(this.collectionID) genericTemplate.setAttribute("oaiheader", oaiheader) def String metadataLanguage = 'fin' //def String lPageURL = "" //def LandingPage landingPage = new LandingPage(localID:lPageURL, href:lPageURL) def List theSpatials = [ new LanguagedValue(value:'Finland') ] def List theTemporals = [ new LanguagedValue(value:'1893-2007') ] def List theProvenances = [ new LanguagedValue(value:dataProvider) ] def List theRights = [ new LanguagedValue(language:'', value:'No restrictions') ] def DCMetadata duMD = new DCMetadata(localID:this.collectionID, language:metadataLanguage,descriptionLevel:'collection', creationDates:['1893-2007', '1909-2007'],issueDates:[], extents:[ new LanguagedValue(value:'1577') ], mediums:[],subjects:[], spatials:theSpatials, temporals:theTemporals, relations:[], provenances:theProvenances, rights:theRights ) def List theTitles = [ new LanguagedValue(language:'fin', value:'Suomen sosialidemokraattisen työväenliikkeen sekä ammattiyhdistysliikkeen asiakirjoja') ] def String abstractSum="Documents of the Finnish trade union and social democratic labour movements" def LanguagedValue collectionAbstract = new LanguagedValue(language:'eng', value:abstractSum) def List itemLanguages = ['fin'] def DCObject dcObject = new DCObject(europeanaType:'TEXT', europeanaRights:'http://www.europeana.eu/rights/rr-f/', cpName:dataProvider, titles:theTitles, altTitles:[], theAbstract:collectionAbstract, descriptions:[], creators:[], contributors:[], publishers:[], du:duMD, dr:new DCResource(languages:itemLanguages) ) genericTemplate.setAttribute("dc", dcObject) def String collectionRes = genericTemplate.toString() //println collectionRes return collectionRes } @Override public void run() { def StringTemplateGroup group = new StringTemplateGroup("genericGroup", templatePath, DefaultTemplateLexer.class); def StringTemplate template = group.getInstanceOf("genericTemplate"); //def parser = new XmlParser(false, true); def String input; String collectionRecord = this.generateDCCollection(template) resultsQueue.put(collectionRecord); def String xmlPartOf = ''+this.collectionID+'' def fragmentToAdd = new XmlSlurper( false, false ).parseText( xmlPartOf ) template.reset() def c = 0; while(!((input = this.inputQueue.take()).equals("END"))){ //just need to add the dc:isPartOf element and the content of dc:source (TA --> Työväen Arkisto ) //if we change the template, it might be possible we have to create a mapping because TA is delivering the oai_dc equals to the HOPE generic domain profile. def xmlRoot = new XmlSlurper( false, true ).parseText(input) def dcNode = xmlRoot.'metadata' dcNode.appendNode(fragmentToAdd) dcNode.identifier.@type = 'local' def sourceNode = dcNode.'*:source' sourceNode.replaceBody(this.dataProvider); def String outputRecord = groovy.xml.XmlUtil.serialize(xmlRoot) //println outputRecord resultsQueue.put(outputRecord); c++ template.reset() } println c +" dc objects enueued" this.resultsQueue.put("END") println "closedQueue" } @Override public void setResultsQueue(BlockingQueue queue) { this.resultsQueue = queue } @Override public void setInputQueue(BlockingQueue queue) { this.inputQueue = queue } /** * TODO: calling the method from the run() does not work. Kinda groovy strange stuff, I do not know why. */ @Override public void closeResultQueue() { this.resultsQueue.put("END") println "closedQueue" } /** * {@inheritDoc} * @see eu.dnetlib.enabling.manager.msro.hope.groovy.RunnableGroovy#setRepositoryId(java.lang.String) */ @Override public void setRepositoryId(String repoId) { this.repositoryId = repoId; } }