1 |
42181
|
sandro.lab
|
package eu.dnetlib.oai.sync;
|
2 |
|
|
|
3 |
|
|
import java.util.concurrent.Callable;
|
4 |
|
|
|
5 |
|
|
import eu.dnetlib.oai.conf.OAIConfigurationExistReader;
|
6 |
|
|
import eu.dnetlib.oai.mongo.MongoPublisherStore;
|
7 |
|
|
import eu.dnetlib.oai.mongo.MongoPublisherStoreDAO;
|
8 |
|
|
import eu.dnetlib.rmi.provision.OaiPublisherException;
|
9 |
|
|
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
|
10 |
43245
|
claudio.at
|
import eu.dnetlib.utils.MetadataReference;
|
11 |
42181
|
sandro.lab
|
import org.apache.commons.logging.Log;
|
12 |
|
|
import org.apache.commons.logging.LogFactory;
|
13 |
42621
|
sandro.lab
|
import org.springframework.beans.factory.annotation.Autowired;
|
14 |
42181
|
sandro.lab
|
|
15 |
|
|
public class OAIStoreSynchronizer {
|
16 |
|
|
|
17 |
|
|
private static final Log log = LogFactory.getLog(OAIStoreSynchronizer.class); // NOPMD by marko on 11/24/08 5:02 PM
|
18 |
|
|
/**
|
19 |
|
|
* OAI Publisher configuration.
|
20 |
|
|
*/
|
21 |
42621
|
sandro.lab
|
@Autowired
|
22 |
42181
|
sandro.lab
|
private OAIConfigurationExistReader configuration;
|
23 |
42621
|
sandro.lab
|
@Autowired
|
24 |
42181
|
sandro.lab
|
private MongoPublisherStoreDAO publisherStoreDAO;
|
25 |
|
|
|
26 |
|
|
public void synchronize(final Iterable<String> records,
|
27 |
43245
|
claudio.at
|
final MetadataReference sourceMetadataFormat,
|
28 |
42181
|
sandro.lab
|
final String recordSource,
|
29 |
|
|
final String dbName,
|
30 |
|
|
final boolean alwaysNewRecord,
|
31 |
|
|
final Callable<?> callback,
|
32 |
|
|
final Callable<?> failCallback) {
|
33 |
|
|
try {
|
34 |
|
|
log.fatal("Synchronizing content for source metadata format " + sourceMetadataFormat);
|
35 |
45499
|
alessia.ba
|
log.fatal("Record source: "+recordSource);
|
36 |
42181
|
sandro.lab
|
MongoPublisherStore store = this.getStore(sourceMetadataFormat, dbName, alwaysNewRecord);
|
37 |
|
|
int count = store.feed(records, recordSource);
|
38 |
44144
|
alessia.ba
|
|
39 |
42181
|
sandro.lab
|
log.info("Content synchronized: store " + sourceMetadataFormat + " fed with " + count + " records");
|
40 |
|
|
executeCallback(callback);
|
41 |
|
|
} catch (Exception e) {
|
42 |
|
|
log.error(e);
|
43 |
|
|
executeCallback(failCallback);
|
44 |
|
|
}
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* Gets the OAI store for the given source metadata format. If the store does not exists, then a new one is created.
|
49 |
|
|
*
|
50 |
43245
|
claudio.at
|
* @param mdRef MDFInfo about the metadata format of the store to get
|
51 |
42181
|
sandro.lab
|
* @return a MongoPublisherStore instance
|
52 |
|
|
*/
|
53 |
43245
|
claudio.at
|
private MongoPublisherStore getStore(final MetadataReference mdRef, final String dbName, final boolean alwaysNewRecord) {
|
54 |
42181
|
sandro.lab
|
this.publisherStoreDAO.setAlwaysNewRecord(alwaysNewRecord);
|
55 |
43245
|
claudio.at
|
MongoPublisherStore store = this.publisherStoreDAO.getStore(mdRef.getFormat(), mdRef.getInterpretation(), mdRef.getLayout(), dbName);
|
56 |
42181
|
sandro.lab
|
if (store == null) {
|
57 |
43245
|
claudio.at
|
log.debug("Creating store for metadata format: \n" + mdRef + " in db: " + dbName);
|
58 |
42181
|
sandro.lab
|
try {
|
59 |
43245
|
claudio.at
|
store = this.publisherStoreDAO.createStore(mdRef.getFormat(), mdRef.getInterpretation(), mdRef.getLayout(), dbName);
|
60 |
42181
|
sandro.lab
|
log.debug("Created store with id: " + store.getId());
|
61 |
|
|
} catch (OaiPublisherException e) {
|
62 |
|
|
throw new OaiPublisherRuntimeException(e);
|
63 |
|
|
}
|
64 |
|
|
}
|
65 |
|
|
return store;
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
protected void executeCallback(final Callable<?> callback) {
|
69 |
|
|
if (callback != null) {
|
70 |
|
|
try {
|
71 |
|
|
callback.call();
|
72 |
|
|
} catch (Exception e) {
|
73 |
|
|
log.error("Error executing callback", e);
|
74 |
|
|
}
|
75 |
|
|
}
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
public OAIConfigurationExistReader getConfiguration() {
|
79 |
|
|
return configuration;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
public void setConfiguration(final OAIConfigurationExistReader configuration) {
|
83 |
|
|
this.configuration = configuration;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
public MongoPublisherStoreDAO getPublisherStoreDAO() {
|
87 |
|
|
return publisherStoreDAO;
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
public void setPublisherStoreDAO(final MongoPublisherStoreDAO publisherStoreDAO) {
|
91 |
|
|
this.publisherStoreDAO = publisherStoreDAO;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
}
|