Project

General

Profile

« Previous | Next » 

Revision 47763

[maven-release-plugin] copy for tag cnr-cql-utils-2.1.1

View differences:

modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/deploy.info
1
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet45/modules/cnr-cql-utils/trunk/", "deploy_repository": "dnet45-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-snapshots", "name": "cnr-cql-utils"}
modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/src/test/java/eu/dnetlib/functionality/cql/mongo/MongoCqlTranslatorTest.java
1
package eu.dnetlib.functionality.cql.mongo;
2

  
3
import java.io.IOException;
4
import java.util.Date;
5

  
6
import com.google.common.collect.Lists;
7
import com.mongodb.BasicDBObject;
8
import com.mongodb.DBObject;
9
import com.mongodb.util.JSON;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.bson.conversions.Bson;
13
import org.bson.types.ObjectId;
14
import org.joda.time.format.DateTimeFormat;
15
import org.joda.time.format.DateTimeFormatter;
16
import org.joda.time.format.ISODateTimeFormat;
17
import org.junit.Ignore;
18
import org.junit.Test;
19
import org.z3950.zing.cql.CQLParseException;
20

  
21
import static org.junit.Assert.assertEquals;
22

  
23
public class MongoCqlTranslatorTest {
24

  
25
	private static final Log log = LogFactory.getLog(MongoCqlTranslatorTest.class); // NOPMD by marko on 11/24/08 5:02 PM
26
	private final MongoCqlTranslator tr = new MongoCqlTranslator();
27

  
28
	@Test
29
	public void testParseEq() throws IOException, CQLParseException {
30
		Bson expected = new BasicDBObject("set", "CEDIASManuscripts");
31
		Bson o = tr.toMongo("set = \"CEDIASManuscripts\"");
32
		assertEquals(expected, o);
33

  
34
	}
35

  
36
	@Test
37
	public void testParseNeq() throws IOException, CQLParseException {
38
		Bson expected = new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts"));
39
		Bson o = tr.toMongo("set <> \"CEDIASManuscripts\"");
40
		assertEquals(expected, o);
41
	}
42

  
43
	@Test
44
	public void testParseAnd() throws IOException, CQLParseException {
45
		BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts")),
46
				new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
47
		Bson o = tr.toMongo("set <> \"CEDIASManuscripts\" AND pippo > x");
48
		log.info(o);
49
		assertEquals(expected, o);
50
	}
51

  
52
	@Test
53
	public void testParseOr() throws IOException, CQLParseException {
54
		BasicDBObject expected = new BasicDBObject("$or", Lists.newArrayList(new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts")),
55
				new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
56
		Bson o = tr.toMongo("set <> \"CEDIASManuscripts\" OR pippo > x");
57
		log.info(o);
58
		assertEquals(expected, o);
59
	}
60

  
61
	@Test
62
	public void testParseNot() throws IOException, CQLParseException {
63
		BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject("set", "CEDIASManuscripts"), new BasicDBObject("$not",
64
				new BasicDBObject("pippo", new BasicDBObject("$gt", "x")))));
65
		Bson o = tr.toMongo("set = \"CEDIASManuscripts\" NOT pippo > x");
66
		//log.info(o)
67
		assertEquals(expected, o);
68
	}
69

  
70
	@Test
71
	public void testParseStar() throws IOException, CQLParseException {
72
		BasicDBObject expected = new BasicDBObject();
73
		Bson o = tr.toMongo("*");
74
		Bson o2 = tr.toMongo("*=*");
75
		assertEquals(expected, o);
76
		assertEquals(expected, o2);
77
	}
78

  
79
	@Test
80
	public void testParseStarAnd() throws IOException, CQLParseException {
81
		BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject(), new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
82
		Bson o = tr.toMongo("* AND pippo > x");
83
		Bson o2 = tr.toMongo("*=* AND pippo > x");
84
		assertEquals(expected, o);
85
		assertEquals(expected, o2);
86
	}
87

  
88
	@Test
89
	public void testParseIdQuery() throws IOException, CQLParseException {
90
		BasicDBObject expected = new BasicDBObject("_id", new BasicDBObject("$gt", new ObjectId("5225e093aabf055637bf2c65")));
91
		Bson o = tr.toMongo("_id > 5225e093aabf055637bf2c65");
92
		assertEquals(expected, o);
93
	}
94

  
95
	@Ignore
96
	@Test
97
	public void testParseWfLoggerQuery() throws IOException, CQLParseException {
98
		BasicDBObject expected = new BasicDBObject("$and",
99
				Lists.newArrayList(
100
						new BasicDBObject("parentDatasourceId", "opendoar____::2294"),
101
						new BasicDBObject("system:profileFamily", "aggregator"),
102
						new BasicDBObject("system:isCompletedSuccessfully", "true")));
103

  
104
		Bson o = tr.toMongo("{\"parentDatasourceId\" : \"opendoar____::2294\", \"system:profileFamily\" : \"aggregator\", \"system:isCompletedSuccessfully\" : \"true\" }");
105
		assertEquals(expected, o);
106
	}
107

  
108
}
modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/src/test/java/eu/dnetlib/functionality/cql/lucene/CqlUtilsTest.java
1
package eu.dnetlib.functionality.cql.lucene;
2

  
3
import static org.junit.Assert.assertTrue;
4

  
5
import java.util.ArrayList;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Map.Entry;
10

  
11
import eu.dnetlib.functionality.cql.CqlGroup;
12
import eu.dnetlib.functionality.cql.CqlUtils;
13
import org.junit.Before;
14
import org.junit.Test;
15
import org.z3950.zing.cql.CQLNode;
16

  
17
import com.google.common.collect.Lists;
18
import com.google.common.collect.Maps;
19

  
20
public class CqlUtilsTest {
21

  
22
	private Map<String, String> weights;
23

  
24
	@Before
25
	public void setUp() {
26
		weights = Maps.newHashMap();
27
		weights.put("title", "2");
28
		weights.put("ugo", "1.5");
29
		weights.put("tag", "3");
30
	}
31

  
32
	@Test
33
	public void test_0() {
34
		String query = "tag = pluto";
35
		CQLNode res = CqlUtils.filter(query, Lists.newArrayList("tag"));
36
		assertTrue(res == null);
37
	}
38

  
39
	@Test
40
	public void test_filter_1() {
41
		String query = "creator = pippo";
42
		CQLNode res = CqlUtils.filter(query, Lists.newArrayList("tag"));
43
		assertTrue(res.toCQL().equals(query));
44
	}
45

  
46
	@Test
47
	public void test_filter_2() {
48
		String query = "creator = pippo and tag = pluto";
49
		CQLNode res = CqlUtils.filter(query, Lists.newArrayList("tag"));
50
		System.out.println(res.toCQL());
51
	}
52

  
53
	@Test
54
	public void test_filter_3() {
55
		String query = "creator = pippo and tag = pluto and title = pizza";
56
		CQLNode res = CqlUtils.filter(query, Lists.newArrayList("tag"));
57
		System.out.println(res.toCQL());
58
	}
59

  
60
	@Test
61
	public void test_filter_4() {
62
		String query = "creator = pippo or tag = pluto and title = pizza";
63
		CQLNode res = CqlUtils.filter(query, Lists.newArrayList("tag"));
64
		System.out.println(res.toCQL());
65
	}
66

  
67
	@Test
68
	public void test_filter_5() {
69
		String query = "creator = pippo and tag = pluto or title = pizza";
70
		CQLNode res = CqlUtils.filter(query, Lists.newArrayList("tag"));
71
		System.out.println(res.toCQL());
72
	}
73

  
74
	//////
75

  
76
	@Test
77
	public void test_group_0() {
78
		String query = "(>s=NAMESPACE s.test=800) and  creator = pippo and tag = pluto or title = pizza or title = pozzo";
79
		Map<String, CQLNode> res = CqlUtils.group(query, Lists.newArrayList("tag", "title"));
80
		print(query, res);
81
	}
82

  
83
	@Test
84
	public void test_group_01() {
85
		String query = "test and language exact \"fin\"";
86
		Map<String, CQLNode> res = CqlUtils.group(query, Lists.newArrayList("language", "repositorycountry"));
87
		print(query, res);
88
	}
89

  
90
	@Test
91
	public void test_group_1() {
92
		String query = "creator = pippo and tag = pluto or title = pizza and tag = roma";
93
		Map<String, CQLNode> res = CqlUtils.group(query, Lists.newArrayList("tag"));
94
		print(query, res);
95
	}
96

  
97
	@Test
98
	public void test_group_1_1() {
99
		String query = "creator = pippo and ((tag = pluto or title = pizza) and tag = roma)";
100
		Map<String, CQLNode> res = CqlUtils.group(query, Lists.newArrayList("tag"));
101
		print(query, res);
102
	}
103

  
104
	@Test
105
	public void test_group_2() {
106
		String query = "creator = pippo and tag = pluto or title = pizza and tag = roma";
107
		Map<String, CQLNode> res = CqlUtils.group(query, Lists.newArrayList("tag", "creator", "title"));
108
		print(query, res);
109
	}
110

  
111
	@Test
112
	public void test_group_3() {
113
		String query = "creator = pippo and tag = pluto or title = pizza and tag = roma";
114
		Map<String, CQLNode> res = CqlUtils.group(query, new ArrayList<String>());
115
		print(query, res);
116
	}
117

  
118
	@Test
119
	public void test_group_3_1() {
120
		String query = "creator = pippo or tag = roma";
121
		Map<String, CQLNode> res = CqlUtils.group(query, Lists.newArrayList("tag"));
122
		print(query, res);
123
	}
124

  
125
	@Test
126
	public void test_group_3_2() {
127
		String query = "tag = roma or blabla or bloblo";
128
		Map<String, CQLNode> res = CqlUtils.group(query, Lists.newArrayList(CqlGroup.defaultTerm));
129
		print(query, res);
130
	}
131

  
132
	////
133

  
134
	@Test
135
	public void test_group_4_1() {
136
		String query = "creator = pippo and tag = pluto or title = pizza and tag = roma";
137
		List<String> res = CqlUtils.listTerms(query, "tag");
138
		System.out.println("[query: " + query + "]");
139
		System.out.println(res);
140
	}
141

  
142
	@Test
143
	public void test_group_4_2() {
144
		String query = "tag = pippo";
145
		List<String> res = CqlUtils.listTerms(query, "tag");
146
		System.out.println("[query: " + query + "]");
147
		System.out.println(res);
148
	}
149

  
150
	@Test
151
	public void test_group_4_3() {
152
		String query = "creator = pippo and tag = pluto or title = pizza or tag = roma and blabla or bloblo";
153
		Map<String, CQLNode> res = CqlUtils.group(query, CqlUtils.listFields(query));
154
		print(query, res);
155
	}
156

  
157
	@Test
158
	public void test_expand_1() {
159
		String query = "tag = roma or blabla or bloblo";
160
		CQLNode res = CqlUtils.expand(query, weights.keySet());
161
		System.out.println("[query: " + query + "]");
162
		System.out.println(res.toCQL());
163
	}
164

  
165
	@Test
166
	public void test_expand_2() {
167
		String query = "blabla or bloblo";
168
		CQLNode res = CqlUtils.expand(query, weights.keySet());
169
		System.out.println("[query: " + query + "]");
170
		System.out.println(res.toCQL());
171
	}
172

  
173
	@Test
174
	public void test_expand_3() {
175
		String query = "blabla";
176
		CQLNode res = CqlUtils.expand(query, weights.keySet());
177
		System.out.println("[query: " + query + "]");
178
		System.out.println(res.toCQL());
179
	}
180

  
181
	@Test
182
	public void test_expand_4() {
183
		String query = "tag = roma or blabla or bloblo";
184
		CQLNode res = CqlUtils.expand(query, new HashSet<String>());
185
		System.out.println("[query: " + query + "]");
186
		System.out.println(res.toCQL());
187
	}
188

  
189
	////
190

  
191
	@Test
192
	public void test_list_fields_0() {
193
		String query = "pippo";
194
		List<String> res = CqlUtils.listFields(query);
195
		System.out.println("[query: " + query + "]");
196
		System.out.println(res);
197
	}
198

  
199
	@Test
200
	public void test_list_fields_1() {
201
		String query = "tag = pippo";
202
		List<String> res = CqlUtils.listFields(query);
203
		System.out.println("[query: " + query + "]");
204
		System.out.println(res);
205
	}
206

  
207
	///////////////////////
208
	private void print(String query, Map<String, CQLNode> res) {
209
		System.out.println("query: [" + query + "]");
210
		for (Entry<String, CQLNode> e : res.entrySet()) {
211
			System.out.println(e.getKey() + ": [" + e.getValue().toCQL() + "]");
212
		}
213
	}
214

  
215
}
modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/src/test/java/eu/dnetlib/functionality/cql/lucene/CqlTranslatorImplTest.java
1
package eu.dnetlib.functionality.cql.lucene;
2

  
3
import java.io.IOException;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7

  
8
import com.google.common.collect.BiMap;
9
import com.google.common.collect.HashBiMap;
10
import com.google.common.collect.Lists;
11
import com.google.common.collect.Maps;
12
import eu.dnetlib.functionality.cql.CqlTranslator;
13
import eu.dnetlib.functionality.cql.CqlTranslatorImpl;
14
import eu.dnetlib.functionality.cql.CqlUtils;
15
import eu.dnetlib.functionality.cql.parse.*;
16
import org.junit.Before;
17
import org.junit.Test;
18
import org.z3950.zing.cql.CQLNode;
19
import org.z3950.zing.cql.CQLParseException;
20
import org.z3950.zing.cql.CQLParser;
21

  
22
import static org.junit.Assert.assertEquals;
23
import static org.junit.Assert.assertNotNull;
24

  
25
public class CqlTranslatorImplTest {
26

  
27
	private CqlTranslator translator;
28

  
29
	private Map<String, String> weights;
30

  
31
	@Before
32
	public void setUp() {
33
		translator = new CqlTranslatorImpl();
34

  
35
		weights = Maps.newHashMap();
36
		weights.put("title", "2");
37
		weights.put("ugo", "1.5");
38
		weights.put("tag", "3");
39
	}
40

  
41
	@Test
42
	public void testToSolr_BOW() throws Exception {
43

  
44
		String cqlQuery = "a b c";
45
		String luceneQuery = translator.toLucene(cqlQuery);
46

  
47
		printQuery(cqlQuery, luceneQuery);
48
	}
49

  
50
	@Test
51
	public void testToSolr_0() throws Exception {
52

  
53
		String cqlQuery = "\"a b x\" and r and \"eng/ita\" or f any \"pippo pluto\" and \"con: duepunti\" not (d < 5 and d > 10)";
54
		String luceneQuery = translator.toLucene(cqlQuery);
55

  
56
		printQuery(cqlQuery, luceneQuery);
57
	}
58

  
59
	@Test
60
	public void testToSolr_00() throws Exception {
61

  
62
		String cqlQuery = "resultdupid exact datacite____::b49fd4e3386d82df348a120cfe43516b OR objidentifier exact datacite____::b49fd4e3386d82df348a120cfe43516b";
63
		String luceneQuery = translator.toLucene(cqlQuery);
64

  
65
		printQuery(cqlQuery, luceneQuery);
66
	}
67

  
68
	@Test
69
	public void testToSolr_1() throws Exception {
70

  
71
		String cqlQuery = "publicationdate =/within \"2000-01-01 2010-01-01\"";
72
		String luceneQuery = translator.toLucene(cqlQuery);
73

  
74
		printQuery(cqlQuery, luceneQuery);
75
	}
76

  
77
	@Test
78
	public void testToSolr_2() throws Exception {
79
		String query = "(_all=faust AND _all=pippo) AND _all<>cinegiornale";
80
		Node node = new AndNode(new AndNode(new TermNode("_all", Relation.EQUAL, "faust"), new TermNode("_all", Relation.EQUAL, "pippo")), new TermNode("_all",
81
				Relation.NOT, "cinegiornale"));
82

  
83
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
84

  
85
		printQuery(query, parsed.asLucene());
86
		System.out.println("NODE:   " + node.toLucene());
87
		System.out.println("NODE:   " + node.toString());
88
		assertEquals(node.toLucene(), parsed.asLucene());
89
	}
90

  
91
	@Test
92
	public void testToSolr_3() throws Exception {
93
		String query = "_all all faust";
94
		Node node = new TermNode("_all", Relation.ALL, "faust");
95
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
96
		printQuery(query, parsed.asLucene());
97
		assertEquals(node.toLucene(), parsed.asLucene());
98

  
99
		query = "__all all \"caracas roma\"";
100

  
101
		parsed = translator.getTranslatedQuery(query);
102
		printQuery(query, parsed.asLucene());
103

  
104
		query = "__all all caracas roma";
105
		parsed = translator.getTranslatedQuery(query);
106
		printQuery(query, parsed.asLucene());
107

  
108
		query = "__all any caracas roma";
109
		parsed = translator.getTranslatedQuery(query);
110
		printQuery(query, parsed.asLucene());
111

  
112
		query = "__all any \"caracas roma\"";
113
		parsed = translator.getTranslatedQuery(query);
114
		printQuery(query, parsed.asLucene());
115

  
116
		query = "__all exact caracas roma";
117
		parsed = translator.getTranslatedQuery(query);
118
		printQuery(query, parsed.asLucene());
119
	}
120

  
121
	@Test
122
	public void testToSolr_4() throws Exception {
123

  
124
		String cqlQuery = "publicationdate =/within \"2000-01-01 2010-01-01\" and title = \"ddd\" and y < 2010 or y <= 2010 or y > 2010 or y >= 2010";
125
		String luceneQuery = translator.toLucene(cqlQuery);
126

  
127
		printQuery(cqlQuery, luceneQuery);
128
	}
129

  
130
	@Test
131
	public void testToSolr_5() throws Exception {
132

  
133
		String cqlQuery = "publicationdate within \"2000-01-01 2010-01-01\"";
134
		String luceneQuery = translator.toLucene(cqlQuery);
135

  
136
		printQuery(cqlQuery, luceneQuery);
137
	}
138

  
139
	@Test
140
	public void testToSolr_6() throws Exception {
141

  
142
		String cqlQuery = "cat sortBy title/sort.descending";
143
		final TranslatedQuery tr = translator.getTranslatedQuery(cqlQuery);
144
		printQuery(cqlQuery, tr.asLucene());
145
		System.out.println(tr.getOptions());
146

  
147
	}
148

  
149
	@Test
150
	public void testToSolr_7() throws Exception {
151

  
152
		String cqlQuery = "title exact true sortBy title/sort.ascending";
153
		CQLNode parsed = new CQLParser().parse(cqlQuery);
154

  
155
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
156

  
157
		assertNotNull(luceneQuery.getOptions().getSort().getField());
158
		assertNotNull(luceneQuery.getOptions().getSort().getMode());
159

  
160
		assertEquals("title", luceneQuery.getOptions().getSort().getField());
161
		assertEquals(SortOperation.Mode.asc, luceneQuery.getOptions().getSort().getMode());
162

  
163
		System.out.println("LUCENE: " + luceneQuery.asLucene() + " OPTIONS:" + luceneQuery.getOptions().getSort().getField() + " "
164
				+ luceneQuery.getOptions().getSort().getMode());
165

  
166
		printQuery(cqlQuery, luceneQuery.asLucene());
167
	}
168

  
169
	@Test
170
	public void testToSolr_8() throws Exception {
171

  
172
		String cqlQuery = "__all all \"caracas - roma\"";
173
		CQLNode parsed = new CQLParser().parse(cqlQuery);
174
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
175

  
176
		printQuery(cqlQuery, luceneQuery.asLucene());
177
	}
178

  
179
	@Test
180
	public void testToSolr_9() throws Exception {
181

  
182
		String cqlQuery = "__all all \"caracas roma\"";
183
		CQLNode parsed = new CQLParser().parse(cqlQuery);
184
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
185

  
186
		printQuery(cqlQuery, luceneQuery.asLucene());
187

  
188
		cqlQuery = "__all all caracas roma";
189
		parsed = new CQLParser().parse(cqlQuery);
190
		luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
191

  
192
		printQuery(cqlQuery, luceneQuery.asLucene());
193

  
194
		cqlQuery = "__all any caracas roma";
195
		parsed = new CQLParser().parse(cqlQuery);
196
		luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
197

  
198
		printQuery(cqlQuery, luceneQuery.asLucene());
199

  
200
		cqlQuery = "__all any \"caracas roma\"";
201
		parsed = new CQLParser().parse(cqlQuery);
202
		luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
203

  
204
		printQuery(cqlQuery, luceneQuery.asLucene());
205

  
206
		cqlQuery = "__all exact caracas roma";
207
		parsed = new CQLParser().parse(cqlQuery);
208
		luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
209

  
210
		printQuery(cqlQuery, luceneQuery.asLucene());
211
	}
212

  
213
	@Test
214
	public void testToSolr_10() throws Exception {
215

  
216
		String cqlQuery = "__all all \"caracas - ro*\"";
217
		CQLNode parsed = new CQLParser().parse(cqlQuery);
218

  
219
		Map<String, List<String>> cqlOptions = Maps.newHashMap();
220
		BiMap<String, String> aliases = HashBiMap.create();
221

  
222
		cqlOptions.put("wildcard", Lists.newArrayList("true"));
223
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap(), cqlOptions, aliases,
224
				new HashMap<String, String>());
225
		printQuery(cqlQuery, luceneQuery.asLucene());
226

  
227
		cqlOptions = Maps.newHashMap();
228
		luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap(), cqlOptions, aliases, new HashMap<String, String>());
229

  
230
		printQuery(cqlQuery, luceneQuery.asLucene());
231
	}
232

  
233
	@Test
234
	public void testToSolr_11() throws Exception {
235

  
236
		String cqlQuery = "__all <> \"kreutz\" and __all <> \"austria\"";
237
		CQLNode parsed = new CQLParser().parse(cqlQuery);
238
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
239

  
240
		printQuery(cqlQuery, luceneQuery.asLucene());
241
	}
242

  
243
	@Test
244
	public void testToSolr_12() throws Exception {
245

  
246
		String cqlQuery = "__all <> \"kreutz\" and __all <> \"austria\" or __all <> \"italia\" and __dsid exact \"wwwww\"";
247
		CQLNode parsed = new CQLParser().parse(cqlQuery);
248
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
249

  
250
		printQuery(cqlQuery, luceneQuery.asLucene());
251
	}
252

  
253
	@Test
254
	public void testToSolr_13() throws Exception {
255

  
256
		String cqlQuery = "__all = faust and __all <> cinegiornale";
257

  
258
		CQLNode parsed = new CQLParser().parse(cqlQuery);
259
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
260

  
261
		printQuery(cqlQuery, luceneQuery.asLucene());
262
	}
263

  
264
	@Test
265
	public void testToSolr_14() throws Exception {
266

  
267
		String cqlQuery = "__all <> cinegiornale and __all = faust";
268

  
269
		CQLNode parsed = new CQLParser().parse(cqlQuery);
270
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
271

  
272
		printQuery(cqlQuery, luceneQuery.asLucene());
273
	}
274

  
275
	@Test
276
	public void testToSolr_15() throws Exception {
277

  
278
		String cqlQuery = "(asdasd or asfgqwr) and textual";
279

  
280
		CQLNode parsed = new CQLParser().parse(cqlQuery);
281
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
282

  
283
		printQuery(cqlQuery, luceneQuery.asLucene());
284
	}
285

  
286
	@Test
287
	public void testToSolr_16() throws Exception {
288

  
289
		String cqlQuery = "title = kreutz and subject any \"austria italy\" or tag = pippo and blabla";
290
		CQLNode parsed = new CQLParser().parse(cqlQuery);
291

  
292
		Map<String, List<String>> options = Maps.newHashMap();
293
		BiMap<String, String> aliases = HashBiMap.create();
294
		IdentityCqlValueTransformerMap map = new IdentityCqlValueTransformerMap();
295

  
296
		CQLNode expand = CqlUtils.expand(parsed, weights.keySet());
297

  
298
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(expand, map, options, aliases, weights);
299

  
300
		printQuery(cqlQuery, luceneQuery.asLucene());
301
	}
302

  
303
	@Test
304
	public void testToSolr_17() throws Exception {
305

  
306
		String cqlQuery = "a = 1 and b = 2 or c = 3 and blabla";
307
		CQLNode parsed = new CQLParser().parse(cqlQuery);
308

  
309
		Map<String, List<String>> options = Maps.newHashMap();
310
		BiMap<String, String> aliases = HashBiMap.create();
311
		IdentityCqlValueTransformerMap map = new IdentityCqlValueTransformerMap();
312

  
313
		Map<String, String> w = Maps.newHashMap();
314
		w.put("a", "2");
315
		w.put("c", "1.5");
316

  
317
		CQLNode expand = CqlUtils.expand(parsed, w.keySet());
318

  
319
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(expand, map, options, aliases, w);
320

  
321
		printQuery(cqlQuery, luceneQuery.asLucene());
322
	}
323

  
324
	@Test
325
	public void testToSolr_18() throws Exception {
326

  
327
		String cqlQuery = "a exact 1";
328
		CQLNode parsed = new CQLParser().parse(cqlQuery);
329

  
330
		Map<String, List<String>> options = Maps.newHashMap();
331
		BiMap<String, String> aliases = HashBiMap.create();
332
		aliases.put("a", "b");
333

  
334
		IdentityCqlValueTransformerMap map = new IdentityCqlValueTransformerMap();
335

  
336
		Map<String, String> w = Maps.newHashMap();
337

  
338
		CQLNode expand = CqlUtils.expand(parsed, w.keySet());
339

  
340
		TranslatedQuery luceneQuery = translator.getTranslatedQuery(expand, map, options, aliases, w);
341

  
342
		printQuery(cqlQuery, luceneQuery.asLucene());
343
	}
344

  
345
	@Test
346
	public void testToSolr_19() throws Exception {
347
		String query = "_all<>faust";
348
		Node node = new TermNode("_all", Relation.NOT, "faust");
349

  
350
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
351

  
352
		printQuery(query, parsed.asLucene());
353
		assertEquals(node.toLucene(), parsed.asLucene());
354

  
355
		query = "_all <> faust nozze";
356
		// node = new TermNode("_all", Relation.NOT, "faust nozze");
357
		parsed = translator.getTranslatedQuery(query);
358
		printQuery(query, parsed.asLucene());
359

  
360
	}
361

  
362
	@Test
363
	public void testToSolr_20() throws Exception {
364

  
365
		String query = "(title = ESTUDIO and title = abierto) not (title = mediante)";
366

  
367
		Node node = new NotNode(new AndNode(new TermNode("title", Relation.EQUAL, "ESTUDIO"), new TermNode("title", Relation.EQUAL, "abierto")), new TermNode(
368
				"title", Relation.EQUAL, "mediante"));
369

  
370
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
371

  
372
		printQuery(query, parsed.asLucene());
373
		assertEquals(node.toLucene(), parsed.asLucene());
374
	}
375

  
376
	@Test
377
	public void testToSolr_21() throws Exception {
378

  
379
		String query = "(title = ESTUDIO and title = abierto) not (title = mediante or title = verde)";
380

  
381
		Node node = new NotNode(new AndNode(new TermNode("title", Relation.EQUAL, "ESTUDIO"), new TermNode("title", Relation.EQUAL, "abierto")), new OrNode(
382
				new TermNode("title", Relation.EQUAL, "mediante"), new TermNode("title", Relation.EQUAL, "verde")));
383

  
384
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
385

  
386
		printQuery(query, parsed.asLucene());
387
		assertEquals(node.toLucene(), parsed.asLucene());
388
	}
389

  
390
	@Test
391
	public void testToSolr_22() throws Exception {
392
		String query = " and itemtype = *";
393
		// Node node = new TermNode("itemtype", Relation.EQUAL, "*");
394

  
395
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
396

  
397
		printQuery(query, parsed.asLucene());
398

  
399
		query = " AND itemtype = *";
400
		parsed = translator.getTranslatedQuery(query);
401
		printQuery(query, parsed.asLucene());
402

  
403
		Map<String, List<String>> cqlOptions = Maps.newHashMap();
404
		BiMap<String, String> aliases = HashBiMap.create();
405

  
406
		cqlOptions.put("wildcard", Lists.newArrayList("true"));
407

  
408
		query = "AND itemtype = vid* borat";
409
		parsed = translator.getTranslatedQuery(query, cqlOptions);
410
		printQuery(query, parsed.asLucene());
411

  
412
		// + - && || ! ( ) { } [ ] ^ " ~ * ? : \
413
		query = " AND f = * vid* bo?ra bo+ra bo-ra bo&ra bo!ra bo]ra bo^ra bo*ra";// bo|ra bo(ra bo)ra bo{ra bo}ra
414
		// bo\"ra bo~ra bo:ra bo\\ra";
415

  
416
		CQLNode cqlNode = new CQLParser().parse(query);
417
		parsed = translator.getTranslatedQuery(cqlNode, new IdentityCqlValueTransformerMap(), cqlOptions, aliases, new HashMap<String, String>());
418
		printQuery(query, parsed.asLucene());
419

  
420
		parsed = translator.getTranslatedQuery(query, cqlOptions);
421
		printQuery(query, parsed.asLucene());
422

  
423
		parsed = translator.getTranslatedQuery(query);
424
		printQuery(query, parsed.asLucene());
425
	}
426

  
427
	@Test
428
	public void testToSolr_23() throws Exception {
429

  
430
		String query = "_all = cat sortBy title/sort.descending";
431
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
432

  
433
		Node node = new TermNode("_all", Relation.EQUAL, "cat");
434

  
435
		printQuery(query, parsed.asLucene());
436
		assertEquals(node.toLucene(), parsed.asLucene());
437

  
438
		assertNotNull(parsed.getOptions().getSort().getField());
439
		assertNotNull(parsed.getOptions().getSort().getMode());
440

  
441
		assertEquals("title", parsed.getOptions().getSort().getField());
442
		assertEquals(SortOperation.Mode.desc, parsed.getOptions().getSort().getMode());
443
	}
444

  
445
	@Test
446
	public void testToSolr_24() throws Exception {
447

  
448
		String query = "invalid exact true sortBy title/sort.ascending";
449

  
450
		// Node node = new TermNode("invalid", Relation.EXACT, "true");
451

  
452
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
453

  
454
		printQuery(query, parsed.asLucene());
455

  
456
		assertNotNull(parsed.getOptions().getSort().getField());
457
		assertNotNull(parsed.getOptions().getSort().getMode());
458

  
459
		assertEquals("title", parsed.getOptions().getSort().getField());
460
		assertEquals(SortOperation.Mode.asc, parsed.getOptions().getSort().getMode());
461
	}
462

  
463
	@Test
464
	public void testToSolr_25() throws Exception {
465

  
466
		// String query = "deleted all true and __dsid all xxxxxxxxxxx";
467

  
468
		// ciao (+a +b +c)
469
		String query = "ciao or (_all all \"a b c\")";
470
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
471
		printQuery(query, parsed.asLucene());
472

  
473
		query = "(__all = ciao) or (__all all \"a b c\")";
474
		parsed = translator.getTranslatedQuery(query);
475
		printQuery(query, parsed.asLucene());
476

  
477
		query = "a and b";
478
		parsed = translator.getTranslatedQuery(query);
479
		printQuery(query, parsed.asLucene());
480
		query = "field=a and field=b";
481
		parsed = translator.getTranslatedQuery(query);
482
		printQuery(query, parsed.asLucene());
483

  
484
		query = "a or b";
485
		parsed = translator.getTranslatedQuery(query);
486
		printQuery(query, parsed.asLucene());
487

  
488
		query = "field=a or field=b";
489
		parsed = translator.getTranslatedQuery(query);
490
		printQuery(query, parsed.asLucene());
491

  
492
		query = "field all a or field all b";
493
		parsed = translator.getTranslatedQuery(query);
494
		printQuery(query, parsed.asLucene());
495
	}
496

  
497
	@Test
498
	public void testToSolr_26() throws Exception {
499

  
500
		String query = "publicationdate =/within \"2000-01-01 2010-01-01\"";
501
		Node node = new TermNode("publicationdate", Relation.WITHIN, "2000-01-01 2010-01-01");
502

  
503
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
504

  
505
		printQuery(query, parsed.asLucene());
506
		assertEquals(node.toLucene(), parsed.asLucene());
507
	}
508

  
509
	@Test
510
	public void testToSolr_27() throws Exception {
511

  
512
		String query = "((((publicationdate =/within \"2000-01-01 2010-01-01\" and title = \"ddd\") and y < 2010) or y <= 2010) or y > 2010) or y >= 2010";
513
		Node node = new OrNode(new OrNode(new OrNode(new AndNode(new AndNode(new TermNode("publicationdate", Relation.WITHIN, "2000-01-01 2010-01-01"),
514
				new TermNode("title", Relation.EQUAL, "ddd")), new TermNode("y", Relation.LT, "2010")), new TermNode("y", Relation.LTE, "2010")), new TermNode(
515
				"y", Relation.GT, "2010")), new TermNode("y", Relation.GTE, "2010"));
516

  
517
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
518

  
519
		printQuery(query, parsed.asLucene());
520
		assertEquals(node.toLucene(), parsed.asLucene());
521
	}
522

  
523
	@Test
524
	public void testToSolr_28() throws Exception {
525

  
526
		String query = "publicationdate =/within \"2000-01-01 2010-01-01\" and (title = \"ddd\" and (y < 2010 or (y <= 2010 or (y > 2010 or y >= 2010))))";
527

  
528
		Node node = new AndNode(new TermNode("publicationdate", Relation.WITHIN, "2000-01-01 2010-01-01"), new AndNode(new TermNode("title", Relation.EQUAL,
529
				"ddd"), new OrNode(new TermNode("y", Relation.LT, "2010"), new OrNode(new TermNode("y", Relation.LTE, "2010"), new OrNode(new TermNode("y",
530
				Relation.GT, "2010"), new TermNode("y", Relation.GTE, "2010"))))));
531

  
532
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
533

  
534
		printQuery(query, parsed.asLucene());
535
		assertEquals(node.toLucene(), parsed.asLucene());
536
	}
537

  
538
	@Test
539
	public void testToSolr_29() throws Exception {
540

  
541
		String query = "dateaccepted =/within \"1900-01-01 2000-01-01\" and dateaccepted >= 2010-01-01";
542

  
543
		Node node = new AndNode(new TermNode("dateaccepted", Relation.WITHIN, "1900-01-01 2000-01-01"),
544
				new TermNode("dateaccepted", Relation.GTE, "2010-01-01"));
545

  
546
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
547

  
548
		printQuery(query, parsed.asLucene());
549
		assertEquals(node.toLucene(), parsed.asLucene());
550

  
551
		query = "dateaccepted =/within \"1900-01-01 2000-01-01\" and dateaccepted > 2010-01-01";
552

  
553
		node = new AndNode(new TermNode("dateaccepted", Relation.WITHIN, "1900-01-01 2000-01-01"), new TermNode("dateaccepted", Relation.GT, "2010-01-01"));
554

  
555
		parsed = translator.getTranslatedQuery(query);
556

  
557
		printQuery(query, parsed.asLucene());
558
		assertEquals(node.toLucene(), parsed.asLucene());
559
	}
560

  
561
	@Test
562
	public void testToSolr_30() throws Exception {
563

  
564
		String query = "a = 1 and b = 2 and c = 3";
565
		Node node = new AndNode(new AndNode(new TermNode("a", Relation.EQUAL, "1"), new TermNode("b", Relation.EQUAL, "2")), new TermNode("c", Relation.EQUAL,
566
				"3"));
567

  
568
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
569

  
570
		printQuery(query, parsed.asLucene());
571
		assertEquals(node.toLucene(), parsed.asLucene());
572
	}
573

  
574
	@Test
575
	public void testToSolr_31() throws Exception {
576

  
577
		String query = "a = \"pippo pluto\" and b = 2 and c = 3";
578
		Node node = new AndNode(new AndNode(new TermNode("a", Relation.EQUAL, "pippo pluto"), new TermNode("b", Relation.EQUAL, "2")), new TermNode("c",
579
				Relation.EQUAL, "3"));
580

  
581
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
582

  
583
		printQuery(query, parsed.asLucene());
584
		assertEquals(node.toLucene(), parsed.asLucene());
585
	}
586

  
587
	@Test
588
	public void testToSolr_32() throws Exception {
589

  
590
		String query = "__all all \"caracas - ro*\"";
591

  
592
		TranslatedQuery parsed = translator.getTranslatedQuery(query);
593
		printQuery(query, parsed.asLucene());
594

  
595
		Map<String, List<String>> cqlOptions = Maps.newHashMap();
596
		cqlOptions.put("wildcard", Lists.newArrayList("true"));
597

  
598
		parsed = translator.getTranslatedQuery(query, cqlOptions);
599
		printQuery(query, parsed.asLucene());
600

  
601
	}
602

  
603
	@Test
604
	public void testToSolr_33() throws Exception {
605
		String query1 = "query=(deletedbyinference = false) AND (((oaftype exact project) and (test)) and (fundinglevel0_id exact corda_______::FP7))";
606
		printQuery(query1, translator.getTranslatedQuery(query1).asLucene());
607

  
608
		String query2 = "query=(((deletedbyinference = false) AND (oaftype exact project)) and (test)) and (fundinglevel0_id exact corda_______::FP7)";
609
		printQuery(query2, translator.getTranslatedQuery(query2).asLucene());
610

  
611
		String query3 = "(deletedbyinference = false) AND (((oaftype exact project) and (test)) and (fundinglevel0_id exact corda_______::FP7))";
612
		printQuery(query3, translator.getTranslatedQuery(query1).asLucene());
613

  
614
		String query4 = "(((deletedbyinference = false) AND (oaftype exact project)) and (test)) and (fundinglevel0_id exact corda_______::FP7)";
615
		printQuery(query4, translator.getTranslatedQuery(query2).asLucene());
616
	}
617

  
618
	@Test
619
	public void testWildcard() throws CQLParseException, IOException {
620

  
621
		Map<String, List<String>> options = new HashMap<String, List<String>>();
622

  
623
		String cqlQuery = "__all = \"test?\"";
624
		String lucene = translator.toLucene(cqlQuery, options);
625
		printQuery(cqlQuery, lucene);
626

  
627
		options.put("wildcard", Lists.newArrayList("true"));
628

  
629
		cqlQuery = "__all = \"test?\"";
630
		lucene = translator.toLucene(cqlQuery, options);
631
		printQuery(cqlQuery, lucene);
632
	}
633

  
634
	@Test
635
	public void testWildcard_2() throws CQLParseException, IOException {
636

  
637
		Map<String, List<String>> options = new HashMap<String, List<String>>();
638

  
639
		String cqlQuery = "thumbnail=localhost*";
640
		String lucene = translator.toLucene(cqlQuery, options);
641
		printQuery(cqlQuery, lucene);
642

  
643
		options.put("wildcard", Lists.newArrayList("true"));
644

  
645
		lucene = translator.toLucene(cqlQuery, options);
646
		printQuery(cqlQuery, lucene);
647
	}
648

  
649
	@Test
650
	public void testDateQuery() throws CQLParseException, IOException {
651
		String cqlQuery = "(resultdateofacceptance <= \"2012-03-15\")";
652
		Map<String, List<String>> options = new HashMap<String, List<String>>();
653
		String lucene = translator.toLucene(cqlQuery, options);
654
		printQuery(cqlQuery, lucene);
655
	}
656

  
657
	@Test
658
	public void testFullISODateQuery() throws CQLParseException, IOException {
659
		String cqlQuery = "(resultdateofacceptance <= 2012-03-15T00:00:00Z)";
660
		Map<String, List<String>> options = new HashMap<String, List<String>>();
661
		String lucene = translator.toLucene(cqlQuery, options);
662
		printQuery(cqlQuery, lucene);
663
	}
664

  
665
	@Test
666
	public void testNonDateQuery() throws CQLParseException, IOException {
667
		String cqlQuery = "(resultdateofacceptance <= 2012.03.15T00:00:00Z)";
668
		Map<String, List<String>> options = new HashMap<String, List<String>>();
669
		String lucene = translator.toLucene(cqlQuery, options);
670
		printQuery(cqlQuery, lucene);
671
	}
672

  
673
	@Test
674
	public void testNonDateQuery2() throws CQLParseException, IOException {
675
		String cqlQuery = "(resultdateofacceptance <= ciao)";
676
		Map<String, List<String>> options = new HashMap<String, List<String>>();
677
		String lucene = translator.toLucene(cqlQuery, options);
678
		printQuery(cqlQuery, lucene);
679
	}
680

  
681
	@Test
682
	public void testDateWrong() throws Exception {
683

  
684
		String cqlQuery = "publicationdate =/within \"2000-01-01 2010.99.01\"";
685
		String luceneQuery = translator.toLucene(cqlQuery);
686

  
687
		printQuery(cqlQuery, luceneQuery);
688
	}
689

  
690
	@Test
691
	public void testToDate() throws Exception {
692

  
693
		String cqlQuery = "publicationdate =/within \"* 2000-01-01\"";
694
		String luceneQuery = translator.toLucene(cqlQuery);
695

  
696
		printQuery(cqlQuery, luceneQuery);
697
	}
698

  
699
	private void printQuery(final String cql, final String lucene) throws CQLParseException, IOException {
700
		System.out.println("CQL:    " + cql);
701
		// System.out.println("PARSED: " + new CQLParser().parse(cql).toCQL());
702
		System.out.println("LUCENE: " + lucene + "\n");
703

  
704
	}
705

  
706
}
modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/src/test/resources/log4j.properties
1
org.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger
2

  
3
log4j.rootLogger=WARN, CONSOLE
4
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
5
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
6
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
7

  
8
log4j.logger.eu.dnetlib=DEBUG
modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/src/main/java/eu/dnetlib/functionality/cql/mongo/MongoCqlTranslator.java
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.lang.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
}
modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/src/main/java/eu/dnetlib/functionality/cql/CqlUtils.java
1
package eu.dnetlib.functionality.cql;
2

  
3
import java.io.IOException;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.Set;
7

  
8
import org.z3950.zing.cql.CQLNode;
9
import org.z3950.zing.cql.CQLOrNode;
10
import org.z3950.zing.cql.CQLParseException;
11
import org.z3950.zing.cql.CQLParser;
12
import org.z3950.zing.cql.ModifierSet;
13

  
14
import com.google.common.base.Predicates;
15
import com.google.common.collect.Lists;
16
import com.google.common.collect.Maps;
17

  
18
public class CqlUtils {
19

  
20
	public static final String cqlDefaultField = "cql.serverChoice";
21
	
22
	private CqlUtils() {}
23

  
24
	public static CQLNode expand(final String query, Set<String> fields) {
25
		return expand(parse(query), fields);
26
	}
27

  
28
	public static CQLNode expand(CQLNode root, Set<String> fields) {
29

  
30
		if (fields.isEmpty()) {
31
			return root;
32
		}
33

  
34
		Map<String, CQLNode> unfielded = group(filter(root, listFields(root)), Lists.newArrayList(CqlUtils.cqlDefaultField));
35
		CQLNode terms = unfielded.get(CqlGroup.defaultTerm);
36

  
37
		if (terms != null) {
38
			CQLNode expand = new CQLExpander().expand(terms, fields);
39
			return new CQLOrNode(root, expand, new ModifierSet("or"));
40
		}
41

  
42
		return root;
43
	}
44

  
45
	public static List<String> listFields(final String query) {
46
		return listFields(parse(query));
47
	}
48

  
49
	public static List<String> listFields(CQLNode root) {
50
		Set<String> fields = new CQLFieldLister().listFields(root);
51
		fields.remove(cqlDefaultField);
52
		return Lists.newArrayList(fields);
53
	}
54

  
55
	public static List<String> listTerms(final String query, final String field) {
56
		return listTerms(parse(query), field);
57
	}
58

  
59
	public static List<String> listTerms(final CQLNode query, final String field) {
60
		return new CqlTermLister().listTerms(group(query, Lists.newArrayList(field)).get(field), field);
61
	}
62

  
63
	public static Map<String, CQLNode> group(final CQLNode root, final List<String> fields) {
64
		Map<String, CQLNode> groups = new CqlGroup().group(root, fields);
65
		groups.put(CqlGroup.defaultTerm, new CqlFilter().filter(root, fields));
66
		return Maps.filterValues(groups, Predicates.notNull());
67
	}
68

  
69
	public static Map<String, CQLNode> group(final String query, final List<String> fields) {
70
		return group(parse(query), fields);
71
	}
72

  
73
	public static CQLNode filter(final CQLNode query, final List<String> fields) {
74
		return new CqlFilter().filter(query, fields);
75
	}
76

  
77
	public static CQLNode filter(final String query, final List<String> fields) {
78
		return filter(parse(query), fields);
79
	}
80

  
81
	public static CQLNode parse(final String query) {
82
		try {
83
			return query != null ? new CQLParser().parse(query) : null;
84
		} catch (CQLParseException e) {
85
			throw new RuntimeException(e);
86
		} catch (IOException e) {
87
			throw new RuntimeException(e);
88
		}
89
	}
90

  
91
}
modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/src/main/java/eu/dnetlib/functionality/cql/CqlFilter.java
1
package eu.dnetlib.functionality.cql;
2

  
3
import java.util.List;
4

  
5
import org.z3950.zing.cql.CQLAndNode;
6
import org.z3950.zing.cql.CQLBooleanNode;
7
import org.z3950.zing.cql.CQLNode;
8
import org.z3950.zing.cql.CQLNotNode;
9
import org.z3950.zing.cql.CQLOrNode;
10
import org.z3950.zing.cql.CQLPrefixNode;
11
import org.z3950.zing.cql.CQLTermNode;
12

  
13
public class CqlFilter {
14

  
15
	public CQLNode filter(final CQLNode node, final List<String> fields) {
16
		return doFilter(node, fields);
17
	}
18

  
19
	private CQLNode doFilter(CQLNode node, List<String> fields) {
20

  
21
		if (node instanceof CQLBooleanNode) {
22
			return doFilter((CQLBooleanNode) node, fields);
23
		}
24

  
25
		if (node instanceof CQLTermNode) {
26
			return doFilter((CQLTermNode) node, fields);
27
		}
28
		
29
		if (node instanceof CQLPrefixNode) {
30
			return node;
31
		}		
32

  
33
		if (node == null) {
34
			return null;
35
		}
36

  
37
		throw new RuntimeException("error choice");
38
	}
39

  
40
	private CQLNode doFilter(CQLBooleanNode node, List<String> fields) {
41

  
42
		CQLNode left = doFilter(node.left, fields);
43
		CQLNode right = doFilter(node.right, fields);
44

  
45
		if (left == null && right == null) {
46
			return null;
47
		}
48

  
49
		if (left == null) {
50
			return right;
51
		}
52

  
53
		if (right == null) {
54
			return left;
55
		}
56

  
57
		if (node instanceof CQLAndNode) {
58
			return new CQLAndNode(left, right, node.ms);
59
		}
60

  
61
		if (node instanceof CQLOrNode) {
62
			return new CQLOrNode(left, right, node.ms);
63
		}
64

  
65
		if (node instanceof CQLNotNode) {
66
			return new CQLNotNode(left, right, node.ms);
67
		}
68

  
69
		throw new RuntimeException("unknow boolean node");
70
	}
71

  
72
	private static CQLNode doFilter(CQLTermNode node, List<String> fields) {
73
		return isTermNodeToFilter(node, fields) ? null : node;
74
	}
75

  
76
	private static boolean isTermNodeToFilter(CQLTermNode node, List<String> fields) {
77
		return fields.contains(node.getIndex().toLowerCase());
78
	}
79

  
80
}
modules/cnr-cql-utils/tags/cnr-cql-utils-2.1.1/src/main/java/eu/dnetlib/functionality/cql/CQLExpander.java
1
package eu.dnetlib.functionality.cql;
2

  
3
import java.util.Set;
4

  
5
import org.apache.commons.lang.StringUtils;
6
import org.z3950.zing.cql.CQLBooleanNode;
7
import org.z3950.zing.cql.CQLNode;
8
import org.z3950.zing.cql.CQLOrNode;
9
import org.z3950.zing.cql.CQLParser;
10
import org.z3950.zing.cql.CQLTermNode;
11
import org.z3950.zing.cql.ModifierSet;
12

  
13
public class CQLExpander {
14

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff