Project

General

Profile

1
package eu.dnetlib.oai.actions;
2

    
3
import javax.xml.transform.TransformerFactory;
4

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

    
20
public class SyncAction extends AbstractOAIStoreAction {
21

    
22
	private static final Log log = LogFactory.getLog(SyncAction.class); // NOPMD by marko on 11/24/08 5:02 PM
23

    
24
	org.springframework.core.io.Resource dmfXsltResource = new ClassPathResource("/eu/dnetlib/oai/xslt/addDMFBlock.xslt");
25
	@Autowired
26
	private OAIStoreSynchronizer synchronizer;
27
	@Autowired
28
	private ResultSetClient resultSetClient;
29
	@Autowired
30
	private TransformerFactory saxonTransformerFactory;
31

    
32
	@Override
33
	public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws Exception {
34
		if(log.isDebugEnabled()) {
35
			log.debug("Job parameters:");
36
			job.getParameters().forEach((k, v) -> log.debug("Name : " + k + " Value : " + v));
37
		}
38

    
39
		String epr = job.getParameters().get(OAIParameterNames.OAI_SYNC_EPR);
40
		String recordSource = job.getParameters().get(OAIParameterNames.OAI_SOURCE);
41
		boolean alwaysNewRecord = Boolean.valueOf(job.getParameters().get(OAIParameterNames.OAI_ALWAYS_NEW_RECORD));
42
		final String dbName = job.getParameters().get(OAIParameterNames.OAI_DB);
43

    
44
		final String collectionName = job.getParameters().get(OAIParameterNames.OAI_COLLECTON);
45
		final MetadataReference mdRef = MetadataReferenceFactory.decode(collectionName, MDREF_SEPARATOR);
46

    
47
		final ResultSet<?> rs = ResultSet.fromJson(epr);
48
		log.info("Synchronizing content for oai on db " + dbName + " for metadata format: " + mdRef);
49
		Iterable<String> records = resultSetClient.iter(rs, String.class);
50
		// 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
51
		// element inside oai:metadata.
52
		if (mdRef.getFormat().equalsIgnoreCase("DMF") || mdRef.getFormat().equalsIgnoreCase("PMF")) {
53
			records = addDMFBlock(records);
54
		}
55
		this.synchronizer.synchronize(records, mdRef, recordSource, dbName, alwaysNewRecord, () -> {
56
			handler.done(job);
57
			return null;
58
		}, () -> {
59
			handler.failed(job, new Exception("Error during OAI synchronization on db " + dbName));
60
			return null;
61
		});
62

    
63
	}
64

    
65
	protected Iterable<String> addDMFBlock(final Iterable<String> input) {
66
		final ApplyXslt addDMFBlockXslt = new ApplyXslt(dmfXsltResource, saxonTransformerFactory);
67
		return Iterables.transform(input, addDMFBlockXslt::apply);
68
	}
69

    
70
	public OAIStoreSynchronizer getSynchronizer() {
71
		return synchronizer;
72
	}
73

    
74
	public void setSynchronizer(final OAIStoreSynchronizer synchronizer) {
75
		this.synchronizer = synchronizer;
76
	}
77

    
78
}
(8-8/8)