Project

General

Profile

1
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
import eu.dnetlib.utils.MetadataReference;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14

    
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
	@Autowired
22
	private OAIConfigurationExistReader configuration;
23
	@Autowired
24
	private MongoPublisherStoreDAO publisherStoreDAO;
25

    
26
	public void synchronize(final Iterable<String> records,
27
			final MetadataReference sourceMetadataFormat,
28
			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
			MongoPublisherStore store = this.getStore(sourceMetadataFormat, dbName, alwaysNewRecord);
36
			int count = store.feed(records, recordSource);
37

    
38

    
39
			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
	 * @param mdRef MDFInfo about the metadata format of the store to get
51
	 * @return a MongoPublisherStore instance
52
	 */
53
	private MongoPublisherStore getStore(final MetadataReference mdRef, final String dbName, final boolean alwaysNewRecord) {
54
		this.publisherStoreDAO.setAlwaysNewRecord(alwaysNewRecord);
55
		MongoPublisherStore store = this.publisherStoreDAO.getStore(mdRef.getFormat(), mdRef.getInterpretation(), mdRef.getLayout(), dbName);
56
		if (store == null) {
57
			log.debug("Creating store for metadata format: \n" + mdRef + " in db: " + dbName);
58
			try {
59
				store = this.publisherStoreDAO.createStore(mdRef.getFormat(), mdRef.getInterpretation(), mdRef.getLayout(), dbName);
60
				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
}
    (1-1/1)