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
|
if(log.isDebugEnabled()) {
|
31
|
log.debug("Job parameters:");
|
32
|
job.getParameters().forEach((k, v) -> log.debug("Name : " + k + " Value : " + v));
|
33
|
}
|
34
|
|
35
|
String epr = job.getParameters().get(OAIParameterNames.OAI_SYNC_EPR);
|
36
|
String recordSource = job.getParameters().get(OAIParameterNames.OAI_SOURCE);
|
37
|
boolean alwaysNewRecord = Boolean.valueOf(job.getParameters().get(OAIParameterNames.OAI_ALWAYS_NEW_RECORD));
|
38
|
final String dbName = job.getParameters().get(OAIParameterNames.OAI_DB);
|
39
|
|
40
|
final String collectionName = job.getParameters().get(OAIParameterNames.OAI_COLLECTON);
|
41
|
final MetadataReference mdRef = MetadataReferenceFactory.decode(collectionName, MDREF_SEPARATOR);
|
42
|
|
43
|
final ResultSet<?> rs = ResultSet.fromJson(epr);
|
44
|
log.info("Synchronizing content for oai on db " + dbName + " for metadata format: " + mdRef);
|
45
|
Iterable<String> records = resultSetClient.iter(rs, String.class);
|
46
|
// 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
|
47
|
// element inside oai:metadata.
|
48
|
if (mdRef.getFormat().equalsIgnoreCase("DMF") || mdRef.getFormat().equalsIgnoreCase("PMF")) {
|
49
|
records = addDMFBlock(records);
|
50
|
}
|
51
|
this.synchronizer.synchronize(records, mdRef, recordSource, dbName, alwaysNewRecord, () -> {
|
52
|
handler.done(job);
|
53
|
return null;
|
54
|
}, () -> {
|
55
|
handler.failed(job, new Exception("Error during OAI synchronization on db " + dbName));
|
56
|
return null;
|
57
|
});
|
58
|
|
59
|
}
|
60
|
|
61
|
protected Iterable<String> addDMFBlock(final Iterable<String> input) {
|
62
|
final ApplyXslt addDMFBlockXslt = new ApplyXslt(dmfXsltResource);
|
63
|
return Iterables.transform(input, addDMFBlockXslt::apply);
|
64
|
}
|
65
|
|
66
|
public OAIStoreSynchronizer getSynchronizer() {
|
67
|
return synchronizer;
|
68
|
}
|
69
|
|
70
|
public void setSynchronizer(final OAIStoreSynchronizer synchronizer) {
|
71
|
this.synchronizer = synchronizer;
|
72
|
}
|
73
|
|
74
|
}
|