Project

General

Profile

1
package eu.dnetlib.oai.actions;
2

    
3
import com.google.common.collect.Iterables;
4
import eu.dnetlib.enabling.resultset.client.ResultSetClient;
5
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
6
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
7
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
8
import eu.dnetlib.oai.sync.OAIStoreSynchronizer;
9
import eu.dnetlib.rmi.common.ResultSet;
10
import eu.dnetlib.rmi.provision.MDFInfo;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.dom4j.io.SAXReader;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.core.io.ClassPathResource;
16

    
17
public class SyncAction extends AbstractOAIStoreAction {
18

    
19
	private static final Log log = LogFactory.getLog(SyncAction.class); // NOPMD by marko on 11/24/08 5:02 PM
20
	private final SAXReader saxReader = new SAXReader();
21
	org.springframework.core.io.Resource dmfXsltResource = new ClassPathResource("/eu/dnetlib/oai/xslt/addDMFBlock.xslt");
22
	@Autowired
23
	private OAIStoreSynchronizer synchronizer;
24
	@Autowired
25
	private ResultSetClient resultSetClient;
26

    
27
	@Override
28
	public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws Exception {
29
		String epr = job.getParameters().get("oai_syncEpr");
30
		String recordSource = job.getParameters().get("oai_recordSource");
31
		String formatName = job.getParameters().get("oai_formatName");
32
		String formatLayout = job.getParameters().get("oai_formatLayout");
33
		String formatInterpretation = job.getParameters().get("oai_formatInterpretation");
34
		boolean alwaysNewRecord = Boolean.valueOf(job.getParameters().get("oai_alwaysNewRecord"));
35
		final String dbName = job.getParameters().get("oai_dbName");
36
		MDFInfo mdf = new MDFInfo("", "", "", formatName, formatLayout, formatInterpretation, "", true);
37

    
38
		ResultSet<?> resutlSet = ResultSet.fromJson(epr);
39
		log.info("Syncronizing content for oai on db " + dbName + "for metadata format: " + mdf);
40
		Iterable<String> records = resultSetClient.iter(resutlSet, String.class);
41
		// in case of DMF there is no "hat element" after oai:metadata: we must add it or the OAI-PMH export will only contain the first
42
		// element inside oai:metadata.
43
		if (formatName.equalsIgnoreCase("DMF")) {
44
			records = addDMFBlock(records);
45
		}
46
		this.synchronizer.synchronize(records, mdf, recordSource, dbName, alwaysNewRecord, () -> {
47
			handler.done(job);
48
			return null;
49
		}, () -> {
50
			handler.failed(job, new Exception("Error during OAI synchronization on db " + dbName));
51
			return null;
52
		});
53

    
54
	}
55

    
56
	protected Iterable<String> addDMFBlock(final Iterable<String> input) {
57
		final ApplyXslt addDMFBlockXslt = new ApplyXslt(dmfXsltResource);
58
		return Iterables.transform(input, addDMFBlockXslt::apply);
59
	}
60

    
61
	public OAIStoreSynchronizer getSynchronizer() {
62
		return synchronizer;
63
	}
64

    
65
	public void setSynchronizer(final OAIStoreSynchronizer synchronizer) {
66
		this.synchronizer = synchronizer;
67
	}
68

    
69
}
(9-9/9)