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.batch;
017
018import org.kuali.rice.kew.actionitem.ActionItem;
019import org.kuali.rice.kew.api.KewApiConstants;
020import org.kuali.rice.kew.service.KEWServiceLocator;
021
022import java.sql.Connection;
023import java.sql.DriverManager;
024import java.sql.ResultSet;
025import java.sql.SQLException;
026import java.sql.Statement;
027
028/**
029 * @see ExternalActnListNotificationService
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class ExternalActnListNotificationServiceImpl implements ExternalActnListNotificationService {
034
035    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
036            .getLogger(ExternalActnListNotificationServiceImpl.class);
037
038    private String password;
039    private String url;
040    private String username;
041
042    /**
043     * Specifies the default polling interval that should be used with this task.
044     */
045    private int externalActnListNotificationPollIntervalSeconds = 15;
046
047    /**
048     * Specifies the initial delay the poller should wait before starting to poll
049     */
050    private int externalActnListNotificationInitialDelaySeconds = 30;
051
052
053    public void run() {
054        LOG.info("checking for action items that have changed.");
055
056        Statement statement = null;
057        ResultSet rs = null;
058
059        try {
060            Connection connection = getConnection();
061            statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
062            rs = statement.executeQuery("SELECT ACTN_TYP, ACTN_ITM_ID FROM KREW_ACTN_ITM_CHANGED_T");
063
064            while (rs.next()) {
065                String actionType = rs.getString("ACTN_TYP");
066                String actionItemId = rs.getString("ACTN_ITM_ID");
067
068                boolean success = notifyExternalActionList(actionType, actionItemId);
069
070                if (success) {
071                    rs.deleteRow();
072                }
073            }
074        } catch (SQLException e) {
075            throw new RuntimeException("Error retrieving items from KREW_ACTN_ITM_CHANGED_T", e);
076        } finally {
077            if (statement != null) {
078                try {
079                 statement.close();
080                } catch (SQLException e) {
081                    LOG.warn("Could not close statement.");
082                }
083            }
084            if (rs != null) {
085                try {
086                    rs.close();
087                } catch (SQLException e) {
088                    LOG.warn("Could not close result set.");
089                }
090            }
091        }
092    }
093
094    private boolean notifyExternalActionList(String actionType, String actionItemId) {
095        LOG.info("actionType: " + actionType + "   actionItemId: " + actionItemId);
096        ActionItem actionItem = null;
097
098        // Get the action item unless it was deleted
099        if (actionType.toString().equalsIgnoreCase(KewApiConstants.ACTION_ITEM_INSERTED) || (actionType
100                .toString().equalsIgnoreCase(KewApiConstants.ACTION_ITEM_UPDATED))) {
101            actionItem = KEWServiceLocator.getActionListService().findByActionItemId(actionItemId);
102        }
103
104        if (actionType.toString().equalsIgnoreCase(KewApiConstants.ACTION_ITEM_INSERTED)) {
105            LOG.info("Code to INSERT into external action list goes here");
106        } else if (actionType.toString().equalsIgnoreCase(KewApiConstants.ACTION_ITEM_UPDATED)) {
107            LOG.info("Code to UPDATE external action list goes here");
108        } else if (actionType.toString().equalsIgnoreCase(KewApiConstants.ACTION_ITEM_DELETED)) {
109            LOG.info("Code to DELETE from external action list goes here");
110        }
111
112        // Currently always return true to indicate successful processing.
113        // This may change once the above code is implemented.
114        return true;
115    }
116
117    public Connection getConnection() throws SQLException {
118        return DriverManager.getConnection(url, username, password);
119    }
120
121    /**
122     * Sets the polling interval time in seconds
123     * @param seconds the polling interval time in seconds
124     */
125    public void setExternalActnListNotificationPollIntervalSeconds(int seconds) {
126        this.externalActnListNotificationPollIntervalSeconds = seconds;
127    }
128
129    /**
130     * Gets the polling interval time in seconds
131     * @return the polling interval time in seconds
132     */
133    public int getExternalActnListNotificationPollIntervalSeconds() {
134        return this.externalActnListNotificationPollIntervalSeconds;
135    }
136
137    /**
138     * Sets the initial delay time in seconds
139     * @param seconds the initial delay time in seconds
140     */
141    public void setExternalActnListNotificationInitialDelaySeconds(int seconds) {
142        this.externalActnListNotificationInitialDelaySeconds = seconds;
143    }
144
145    /**
146     * Gets the initial delay time in seconds
147     * @return the initial delay time in seconds
148     */
149    public int getExternalActnListNotificationInitialDelaySeconds() {
150        return this.externalActnListNotificationInitialDelaySeconds;
151    }
152
153    public void setPassword(String password) {
154        this.password = password;
155    }
156
157    public synchronized void setUrl(String url) {
158        this.url = url;
159    }
160
161    public void setUsername(String username) {
162        this.username = username;
163    }
164
165}