View Javadoc
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.opensearch.config.exentity;
17  
18  import java.util.Map;
19  
20  import org.codelibs.core.lang.StringUtil;
21  import org.codelibs.fess.Constants;
22  import org.codelibs.fess.exception.JobNotFoundException;
23  import org.codelibs.fess.opensearch.config.bsentity.BsScheduledJob;
24  import org.codelibs.fess.util.ComponentUtil;
25  import org.lastaflute.job.LaScheduledJob;
26  import org.lastaflute.job.key.LaJobUnique;
27  
28  /**
29   * @author FreeGen
30   */
31  public class ScheduledJob extends BsScheduledJob {
32  
33      private static final long serialVersionUID = 1L;
34  
35      @Override
36      public String getScriptType() {
37          final String st = super.getScriptType();
38          if (StringUtil.isBlank(st)) {
39              return "groovy";
40          }
41          return st;
42      }
43  
44      public boolean isLoggingEnabled() {
45          return Constants.T.equals(getJobLogging());
46      }
47  
48      public boolean isCrawlerJob() {
49          return Constants.T.equals(getCrawler());
50      }
51  
52      public boolean isEnabled() {
53          return Constants.T.equals(getAvailable());
54      }
55  
56      public boolean isRunning() {
57          return ComponentUtil.getJobManager().findJobByUniqueOf(LaJobUnique.of(getId())).map(LaScheduledJob::isExecutingNow).orElse(false);
58      }
59  
60      public void start() {
61          ComponentUtil.getJobManager().findJobByUniqueOf(LaJobUnique.of(getId())).ifPresent(job -> {
62              job.launchNow();
63          }).orElse(() -> {
64              throw new JobNotFoundException(this);
65          });
66      }
67  
68      public void start(final Map<String, Object> params) {
69          ComponentUtil.getJobManager().findJobByUniqueOf(LaJobUnique.of(getId())).ifPresent(job -> {
70              if (params != null && !params.isEmpty()) {
71                  job.launchNow(op -> {
72                      params.forEach(op::param);
73                  });
74              } else {
75                  job.launchNow();
76              }
77          }).orElse(() -> {
78              throw new JobNotFoundException(this);
79          });
80      }
81  
82      public void stop() {
83          ComponentUtil.getJobManager().findJobByUniqueOf(LaJobUnique.of(getId())).ifPresent(job -> {
84              job.stopNow();
85          }).orElse(() -> {
86              throw new JobNotFoundException(this);
87          });
88      }
89  
90      public String getId() {
91          return asDocMeta().id();
92      }
93  
94      public void setId(final String id) {
95          asDocMeta().id(id);
96      }
97  
98      public Long getVersionNo() {
99          return asDocMeta().version();
100     }
101 
102     public void setVersionNo(final Long version) {
103         asDocMeta().version(version);
104     }
105 
106     @Override
107     public String toString() {
108         return "ScheduledJob [available=" + available + ", crawler=" + crawler + ", createdBy=" + createdBy + ", createdTime=" + createdTime
109                 + ", cronExpression=" + cronExpression + ", jobLogging=" + jobLogging + ", name=" + name + ", scriptData=" + scriptData
110                 + ", scriptType=" + scriptType + ", sortOrder=" + sortOrder + ", target=" + target + ", updatedBy=" + updatedBy
111                 + ", updatedTime=" + updatedTime + ", docMeta=" + docMeta + "]";
112     }
113 }