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