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