-
Notifications
You must be signed in to change notification settings - Fork 234
/
LuceneQuery.java
173 lines (150 loc) · 5 KB
/
LuceneQuery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
* * Copyright 2012 Impetus Infotech.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
******************************************************************************/
package com.impetus.kundera.query;
import java.util.List;
import java.util.Map;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.impetus.kundera.Constants;
import com.impetus.kundera.client.Client;
import com.impetus.kundera.client.ClientBase;
import com.impetus.kundera.metadata.MetadataBuilder;
import com.impetus.kundera.metadata.model.EntityMetadata;
import com.impetus.kundera.persistence.EntityReader;
import com.impetus.kundera.persistence.PersistenceDelegator;
/**
* The Class LuceneQuery.
*
* @author animesh.kumar
*/
public class LuceneQuery extends QueryImpl implements Query
{
/** the log used by this class. */
private static Log log = LogFactory.getLog(MetadataBuilder.class);
/** The max result. */
int maxResult = Constants.INVALID;
/** The lucene query. */
String luceneQuery;
/**
* Instantiates a new lucene query.
*
* @param jpaQuery
* the jpa query
* @param kunderaQuery
* the kundera query
* @param pd
* the pd
* @param persistenceUnits
* the persistence units
*/
public LuceneQuery(String jpaQuery, KunderaQuery kunderaQuery, PersistenceDelegator pd)
{
super(jpaQuery, pd);
this.kunderaQuery = kunderaQuery;
}
/**
* Sets the lucene query.
*
* @param luceneQuery
* the new lucene query
*/
public void setLuceneQuery(String luceneQuery)
{
this.luceneQuery = luceneQuery;
}
// @see com.impetus.kundera.query.QueryImpl#getResultList()
/*
* (non-Javadoc)
*
* @see com.impetus.kundera.query.QueryImpl#getResultList()
*/
@Override
public List<?> getResultList()
{
if(log.isDebugEnabled())
log.debug("JPA Query: " + query);
// get luence query
String q = luceneQuery;
if (null == q)
{
q = getLuceneQueryFromJPAQuery();
}
if(log.isDebugEnabled())
log.debug("Lucene Query: " + q);
EntityMetadata m = kunderaQuery.getEntityMetadata();
Client client = persistenceDelegeator.getClient(m);
Map<String, Object> searchFilter = ((ClientBase) client).getIndexManager().search(q, -1, maxResult);
if (kunderaQuery.isAliasOnly())
{
String[] primaryKeys = searchFilter.values().toArray(new String[] {});
return persistenceDelegeator.find(m.getEntityClazz(), primaryKeys);
}
else
{
return persistenceDelegeator.find(m.getEntityClazz(), searchFilter);
}
}
// @see com.impetus.kundera.query.QueryImpl#setMaxResults(int)
/*
* (non-Javadoc)
*
* @see com.impetus.kundera.query.QueryImpl#setMaxResults(int)
*/
@Override
public Query setMaxResults(int maxResult)
{
this.maxResult = maxResult;
return this;
}
/*
* (non-Javadoc)
*
* @see
* com.impetus.kundera.query.QueryImpl#populateEntities(com.impetus.kundera
* .metadata.model.EntityMetadata, com.impetus.kundera.client.Client)
*/
@Override
protected List<Object> populateEntities(EntityMetadata m, Client client)
{
throw new UnsupportedOperationException("Method not supported for default indexing");
}
@Override
protected EntityReader getReader()
{
throw new UnsupportedOperationException("Method not supported for default indexing");
}
@Override
protected List<Object> recursivelyPopulateEntities(EntityMetadata m, Client client)
{
throw new UnsupportedOperationException("Method not supported for Lucene indexing");
}
/*
* (non-Javadoc)
*
* @see com.impetus.kundera.query.QueryImpl#onExecuteUpdate()
*/
@Override
protected int onExecuteUpdate()
{
if (kunderaQuery.isDeleteUpdate())
{
List result = getResultList();
return result != null ? result.size() : 0;
}
return 0;
}
}