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.oai.utils.OAIParameterNames;
10
import eu.dnetlib.rmi.common.ResultSet;
11
import eu.dnetlib.utils.MetadataReference;
12
import eu.dnetlib.utils.MetadataReferenceFactory;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.core.io.ClassPathResource;
17

    
18
public class SyncAction extends AbstractOAIStoreAction {
19

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

    
22
	org.springframework.core.io.Resource dmfXsltResource = new ClassPathResource("/eu/dnetlib/oai/xslt/addDMFBlock.xslt");
23
	@Autowired
24
	private OAIStoreSynchronizer synchronizer;
25
	@Autowired
26
	private ResultSetClient resultSetClient;
27

    
28
	@Override
29
	public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws Exception {
30
		String epr = job.getParameters().get(OAIParameterNames.OAI_SYNC_EPR);
31
		String recordSource = job.getParameters().get(OAIParameterNames.OAI_SOURCE);
32
		boolean alwaysNewRecord = Boolean.valueOf(job.getParameters().get(OAIParameterNames.OAI_ALWAYS_NEW_RECORD));
33
		final String dbName = job.getParameters().get(OAIParameterNames.OAI_DB);
34

    
35
		final String collectionName = job.getParameters().get(OAIParameterNames.OAI_COLLECTON);
36
		final MetadataReference mdRef = MetadataReferenceFactory.decode(collectionName, MDREF_SEPARATOR);
37

    
38
		final ResultSet<?> rs = ResultSet.fromJson(epr);
39
		log.info("Synchronizing content for oai on db " + dbName + " for metadata format: " + mdRef);
40
		Iterable<String> records = resultSetClient.iter(rs, 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 (mdRef.getFormat().equalsIgnoreCase("DMF") || mdRef.getFormat().equalsIgnoreCase("PMF")) {
44
			records = addDMFBlock(records);
45
		}
46
		this.synchronizer.synchronize(records, mdRef, 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
}
(8-8/8)