All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.lastaflute.job.subsidiary.CronOption Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2015-2017 the original author or authors.
 *
 * 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.lastaflute.job.subsidiary;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.dbflute.optional.OptionalThing;
import org.lastaflute.job.key.LaJobKey;
import org.lastaflute.job.key.LaJobNote;
import org.lastaflute.job.key.LaJobUnique;
import org.lastaflute.job.log.JobNoticeLogLevel;

/**
 * @author jflute
 * @since 0.2.0 (2016/01/11 Monday)
 */
public class CronOption implements InitialCronOption, VaryingCronOption, JobSubIdentityAttr {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    protected String jobTitle;
    protected String jobDesc;
    protected LaJobUnique jobUnique;
    protected CronParamsSupplier paramsSupplier;
    protected List triggeringJobKeyList;
    protected JobNoticeLogLevel noticeLogLevel = JobNoticeLogLevel.INFO;

    // ===================================================================================
    //                                                                              Facade
    //                                                                              ======
    @Override
    public CronOption title(String jobTitle) {
        if (jobTitle == null || jobTitle.trim().isEmpty()) {
            throw new IllegalArgumentException("The argument 'jobTitle' should not be null or empty: " + jobTitle);
        }
        this.jobTitle = jobTitle;
        return this;
    }

    @Override
    public CronOption desc(String jobDesc) {
        if (jobDesc == null || jobDesc.trim().isEmpty()) {
            throw new IllegalArgumentException("The argument 'jobDesc' should not be null or empty: " + jobDesc);
        }
        this.jobDesc = jobDesc;
        return this;
    }

    @Override
    public CronOption uniqueBy(String uniqueCode) {
        if (uniqueCode == null || uniqueCode.trim().isEmpty()) {
            throw new IllegalArgumentException("The argument 'uniqueCode' should not be null or empty: " + uniqueCode);
        }
        this.jobUnique = createJobUnique(uniqueCode);
        return this;
    }

    protected LaJobUnique createJobUnique(String uniqueCode) {
        return LaJobUnique.of(uniqueCode);
    }

    @Override
    public CronOption params(CronParamsSupplier noArgLambda) {
        if (noArgLambda == null) {
            throw new IllegalArgumentException("The argument 'noArgLambda' should not be null.");
        }
        paramsSupplier = noArgLambda;
        return this;
    }

    @Override
    public CronOption triggeredBy(RegisteredJob triggeringJob) {
        if (triggeringJob == null) {
            throw new IllegalArgumentException("The argument 'triggeringJob' should not be null or empty: " + triggeringJob);
        }
        if (triggeringJobKeyList == null) {
            triggeringJobKeyList = new ArrayList();
        }
        triggeringJobKeyList.add(triggeringJob.getJobKey());
        return this;
    }

    // -----------------------------------------------------
    //                                      Notice Log Level
    //                                      ----------------
    @Override
    public CronOption changeNoticeLogToDebug() {
        noticeLogLevel = JobNoticeLogLevel.DEBUG;
        return this;
    }

    @Override
    public CronOption changeNoticeLogToSuppressed() {
        noticeLogLevel = JobNoticeLogLevel.SUPPRESSED;
        return this;
    }

    // ===================================================================================
    //                                                                      Basic Override
    //                                                                      ==============
    @Override
    public String toString() {
        final String uniqueExp = jobUnique != null ? "hasJobUnique(" + jobUnique + ")" : "noJobUnique";
        final String paramsExp = paramsSupplier != null ? "hasParams" : "noParams";
        return "option:{" + uniqueExp + ", " + paramsExp + ", " + noticeLogLevel + "}";
    }

    // ===================================================================================
    //                                                                            Accessor
    //                                                                            ========
    @Override
    public OptionalThing getJobNote() {
        final LaJobNote note = (jobTitle != null || jobDesc != null) ? LaJobNote.of(jobTitle, jobDesc) : null;
        return OptionalThing.ofNullable(note, () -> {
            throw new IllegalStateException("Not found the job note (both title and description).");
        });
    }

    @Override
    public OptionalThing getJobUnique() {
        return OptionalThing.ofNullable(jobUnique, () -> {
            throw new IllegalStateException("Not found the application unique code.");
        });
    }

    @Override
    public OptionalThing getParamsSupplier() {
        return OptionalThing.ofNullable(paramsSupplier, () -> {
            throw new IllegalStateException("Not found the parameters supplier.");
        });
    }

    public List getTriggeringJobKeyList() {
        return triggeringJobKeyList != null ? Collections.unmodifiableList(triggeringJobKeyList) : Collections.emptyList();
    }

    @Override
    public JobNoticeLogLevel getNoticeLogLevel() {
        return noticeLogLevel;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy