001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.common.service.job;
018
019import com.echothree.control.user.authentication.common.AuthenticationUtil;
020import com.echothree.control.user.authentication.common.result.GetJobUserVisitResult;
021import com.echothree.control.user.job.common.JobUtil;
022import com.echothree.model.control.job.common.workflow.JobStatusConstants;
023import com.echothree.model.data.user.common.pk.UserVisitPK;
024import javax.naming.NamingException;
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027
028public abstract class BaseScheduledJob {
029
030    private Log log = null;
031    protected String jobName;
032    protected UserVisitPK userVisitPK;
033
034    protected BaseScheduledJob(String jobName) {
035        this.jobName = jobName;
036    }
037
038    protected Log getLog() {
039        if(log == null) {
040            log = LogFactory.getLog(this.getClass());
041        }
042
043        return log;
044    }
045
046    private void startJob(UserVisitPK userVisitPK)
047            throws NamingException {
048        var commandForm = JobUtil.getHome().getStartJobForm();
049
050        commandForm.setJobName(jobName);
051
052        JobUtil.getHome().startJob(userVisitPK, commandForm);
053    }
054
055    private void endJob(UserVisitPK userVisitPK)
056            throws NamingException {
057        var commandForm = JobUtil.getHome().getEndJobForm();
058
059        commandForm.setJobName(jobName);
060
061        JobUtil.getHome().endJob(userVisitPK, commandForm);
062    }
063
064    public void executeJob()
065            throws NamingException {
066        var commandForm = AuthenticationUtil.getHome().getGetJobUserVisitForm();
067
068        commandForm.setJobName(jobName);
069
070        var commandResult = AuthenticationUtil.getHome().getJobUserVisit(commandForm);
071
072        if(commandResult.hasErrors()) {
073            getLog().error(commandResult);
074        } else {
075            var executionResult = commandResult.getExecutionResult();
076            var getJobUserVisitResult = (GetJobUserVisitResult)executionResult.getResult();
077
078            userVisitPK = getJobUserVisitResult.getUserVisitPK();
079
080            if(userVisitPK != null) {
081                if(getJobUserVisitResult.getJob().getJobStatus().getWorkflowStep().getWorkflowStepName().equals(JobStatusConstants.WorkflowStep_ENABLED)) {
082                    startJob(userVisitPK);
083                    execute();
084                    endJob(userVisitPK);
085                }
086
087                AuthenticationUtil.getHome().invalidateUserVisit(userVisitPK);
088            }
089        }
090    }
091
092    public abstract void execute()
093            throws NamingException;
094
095}