Project

General

Profile

1 44133 michele.ar
package eu.dnetlib.data.mdstore.plugins;
2
3 44476 michele.ar
import java.time.LocalDateTime;
4
import java.time.temporal.ChronoUnit;
5 44133 michele.ar
import java.util.Map;
6
7
import org.apache.commons.lang3.StringUtils;
8 44476 michele.ar
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10 44649 michele.ar
import org.springframework.beans.factory.annotation.Autowired;
11 44133 michele.ar
12 44649 michele.ar
import eu.dnetlib.data.mdstore.modular.MDStoreFeeder;
13 44476 michele.ar
import eu.dnetlib.data.mdstore.modular.action.DoneCallback;
14 56978 michele.ar
import eu.dnetlib.data.mdstore.modular.action.FailedCallback;
15 44133 michele.ar
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 44476 michele.ar
import eu.dnetlib.data.mdstore.modular.mongodb.MDStoreDaoImpl;
19
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore;
20 44133 michele.ar
import eu.dnetlib.rmi.data.MDStoreServiceException;
21
22
public abstract class AbstractIstiMDStorePlugin implements MDStorePlugin {
23
24 44649 michele.ar
	public MDStoreFeeder getFeeder() {
25
		return feeder;
26
	}
27
28
	public void setFeeder(final MDStoreFeeder feeder) {
29
		this.feeder = feeder;
30
	}
31
32 44476 michele.ar
	private MDStoreDaoImpl dao;
33
34 44649 michele.ar
	@Autowired
35
	private MDStoreFeeder feeder;
36
37 44476 michele.ar
	private static final Log log = LogFactory.getLog(AbstractIstiMDStorePlugin.class);
38
39
	@Override
40 56978 michele.ar
	public final void run(final MDStoreDao dao, final Map<String, String> params, final DoneCallback doneCallback, final FailedCallback failedCallback) {
41 44649 michele.ar
42 44476 michele.ar
		if (dao instanceof MDStoreDaoImpl) {
43
			setDao((MDStoreDaoImpl) dao);
44
		} else {
45
			log.error("a mongo mdStore service is required");
46 56978 michele.ar
			failedCallback.call(new MDStoreServiceException("a mongo mdStore service is required"));
47 44476 michele.ar
		}
48
49
		final LocalDateTime start = LocalDateTime.now();
50 56978 michele.ar
		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 44476 michele.ar
	}
59
60 49210 michele.ar
	protected abstract void process(final MongoMDStore store, final Map<String, String> params) throws MDStoreServiceException;
61 44476 michele.ar
62
	protected MongoMDStore resolveStore(final Map<String, String> params, final String key) throws MDStoreServiceException {
63 44133 michele.ar
		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 44476 michele.ar
		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 44133 michele.ar
80 44649 michele.ar
	protected void touch(final MongoMDStore store) throws MDStoreServiceException {
81
		feeder.touch(store.getId(), dao.refreshSize(store.getId()));
82
	}
83
84 44476 michele.ar
	public MDStoreDaoImpl getDao() {
85
		return dao;
86 44133 michele.ar
	}
87
88 44476 michele.ar
	public void setDao(final MDStoreDaoImpl dao) {
89
		this.dao = dao;
90
	}
91
92 44133 michele.ar
}