View Javadoc
1   /*
2    * Copyright 2012-2019 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.app.web.api.admin.dict.mapping;
17  
18  import static org.codelibs.fess.app.web.admin.dict.mapping.AdminDictMappingAction.createCharMappingItem;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.stream.Collectors;
24  
25  import javax.annotation.Resource;
26  
27  import org.codelibs.fess.app.pager.CharMappingPager;
28  import org.codelibs.fess.app.service.CharMappingService;
29  import org.codelibs.fess.app.web.CrudMode;
30  import org.codelibs.fess.app.web.admin.dict.mapping.UploadForm;
31  import org.codelibs.fess.app.web.api.ApiResult;
32  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
33  import org.codelibs.fess.dict.mapping.CharMappingFile;
34  import org.codelibs.fess.dict.mapping.CharMappingItem;
35  import org.lastaflute.web.Execute;
36  import org.lastaflute.web.response.JsonResponse;
37  import org.lastaflute.web.response.StreamResponse;
38  
39  public class ApiAdminDictMappingAction extends FessApiAdminAction {
40  
41      @Resource
42      private CharMappingService charMappingService;
43  
44      // GET /api/admin/dict/mapping/settings/{dictId}
45      @Execute
46      public JsonResponse<ApiResult> get$settings(final String dictId, final SearchBody body) {
47          body.dictId = dictId;
48          validateApi(body, messages -> {});
49          final CharMappingPager pager = copyBeanToNewBean(body, CharMappingPager.class);
50          return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
51                  .settings(
52                          charMappingService.getCharMappingList(body.dictId, pager).stream()
53                                  .map(protwordsItem -> createEditBody(protwordsItem, dictId)).collect(Collectors.toList()))
54                  .status(ApiResult.Status.OK).result());
55      }
56  
57      // GET /api/admin/dict/mapping/setting/{dictId}/{id}
58      @Execute
59      public JsonResponse<ApiResult> get$setting(final String dictId, final long id) {
60          return asJson(new ApiResult.ApiConfigResponse()
61                  .setting(charMappingService.getCharMappingItem(dictId, id).map(entity -> createEditBody(entity, dictId)).orElseGet(() -> {
62                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
63                      return null;
64                  })).status(ApiResult.Status.OK).result());
65      }
66  
67      // PUT /api/admin/dict/mapping/setting/{dictId}
68      @Execute
69      public JsonResponse<ApiResult> put$setting(final String dictId, final CreateBody body) {
70          body.dictId = dictId;
71          validateApi(body, messages -> {});
72          body.crudMode = CrudMode.CREATE;
73          final CharMappingItem entity = createCharMappingItem(this, body, () -> {
74              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
75              return null;
76          }).orElseGet(() -> {
77              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
78              return null;
79          });
80          charMappingService.store(body.dictId, entity);
81          return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(true).status(ApiResult.Status.OK)
82                  .result());
83      }
84  
85      // POST /api/admin/dict/mapping/setting/{dictId}
86      @Execute
87      public JsonResponse<ApiResult> post$setting(final String dictId, final EditBody body) {
88          body.dictId = dictId;
89          validateApi(body, messages -> {});
90          body.crudMode = CrudMode.EDIT;
91          final CharMappingItem entity = createCharMappingItem(this, body, () -> {
92              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, String.valueOf(body.id)));
93              return null;
94          }).orElseGet(() -> {
95              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(body.id)));
96              return null;
97          });
98          charMappingService.store(body.dictId, entity);
99          return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(false).status(ApiResult.Status.OK)
100                 .result());
101     }
102 
103     // DELETE /api/admin/dict/mapping/setting/{dictId}/{id}
104     @Execute
105     public JsonResponse<ApiResult> delete$setting(final String dictId, final long id) {
106         charMappingService.getCharMappingItem(dictId, id).ifPresent(entity -> {
107             charMappingService.delete(dictId, entity);
108             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
109         }).orElse(() -> {
110             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
111         });
112         return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(id)).created(false).status(ApiResult.Status.OK).result());
113     }
114 
115     // POST /api/admin/dict/mapping/upload/{dictId}
116     @Execute
117     public JsonResponse<ApiResult> post$upload(final String dictId, final UploadForm form) {
118         form.dictId = dictId;
119         validateApi(form, messages -> {});
120         final CharMappingFile file = charMappingService.getCharMappingFile(form.dictId).orElseGet(() -> {
121             throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
122             return null;
123         });
124         try (InputStream inputStream = form.charMappingFile.getInputStream()) {
125             file.update(inputStream);
126         } catch (final IOException e) {
127             throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
128         }
129         return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
130     }
131 
132     // GET /api/admin/dict/mapping/download/{dictId}
133     @Execute
134     public StreamResponse get$download(final String dictId, final DownloadBody body) {
135         body.dictId = dictId;
136         validateApi(body, messages -> {});
137         return charMappingService.getCharMappingFile(body.dictId).map(file -> {
138             return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
139                 file.writeOut(out);
140             });
141         }).orElseGet(() -> {
142             throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));
143             return null;
144         });
145     }
146 
147     protected EditBody createEditBody(final CharMappingItem entity, final String dictId) {
148         final EditBodyfess/app/web/api/admin/dict/mapping/EditBody.html#EditBody">EditBody body = new EditBody();
149         body.id = entity.getId();
150         body.dictId = dictId;
151         body.inputs = entity.getInputsValue();
152         body.output = entity.getOutput();
153         return body;
154     }
155 }