This repository was archived by the owner on Jan 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathRecipeSearch.java
More file actions
395 lines (353 loc) · 11.9 KB
/
Copy pathRecipeSearch.java
File metadata and controls
395 lines (353 loc) · 11.9 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* Copyright (c) 2006 Google Inc.
*
* 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 sample.gbase.recipe;
import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.api.gbase.client.GoogleBaseService;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* A recipe search.
*
* There will be such an object in all cases, even
* if no query has been run. This object is created
* by RecipeSearchServlet and displayed by the JSP.
*/
public class RecipeSearch {
private final GoogleBaseService service;
private Set<String> mainIngredient;
private Set<String> cuisine;
private Integer cookingTime;
private String query;
/** The query string, with the unsupported characters replaced with spaces.*/
private String queryClean;
/** The index of the first retrieved recipe. */
private int startIndex = 0;
/** Set to true to perform the search on the user's items. */
private boolean ownItems;
/** Total number of results, -1 means the query hasn't been run yet. */
protected int total = -1;
protected List<Recipe> recipes;
private static final int DEFAULT_MAX_RESULTS = 10;
private int maxResults = DEFAULT_MAX_RESULTS;
private final FeedURLFactory urlFactory;
/**
* Create a new search.
*
* @param service Google data API service
* @param urlFactory feed URL factory to be used when creating a Query
* @param ownItems true to show only the items of the authenticated user
*/
public RecipeSearch(GoogleBaseService service,
FeedURLFactory urlFactory,
boolean ownItems) {
this.service = service;
this.urlFactory = urlFactory;
this.ownItems = ownItems;
mainIngredient = null;
cuisine = null;
cookingTime = null;
query = null;
queryClean = null;
}
/**
* Checks whether the search has been run and there is at least one result.
*
* @return true if the search has been run and there is at least one result
*/
public boolean hasResults() {
return recipes != null && ! recipes.isEmpty();
}
/**
* Gets the result of the search, if it has been run.
*
* @return a list of Recipe which might be empty if the
* search has been run, or null if the search has not
* been run yet
*/
public List<Recipe> getRecipes() {
return recipes;
}
/**
* Checks if the search will be done for the authenticated user's items only.
*
* @return true if the search returns only the items that belong to the
* authenticated user
*/
public boolean isOwnItems() {
return ownItems;
}
/**
* Specifies if we are searching the authenticated user's items.
*
* @param ownItems true to search only the authenticated user's items
*/
public void setOwnItems(boolean ownItems) {
this.ownItems = ownItems;
}
/** Gets the current main ingredients, or null. */
public Set<String> getMainIngredientValues() {
return mainIngredient;
}
/** Sets the main ingredient. */
public void setMainIngredientValues(String[] mainIngredient) {
this.mainIngredient = RecipeUtil.validateValues(mainIngredient);
}
/** Gets the current cuisines, or null. */
public Set<String> getCuisineValues() {
return cuisine;
}
/** Sets the cuisine. */
public void setCuisineValues(String[] cuisine) {
this.cuisine = RecipeUtil.validateValues(cuisine);
}
/** Gets the current (maximum) cooking time, or null. */
public Integer getCookingTime() {
return cookingTime;
}
/** Sets the current maximum cooking time. */
public void setCookingTime(Integer cookingTime) {
this.cookingTime = cookingTime;
}
/** Gets the current page length. */
public int getMaxResults() {
return maxResults;
}
/** Sets the page length. */
public void setMaxResults(int maxResults) {
this.maxResults = maxResults;
}
/**
* Gets the total number of recipes that matched the query, which
* might be larger than the page length.
*
* @return the total, or -1 if the total is unknown, either because
* the query has not been run or because the total was not in
* the result
*/
public int getTotal() {
return total;
}
/**
* Gets the index of the first result to return.
*
* @return a positive value
*/
public int getStartIndex() {
return startIndex;
}
/**
* Sets the index of the first result to return.
* @param startIndex a positive value
*/
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
/** Gets the current page. */
public int getCurrentPage() {
return startIndex / maxResults;
}
/**
* Gets a description of the retrieved (current) interval, showing
* the index of the first item and the index of the last item.
*
* @return short description of the current page interval
*/
public String getCurrentPageInterval() {
return "" + (startIndex + 1) + " - " +
Math.min(startIndex + maxResults, total);
}
/** Gets the number of pages needed to contain all the results. */
public int getTotalPages() {
if (total == 0) {
return 0;
}
/* Using total-1, because if we have 10 results and 10 items per page,
* we still want one page.
*/
return (total - 1) / maxResults;
}
/** Runs the query and fills the result. */
public void runQuery() throws IOException, ServiceException {
GoogleBaseQuery query = createQuery();
System.out.println("Searching: " + query.getUrl());
GoogleBaseFeed feed = service.query(query);
List<Recipe> result = new ArrayList<Recipe>(maxResults);
for (GoogleBaseEntry entry : feed.getEntries()) {
result.add(new Recipe(entry));
}
this.recipes = result;
total = feed.getTotalResults();
}
/**
* Creates a GoogleBaseQuery that searches for recipes, according to
* the various properties of the RecipeSearch.
*
* @return a query to be used for querying with a
* {@link com.google.api.gbase.client.GoogleBaseService
* GoogleBaseService}
* @see com.google.api.gbase.client.GoogleBaseService#query(com.google.gdata.client.Query)
*/
private GoogleBaseQuery createQuery() {
URL queryUrl;
if (ownItems) {
queryUrl = urlFactory.getItemsFeedURL();
} else {
queryUrl = urlFactory.getSnippetsFeedURL();
}
GoogleBaseQuery query = new GoogleBaseQuery(queryUrl);
query.setMaxResults(maxResults);
if (startIndex > 0) {
// the first index is 1
query.setStartIndex(startIndex + 1);
}
query.setGoogleBaseQuery(createQueryString());
return query;
}
/**
* Creates a full text query out of the values of the query, mainIngredient,
* cuisine and cookingTime.
*
* @return a query to be used for setting the full text query of a
* {@link com.google.api.gbase.client.GoogleBaseQuery}
* @see com.google.api.gbase.client.GoogleBaseQuery#setFullTextQuery(String)
*/
private String createQueryString() {
StringBuffer retval = new StringBuffer(RecipeUtil.RECIPE_ITEMTYPE_QUERY);
if (queryClean != null) {
retval.append(queryClean);
}
appendAttributeCondition(retval, "main ingredient", mainIngredient, true);
appendAttributeCondition(retval, "cuisine", cuisine, false);
if (cookingTime != null) {
Collection<String> cookingTimes = new ArrayList<String>();
cookingTimes.add("0.." + cookingTime + " min");
cookingTimes.add("0.." + cookingTime + " minutes");
appendAttributeCondition(retval, "cooking time", cookingTimes, false);
}
return retval.toString();
}
/**
* Appends a filtering condition to a full text query.
* It is composed by simple [name: value] conditions
* joined by and AND or an OR operation.
*
* @param sb a StringBuffer for creating a full text query
* @param name name of the attributes
* @param values values the attributes have to match
* @param isAnd true if the attributes have to match all the values,
* false if the attributes have to match at least one value
*/
private static void appendAttributeCondition(StringBuffer sb,
String name,
Collection<String> values,
boolean isAnd) {
if (values != null && !values.isEmpty()) {
sb.append(" (");
Iterator iter = values.iterator();
while (iter.hasNext()) {
sb.append("[").append(name).append(": ").append(iter.next()).append("]");
if (iter.hasNext()) {
sb.append(isAnd ? " " : "|");
}
}
sb.append(")");
}
}
/** Returns true when the current page is not the first page. */
public boolean hasPreviousPage() {
return getCurrentPage() > 0;
}
/** Returns true when the current page is not the last one. */
public boolean hasNextPage() {
return getTotalPages() > getCurrentPage();
}
/** Gets the expected number of recipes for the next page. */
public int getNextPageSize() {
return Math.min(maxResults, total - (getCurrentPage() + 1) * maxResults);
}
/** Gets a description of the query used in the search. */
public StringBuffer getFilterDescription() {
StringBuffer retval = new StringBuffer();
if (queryClean != null && ! "".equals(queryClean)) {
retval.append("<b>keywords</b> are <b>").
append(queryClean).
append("</b> ");
}
addCollectionDescription(retval, cuisine, "cuisine", false);
addCollectionDescription(retval, mainIngredient, "main ingredient", true);
if (cookingTime != null) {
if (retval.length() > 0) {
retval.append("and ");
}
retval.append("<b>cooking time</b> is under <b>").
append(cookingTime).
append(" ").
append(RecipeUtil.COOKING_TIME_UNIT).
append("</b> ");
}
if (retval.length() > 0) {
retval.insert(0, "where ");
}
return retval;
}
private static void addCollectionDescription(StringBuffer buffer,
Collection<String> collection,
String name,
boolean isAnd) {
if (collection != null && ! collection.isEmpty()) {
if (buffer.length() > 0) {
buffer.append("and ");
}
buffer.append("<b>").append(name).append("</b> is");
Iterator<String> iter = collection.iterator();
while (iter.hasNext()) {
buffer.append(" <b>").append(iter.next()).append("</b> ");
if (iter.hasNext()) {
buffer.append(isAnd ? "and " : "or ");
}
}
}
}
/**
* Sets the query string.
*
* @param query the query string, as provided by user
* @throws NullPointerException if the {@code query} is null.
*/
public void setQuery(String query) {
if (query != null) {
this.query = query;
this.queryClean = RecipeUtil.cleanQueryString(query);
} else {
throw new NullPointerException("Query must not be null.");
}
}
/**
* Returns the original query string, as specified in the
* {@link #setQuery(String)} method.
*/
public String getQuery() {
return query;
}
}