001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.form.GetJobUserVisitForm; 021import com.echothree.control.user.authentication.common.result.GetJobUserVisitResult; 022import com.echothree.control.user.job.common.JobUtil; 023import com.echothree.control.user.job.common.form.EndJobForm; 024import com.echothree.control.user.job.common.form.StartJobForm; 025import com.echothree.model.control.job.common.workflow.JobStatusConstants; 026import com.echothree.model.data.user.common.pk.UserVisitPK; 027import com.echothree.util.common.command.CommandResult; 028import com.echothree.util.common.command.ExecutionResult; 029import javax.naming.NamingException; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032 033public abstract class BaseScheduledJob { 034 035 private Log log = null; 036 protected String jobName; 037 protected UserVisitPK userVisitPK; 038 039 protected BaseScheduledJob(String jobName) { 040 this.jobName = jobName; 041 } 042 043 protected Log getLog() { 044 if(log == null) { 045 log = LogFactory.getLog(this.getClass()); 046 } 047 048 return log; 049 } 050 051 private void startJob(UserVisitPK userVisitPK) 052 throws NamingException { 053 StartJobForm commandForm = JobUtil.getHome().getStartJobForm(); 054 055 commandForm.setJobName(jobName); 056 057 JobUtil.getHome().startJob(userVisitPK, commandForm); 058 } 059 060 private void endJob(UserVisitPK userVisitPK) 061 throws NamingException { 062 EndJobForm commandForm = JobUtil.getHome().getEndJobForm(); 063 064 commandForm.setJobName(jobName); 065 066 JobUtil.getHome().endJob(userVisitPK, commandForm); 067 } 068 069 public void executeJob() 070 throws NamingException { 071 GetJobUserVisitForm commandForm = AuthenticationUtil.getHome().getGetJobUserVisitForm(); 072 073 commandForm.setJobName(jobName); 074 075 CommandResult commandResult = AuthenticationUtil.getHome().getJobUserVisit(commandForm); 076 077 if(commandResult.hasErrors()) { 078 getLog().error(commandResult); 079 } else { 080 ExecutionResult executionResult = commandResult.getExecutionResult(); 081 GetJobUserVisitResult getJobUserVisitResult = (GetJobUserVisitResult)executionResult.getResult(); 082 083 userVisitPK = getJobUserVisitResult.getUserVisitPK(); 084 085 if(userVisitPK != null) { 086 if(getJobUserVisitResult.getJob().getJobStatus().getWorkflowStep().getWorkflowStepName().equals(JobStatusConstants.WorkflowStep_ENABLED)) { 087 startJob(userVisitPK); 088 execute(); 089 endJob(userVisitPK); 090 } 091 092 AuthenticationUtil.getHome().invalidateUserVisit(userVisitPK); 093 } 094 } 095 } 096 097 public abstract void execute() 098 throws NamingException; 099 100}