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.Derivative 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.LidoResourceWrap import eu.dnetlib.hope.domains.visual.LidoTitle import eu.dnetlib.hope.domains.visual.TypedSubjects /** * DNetGroovy class for SSA F_9004. Records come with no derivative2 (for now, becasue the SOR does not produce them yet). * @author alessia * */ class SozArchLIDO9004 implements RunnableGroovy { def String templatePath = "/var/lib/hope-scripts/templates" def SozArchUtils utils = new SozArchUtils() def BlockingQueue resultsQueue, inputQueue; def String collectionID = 'F_9004' /** * 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 = "http://www.bild-video-ton.ch/archiv/images?Bilder.ssaBestand=F_9004 Gewerkschaft Verkauf Handel Transport Lebensmittel (VHTL) - Fédération du commerce, des transports et de l'alimentation (FCTA)" def String providerName = 'Schweizerisches Sozialarchiv' def String abstractSum="Die Gewerkschaft VHTL lieferte vier Filme ab." def OAIHeader collOaiheader = new OAIHeader(dateOfCollection:dateOfCollection, repositoryId:repoId, setSpec:setSpec, identifier:collectionID, objIdentifier:repoId+"::"+collectionID) def LIDOCollection coll9004 = new LIDOCollection(collectionID:collectionID, langCode:'ger', titles:[ new LidoTitle(value:"F_9004 Gewerkschaft Verkauf Handel Transport Lebensmittel (VHTL) - Fédération du commerce, des transports et de l'alimentation (FCTA)") ], abstractSummaries:[ new LidoDescription(type:'abstract', value:abstractSum) ], collItemLanguages:['ger'], creationDates:['1950P10Y'], accumulationDates:['2004'], owners:[providerName], sizeItems:'4', itemType:'VIDEO', itemFormatStm:'', itemFormats:['Film: 16mm, Azetat'], associatedPub:'http://findmittel.ch/archive/archNeu/ArVHTL.html', typedSubjects:[ new TypedSubjects(type:'association', dates:['1950-1960'], places:[ new LidoPlace(preferredName:'CH') ]) ], subjects:['Gewerkschaften'], repository:providerName, useRights:"Jede Art der Verwendung und der Reproduktion setzt eine Bewilligung und einen vereinbarten Zweck voraus. Jede weitere Nutzung bedarf der erneuten Zustimmung des Schweizerischen Sozialarchivs oder des jeweiligen Rechteinhabers.", accessRights:'Auf den Bestand oder auf Teile davon gewährt das Schweizerische Sozialarchiv öffentlichen, kostenlosen Zugang. Weitere Bestimmungen finden Sie auf unseren Websites.', europeanaType:'VIDEO', landingPage:new LandingPage(href:lPageURL, localID:lPageURL)) def String collection9004 = coll9004.getXML(template, collOaiheader, providerName, coll9004) return collection9004 } @Override public void run() { def StringTemplateGroup group = new StringTemplateGroup("visualGroup", templatePath, DefaultTemplateLexer.class); def StringTemplate 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; 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 repoId = oaiHeaderNode.'dri:repositoryId'.text() def setSpec = oaiHeaderNode.setSpec.text() //create collection from constants values from the mapping if(c == 0){ def String collection9004 = generateCollectionRecord(template, repoId, setSpec, dateOfCollection) resultsQueue.put(collection9004); c++ } def Node sozRoot = xmlRoot.'**'.ImagicXML[0] def NodeList sozObjects = sozRoot.Update.Row println "Found "+sozObjects.size()+" rows" sozObjects.each { String lidoObject = utils.transformSozObjekt(it, template, dateOfCollection, repoId, setSpec, collectionID, generateDigitalResources) resultsQueue.put(lidoObject); c++ template.reset() } } println c +" lido objects enueued" this.resultsQueue.put("END") println "closedQueue" } /** * Generate digital resources, if needed. It returns an object of class LidoResourceWrap. * The returned object is never null, but all properties can be empty. */ def generateDigitalResources = {Node sozObjekt -> def String resourcePid = 'http://hdl.handle.net/'+sozObjekt.Field.find{it.'@Name'.equals('tk_pid')}.'@Value' def String europeanaType = sozObjekt.Field.find{it.'@Name'.equals('dcTY_typ')}?.'@Value' def String rights = sozObjekt.Field.find{it.'@Name'.equals('ssaRechte')}?.'@Value' def String trans = '' //set resourcePid to null, so we do not generate digital resources for closed objects if(rights.equals("kein Onlinezugriff auf Ressource!") || rights.equals("kein Online-Zugriff auf Ressource!")){ resourcePid = '' } if(europeanaType.equals('SOUND') || europeanaType.equals('VIDEO')) trans = StringEscapeUtils.escapeXml(utils.recodeLineBreaks(sozObjekt.Field.find{it.'@Name'.equals('dcBE_besch')}?.'@Value')) def ders = [ new Derivative(pid:resourcePid+'?locatt=view:level1', type:'derivative2'), new Derivative(pid:resourcePid+'?locatt=view:level3', type:'derivative3') ] def LidoResourceWrap resWrap = new LidoResourceWrap(localID:resourcePid, pid:resourcePid, europeanaType:sozObjekt.Field.find{it.'@Name'.equals('dcTY_typ')}?.'@Value', languages:sozObjekt.Field.findAll{it.'@Name'.equals('dcLA_spra')}*.'@Value', creditLine:rights,transcription:trans, derivatives:ders) return resWrap; } @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" } /** * TODO: use this to set the repoId of the collection instead of the current trick * {@inheritDoc} * @see eu.dnetlib.enabling.manager.msro.hope.groovy.RunnableGroovy#setRepositoryId(java.lang.String) */ @Override public void setRepositoryId(String repoId) {} }