Project

General

Profile

1
package eu.dnetlib.data.mdstore.plugins;
2

    
3
import java.time.LocalDateTime;
4
import java.time.temporal.ChronoUnit;
5
import java.util.Map;
6

    
7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
11

    
12
import eu.dnetlib.data.mdstore.modular.MDStoreFeeder;
13
import eu.dnetlib.data.mdstore.modular.action.DoneCallback;
14
import eu.dnetlib.data.mdstore.modular.action.FailedCallback;
15
import eu.dnetlib.data.mdstore.modular.action.MDStorePlugin;
16
import eu.dnetlib.data.mdstore.modular.connector.MDStore;
17
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
18
import eu.dnetlib.data.mdstore.modular.mongodb.MDStoreDaoImpl;
19
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore;
20
import eu.dnetlib.rmi.data.MDStoreServiceException;
21

    
22
public abstract class AbstractIstiMDStorePlugin implements MDStorePlugin {
23

    
24
	public MDStoreFeeder getFeeder() {
25
		return feeder;
26
	}
27

    
28
	public void setFeeder(final MDStoreFeeder feeder) {
29
		this.feeder = feeder;
30
	}
31

    
32
	private MDStoreDaoImpl dao;
33

    
34
	@Autowired
35
	private MDStoreFeeder feeder;
36

    
37
	private static final Log log = LogFactory.getLog(AbstractIstiMDStorePlugin.class);
38

    
39
	@Override
40
	public final void run(final MDStoreDao dao, final Map<String, String> params, final DoneCallback doneCallback, final FailedCallback failedCallback) {
41

    
42
		if (dao instanceof MDStoreDaoImpl) {
43
			setDao((MDStoreDaoImpl) dao);
44
		} else {
45
			log.error("a mongo mdStore service is required");
46
			failedCallback.call(new MDStoreServiceException("a mongo mdStore service is required"));
47
		}
48

    
49
		final LocalDateTime start = LocalDateTime.now();
50
		try {
51
			process(resolveStore(params, "mdId"), params);
52
			log.info("Plugin Execution Time: " + ChronoUnit.SECONDS.between(start, LocalDateTime.now()));
53
			doneCallback.call(params);
54
		} catch (final Throwable e) {
55
			log.error(e.getMessage(), e);
56
			failedCallback.call(e);
57
		}
58
	}
59

    
60
	protected abstract void process(final MongoMDStore store, final Map<String, String> params) throws MDStoreServiceException;
61

    
62
	protected MongoMDStore resolveStore(final Map<String, String> params, final String key) throws MDStoreServiceException {
63
		final String mdId = params.get(key);
64

    
65
		if (StringUtils.isBlank(mdId)) { throw new MDStoreServiceException("mdID is empty"); }
66

    
67
		final MDStore store = dao.getMDStore(mdId);
68

    
69
		if (store == null) {
70
			log.error("mdStore not found, id: " + mdId);
71
			throw new MDStoreServiceException("mdStore not found, id: " + mdId);
72
		} else if (store instanceof MongoMDStore) {
73
			return (MongoMDStore) store;
74
		} else {
75
			log.error("a mongo mdStore is required");
76
			throw new MDStoreServiceException("a mongo mdStore is required");
77
		}
78
	}
79

    
80
	protected void touch(final MongoMDStore store) throws MDStoreServiceException {
81
		feeder.touch(store.getId(), dao.refreshSize(store.getId()));
82
	}
83

    
84
	public MDStoreDaoImpl getDao() {
85
		return dao;
86
	}
87

    
88
	public void setDao(final MDStoreDaoImpl dao) {
89
		this.dao = dao;
90
	}
91

    
92
}
(1-1/11)