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.LandingPage import eu.dnetlib.hope.domains.common.OAIHeader import eu.dnetlib.hope.domains.visual.LIDOCollection import eu.dnetlib.hope.domains.visual.LidoDescription import eu.dnetlib.hope.domains.visual.LidoPlace import eu.dnetlib.hope.domains.visual.LidoTitle import eu.dnetlib.hope.domains.visual.TypedSubjects /** * DNetGroovy class for AMSAB Visual collection * @author alessia * */ class AMSABVisualCollection implements RunnableGroovy { def String templatePath = "/var/lib/hope-scripts/templates" def AMSABVisualUtils utils = new AMSABVisualUtils() def BlockingQueue resultsQueue, inputQueue; def String collectionID = '648DEBEF-D576-47C7-B2F4-5D2F1CFD795A' def String repositoryID /** * Driver namespace for elements in the OAI header. */ def dri = new groovy.xml.Namespace("http://www.driver-repository.eu/namespace/dri", 'dri') def generateCollectionRecord = { template, repoId, setSpec, dateOfCollection -> def String lPageURL = '' def String providerName = 'Amsab-Institute of Social History' def String abstractSum='''Collection of visual material concerning Belgian social history: photographs, posters, flags, etc., 1830-2013''' def OAIHeader collOaiheader = new OAIHeader(dateOfCollection:dateOfCollection, repositoryId:repoId, setSpec:setSpec, identifier:collectionID, objIdentifier:repoId+"::"+collectionID) def LIDOCollection coll = new LIDOCollection(collectionID:collectionID,collectionPID:collectionID, langCode:'Dutch', titles:[ new LidoTitle(value:'Visual collection') ], abstractSummaries:[ new LidoDescription(type:'abstract', value:abstractSum, langCode:'eng') ], descriptions:[], creationDates:['1830-2013'], accumulationDates:['2004-2013'], owners:[providerName], sizeItems:'33712', itemType:'IMAGE', itemFormatStm:'', associatedPub:'', typedSubjects:[ new TypedSubjects(type:'association', dates:['1830-2013'], places:[ new LidoPlace(preferredName:'Belgium') ]) ], subjects:[], repository:providerName, useRights:"Europeana Rights Reserved Free Access", accessRights:'Europeana Rights Reserved Free Access', europeanaType:'IMAGE', landingPage:new LandingPage(href:lPageURL, localID:lPageURL)) def String collection = coll.getXML(template, collOaiheader, providerName, coll) return collection } @Override public void run() { def StringTemplate template = null try{ def StringTemplateGroup group = new StringTemplateGroup("visualGroup", templatePath, DefaultTemplateLexer.class); template = group.getInstanceOf("visualTemplate"); //iterates on the input xml to generate records contained by the collection created above def parser = new XmlParser(false, true); def String input; def c = 0; def skipped = 0; while(!((input = this.inputQueue.take()).equals("END"))){ template.reset() def Node xmlRoot = parser.parseText(input) def Node oaiHeaderNode = xmlRoot.'**'.header[0] //Common OAI Header elements def dateOfCollection = oaiHeaderNode.'dri:dateOfCollection'.text() def setSpec = oaiHeaderNode.setSpec.text() def id = oaiHeaderNode.identifier.text() //create collection from constants values from the mapping if(c == 0){ def String collection = generateCollectionRecord(template, this.repositoryID, setSpec, dateOfCollection) resultsQueue.put(collection) c++ } def Node record = xmlRoot.'**'.metadata.record[0] if(hasTitle(record)){ String lidoObject = utils.transform(record, template, dateOfCollection, this.repositoryID, setSpec, collectionID) resultsQueue.put(lidoObject) c++ template.reset() } else { println "Record "+id+" has no title --> Skipping" skipped++ } } println c +" lido objects enueued" println skipped+" skipped" this.resultsQueue.put("END") println "closedQueue" }catch(Exception e){ println "An exception in groovy script: "+e.getMessage() e.printStackTrace() this.resultsQueue.put("END") println "closedQueue" } finally{ if(template) template.reset() } } def hasTitle = {Node recordNode -> def List titles = recordNode.title*.text().findAll{it != null && it.trim() != ''} return !titles.isEmpty() } @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} }