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
			log.info("Content synchronized: store " + sourceMetadataFormat + " fed with " + count + " records");
39
			executeCallback(callback);
40
		} catch (Exception e) {
41
			log.error(e);
42
			executeCallback(failCallback);
43
		}
44
	}
45

    
46
	/**
47
	 * Gets the OAI store for the given source metadata format. If the store does not exists, then a new one is created.
48
	 *
49
	 * @param mdRef MDFInfo about the metadata format of the store to get
50
	 * @return a MongoPublisherStore instance
51
	 */
52
	private MongoPublisherStore getStore(final MetadataReference mdRef, final String dbName, final boolean alwaysNewRecord) {
53
		this.publisherStoreDAO.setAlwaysNewRecord(alwaysNewRecord);
54
		MongoPublisherStore store = this.publisherStoreDAO.getStore(mdRef.getFormat(), mdRef.getInterpretation(), mdRef.getLayout(), dbName);
55
		if (store == null) {
56
			log.debug("Creating store for metadata format: \n" + mdRef + " in db: " + dbName);
57
			try {
58
				store = this.publisherStoreDAO.createStore(mdRef.getFormat(), mdRef.getInterpretation(), mdRef.getLayout(), dbName);
59
				log.debug("Created store with id: " + store.getId());
60
			} catch (OaiPublisherException e) {
61
				throw new OaiPublisherRuntimeException(e);
62
			}
63
		}
64
		return store;
65
	}
66

    
67
	protected void executeCallback(final Callable<?> callback) {
68
		if (callback != null) {
69
			try {
70
				callback.call();
71
			} catch (Exception e) {
72
				log.error("Error executing callback", e);
73
			}
74
		}
75
	}
76

    
77
	public OAIConfigurationExistReader getConfiguration() {
78
		return configuration;
79
	}
80

    
81
	public void setConfiguration(final OAIConfigurationExistReader configuration) {
82
		this.configuration = configuration;
83
	}
84

    
85
	public MongoPublisherStoreDAO getPublisherStoreDAO() {
86
		return publisherStoreDAO;
87
	}
88

    
89
	public void setPublisherStoreDAO(final MongoPublisherStoreDAO publisherStoreDAO) {
90
		this.publisherStoreDAO = publisherStoreDAO;
91
	}
92

    
93
}
    (1-1/1)