My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package in.partake.controller.api.event;

import in.partake.controller.api.PartakeAPIActionSupport;
import in.partake.model.dao.DAOException;
import in.partake.model.dto.Event;
import in.partake.model.dto.auxiliary.EventCategory;
import in.partake.resource.UserErrorCode;
import in.partake.service.EventService;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.queryParser.ParseException;

// search API can take
// 1) query (String)
// 2) category (String)
// 3) beforeDeadlineOnly (Boolean)
// 4) sortOrder (String)
// 5) maxNum (integer)

public class SearchAction extends PartakeAPIActionSupport {
private static final long serialVersionUID = 1L;
// private static final Logger logger = Logger.getLogger(SearchAction.class);
private static final String DEFAULT_CATEGORY = EventCategory.getAllEventCategory();
private static final String DEFAULT_BEFORE_DEADLINE_ONLY = "true";
private static final String DEFAULT_SORT_ORDER = "score";
private static final int DEFAULT_MAX_NUM = 10;

public static final int MAX_NUM = 100;

public String search() throws DAOException {
String query = getQuery();

String category = getCategory();
if (category == null) { return renderInvalid(UserErrorCode.MISSING_SEARCH_CATEGORY); }

String sortOrder = getSortOrder();
if (sortOrder == null) { return renderInvalid(UserErrorCode.MISSING_SEARCH_ORDER); }

final String beforeDeadlineOnly;
final int maxNum;
try {
beforeDeadlineOnly = getBeforeDeadlineOnly();
maxNum = getMaxNum();
} catch (IllegalRequestException e) {
return renderInvalid(e.getErrorCode());
}

try {
List<Event> events = EventService.get().search(query, category, sortOrder, Boolean.parseBoolean(beforeDeadlineOnly), maxNum);

JSONArray jsonEventsArray = new JSONArray();
for (Event event : events) {
jsonEventsArray.add(event.toSafeJSON());
}
JSONObject obj = new JSONObject();
obj.put("events", jsonEventsArray);
return renderOK(obj);
} catch (IllegalArgumentException e) {
return renderInvalid(UserErrorCode.INVALID_SEARCH_QUERY);
} catch (ParseException e) {
return renderInvalid(UserErrorCode.INVALID_SEARCH_QUERY);
}


}

private String getQuery() {
String query = getParameter("query");
return StringUtils.trimToEmpty(query);
}

private String getCategory() {
String category = getParameter("category");
if (category == null) { return DEFAULT_CATEGORY; }

category = category.trim();
if (EventCategory.getAllEventCategory().equals(category) || EventCategory.isValidCategoryName(category)) {
return category;
} else {
return null;
}
}

private String getBeforeDeadlineOnly() throws IllegalRequestException {
String beforeDeadlineOnly = getParameter("beforeDeadlineOnly");
if (beforeDeadlineOnly == null) { return DEFAULT_BEFORE_DEADLINE_ONLY; }

if ("true".equalsIgnoreCase(beforeDeadlineOnly)) {
return "true";
}
if ("false".equalsIgnoreCase(beforeDeadlineOnly)) {
return "false";
}

throw new IllegalRequestException(UserErrorCode.INVALID_SEARCH_DEADLINE);
}

private String getSortOrder() {
String sortOrder = getParameter("sortOrder");
if (sortOrder == null) { return DEFAULT_SORT_ORDER; }

sortOrder = sortOrder.trim();
if ("score".equalsIgnoreCase(sortOrder)) { return "score"; }
if ("createdAt".equalsIgnoreCase(sortOrder)) { return "createdAt"; }
if ("deadline".equalsIgnoreCase(sortOrder)) { return "deadline"; }
if ("deadline-r".equalsIgnoreCase(sortOrder)) { return "deadline-r"; }
if ("beginDate".equalsIgnoreCase(sortOrder)) { return "beginDate"; }
if ("beginDate-r".equalsIgnoreCase(sortOrder)) { return "beginDate-r"; }

return null;
}

private int getMaxNum() throws IllegalRequestException {
String maxNum = getParameter("maxNum");
if (maxNum == null) {
return DEFAULT_MAX_NUM;
}

try {
int v = Integer.parseInt(StringUtils.trim(maxNum));
if (v <= 0 || MAX_NUM < v) {
throw new IllegalRequestException(UserErrorCode.INVALID_SEARCH_MAXNUM);
}

return v;
} catch (NumberFormatException e) {
throw new IllegalRequestException(UserErrorCode.INVALID_SEARCH_MAXNUM);
}
}

// 他のActionクラスでも使うようならcontroller.apiパッケージに移動することも検討
private static class IllegalRequestException extends Exception {
private static final long serialVersionUID = -2150899144288175828L;
private final UserErrorCode errorCode;
IllegalRequestException(UserErrorCode errorCode) {
if (errorCode == null) {
throw new IllegalArgumentException();
}
this.errorCode = errorCode;
}
UserErrorCode getErrorCode() {
return this.errorCode;
}
}
}

Change log

r670 by ma...@mayah.jp on Jan 23, 2012   Diff
Adding Postgres9Entities.
Go to: 
Project members, sign in to write a code review

Older revisions

r631 by ma...@mayah.jp on Jan 6, 2012   Diff
SearchActionTest: added Japanese query
tests.
r600 by skypencil on Aug 14, 2011   Diff
Event検索APIでmaxNumを省略可能に変更
r596 by skypencil on Aug 14, 2011   Diff
refactoring. we should avoid to use
literal.
All revisions of this file

File info

Size: 5401 bytes, 150 lines
Powered by Google Project Hosting