1
|
package eu.dnetlib.functionality.cql.mongo;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.List;
|
5
|
|
6
|
import com.google.common.collect.Lists;
|
7
|
import com.mongodb.BasicDBObject;
|
8
|
import com.mongodb.BasicDBObjectBuilder;
|
9
|
import eu.dnetlib.functionality.cql.parse.Relation;
|
10
|
import eu.dnetlib.functionality.cql.parse.Relations;
|
11
|
import org.apache.commons.lang3.StringUtils;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
import org.bson.conversions.Bson;
|
15
|
import org.bson.types.ObjectId;
|
16
|
import org.joda.time.DateTime;
|
17
|
import org.joda.time.format.DateTimeFormat;
|
18
|
import org.joda.time.format.DateTimeFormatter;
|
19
|
import org.joda.time.format.ISODateTimeFormat;
|
20
|
import org.z3950.zing.cql.*;
|
21
|
|
22
|
/**
|
23
|
* Created by claudio on 12/09/16.
|
24
|
*/
|
25
|
public class MongoCqlTranslator {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(MongoCqlTranslator.class); // NOPMD by marko on 11/24/08 5:02 PM
|
28
|
|
29
|
//field names containing dates for the OAI publisher. See also OAICOnfigurationReader constants that are not visible here.
|
30
|
//TODO: Generalize
|
31
|
private final List<String> dateFields = Lists.newArrayList("datestamp", "lastCollectionDate");
|
32
|
/**
|
33
|
* Parses the given query string into a mongo DBObject.
|
34
|
*
|
35
|
* @param query
|
36
|
* String to parse
|
37
|
* @return DBObject corresponding to the query string
|
38
|
*/
|
39
|
public static Bson toMongo(final String query) throws CQLParseException, IOException {
|
40
|
log.debug("PARSING: " + query);
|
41
|
if (StringUtils.isBlank(query)) return new BasicDBObject();
|
42
|
final Bson parsed = new MongoCqlTranslator().doParse(query);
|
43
|
log.debug(parsed);
|
44
|
return parsed;
|
45
|
}
|
46
|
|
47
|
private Bson doParse(final String query) throws IOException, CQLParseException {
|
48
|
CQLParser parser = new CQLParser();
|
49
|
CQLNode root;
|
50
|
root = parser.parse(query);
|
51
|
return doParse(root);
|
52
|
}
|
53
|
|
54
|
private Bson doParse(final CQLNode node) throws CQLParseException {
|
55
|
if (node instanceof CQLTermNode) return doTranslate((CQLTermNode) node);
|
56
|
if (node instanceof CQLBooleanNode) return doTranslate((CQLBooleanNode) node);
|
57
|
|
58
|
throw new RuntimeException("error choice for CQLNode " + node.getClass());
|
59
|
}
|
60
|
|
61
|
private Bson doTranslate(final CQLTermNode termNode) throws CQLParseException {
|
62
|
if (termNode.getTerm().equals("*")) return new BasicDBObject();
|
63
|
String relation = termNode.getRelation().getBase();
|
64
|
Relation rel = Relations.get(relation);
|
65
|
return this.handleRelationNode(rel, termNode);
|
66
|
}
|
67
|
|
68
|
private Bson handleRelationNode(final Relation rel, final CQLTermNode termNode) throws CQLParseException {
|
69
|
BasicDBObject mongoQueryObject = new BasicDBObject();
|
70
|
String term = termNode.getTerm();
|
71
|
String indexName = termNode.getIndex();
|
72
|
Object termObj = term;
|
73
|
if (dateFields.contains(indexName)){
|
74
|
OAIDate termDate = this.parseDate(term);
|
75
|
return handleDateRelationNode(indexName, rel, termDate);
|
76
|
}
|
77
|
if (indexName.equals("_id")) {
|
78
|
termObj = new ObjectId(term);
|
79
|
}
|
80
|
switch (rel) {
|
81
|
case EQUAL:
|
82
|
case EXACT:
|
83
|
mongoQueryObject.put(indexName, termObj);
|
84
|
break;
|
85
|
case NOT:
|
86
|
mongoQueryObject.put(indexName, new BasicDBObject("$ne", termObj));
|
87
|
break;
|
88
|
case GT:
|
89
|
mongoQueryObject.put(indexName, new BasicDBObject("$gt", termObj));
|
90
|
break;
|
91
|
case GTE:
|
92
|
mongoQueryObject.put(indexName, new BasicDBObject("$gte", termObj));
|
93
|
break;
|
94
|
case LT:
|
95
|
mongoQueryObject.put(indexName, new BasicDBObject("$lt", termObj));
|
96
|
break;
|
97
|
case LTE:
|
98
|
mongoQueryObject.put(indexName, new BasicDBObject("$lte", termObj));
|
99
|
break;
|
100
|
default:
|
101
|
throw new CQLParseException("Can't parse query: relation " + rel + " not supported!");
|
102
|
}
|
103
|
return mongoQueryObject;
|
104
|
}
|
105
|
|
106
|
private Bson doTranslate(final CQLBooleanNode node) throws CQLParseException {
|
107
|
if (node instanceof CQLAndNode) return getBooleanQuery("$and", node);
|
108
|
if (node instanceof CQLOrNode) return getBooleanQuery("$or", node);
|
109
|
if (node instanceof CQLNotNode) return getNotQuery((CQLNotNode) node);
|
110
|
throw new RuntimeException("error choice for CQLBooleanNode " + node.getClass());
|
111
|
}
|
112
|
|
113
|
private Bson getBooleanQuery(final String mongoOperator, final CQLBooleanNode node) throws CQLParseException {
|
114
|
Bson left = doParse(node.left);
|
115
|
Bson right = doParse(node.right);
|
116
|
BasicDBObject opQuery = new BasicDBObject();
|
117
|
List<Bson> termList = Lists.newArrayList(left, right);
|
118
|
opQuery.put(mongoOperator, termList);
|
119
|
return opQuery;
|
120
|
}
|
121
|
|
122
|
private Bson getNotQuery(final CQLNotNode node) throws CQLParseException {
|
123
|
Bson left = doParse(node.left);
|
124
|
Bson right = doParse(node.right);
|
125
|
Bson notRight = new BasicDBObject("$not", right);
|
126
|
BasicDBObject andQuery = new BasicDBObject();
|
127
|
List<Bson> termList = Lists.newArrayList(left, notRight);
|
128
|
andQuery.put("$and", termList);
|
129
|
return andQuery;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* The construction of the query changes based on the granularity of the date to handle.
|
134
|
* <p>
|
135
|
* If the date has yyyy-MM-ddThh:mm:ssZ granularity we have to create a range query because in mongo we have milliseconds, hence an
|
136
|
* exact match will return nothing.
|
137
|
* </p>
|
138
|
* <p>
|
139
|
* If the date has yyyy-MM-dd granularity then we have to trick the query. If we are interested in the date 2013-10-28, the date has
|
140
|
* been converted into 2013-10-28T00:00:00Z : if we ask for datestamp = 2013-10-28T00:00:00Z, we'll get nothing: we have to ask for
|
141
|
* records whose day is the one specified by the date.
|
142
|
*
|
143
|
* </p>
|
144
|
*
|
145
|
* @param indexName
|
146
|
* @param rel
|
147
|
* @param date
|
148
|
* @return
|
149
|
*/
|
150
|
private Bson handleDateRelationNode(final String indexName, final Relation rel, final OAIDate date) {
|
151
|
BasicDBObject mongoQueryObject = new BasicDBObject();
|
152
|
DateTime fromDate = date.date;
|
153
|
switch (rel) {
|
154
|
case EQUAL:
|
155
|
case EXACT:
|
156
|
if (date.onlyDate) {
|
157
|
DateTime endDate = date.date.plusDays(1);
|
158
|
mongoQueryObject.put(indexName, BasicDBObjectBuilder.start("$gte", fromDate.toDate()).append("$lt", endDate.toDate()).get());
|
159
|
} else {
|
160
|
DateTime endDate = date.date.plusSeconds(1);
|
161
|
mongoQueryObject.put(indexName, BasicDBObjectBuilder.start("$gte", fromDate.toDate()).append("$lt", endDate.toDate()).get());
|
162
|
}
|
163
|
break;
|
164
|
case NOT:
|
165
|
mongoQueryObject.put(indexName, new BasicDBObject("$ne", fromDate.toDate()));
|
166
|
break;
|
167
|
case GT:
|
168
|
mongoQueryObject.put(indexName, new BasicDBObject("$gt", fromDate.toDate()));
|
169
|
break;
|
170
|
case GTE:
|
171
|
mongoQueryObject.put(indexName, new BasicDBObject("$gte", fromDate.toDate()));
|
172
|
break;
|
173
|
case LT:
|
174
|
mongoQueryObject.put(indexName, new BasicDBObject("$lt", fromDate.toDate()));
|
175
|
break;
|
176
|
case LTE:
|
177
|
/*
|
178
|
If the request is date <= YYYY-MM-DD then we need to change the date.
|
179
|
The parseDate returned YYYY-MM-DDT00:00:00Z, but we need YYYY-MM-DDT23:59:59Z.
|
180
|
To simplify we can add one day and perform < instead of <=.
|
181
|
*/
|
182
|
if (date.onlyDate) {
|
183
|
fromDate = date.date.plusDays(1);
|
184
|
mongoQueryObject.put(indexName, new BasicDBObject("$lt", fromDate.toDate()));
|
185
|
} else {
|
186
|
mongoQueryObject.put(indexName, new BasicDBObject("$lte", fromDate.toDate()));
|
187
|
}
|
188
|
break;
|
189
|
default:
|
190
|
throw new RuntimeException("Can't parse query: relation " + rel + " not supported!");
|
191
|
}
|
192
|
return mongoQueryObject;
|
193
|
}
|
194
|
|
195
|
|
196
|
private OAIDate parseDate(final String date) {
|
197
|
DateTimeFormatter dateNoTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd").withZoneUTC();
|
198
|
DateTimeFormatter iso8601NoMsTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").withZoneUTC();
|
199
|
DateTimeFormatter iso8601Formatter = ISODateTimeFormat.dateTime().withZoneUTC();
|
200
|
OAIDate res = null;
|
201
|
try {
|
202
|
log.debug("Using default " + iso8601Formatter.getClass());
|
203
|
DateTime dt = iso8601Formatter.parseDateTime(date);
|
204
|
res = new OAIDate(dt, false);
|
205
|
} catch (Exception e) {
|
206
|
try {
|
207
|
log.debug("Switching to ISO with no millisecond date formatter: yyyy-MM-dd'T'HH:mm:ssZ");
|
208
|
DateTime dt = iso8601NoMsTimeFormatter.parseDateTime(date);
|
209
|
res = new OAIDate(dt, false);
|
210
|
} catch (Exception ex) {
|
211
|
log.debug("Switching to simple date formatter: yyyy-MM-dd");
|
212
|
DateTime dt = dateNoTimeFormatter.parseDateTime(date);
|
213
|
res = new OAIDate(dt, true);
|
214
|
}
|
215
|
}
|
216
|
return res;
|
217
|
}
|
218
|
|
219
|
class OAIDate {
|
220
|
|
221
|
DateTime date;
|
222
|
boolean onlyDate;
|
223
|
|
224
|
OAIDate(final DateTime date, final boolean onlyDate) {
|
225
|
this.date = date;
|
226
|
this.onlyDate = onlyDate;
|
227
|
}
|
228
|
|
229
|
}
|
230
|
|
231
|
|
232
|
}
|