Project

General

Profile

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

    
3
import java.util.List;
4

    
5
import org.apache.commons.logging.Log;
6
import org.apache.commons.logging.LogFactory;
7
import org.springframework.beans.factory.annotation.Required;
8

    
9
import eu.dnetlib.data.mdstore.MDStoreServiceException;
10
import eu.dnetlib.data.mdstore.modular.connector.MDStore;
11
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
12
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
13
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
14
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
15
import eu.dnetlib.miscutils.datetime.DateUtils;
16

    
17
public class MDStoreFeeder {
18

    
19
	private static final Log log = LogFactory.getLog(MDStoreFeeder.class);
20

    
21
	private MDStoreDao dao;
22

    
23
	private ResultSetClientFactory resultSetClientFactory;
24

    
25
	private UniqueServiceLocator serviceLocator;
26

    
27
	private boolean syncFeed = true;
28

    
29
	public void feed(final String mdId,
30
			final String rsEpr,
31
			final String storingType,
32
			final boolean sync,
33
			final List<MDFormatDescription> mdformats,
34
			final FeedDoneCallback doneCallback,
35
			final FeedFailedCallback failCallback) throws MDStoreServiceException {
36
		log.info("Start feeding mdstore " + mdId + " with epr " + rsEpr);
37

    
38
		try {
39
			final boolean refresh = "REFRESH".equals(storingType);
40

    
41
			final MDStore mdstore = dao.startTransaction(mdId, refresh);
42

    
43
			final Iterable<String> records = resultSetClientFactory.getClient(rsEpr);
44

    
45
			if (refresh) {
46
				mdstore.truncate();
47
			}
48

    
49
			if (mdformats == null) {
50
				mdstore.feed(records, refresh);
51
			} else {
52
				mdstore.feed(records, refresh, mdformats);
53
			}
54

    
55
			dao.commit(mdstore.getId(), mdId);
56

    
57
			int size = dao.refreshSize(mdId);
58

    
59
			touch(mdId, size);
60

    
61
			log.info("Finished feeding mdstore " + mdId + " - new size: " + size);
62

    
63
			doneCallback.call(size);
64
		} catch (Exception e) {
65
			log.error("Error feeding mdstore: " + mdId);
66
			failCallback.call(e);
67
		}
68
	}
69

    
70
	/**
71
	 * Sets the last modified date in the profile.
72
	 *
73
	 * @param mdId
74
	 */
75
	public void touch(final String mdId, final int size) {
76
		try {
77
			final String now = DateUtils.now_ISO8601();
78

    
79
			final String mdstoreXUpdate = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '" + mdId + "']"
80
					+ "return update value $x//LAST_STORAGE_DATE with '" + now + "'";
81

    
82
			serviceLocator.getService(ISRegistryService.class).executeXUpdate(mdstoreXUpdate);
83

    
84
			touchSize(mdId, size);
85
		} catch (final Exception e) {
86
			throw new IllegalStateException(e);
87
		}
88
	}
89

    
90
	public void touchSize(final String mdId, final int size) {
91
		try {
92
			final String mdstoreNumberXUpdate = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '" + mdId + "']"
93
					+ "return update value $x//NUMBER_OF_RECORDS with '" + size + "'";
94

    
95
			serviceLocator.getService(ISRegistryService.class).executeXUpdate(mdstoreNumberXUpdate);
96
		} catch (final Exception e) {
97
			throw new IllegalStateException(e);
98
		}
99
	}
100

    
101
	public MDStoreDao getDao() {
102
		return dao;
103
	}
104

    
105
	@Required
106
	public void setDao(final MDStoreDao dao) {
107
		this.dao = dao;
108
	}
109

    
110
	public ResultSetClientFactory getResultSetClientFactory() {
111
		return resultSetClientFactory;
112
	}
113

    
114
	@Required
115
	public void setResultSetClientFactory(final ResultSetClientFactory resultSetClientFactory) {
116
		this.resultSetClientFactory = resultSetClientFactory;
117
	}
118

    
119
	public boolean isSyncFeed() {
120
		return syncFeed;
121
	}
122

    
123
	public void setSyncFeed(final boolean syncFeed) {
124
		this.syncFeed = syncFeed;
125
	}
126

    
127
	public UniqueServiceLocator getServiceLocator() {
128
		return serviceLocator;
129
	}
130

    
131
	@Required
132
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
133
		this.serviceLocator = serviceLocator;
134
	}
135

    
136
}
(12-12/20)