Project

General

Profile

1
package eu.dnetlib.oai.actions;
2

    
3
import java.util.concurrent.Callable;
4
import javax.annotation.Resource;
5

    
6
import com.google.common.collect.Iterables;
7
import eu.dnetlib.enabling.resultset.client.ResultSetClient;
8
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
9
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
10
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
11
import eu.dnetlib.oai.sync.OAIStoreSynchronizer;
12
import eu.dnetlib.rmi.common.ResultSet;
13
import eu.dnetlib.rmi.provision.MDFInfo;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.dom4j.io.SAXReader;
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
	private final SAXReader saxReader = new SAXReader();
24
	org.springframework.core.io.Resource dmfXsltResource = new ClassPathResource("/eu/dnetlib/data/oai/store/xslt/addDMFBlock.xslt");
25
	@Resource
26
	private OAIStoreSynchronizer synchronizer;
27
	@Autowired
28
	private ResultSetClient resultSetClient;
29

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

    
41
		ResultSet<?> resutlSet = ResultSet.fromJson(epr);
42
		log.info("Syncronizing content for oai on db " + dbName + "for metadata format: " + mdf);
43
		Iterable<String> records = resultSetClient.iter(resutlSet, String.class);
44
		// 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
45
		// element inside oai:metadata.
46
		if (formatName.equalsIgnoreCase("DMF")) {
47
			records = addDMFBlock(records);
48
		}
49
		this.synchronizer.synchronize(records, mdf, recordSource, dbName, alwaysNewRecord, new Callable<Object>() {
50

    
51
			@Override
52
			public Object call() {
53
				handler.done(job);
54
				return null;
55
			}
56
		}, new Callable<Object>() {
57

    
58
			@Override
59
			public Object call() {
60
				handler.failed(job, new Exception("Error during OAI synchronization on db " + dbName));
61
				return null;
62
			}
63
		});
64

    
65
	}
66

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

    
72
	public OAIStoreSynchronizer getSynchronizer() {
73
		return synchronizer;
74
	}
75

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

    
80
}
(9-9/9)