|
@@ -0,0 +1,143 @@
|
|
|
+package org.codelibs.fess.app.web.api.admin.dict.mapping;
|
|
|
+
|
|
|
+import org.codelibs.fess.app.pager.CharMappingPager;
|
|
|
+import org.codelibs.fess.app.service.CharMappingService;
|
|
|
+import org.codelibs.fess.app.web.CrudMode;
|
|
|
+import org.codelibs.fess.app.web.admin.dict.mapping.AdminDictMappingAction;
|
|
|
+import org.codelibs.fess.app.web.admin.dict.mapping.UploadForm;
|
|
|
+import org.codelibs.fess.app.web.api.ApiResult;
|
|
|
+import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
|
|
|
+import org.codelibs.fess.dict.mapping.CharMappingFile;
|
|
|
+import org.codelibs.fess.dict.mapping.CharMappingItem;
|
|
|
+import org.lastaflute.web.Execute;
|
|
|
+import org.lastaflute.web.response.JsonResponse;
|
|
|
+import org.lastaflute.web.response.StreamResponse;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+public class ApiAdminDictMappingAction extends FessApiAdminAction {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CharMappingService charMappingService;
|
|
|
+
|
|
|
+ // GET /api/admin/dict/mapping/settings/{dictId}
|
|
|
+ @Execute
|
|
|
+ public JsonResponse<ApiResult> get$settings(final String dictId, final SearchBody body) {
|
|
|
+ body.dictId = dictId;
|
|
|
+ validateApi(body, messages -> {});
|
|
|
+ CharMappingPager pager = new CharMappingPager();
|
|
|
+ if (body.pageNumber != null) {
|
|
|
+ pager.setCurrentPageNumber(body.pageNumber);
|
|
|
+ }
|
|
|
+ return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
|
|
|
+ .settings(
|
|
|
+ charMappingService.getCharMappingList(body.dictId, pager).stream()
|
|
|
+ .map(protwordsItem -> createEditBody(protwordsItem, dictId)).collect(Collectors.toList()))
|
|
|
+ .status(ApiResult.Status.OK).result());
|
|
|
+ }
|
|
|
+
|
|
|
+ // GET /api/admin/dict/mapping/setting/{dictId}/{id}
|
|
|
+ @Execute
|
|
|
+ public JsonResponse<ApiResult> get$setting(final String dictId, final long id) {
|
|
|
+ return asJson(new ApiResult.ApiConfigResponse()
|
|
|
+ .setting(charMappingService.getCharMappingItem(dictId, id).map(entity -> createEditBody(entity, dictId)).orElseGet(() -> {
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
|
|
|
+ return null;
|
|
|
+ })).status(ApiResult.Status.OK).result());
|
|
|
+ }
|
|
|
+
|
|
|
+ // PUT /api/admin/dict/mapping/setting/{dictId}
|
|
|
+ @Execute
|
|
|
+ public JsonResponse<ApiResult> put$setting(final String dictId, final CreateBody body) {
|
|
|
+ body.dictId = dictId;
|
|
|
+ validateApi(body, messages -> {});
|
|
|
+ body.crudMode = CrudMode.CREATE;
|
|
|
+ final CharMappingItem entity = new AdminDictMappingAction().createCharMappingItem(body, () -> {
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
|
|
|
+ return null;
|
|
|
+ }).orElseGet(() -> {
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ charMappingService.store(body.dictId, entity);
|
|
|
+ return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(true).status(ApiResult.Status.OK)
|
|
|
+ .result());
|
|
|
+ }
|
|
|
+
|
|
|
+ // POST /api/admin/dict/mapping/setting/{dictId}
|
|
|
+ @Execute
|
|
|
+ public JsonResponse<ApiResult> post$setting(final String dictId, final EditBody body) {
|
|
|
+ body.dictId = dictId;
|
|
|
+ validateApi(body, messages -> {});
|
|
|
+ body.crudMode = CrudMode.EDIT;
|
|
|
+ final CharMappingItem entity = new AdminDictMappingAction().createCharMappingItem(body, () -> {
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, String.valueOf(body.id)));
|
|
|
+ return null;
|
|
|
+ }).orElseGet(() -> {
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(body.id)));
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ charMappingService.store(body.dictId, entity);
|
|
|
+ return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(false).status(ApiResult.Status.OK)
|
|
|
+ .result());
|
|
|
+ }
|
|
|
+
|
|
|
+ // DELETE /api/admin/dict/mapping/setting/{dictId}/{id}
|
|
|
+ @Execute
|
|
|
+ public JsonResponse<ApiResult> delete$setting(final String dictId, final long id) {
|
|
|
+ charMappingService.getCharMappingItem(dictId, id).ifPresent(entity -> {
|
|
|
+ charMappingService.delete(dictId, entity);
|
|
|
+ saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
|
|
|
+ }).orElse(() -> {
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
|
|
|
+ });
|
|
|
+ return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(id)).created(false).status(ApiResult.Status.OK).result());
|
|
|
+ }
|
|
|
+
|
|
|
+ // POST /api/admin/dict/mapping/upload/{dictId}
|
|
|
+ @Execute
|
|
|
+ public JsonResponse<ApiResult> post$upload(final String dictId, final UploadForm form) {
|
|
|
+ form.dictId = dictId;
|
|
|
+ validateApi(form, messages -> {});
|
|
|
+ final CharMappingFile file = charMappingService.getCharMappingFile(form.dictId).orElseGet(() -> {
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ try (InputStream inputStream = form.charMappingFile.getInputStream()) {
|
|
|
+ file.update(inputStream);
|
|
|
+ } catch (final Throwable e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
|
|
|
+ }
|
|
|
+ return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
|
|
|
+ }
|
|
|
+
|
|
|
+ // GET /api/admin/dict/mapping/download/{dictId}
|
|
|
+ @Execute
|
|
|
+ public StreamResponse get$download(final String dictId, final DownloadBody body) {
|
|
|
+ body.dictId = dictId;
|
|
|
+ validateApi(body, messages -> {});
|
|
|
+ return charMappingService.getCharMappingFile(body.dictId).map(file -> {
|
|
|
+ return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
|
|
|
+ try (InputStream inputStream = file.getInputStream()) {
|
|
|
+ out.write(inputStream);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }).orElseGet(() -> {
|
|
|
+ throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ protected EditBody createEditBody(final CharMappingItem entity, final String dictId) {
|
|
|
+ final EditBody body = new EditBody();
|
|
|
+ body.id = entity.getId();
|
|
|
+ body.dictId = dictId;
|
|
|
+ body.inputs = entity.getInputsValue();
|
|
|
+ body.output = entity.getOutput();
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+}
|