diff --git a/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java b/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java index f922c4c26..5b1871d33 100644 --- a/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java +++ b/src/main/java/org/codelibs/fess/app/web/api/ApiResult.java @@ -114,6 +114,40 @@ public class ApiResult { } } + public static class ApiLogResponse extends ApiResponse { + protected Object log; + + public ApiLogResponse log(final Object log) { + this.log = log; + return this; + } + + @Override + public ApiResult result() { + return new ApiResult(this); + } + } + + public static class ApiLogsResponse extends ApiResponse { + protected List logs; + protected long total = 0; + + public ApiLogsResponse logs(final List logs) { + this.logs = logs; + return this; + } + + public ApiLogsResponse total(final long total) { + this.total = total; + return this; + } + + @Override + public ApiResult result() { + return new ApiResult(this); + } + } + public static class ApiErrorResponse extends ApiResponse { protected String message; diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/ApiAdminJoblogAction.java b/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/ApiAdminJoblogAction.java new file mode 100644 index 000000000..1a3dbb1ed --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/ApiAdminJoblogAction.java @@ -0,0 +1,94 @@ +/* + * Copyright 2012-2017 CodeLibs Project and the Others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.codelibs.fess.app.web.api.admin.joblog; + +import java.util.List; +import java.util.stream.Collectors; + +import javax.annotation.Resource; + +import org.codelibs.fess.Constants; +import org.codelibs.fess.app.pager.JobLogPager; +import org.codelibs.fess.app.service.JobLogService; +import org.codelibs.fess.app.web.api.ApiResult; +import org.codelibs.fess.app.web.api.ApiResult.ApiLogResponse; +import org.codelibs.fess.app.web.api.ApiResult.ApiResponse; +import org.codelibs.fess.app.web.api.ApiResult.Status; +import org.codelibs.fess.app.web.api.admin.FessApiAdminAction; +import org.codelibs.fess.es.config.exentity.JobLog; +import org.lastaflute.web.Execute; +import org.lastaflute.web.response.JsonResponse; + +/** + * @author Keiichi Watanabe + */ +public class ApiAdminJoblogAction extends FessApiAdminAction { + + // =================================================================================== + // Attribute + // ========= + @Resource + private JobLogService jobLogService; + + // =================================================================================== + // Search Execute + // ============== + + // GET /api/admin/joblog/logs + @Execute + public JsonResponse logs(final SearchBody body) { + validateApi(body, messages -> {}); + final JobLogPager pager = new JobLogPager(); + copyBeanToBean(body, pager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE)); + final List list = jobLogService.getJobLogList(pager); + return asJson(new ApiResult.ApiLogsResponse() + .logs(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList())).total(pager.getAllRecordCount()) + .status(ApiResult.Status.OK).result()); + } + + // GET /api/admin/joblog/log/{id} + @Execute + public JsonResponse get$log(final String id) { + return asJson(new ApiLogResponse().log(jobLogService.getJobLog(id).map(entity -> createEditBody(entity)).orElseGet(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + return null; + })).status(Status.OK).result()); + } + + // DELETE /api/admin/joblog/log/{id} + @Execute + public JsonResponse delete$log(final String id) { + jobLogService.getJobLog(id).ifPresent(entity -> { + try { + jobLogService.delete(entity); + saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL)); + } catch (final Exception e) { + throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e))); + } + }).orElse(() -> { + throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id)); + }); + return asJson(new ApiResponse().status(Status.OK).result()); + } + + protected EditBody createEditBody(final JobLog entity) { + final EditBody body = new EditBody(); + copyBeanToBean(entity, body, copyOp -> { + copyOp.excludeNull(); + }); + return body; + } +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/EditBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/EditBody.java new file mode 100644 index 000000000..21ad532c7 --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/EditBody.java @@ -0,0 +1,22 @@ +/* + * Copyright 2012-2017 CodeLibs Project and the Others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.codelibs.fess.app.web.api.admin.joblog; + +import org.codelibs.fess.app.web.admin.joblog.EditForm; + +public class EditBody extends EditForm { + +} diff --git a/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/SearchBody.java b/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/SearchBody.java new file mode 100644 index 000000000..cd7015e4b --- /dev/null +++ b/src/main/java/org/codelibs/fess/app/web/api/admin/joblog/SearchBody.java @@ -0,0 +1,22 @@ +/* + * Copyright 2012-2017 CodeLibs Project and the Others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package org.codelibs.fess.app.web.api.admin.joblog; + +import org.codelibs.fess.app.web.admin.joblog.SearchForm; + +public class SearchBody extends SearchForm { + +}