1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.app.web.api.admin.keymatch;
17
18 import static org.codelibs.fess.app.web.admin.keymatch.AdminKeymatchAction.getKeyMatch;
19
20 import java.util.List;
21 import java.util.stream.Collectors;
22
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.codelibs.fess.app.pager.KeyMatchPager;
26 import org.codelibs.fess.app.service.KeyMatchService;
27 import org.codelibs.fess.app.web.CrudMode;
28 import org.codelibs.fess.app.web.api.ApiResult;
29 import org.codelibs.fess.app.web.api.ApiResult.ApiConfigResponse;
30 import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
31 import org.codelibs.fess.app.web.api.ApiResult.ApiUpdateResponse;
32 import org.codelibs.fess.app.web.api.ApiResult.Status;
33 import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
34 import org.codelibs.fess.opensearch.config.exentity.KeyMatch;
35 import org.lastaflute.web.Execute;
36 import org.lastaflute.web.response.JsonResponse;
37
38 import jakarta.annotation.Resource;
39
40
41
42
43
44 public class ApiAdminKeymatchAction extends FessApiAdminAction {
45
46
47 private static final Logger logger = LogManager.getLogger(ApiAdminKeymatchAction.class);
48
49
50
51
52
53
54
55 public ApiAdminKeymatchAction() {
56 super();
57 }
58
59
60
61
62
63 @Resource
64 private KeyMatchService keyMatchService;
65
66
67
68
69
70
71
72
73
74
75
76
77
78 @Execute
79 public JsonResponse<ApiResult> settings(final SearchBody body) {
80 validateApi(body, messages -> {});
81 final KeyMatchPager pager = copyBeanToNewBean(body, KeyMatchPager.class);
82 final List<KeyMatch> list = keyMatchService.getKeyMatchList(pager);
83 return asJson(
84 new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
85 .total(pager.getAllRecordCount())
86 .status(ApiResult.Status.OK)
87 .result());
88 }
89
90
91
92
93
94
95
96
97 @Execute
98 public JsonResponse<ApiResult> get$setting(final String id) {
99 return asJson(new ApiConfigResponse().setting(keyMatchService.getKeyMatch(id).map(this::createEditBody).orElseGet(() -> {
100 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
101 return null;
102 })).status(Status.OK).result());
103 }
104
105
106
107
108
109
110
111
112 @Execute
113 public JsonResponse<ApiResult> post$setting(final CreateBody body) {
114 validateApi(body, messages -> {});
115 body.crudMode = CrudMode.CREATE;
116 final KeyMatch keyMatch = getKeyMatch(body).map(entity -> {
117 try {
118 keyMatchService.store(entity);
119 } catch (final Exception e) {
120 logger.warn("Failed to process a request.", e);
121 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
122 }
123 return entity;
124 }).orElseGet(() -> {
125 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
126 return null;
127 });
128
129 return asJson(new ApiUpdateResponse().id(keyMatch.getId()).created(true).status(Status.OK).result());
130 }
131
132
133
134
135
136
137
138
139 @Execute
140 public JsonResponse<ApiResult> put$setting(final EditBody body) {
141 validateApi(body, messages -> {});
142 body.crudMode = CrudMode.EDIT;
143 final KeyMatch keyMatch = getKeyMatch(body).map(entity -> {
144 try {
145 keyMatchService.store(entity);
146 } catch (final Exception e) {
147 logger.warn("Failed to process a request.", e);
148 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
149 }
150 return entity;
151 }).orElseGet(() -> {
152 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id));
153 return null;
154 });
155 return asJson(new ApiUpdateResponse().id(keyMatch.getId()).created(false).status(Status.OK).result());
156 }
157
158
159
160
161
162
163
164
165 @Execute
166 public JsonResponse<ApiResult> delete$setting(final String id) {
167 keyMatchService.getKeyMatch(id).ifPresent(entity -> {
168 try {
169 keyMatchService.delete(entity);
170 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
171 } catch (final Exception e) {
172 logger.warn("Failed to process a request.", e);
173 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
174 }
175 }).orElse(() -> {
176 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
177 });
178 return asJson(new ApiResponse().status(Status.OK).result());
179 }
180
181
182
183
184
185
186
187 protected EditBody createEditBody(final KeyMatch entity) {
188 final EditBody body = new EditBody();
189 copyBeanToBean(entity, body, copyOp -> {
190 copyOp.excludeNull();
191 });
192 return body;
193 }
194 }