Project

General

Profile

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

    
3
import com.mongodb.BasicDBObject;
4
import com.mongodb.DBCursor;
5
import com.mongodb.DBObject;
6
import com.mongodb.QueryBuilder;
7
import com.mongodb.gridfs.GridFS;
8
import com.mongodb.gridfs.GridFSDBFile;
9
import eu.dnetlib.enabling.resultset.listener.ResultSetListener;
10
import eu.dnetlib.rmi.common.ResultSetException;
11
import eu.dnetlib.rmi.data.ObjectStoreFile;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14

    
15
/**
16
 * The listener interface for receiving gridFSObjectstoreResultSet events. The class that is interested in processing a
17
 * gridFSObjectstoreResultSet event implements this interface, and the object created with that class is registered with a component using
18
 * the component's <code>addGridFSObjectstoreResultSetListener<code> method. When
19
 * the gridFSObjectstoreResultSet event occurs, that object's appropriate
20
 * method is invoked.
21
 */
22
public class GridFSObjectstoreResultSetListener implements ResultSetListener<ObjectStoreFile> {
23

    
24
	/**
25
	 * The Constant log.
26
	 */
27
	private static final Log log = LogFactory.getLog(GridFSObjectstoreResultSetListener.class);
28

    
29
	/**
30
	 * The from date.
31
	 */
32
	private Long fromDate;
33

    
34
	/**
35
	 * The until date.
36
	 */
37
	private Long untilDate;
38

    
39
	/**
40
	 * The records.
41
	 */
42
	private Iterable<String> records;
43

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

    
49
	/**
50
	 * The collection.
51
	 */
52
	private GridFS collection;
53

    
54
	/**
55
	 * The base uri.
56
	 */
57
	private String baseURI;
58

    
59
	/**
60
	 * The current cursor.
61
	 */
62
	private DBCursor currentCursor;
63

    
64
	public GridFSObjectstoreResultSetListener(final Long fromDate,
65
			final Long untilDate,
66
			final String objectStoreID,
67
			final GridFS collection,
68
			final String baseURI,
69
			final Iterable<String> records) {
70
		this.fromDate = fromDate;
71
		this.untilDate = untilDate;
72
		this.objectStoreID = objectStoreID;
73
		this.collection = collection;
74
		this.baseURI = baseURI;
75
		this.records = records;
76
		createCurrentCursor();
77
	}
78

    
79
	@Override
80
	public boolean hasNext() {
81
		return currentCursor.hasNext();
82
	}
83

    
84
	@Override
85
	public ObjectStoreFile next() throws ResultSetException {
86
		return ObjectStoreFileUtility.build((GridFSDBFile) currentCursor.next(), baseURI, objectStoreID);
87
	}
88

    
89
	@Override
90
	public int getCount() {
91
		return currentCursor.itcount();
92
	}
93

    
94
	@Override
95
	public int getTotal() {
96
		return currentCursor.count();
97
	}
98

    
99
	/**
100
	 * Creates the current cursor.
101
	 */
102
	private void createCurrentCursor() {
103
		if (log.isDebugEnabled()) {
104
			log.debug(String.format("Staring query "));
105
		}
106

    
107
		DBObject query;
108
		if (records != null) {
109
			QueryBuilder qBuilder = QueryBuilder.start("metadata.id").in(records);
110
			query = qBuilder.get();
111
		} else if ((fromDate != null) && (untilDate != null)) {
112
			query = new BasicDBObject();
113
			query.put("$gt", fromDate.doubleValue());
114
			query.put("$lt", untilDate.doubleValue());
115
			if (currentCursor != null) {
116
				currentCursor.close();
117
			}
118
		} else
119
			throw new IllegalArgumentException("Missing parameters on Delivery must provide either from, to, or ObjectStoreIDs");
120
		currentCursor = collection.getFileList(new BasicDBObject("metadata.timestamp", query)).sort(new BasicDBObject("_id", 1));
121
	}
122

    
123
	/**
124
	 * Gets the from date.
125
	 *
126
	 * @return the from date
127
	 */
128
	public Long getFromDate() {
129
		return fromDate;
130
	}
131

    
132
	/**
133
	 * Sets the from date.
134
	 *
135
	 * @param fromdate the new from date
136
	 */
137
	public void setFromDate(final Long fromdate) {
138
		this.fromDate = fromdate;
139
	}
140

    
141
	/**
142
	 * Gets the until date.
143
	 *
144
	 * @return the until date
145
	 */
146
	public Long getUntilDate() {
147
		return untilDate;
148
	}
149

    
150
	/**
151
	 * Sets the until date.
152
	 *
153
	 * @param untilDate the new until date
154
	 */
155
	public void setUntilDate(final Long untilDate) {
156
		this.untilDate = untilDate;
157
	}
158

    
159
	/**
160
	 * Gets the records.
161
	 *
162
	 * @return the records
163
	 */
164
	public Iterable<String> getRecords() {
165
		return records;
166
	}
167

    
168
	/**
169
	 * Sets the records.
170
	 *
171
	 * @param records the new records
172
	 */
173
	public void setRecords(final Iterable<String> records) {
174
		this.records = records;
175
	}
176

    
177
	/**
178
	 * Gets the collection.
179
	 *
180
	 * @return the collection
181
	 */
182
	public GridFS getCollection() {
183
		return collection;
184
	}
185

    
186
	/**
187
	 * Sets the collection.
188
	 *
189
	 * @param collection the new collection
190
	 */
191
	public void setCollection(final GridFS collection) {
192
		this.collection = collection;
193
	}
194

    
195
	/**
196
	 * Gets the base uri.
197
	 *
198
	 * @return the base uri
199
	 */
200
	public String getBaseURI() {
201
		return baseURI;
202
	}
203

    
204
	/**
205
	 * Sets the base uri.
206
	 *
207
	 * @param baseURI the new base uri
208
	 */
209
	public void setBaseURI(final String baseURI) {
210
		this.baseURI = baseURI;
211
	}
212

    
213
	/**
214
	 * Gets the object store id.
215
	 *
216
	 * @return the object store id
217
	 */
218
	public String getObjectStoreID() {
219
		return objectStoreID;
220
	}
221

    
222
	/**
223
	 * Sets the object store id.
224
	 *
225
	 * @param objectStoreID the new object store id
226
	 */
227
	public void setObjectStoreID(final String objectStoreID) {
228
		this.objectStoreID = objectStoreID;
229
	}
230

    
231
}
(3-3/5)