Project

General

Profile

1
package eu.dnetlib.oai.sync;
2

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

    
6
import eu.dnetlib.oai.conf.OAIConfigurationExistReader;
7
import eu.dnetlib.oai.mongo.MongoPublisherStore;
8
import eu.dnetlib.oai.mongo.MongoPublisherStoreDAO;
9
import eu.dnetlib.rmi.provision.MDFInfo;
10
import eu.dnetlib.rmi.provision.OaiPublisherException;
11
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
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
	@Resource
22
	private OAIConfigurationExistReader configuration;
23
	@Resource
24
	private MongoPublisherStoreDAO publisherStoreDAO;
25

    
26
	public void synchronize(final Iterable<String> records,
27
			final MDFInfo 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
			log.info("Content synchronized: store " + sourceMetadataFormat + " fed with " + count + " records");
38
			executeCallback(callback);
39
		} catch (Exception e) {
40
			log.error(e);
41
			executeCallback(failCallback);
42
		}
43
	}
44

    
45
	/**
46
	 * Gets the OAI store for the given source metadata format. If the store does not exists, then a new one is created.
47
	 *
48
	 * @param sourceMetadataFormat MDFInfo about the metadata format of the store to get
49
	 * @return a MongoPublisherStore instance
50
	 */
51
	private MongoPublisherStore getStore(final MDFInfo sourceMetadataFormat, final String dbName, final boolean alwaysNewRecord) {
52
		this.publisherStoreDAO.setAlwaysNewRecord(alwaysNewRecord);
53
		MongoPublisherStore store = this.publisherStoreDAO.getStore(sourceMetadataFormat.getSourceFormatName(),
54
				sourceMetadataFormat.getSourceFormatInterpretation(), sourceMetadataFormat.getSourceFormatLayout(), dbName);
55
		if (store == null) {
56
			log.debug("Creating store for metadata format: \n" + sourceMetadataFormat + " in db: " + dbName);
57
			try {
58
				store = this.publisherStoreDAO.createStore(sourceMetadataFormat.getSourceFormatName(), sourceMetadataFormat.getSourceFormatInterpretation(),
59
						sourceMetadataFormat.getSourceFormatLayout(), 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)