1 /*
2 * Copyright 2012-2025 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.joblog;
17
18 import java.util.List;
19 import java.util.stream.Collectors;
20
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.Logger;
23 import org.codelibs.fess.app.pager.JobLogPager;
24 import org.codelibs.fess.app.service.JobLogService;
25 import org.codelibs.fess.app.web.api.ApiResult;
26 import org.codelibs.fess.app.web.api.ApiResult.ApiLogResponse;
27 import org.codelibs.fess.app.web.api.ApiResult.ApiResponse;
28 import org.codelibs.fess.app.web.api.ApiResult.Status;
29 import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
30 import org.codelibs.fess.opensearch.config.exentity.JobLog;
31 import org.lastaflute.web.Execute;
32 import org.lastaflute.web.response.JsonResponse;
33
34 import jakarta.annotation.Resource;
35
36 /**
37 * API action for admin job log management.
38 * Provides RESTful API endpoints for viewing and managing job execution logs in the Fess search engine.
39 * Job logs contain information about crawling jobs, indexing tasks, and system maintenance operations.
40 *
41 */
42 public class ApiAdminJoblogAction extends FessApiAdminAction {
43
44 private static final Logger logger = LogManager.getLogger(ApiAdminJoblogAction.class);
45
46 // ===================================================================================
47 // Constructor
48 // ===========
49 /**
50 * Default constructor.
51 */
52 public ApiAdminJoblogAction() {
53 super();
54 }
55
56 // ===================================================================================
57 // Attribute
58 // =========
59 /** Service for managing job log data */
60 @Resource
61 private JobLogService jobLogService;
62
63 // ===================================================================================
64 // Search Execute
65 // ==============
66
67 // GET /api/admin/joblog/logs
68 /**
69 * Returns list of job logs.
70 * Supports filtering and pagination for job execution history.
71 *
72 * @param body search parameters for filtering and pagination
73 * @return JSON response containing job logs list with pagination info
74 */
75 @Execute
76 public JsonResponse<ApiResult> logs(final SearchBody body) {
77 validateApi(body, messages -> {});
78 final JobLogPager pager = copyBeanToNewBean(body, JobLogPager.class);
79 final List<JobLog> list = jobLogService.getJobLogList(pager);
80 return asJson(new ApiResult.ApiLogsResponse<EditBody>().logs(list.stream().map(this::createEditBody).collect(Collectors.toList()))
81 .total(pager.getAllRecordCount())
82 .status(ApiResult.Status.OK)
83 .result());
84 }
85
86 // GET /api/admin/joblog/log/{id}
87 /**
88 * Returns specific job log by ID.
89 * Provides detailed information about a particular job execution.
90 *
91 * @param id the job log ID
92 * @return JSON response containing the job log details
93 */
94 @Execute
95 public JsonResponse<ApiResult> get$log(final String id) {
96 return asJson(new ApiLogResponse().log(jobLogService.getJobLog(id).map(this::createEditBody).orElseGet(() -> {
97 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
98 return null;
99 })).status(Status.OK).result());
100 }
101
102 // DELETE /api/admin/joblog/log/{id}
103 /**
104 * Deletes a specific job log.
105 * Useful for cleaning up old job execution records.
106 *
107 * @param id the job log ID to delete
108 * @return JSON response with deletion status
109 */
110 @Execute
111 public JsonResponse<ApiResult> delete$log(final String id) {
112 jobLogService.getJobLog(id).ifPresent(entity -> {
113 try {
114 jobLogService.delete(entity);
115 saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
116 } catch (final Exception e) {
117 logger.warn("Failed to process a request.", e);
118 throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
119 }
120 }).orElse(() -> {
121 throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
122 });
123 return asJson(new ApiResponse().status(Status.OK).result());
124 }
125
126 /**
127 * Creates an edit body from a job log entity for API responses.
128 *
129 * @param entity the job log entity to convert
130 * @return edit body containing the entity data
131 */
132 protected EditBody createEditBody(final JobLog entity) {
133 final EditBody body = new EditBody();
134 copyBeanToBean(entity, body, copyOp -> {
135 copyOp.excludeNull();
136 });
137 return body;
138 }
139 }