Project

General

Profile

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

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Set;
6

    
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9

    
10
import com.google.common.collect.Lists;
11
import com.mongodb.BasicDBObject;
12
import com.mongodb.DBCursor;
13
import com.mongodb.DBObject;
14
import com.mongodb.QueryBuilder;
15
import com.mongodb.gridfs.GridFS;
16
import com.mongodb.gridfs.GridFSDBFile;
17

    
18
import eu.dnetlib.enabling.resultset.ResultSet;
19
import eu.dnetlib.enabling.resultset.ResultSetAware;
20
import eu.dnetlib.enabling.resultset.ResultSetListener;
21
import eu.dnetlib.miscutils.collections.MappedCollection;
22

    
23
/**
24
 * The listener interface for receiving gridFSObjectstoreResultSet events. The class that is interested in processing a
25
 * gridFSObjectstoreResultSet event implements this interface, and the object created with that class is registered with a component using
26
 * the component's <code>addGridFSObjectstoreResultSetListener<code> method. When the gridFSObjectstoreResultSet event occurs, that object's
27
 * appropriate method is invoked.
28
 *
29
 * @see GridFSObjectstoreResultSetEvent
30
 */
31
public class GridFSObjectstoreResultSetListener implements ResultSetListener, ResultSetAware {
32

    
33
	/** The Constant log. */
34
	private static final Log log = LogFactory.getLog(GridFSObjectstoreResultSetListener.class);
35

    
36
	/** The from date. */
37
	private Long fromDate;
38

    
39
	/** The until date. */
40
	private Long untilDate;
41

    
42
	/** The records. */
43
	private List<String> records;
44

    
45
	/** The object store id. */
46
	private String objectStoreID;
47

    
48
	/** The collection. */
49
	private GridFS collection;
50

    
51
	/** The base uri. */
52
	private String baseURI;
53

    
54
	/** The current size. */
55
	private int currentSize = -1;
56

    
57
	/** The current cursor. */
58
	private DBCursor currentCursor;
59

    
60
	/** The cursor position. */
61
	private long cursorPosition;
62

    
63
	/** The tags. */
64
	private Set<String> tags;
65

    
66
	/*
67
	 * (non-Javadoc)
68
	 *
69
	 * @see eu.dnetlib.enabling.resultset.TypedResultSetListener#getResult(int, int)
70
	 */
71
	@Override
72
	public List<String> getResult(final int from, final int to) {
73
		log.debug(String.format("ObjectStoreId :%s, from: %d, to: %d", objectStoreID, from, to));
74
		if (records != null) {
75
			List<String> ids = Lists.newArrayList();
76
			for (int i = from; i < to; i++) {
77
				ids.add(records.get(i));
78
			}
79
			QueryBuilder qBuilder = QueryBuilder.start("metadata.id").in(ids);
80
			DBObject q = qBuilder.get();
81
			List<GridFSDBFile> out = collection.find(q);
82

    
83
			return MappedCollection.listMap(out, ObjectStoreFileUtility.asJSON(baseURI, objectStoreID));
84
		} else if (tags != null && !tags.isEmpty()) {
85
			if (currentCursor == null || cursorPosition > from) {
86
				createCurrentCursor(this.tags);
87
			}
88
			while (cursorPosition < from) {
89
				currentCursor.next();
90
				cursorPosition++;
91
			}
92
			ArrayList<GridFSDBFile> out = new ArrayList<GridFSDBFile>();
93

    
94
			for (int i = from; i <= to; i++) {
95
				if (currentCursor.hasNext()) {
96
					out.add((GridFSDBFile) currentCursor.next());
97
					cursorPosition++;
98
				}
99
			}
100

    
101
			return MappedCollection.listMap(out, ObjectStoreFileUtility.asJSON(baseURI, objectStoreID));
102

    
103
		}
104

    
105
		else if (fromDate != null && untilDate != null) {
106
			if (currentCursor == null || cursorPosition > from) {
107
				createCurrentCursor();
108
			}
109
			while (cursorPosition < from) {
110
				currentCursor.next();
111
				cursorPosition++;
112
			}
113
			ArrayList<GridFSDBFile> out = new ArrayList<GridFSDBFile>();
114

    
115
			for (int i = from; i <= to; i++) {
116
				if (currentCursor.hasNext()) {
117
					out.add((GridFSDBFile) currentCursor.next());
118
					cursorPosition++;
119
				}
120
			}
121

    
122
			return MappedCollection.listMap(out, ObjectStoreFileUtility.asJSON(baseURI, objectStoreID));
123
		}
124

    
125
		throw new IllegalArgumentException("Missing parameters on Delivery must provide either from, to, or ObjectStoreIDs");
126
	}
127

    
128
	/**
129
	 * Creates the current cursor.
130
	 */
131
	private void createCurrentCursor(final Set<String> tags) {
132
		QueryBuilder qBuilder = QueryBuilder.start("metadata.tags").in(tags);
133
		if (currentCursor != null) {
134
			currentCursor.close();
135
		}
136
		currentCursor = collection.getFileList(qBuilder.get()).sort(new BasicDBObject("_id", 1));
137
		cursorPosition = 1;
138
	}
139

    
140
	/**
141
	 * Creates the current cursor.
142
	 */
143
	private void createCurrentCursor() {
144
		BasicDBObject query = new BasicDBObject();
145
		query.put("$gt", fromDate.doubleValue());
146
		query.put("$lt", untilDate.doubleValue());
147
		if (currentCursor != null) {
148
			currentCursor.close();
149
		}
150
		currentCursor = collection.getFileList(new BasicDBObject("metadata.timestamp", query)).sort(new BasicDBObject("_id", 1));
151
		cursorPosition = 1;
152
	}
153

    
154
	/*
155
	 * (non-Javadoc)
156
	 *
157
	 * @see eu.dnetlib.enabling.resultset.TypedResultSetListener#getSize()
158
	 */
159
	@Override
160
	public int getSize() {
161
		if (currentSize == -1) {
162
			currentSize = calculateSize();
163
		}
164
		return currentSize;
165
	}
166

    
167
	/**
168
	 * Calculate size.
169
	 *
170
	 * @return the int
171
	 */
172
	private int calculateSize() {
173
		if (records != null) {
174
			QueryBuilder qBuilder = QueryBuilder.start("metadata.id").in(records);
175
			DBObject q = qBuilder.get();
176
			List<GridFSDBFile> out = collection.find(q);
177
			return out.size();
178
		} else if (fromDate != null && untilDate != null) {
179
			BasicDBObject query = new BasicDBObject();
180
			query.put("$gt", fromDate.doubleValue());
181
			query.put("$lt", untilDate.doubleValue());
182
			return collection.getFileList(new BasicDBObject("metadata.timestamp", query)).size();
183
		} else if (tags != null) {
184
			QueryBuilder qBuilder = QueryBuilder.start("metadata.tags").in(tags);
185
			DBObject q = qBuilder.get();
186
			List<GridFSDBFile> out = collection.find(q);
187
			return out.size();
188
		}
189
		return 0;
190
	}
191

    
192
	/*
193
	 * (non-Javadoc)
194
	 *
195
	 * @see eu.dnetlib.enabling.resultset.ResultSetAware#setResultSet(eu.dnetlib.enabling.resultset.ResultSet)
196
	 */
197
	@Override
198
	public void setResultSet(final ResultSet resultSet) {
199
		resultSet.close();
200
	}
201

    
202
	/**
203
	 * Gets the from date.
204
	 *
205
	 * @return the from date
206
	 */
207
	public Long getFromDate() {
208
		return fromDate;
209
	}
210

    
211
	/**
212
	 * Sets the from date.
213
	 *
214
	 * @param fromdate
215
	 *            the new from date
216
	 */
217
	public void setFromDate(final Long fromdate) {
218
		this.fromDate = fromdate;
219
	}
220

    
221
	/**
222
	 * Gets the until date.
223
	 *
224
	 * @return the until date
225
	 */
226
	public Long getUntilDate() {
227
		return untilDate;
228
	}
229

    
230
	/**
231
	 * Sets the until date.
232
	 *
233
	 * @param untilDate
234
	 *            the new until date
235
	 */
236
	public void setUntilDate(final Long untilDate) {
237
		this.untilDate = untilDate;
238
	}
239

    
240
	/**
241
	 * Gets the records.
242
	 *
243
	 * @return the records
244
	 */
245
	public List<String> getRecords() {
246
		return records;
247
	}
248

    
249
	/**
250
	 * Sets the records.
251
	 *
252
	 * @param records
253
	 *            the new records
254
	 */
255
	public void setRecords(final List<String> records) {
256
		this.records = records;
257
	}
258

    
259
	/**
260
	 * Gets the collection.
261
	 *
262
	 * @return the collection
263
	 */
264
	public GridFS getCollection() {
265
		return collection;
266
	}
267

    
268
	/**
269
	 * Sets the collection.
270
	 *
271
	 * @param collection
272
	 *            the new collection
273
	 */
274
	public void setCollection(final GridFS collection) {
275
		this.collection = collection;
276
	}
277

    
278
	/**
279
	 * Gets the base uri.
280
	 *
281
	 * @return the base uri
282
	 */
283
	public String getBaseURI() {
284
		return baseURI;
285
	}
286

    
287
	/**
288
	 * Sets the base uri.
289
	 *
290
	 * @param baseURI
291
	 *            the new base uri
292
	 */
293
	public void setBaseURI(final String baseURI) {
294
		this.baseURI = baseURI;
295
	}
296

    
297
	/**
298
	 * Gets the object store id.
299
	 *
300
	 * @return the object store id
301
	 */
302
	public String getObjectStoreID() {
303
		return objectStoreID;
304
	}
305

    
306
	/**
307
	 * Sets the object store id.
308
	 *
309
	 * @param objectStoreID
310
	 *            the new object store id
311
	 */
312
	public void setObjectStoreID(final String objectStoreID) {
313
		this.objectStoreID = objectStoreID;
314
	}
315

    
316
	/**
317
	 * @return the tags
318
	 */
319
	public Set<String> getTags() {
320
		return tags;
321
	}
322

    
323
	/**
324
	 * @param tags
325
	 *            the tags to set
326
	 */
327
	public void setTags(final Set<String> tags) {
328
		this.tags = tags;
329
	}
330
}
(3-3/5)