1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.api;
17
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Locale;
21 import java.util.Map;
22 import java.util.stream.Collectors;
23
24 import org.codelibs.fess.entity.SearchRenderData;
25 import org.codelibs.fess.mylasta.action.FessMessages;
26 import org.codelibs.fess.util.ComponentUtil;
27 import org.codelibs.fess.util.FacetResponse;
28 import org.lastaflute.web.util.LaRequestUtil;
29 import org.lastaflute.web.validation.VaMessenger;
30
31 public class ApiResult {
32
33 protected ApiResponse response = null;
34
35 public ApiResult(final ApiResponse response) {
36 this.response = response;
37 }
38
39 public enum Status {
40 OK(0), BAD_REQUEST(1), SYSTEM_ERROR(2), UNAUTHORIZED(3);
41 private final int id;
42
43 private Status(final int id) {
44 this.id = id;
45 }
46
47 public int getId() {
48 return id;
49 }
50 }
51
52 public static class ApiResponse {
53 protected String version = ComponentUtil.getSystemHelper().getProductVersion();
54 protected int status;
55
56 public ApiResponse status(final Status status) {
57 this.status = status.getId();
58 return this;
59 }
60
61 public ApiResult result() {
62 return new ApiResult(this);
63 }
64 }
65
66 public static class ApiUpdateResponse extends ApiResponse {
67 protected String id;
68 protected boolean created;
69
70 public ApiUpdateResponse id(final String id) {
71 this.id = id;
72 return this;
73 }
74
75 public ApiUpdateResponse created(final boolean created) {
76 this.created = created;
77 return this;
78 }
79
80 @Override
81 public ApiResult result() {
82 return new ApiResult(this);
83 }
84 }
85
86 public static class ApiDeleteResponse extends ApiResponse {
87 protected long count = 1;
88
89 public ApiDeleteResponse count(final long count) {
90 this.count = count;
91 return this;
92 }
93
94 @Override
95 public ApiResult result() {
96 return new ApiResult(this);
97 }
98 }
99
100 public static class ApiConfigResponse extends ApiResponse {
101 protected Object setting;
102
103 public ApiConfigResponse setting(final Object setting) {
104 this.setting = setting;
105 return this;
106 }
107
108 @Override
109 public ApiResult result() {
110 return new ApiResult(this);
111 }
112 }
113
114 public static class ApiConfigsResponse<T> extends ApiResponse {
115 protected List<T> settings;
116 protected long total = 0;
117
118 public ApiConfigsResponse<T> settings(final List<T> settings) {
119 this.settings = settings;
120 this.total = settings.size();
121 return this;
122 }
123
124 public ApiConfigsResponse<T> total(final long total) {
125 this.total = total;
126 return this;
127 }
128
129 @Override
130 public ApiResult result() {
131 return new ApiResult(this);
132 }
133 }
134
135 public static class ApiDocResponse extends ApiResponse {
136 protected Object doc;
137
138 public ApiDocResponse doc(final Object doc) {
139 this.doc = doc;
140 return this;
141 }
142
143 @Override
144 public ApiResult result() {
145 return new ApiResult(this);
146 }
147 }
148
149 public static class ApiDocsResponse extends ApiResponse {
150 protected String queryId;
151 protected List<Map<String, Object>> docs;
152 protected String highlightParams;
153 protected String execTime;
154 protected int pageSize;
155 protected int pageNumber;
156 protected long recordCount;
157 protected String recordCountRelation;
158 protected int pageCount;
159 protected boolean nextPage;
160 protected boolean prevPage;
161 protected long startRecordNumber;
162 protected long endRecordNumber;
163 protected List<String> pageNumbers;
164 protected boolean partial;
165 protected long queryTime;
166 protected String searchQuery;
167 protected long requestedTime;
168 protected List<Map<String, Object>> facetField;
169 protected List<Map<String, Object>> facetQuery;
170
171 public ApiDocsResponse renderData(final SearchRenderData data) {
172 queryId = data.getQueryId();
173 docs = data.getDocumentItems();
174 highlightParams = data.getAppendHighlightParams();
175 execTime = data.getExecTime();
176 pageSize = data.getPageSize();
177 pageNumber = data.getCurrentPageNumber();
178 recordCount = data.getAllRecordCount();
179 recordCountRelation = data.getAllRecordCountRelation();
180 pageCount = data.getAllPageCount();
181 nextPage = data.isExistNextPage();
182 prevPage = data.isExistPrevPage();
183 startRecordNumber = data.getCurrentStartRecordNumber();
184 endRecordNumber = data.getCurrentEndRecordNumber();
185 pageNumbers = data.getPageNumberList();
186 partial = data.isPartialResults();
187 queryTime = data.getQueryTime();
188 searchQuery = data.getSearchQuery();
189 requestedTime = data.getRequestedTime();
190 final FacetResponse facetResponse = data.getFacetResponse();
191 if (facetResponse != null && facetResponse.hasFacetResponse()) {
192
193 if (facetResponse.getFieldList() != null) {
194 facetField = facetResponse.getFieldList().stream().map(field -> {
195 final Map<String, Object> fieldMap = new HashMap<>(2, 1f);
196 fieldMap.put("name", field.getName());
197 fieldMap.put("result", field.getValueCountMap().entrySet().stream().map(e -> {
198 final Map<String, Object> valueCount = new HashMap<>(2, 1f);
199 valueCount.put("value", e.getKey());
200 valueCount.put("count", e.getValue());
201 return valueCount;
202 }).collect(Collectors.toList()));
203 return fieldMap;
204 }).collect(Collectors.toList());
205 }
206
207 if (facetResponse.getQueryCountMap() != null) {
208 facetQuery = facetResponse.getQueryCountMap().entrySet().stream().map(e -> {
209 final Map<String, Object> valueCount = new HashMap<>(2, 1f);
210 valueCount.put("value", e.getKey());
211 valueCount.put("count", e.getValue());
212 return valueCount;
213 }).collect(Collectors.toList());
214
215 }
216 }
217 return this;
218 }
219
220 @Override
221 public ApiResult result() {
222 return new ApiResult(this);
223 }
224 }
225
226 public static class ApiLogResponse extends ApiResponse {
227 protected Object log;
228
229 public ApiLogResponse log(final Object log) {
230 this.log = log;
231 return this;
232 }
233
234 @Override
235 public ApiResult result() {
236 return new ApiResult(this);
237 }
238 }
239
240 public static class ApiLogsResponse<T> extends ApiResponse {
241 protected List<T> logs;
242 protected long total = 0;
243
244 public ApiLogsResponse<T> logs(final List<T> logs) {
245 this.logs = logs;
246 return this;
247 }
248
249 public ApiLogsResponse<T> total(final long total) {
250 this.total = total;
251 return this;
252 }
253
254 @Override
255 public ApiResult result() {
256 return new ApiResult(this);
257 }
258 }
259
260 public static class ApiLogFilesResponse extends ApiResponse {
261 protected List<Map<String, Object>> files;
262 protected long total = 0;
263
264 public ApiLogFilesResponse files(final List<Map<String, Object>> files) {
265 this.files = files;
266 return this;
267 }
268
269 public ApiLogFilesResponse total(final long total) {
270 this.total = total;
271 return this;
272 }
273
274 @Override
275 public ApiResult result() {
276 return new ApiResult(this);
277 }
278 }
279
280 public static class ApiBackupFilesResponse extends ApiResponse {
281 protected List<Map<String, String>> files;
282 protected long total = 0;
283
284 public ApiBackupFilesResponse files(final List<Map<String, String>> files) {
285 this.files = files;
286 return this;
287 }
288
289 public ApiBackupFilesResponse total(final long total) {
290 this.total = total;
291 return this;
292 }
293
294 @Override
295 public ApiResult result() {
296 return new ApiResult(this);
297 }
298 }
299
300 public static class ApiSystemInfoResponse extends ApiResponse {
301 protected List<Map<String, String>> envProps;
302 protected List<Map<String, String>> systemProps;
303 protected List<Map<String, String>> fessProps;
304 protected List<Map<String, String>> bugReportProps;
305
306 public ApiSystemInfoResponse envProps(final List<Map<String, String>> envProps) {
307 this.envProps = envProps;
308 return this;
309 }
310
311 public ApiSystemInfoResponse systemProps(final List<Map<String, String>> systemProps) {
312 this.systemProps = systemProps;
313 return this;
314 }
315
316 public ApiSystemInfoResponse fessProps(final List<Map<String, String>> fessProps) {
317 this.fessProps = fessProps;
318 return this;
319 }
320
321 public ApiSystemInfoResponse bugReportProps(final List<Map<String, String>> bugReportProps) {
322 this.bugReportProps = bugReportProps;
323 return this;
324 }
325
326 @Override
327 public ApiResult result() {
328 return new ApiResult(this);
329 }
330 }
331
332 public static class ApiErrorResponse extends ApiResponse {
333 protected String message;
334
335 public ApiErrorResponse message(final String message) {
336 this.message = message;
337 return this;
338 }
339
340 public ApiErrorResponse message(final VaMessenger<FessMessages> validationMessagesLambda) {
341 final FessMessageson/FessMessages.html#FessMessages">FessMessages messages = new FessMessages();
342 validationMessagesLambda.message(messages);
343 message =
344 ComponentUtil.getMessageManager()
345 .toMessageList(LaRequestUtil.getOptionalRequest().map(r -> r.getLocale()).orElse(Locale.ENGLISH), messages)
346 .stream().collect(Collectors.joining(" "));
347 return this;
348 }
349 }
350 }