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