001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.rice.kew.impl.stuck;
017
018import org.kuali.rice.kew.service.KEWServiceLocator;
019import org.quartz.Job;
020import org.quartz.JobExecutionContext;
021import org.quartz.JobExecutionException;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import java.util.List;
026
027/**
028 * Created by ewestfal on 6/28/17.
029 */
030public class StuckDocumentNotificationJob implements Job {
031
032    private static final Logger LOG = LoggerFactory.getLogger(StuckDocumentNotificationJob.class);
033
034    private volatile StuckDocumentService stuckDocumentService;
035    private volatile StuckDocumentNotifier notifier;
036
037    @Override
038    public void execute(JobExecutionContext context) throws JobExecutionException {
039        checkDependenciesAvailable();
040        List<StuckDocument> stuckDocuments = getStuckDocumentService().findAllStuckDocuments();
041        if (!stuckDocuments.isEmpty()) {
042            getNotifier().notify(stuckDocuments);
043        }
044    }
045
046    /**
047     * Checks if needed dependencies are available in order to run this job. Due to the fact that this is a quartz job,
048     * it could trigger while the system is offline and then immediately get fired when the system starts up and due to
049     * the startup process it could attempt to execute while not all of the necessary services are fully initialized.
050     */
051    private void checkDependenciesAvailable() throws JobExecutionException {
052        if (getStuckDocumentService() == null || getNotifier() == null) {
053            String message = "Dependencies are not available for the stuck document notification job";
054            LOG.warn(message);
055            throw new JobExecutionException(message);
056        }
057    }
058
059
060    protected StuckDocumentService getStuckDocumentService() {
061        if (this.stuckDocumentService == null) {
062            this.stuckDocumentService = KEWServiceLocator.getStuckDocumentService();
063        }
064        return this.stuckDocumentService;
065    }
066
067    public void setStuckDocumentService(StuckDocumentService stuckDocumentService) {
068        this.stuckDocumentService = stuckDocumentService;
069    }
070
071
072    protected StuckDocumentNotifier getNotifier() {
073        if (this.notifier == null) {
074            this.notifier = KEWServiceLocator.getStuckDocumentNotifier();
075        }
076        return notifier;
077    }
078
079    public void setNotifier(StuckDocumentNotifier notifier) {
080        this.notifier = notifier;
081    }
082}