Project

General

Profile

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

    
3
import com.google.common.collect.Iterables;
4
import com.google.common.collect.Lists;
5
import eu.dnetlib.data.objectstore.modular.connector.ObjectStore;
6
import eu.dnetlib.data.objectstore.modular.connector.ObjectStoreDao;
7
import eu.dnetlib.data.objectstore.rmi.MetadataObjectRecord;
8
import eu.dnetlib.data.objectstore.rmi.ObjectStoreFile;
9
import eu.dnetlib.data.objectstore.rmi.ObjectStoreServiceException;
10
import eu.dnetlib.data.objectstore.rmi.Protocols;
11
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
12
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
13
import eu.dnetlib.enabling.resultset.client.ResultSetClientFactory;
14
import eu.dnetlib.miscutils.datetime.DateUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.springframework.beans.factory.annotation.Required;
18

    
19
/**
20
 * The Class ModularObjectStoreFeeder, responsible to feed data into the object Store
21
 */
22
public class ModularObjectStoreFeeder {
23

    
24
	private static final Log log = LogFactory.getLog(ModularObjectStoreFeeder.class);
25

    
26
	/** The dao of the objectStore. */
27
	private ObjectStoreDao dao;
28

    
29
	/** The result set client factory. */
30
	private ResultSetClientFactory resultSetClientFactory;
31

    
32
	/** The service locator. */
33
	private UniqueServiceLocator serviceLocator;
34

    
35
	/**
36
	 * Gets the dao.
37
	 *
38
	 * @return the dao
39
	 */
40
	public ObjectStoreDao getDao() {
41
		return dao;
42
	}
43

    
44
	/**
45
	 * Sets the dao.
46
	 *
47
	 * @param dao
48
	 *            the new dao
49
	 */
50
	@Required
51
	public void setDao(final ObjectStoreDao dao) {
52
		this.dao = dao;
53
	}
54

    
55
	/**
56
	 * Gets the result set client factory.
57
	 *
58
	 * @return the result set client factory
59
	 */
60
	public ResultSetClientFactory getResultSetClientFactory() {
61
		return resultSetClientFactory;
62
	}
63

    
64
	/**
65
	 * Sets the result set client factory.
66
	 *
67
	 * @param resultSetClientFactory
68
	 *            the new result set client factory
69
	 */
70

    
71
	@Required
72
	public void setResultSetClientFactory(final ResultSetClientFactory resultSetClientFactory) {
73
		this.resultSetClientFactory = resultSetClientFactory;
74
	}
75

    
76
	/**
77
	 * Feed metadata object record.
78
	 *
79
	 * @param objectStoreID
80
	 *            the object store id
81
	 * @param rsEpr
82
	 *            the rs epr
83
	 * @param mime
84
	 *            the mime
85
	 * @throws ObjectStoreServiceException
86
	 */
87
	public int feedMetadataObjectRecord(final String objectStoreID, final String rsEpr, final String mime) throws ObjectStoreServiceException {
88

    
89
		final Iterable<String> records = resultSetClientFactory.getClient(rsEpr);
90
		Iterable<MetadataObjectRecord> toIngest = Iterables.transform(records, input -> {
91
			MetadataObjectRecord record = MetadataObjectRecord.initFromJson(input);
92
			if (record != null) {
93
				record.setMime(mime);
94
			} else {
95
				log.error("An input record is null :" + input);
96
			}
97
			return record;
98
		});
99
		ObjectStore store = dao.getObjectStore(objectStoreID);
100
		int size = store.feedMetadataRecord(toIngest, true);
101
		touch(objectStoreID, size);
102
		return size;
103
	}
104

    
105
	/**
106
	 * Feed object in the object store starting from an EPR of objectMetadata
107
	 *
108
	 * @param obsId
109
	 *            the objectStore id
110
	 * @param rsEpr
111
	 *            the result set Endpoint-reference
112
	 * @param protocol
113
	 *            the protocol
114
	 * @param login
115
	 *            the login
116
	 * @param password
117
	 *            the password
118
	 * @param mime
119
	 *            the mime type
120
	 * @throws ObjectStoreServiceException
121
	 */
122
	public void feed(final String obsId, final String rsEpr, final Protocols protocol, final String login, final String password, final String mime)
123
			throws ObjectStoreServiceException {
124
		final Iterable<String> records = resultSetClientFactory.getClient(rsEpr);
125
		ObjectBroker objectBroker = new ObjectBroker(protocol, login, password, mime);
126
		Iterable<ObjectStoreRecord> toIngest = Iterables.transform(records, objectBroker);
127
		ObjectStore store = dao.getObjectStore(obsId);
128
		int size = store.feed(toIngest, true);
129
		touch(obsId, size);
130
	}
131

    
132
	/**
133
	 * Feed a single object in the object Stroe.
134
	 *
135
	 * @param objectStoreID
136
	 *            the object store id
137
	 * @param objectID
138
	 *            the object id
139
	 * @param URIObject
140
	 *            the URI of object
141
	 * @param protocol
142
	 *            the protocol
143
	 * @param login
144
	 *            the login
145
	 * @param password
146
	 *            the password
147
	 * @param mime
148
	 *            the mime type
149
	 * @throws ObjectStoreServiceException
150
	 */
151
	public void feedObject(final String objectStoreID,
152
			final String objectID,
153
			final String URIObject,
154
			final Protocols protocol,
155
			final String login,
156
			final String password,
157
			final String mime) throws ObjectStoreServiceException {
158
		ObjectStoreFile inputFile = new ObjectStoreFile();
159
		inputFile.setURI(URIObject);
160
		inputFile.setObjectID(objectID);
161
		ObjectBroker objectBroker = new ObjectBroker(protocol, login, password, mime);
162
		Iterable<ObjectStoreRecord> toIngest = Iterables.transform(Lists.newArrayList(inputFile.toJSON()), objectBroker);
163
		ObjectStore store = dao.getObjectStore(objectStoreID);
164
		int size = store.feed(toIngest, true);
165
		touch(objectStoreID, size);
166
	}
167

    
168
	/**
169
	 * Feed object record.
170
	 *
171
	 * @param objectStoreID
172
	 *            the object store id
173
	 * @param record
174
	 *            the record
175
	 * @return the string
176
	 * @throws ObjectStoreServiceException
177
	 */
178
	public String feedObjectRecord(final String objectStoreID, final ObjectStoreRecord record) throws ObjectStoreServiceException {
179
		ObjectStore store = dao.getObjectStore(objectStoreID);
180
		return store.feedObjectRecord(record);
181
	}
182

    
183
	/**
184
	 * Sets the last modified date in the profile.
185
	 *
186
	 * @param obsId
187
	 *            the obs id
188
	 * @param size
189
	 *            the size
190
	 */
191
	public void touch(final String obsId, final int size) {
192
		try {
193
			final String now = DateUtils.now_ISO8601();
194

    
195
			final String mdstoreXUpdate = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '" + obsId + "']"
196
					+ "return update value $x//LAST_STORAGE_DATE with '" + now + "'";
197

    
198
			serviceLocator.getService(ISRegistryService.class, true).executeXUpdate(mdstoreXUpdate);
199

    
200
			touchSize(obsId, size);
201
		} catch (final Exception e) {
202
			throw new IllegalStateException(e);
203
		}
204
	}
205

    
206
	/**
207
	 * Touch size.
208
	 *
209
	 * @param obsId
210
	 *            the obs id
211
	 * @param size
212
	 *            the size
213
	 */
214
	public void touchSize(final String obsId, final int size) {
215
		try {
216
			final String mdstoreNumberXUpdate = "for $x in //RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '" + obsId + "']"
217
					+ "return update value $x//COUNT_STORE with '" + size + "'";
218

    
219
			serviceLocator.getService(ISRegistryService.class, true).executeXUpdate(mdstoreNumberXUpdate);
220
		} catch (final Exception e) {
221
			throw new IllegalStateException(e);
222
		}
223
	}
224

    
225
	public UniqueServiceLocator getServiceLocator() {
226
		return serviceLocator;
227
	}
228

    
229
	@Required
230
	public void setServiceLocator(final UniqueServiceLocator serviceLocator) {
231
		this.serviceLocator = serviceLocator;
232
	}
233

    
234
}
(8-8/15)