View Javadoc
1   /*
2    * Copyright 2012-2017 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.seunjeon;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.stream.Collectors;
22  
23  import javax.annotation.Resource;
24  
25  import org.codelibs.fess.app.pager.SeunjeonPager;
26  import org.codelibs.fess.app.service.SeunjeonService;
27  import org.codelibs.fess.app.web.CrudMode;
28  import org.codelibs.fess.app.web.admin.dict.seunjeon.AdminDictSeunjeonAction;
29  import org.codelibs.fess.app.web.admin.dict.seunjeon.UploadForm;
30  import org.codelibs.fess.app.web.api.ApiResult;
31  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
32  import org.codelibs.fess.dict.seunjeon.SeunjeonFile;
33  import org.codelibs.fess.dict.seunjeon.SeunjeonItem;
34  import org.lastaflute.web.Execute;
35  import org.lastaflute.web.response.JsonResponse;
36  import org.lastaflute.web.response.StreamResponse;
37  
38  public class ApiAdminDictSeunjeonAction extends FessApiAdminAction {
39  
40      @Resource
41      private SeunjeonService seunjeonService;
42  
43      // GET /api/admin/dict/seunjeon/settings/{dictId}
44      @Execute
45      public JsonResponse<ApiResult> get$settings(final String dictId, final SearchBody body) {
46          body.dictId = dictId;
47          validateApi(body, messages -> {});
48          final SeunjeonPager pager = copyBeanToNewBean(body, SeunjeonPager.class);
49          return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
50                  .settings(
51                          seunjeonService.getSeunjeonList(body.dictId, pager).stream()
52                                  .map(protwordsItem -> createEditBody(protwordsItem, dictId)).collect(Collectors.toList()))
53                  .status(ApiResult.Status.OK).result());
54      }
55  
56      // GET /api/admin/dict/seunjeon/setting/{dictId}/{id}
57      @Execute
58      public JsonResponse<ApiResult> get$setting(final String dictId, final long id) {
59          return asJson(new ApiResult.ApiConfigResponse()
60                  .setting(seunjeonService.getSeunjeonItem(dictId, id).map(entity -> createEditBody(entity, dictId)).orElseGet(() -> {
61                      throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
62                      return null;
63                  })).status(ApiResult.Status.OK).result());
64      }
65  
66      // PUT /api/admin/dict/seunjeon/setting/{dictId}
67      @Execute
68      public JsonResponse<ApiResult> put$setting(final String dictId, final CreateBody body) {
69          body.dictId = dictId;
70          validateApi(body, messages -> {});
71          body.crudMode = CrudMode.CREATE;
72          final SeunjeonItem entity = new AdminDictSeunjeonAction().createSeunjeonItem(body, () -> {
73              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
74              return null;
75          }).orElseGet(() -> {
76              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateInstance(GLOBAL));
77              return null;
78          });
79          seunjeonService.store(body.dictId, entity);
80          return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(true).status(ApiResult.Status.OK)
81                  .result());
82      }
83  
84      // POST /api/admin/dict/seunjeon/setting/{dictId}
85      @Execute
86      public JsonResponse<ApiResult> post$setting(final String dictId, final EditBody body) {
87          body.dictId = dictId;
88          validateApi(body, messages -> {});
89          body.crudMode = CrudMode.EDIT;
90          final SeunjeonItem entity = new AdminDictSeunjeonAction().createSeunjeonItem(body, () -> {
91              throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, String.valueOf(body.id)));
92              return null;
93          }).orElseGet(() -> {
94              throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(body.id)));
95              return null;
96          });
97          seunjeonService.store(body.dictId, entity);
98          return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(entity.getId())).created(false).status(ApiResult.Status.OK)
99                  .result());
100     }
101 
102     // DELETE /api/admin/dict/seunjeon/setting/{dictId}/{id}
103     @Execute
104     public JsonResponse<ApiResult> delete$setting(final String dictId, final long id) {
105         seunjeonService.getSeunjeonItem(dictId, id).ifPresent(entity -> {
106             seunjeonService.delete(dictId, entity);
107             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
108         }).orElse(() -> {
109             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, String.valueOf(id)));
110         });
111         return asJson(new ApiResult.ApiUpdateResponse().id(String.valueOf(id)).created(false).status(ApiResult.Status.OK).result());
112     }
113 
114     // POST /api/admin/dict/seunjeon/upload/{dictId}
115     @Execute
116     public JsonResponse<ApiResult> post$upload(final String dictId, final UploadForm form) {
117         form.dictId = dictId;
118         validateApi(form, messages -> {});
119         final SeunjeonFile file = seunjeonService.getSeunjeonFile(form.dictId).orElseGet(() -> {
120             throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
121             return null;
122         });
123         try (InputStream inputStream = form.seunjeonFile.getInputStream()) {
124             file.update(inputStream);
125         } catch (final IOException e) {
126             throwValidationErrorApi(messages -> messages.addErrorsFailedToUploadProtwordsFile(GLOBAL));
127         }
128         return asJson(new ApiResult.ApiResponse().status(ApiResult.Status.OK).result());
129     }
130 
131     // GET /api/admin/dict/seunjeon/download/{dictId}
132     @Execute
133     public StreamResponse get$download(final String dictId, final DownloadBody body) {
134         body.dictId = dictId;
135         validateApi(body, messages -> {});
136         return seunjeonService.getSeunjeonFile(body.dictId).map(file -> {
137             return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
138                 try (InputStream inputStream = file.getInputStream()) {
139                     out.write(inputStream);
140                 }
141             });
142         }).orElseGet(() -> {
143             throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));
144             return null;
145         });
146     }
147 
148     protected EditBody createEditBody(final SeunjeonItem entity, final String dictId) {
149         final EditBody body = new EditBody();
150         body.id = entity.getId();
151         body.dictId = dictId;
152         body.inputs = entity.getInputsValue();
153         return body;
154     }
155 }