View Javadoc
1   /*
2    * Copyright 2012-2021 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.scheduler;
17  
18  import static org.codelibs.fess.app.web.admin.scheduler.AdminSchedulerAction.getScheduledJob;
19  
20  import java.util.List;
21  import java.util.stream.Collectors;
22  
23  import javax.annotation.Resource;
24  
25  import org.codelibs.fess.Constants;
26  import org.codelibs.fess.app.pager.SchedulerPager;
27  import org.codelibs.fess.app.service.ScheduledJobService;
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.ApiResult.ApiResponse;
31  import org.codelibs.fess.app.web.api.ApiResult.Status;
32  import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
33  import org.codelibs.fess.es.config.exentity.ScheduledJob;
34  import org.lastaflute.web.Execute;
35  import org.lastaflute.web.response.HtmlResponse;
36  import org.lastaflute.web.response.JsonResponse;
37  
38  public class ApiAdminSchedulerAction extends FessApiAdminAction {
39  
40      @Resource
41      private ScheduledJobService scheduledJobService;
42  
43      @Execute
44      public HtmlResponse index() {
45          throw new UnsupportedOperationException();
46      }
47  
48      // POST /api/admin/scheduler/{id}/start
49      @Execute(urlPattern = "{}/@word")
50      public JsonResponse<ApiResult> post$start(final String id) {
51          scheduledJobService.getScheduledJob(id).ifPresent(entity -> {
52              if (!entity.isEnabled() || entity.isRunning()) {
53                  throwValidationErrorApi(messages -> {
54                      messages.addErrorsFailedToStartJob(GLOBAL, entity.getName());
55                  });
56              }
57              try {
58                  entity.start();
59              } catch (final Exception e) {
60                  throwValidationErrorApi(messages -> {
61                      messages.addErrorsFailedToStartJob(GLOBAL, entity.getName());
62                  });
63              }
64          }).orElse(() -> {
65              throwValidationErrorApi(messages -> {
66                  messages.addErrorsFailedToStartJob(GLOBAL, id);
67              });
68          });
69          return asJson(new ApiResponse().status(Status.OK).result());
70      }
71  
72      // POST /api/admin/scheduler/{id}/stop
73      @Execute(urlPattern = "{}/@word")
74      public JsonResponse<ApiResult> post$stop(final String id) {
75          scheduledJobService.getScheduledJob(id).ifPresent(entity -> {
76              try {
77                  entity.stop();
78              } catch (final Exception e) {
79                  throwValidationErrorApi(messages -> {
80                      messages.addErrorsFailedToStopJob(GLOBAL, entity.getName());
81                  });
82              }
83          }).orElse(() -> {
84              throwValidationErrorApi(messages -> {
85                  messages.addErrorsFailedToStartJob(GLOBAL, id);
86              });
87          });
88          return asJson(new ApiResponse().status(Status.OK).result());
89      }
90  
91      // GET /api/admin/scheduler
92      // POST /api/admin/scheduler
93      @Execute
94      public JsonResponse<ApiResult> settings(final SearchBody body) {
95          validateApi(body, messages -> {});
96          final SchedulerPager pager = copyBeanToNewBean(body, SchedulerPager.class);
97          final List<ScheduledJob> list = scheduledJobService.getScheduledJobList(pager);
98          return asJson(
99                  new ApiResult.ApiConfigsResponse<EditBody>().settings(list.stream().map(this::createEditBody).collect(Collectors.toList()))
100                         .total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
101     }
102 
103     // GET /api/admin/scheduler/setting/{id}
104     @Execute
105     public JsonResponse<ApiResult> get$setting(final String id) {
106         return asJson(new ApiResult.ApiConfigResponse()
107                 .setting(scheduledJobService.getScheduledJob(id).map(this::createEditBody).orElseGet(() -> {
108                     throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
109                     return null;
110                 })).status(ApiResult.Status.OK).result());
111     }
112 
113     // PUT /api/admin/scheduler/setting
114     @Execute
115     public JsonResponse<ApiResult> put$setting(final CreateBody body) {
116         validateApi(body, messages -> {});
117         body.crudMode = CrudMode.CREATE;
118         final ScheduledJob entity = getScheduledJob(body).orElseGet(() -> {
119             throwValidationErrorApi(messages -> {
120                 messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
121             });
122             return null;
123         });
124         try {
125             scheduledJobService.store(entity);
126             saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
127         } catch (final Exception e) {
128             throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
129         }
130         return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(true).status(ApiResult.Status.OK).result());
131     }
132 
133     // POST /api/admin/scheduler/setting
134     @Execute
135     public JsonResponse<ApiResult> post$setting(final EditBody body) {
136         validateApi(body, messages -> {});
137         body.crudMode = CrudMode.EDIT;
138         final ScheduledJob entity = getScheduledJob(body).orElseGet(() -> {
139             throwValidationErrorApi(messages -> {
140                 messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
141             });
142             return null;
143         });
144         try {
145             scheduledJobService.store(entity);
146         } catch (final Exception e) {
147             throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
148         }
149         return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
150     }
151 
152     // DELETE /api/admin/scheduler/setting/{id}
153     @Execute
154     public JsonResponse<ApiResult> delete$setting(final String id) {
155         final ScheduledJob entity = scheduledJobService.getScheduledJob(id).orElseGet(() -> {
156             throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
157             return null;
158         });
159         try {
160             scheduledJobService.delete(entity);
161             saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
162         } catch (final Exception e) {
163             throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
164         }
165         return asJson(new ApiResult.ApiUpdateResponse().id(id).created(false).status(ApiResult.Status.OK).result());
166     }
167 
168     protected EditBody createEditBody(final ScheduledJob entity) {
169         final EditBody body = new EditBody();
170         copyBeanToBean(entity, body, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
171         body.running = entity.isRunning();
172         return body;
173     }
174 
175 }