dao.RuleDao.java Source code

Java tutorial

Introduction

Here is the source code for dao.RuleDao.java

Source

/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import logging.Level;
import logging.Logger;
import model.NewRule;
import model.Project;

import org.json.simple.JSONObject;

import exceptions.ConnectionClosedException;
import exceptions.DatabaseException;
import exceptions.DatabaseValueNotFoundException;
import exceptions.MissingPropertiesFile;

/**
 * @author martijn
 *
 */
public class RuleDao extends DataAccessObject {
    /**
     * @throws IOException
     * @throws DatabaseException
     * @throws MissingPropertiesFile
     * @throws SQLException
     * @throws ConnectionClosedException
     */
    public RuleDao()
            throws IOException, DatabaseException, MissingPropertiesFile, SQLException, ConnectionClosedException {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * Deletes a rule from a target DB and from the local APEX db
     * 
     * @param id
     * @return
     * @throws Exception
     */
    public boolean deleteRule(int id) throws Exception {
        int brgId = 0;
        String name = "";
        PreparedStatement statement = this.getApexConnection()
                .prepareStatement("SELECT BRG_PROJECTID, NAME FROM BUSINESSRULE WHERE ID=?");
        statement.setInt(1, id);
        ResultSet result = statement.executeQuery();
        while (result.next()) {
            brgId = result.getInt("BRG_PROJECTID");
            name = result.getString("NAME");
        }
        statement.close();
        Statement statementTarget = null;

        try {
            Project project = this.getProject(brgId);
            statementTarget = this.getTargetConnection(project.getTargetConfig()).createStatement();
            statementTarget.executeUpdate("DROP TRIGGER " + name.toUpperCase());
            statementTarget.close();
        } catch (Exception e) {
            Logger log = new Logger();
            log.out(Level.ERROR, "deleteRule", "Rule or project doesn't excist");
        }

        Statement statementRemoveApex = this.getApexConnection().createStatement();
        statementRemoveApex.addBatch("DELETE FROM RULE_COLUMN WHERE BUSINESSRULEID=" + id);
        statementRemoveApex.addBatch("DELETE FROM RULE_TABLE WHERE BUSINESSRULEID=" + id);
        statementRemoveApex.addBatch("DELETE FROM RULE_VALUE WHERE BUSINESSRULEID=" + id);
        statementRemoveApex.addBatch("DELETE FROM BUSINESSRULE WHERE ID=" + id);
        statementRemoveApex.executeBatch();
        statementRemoveApex.close();

        return true;
    }

    /**
     * Create a new rule based on incoming NewRule class
     * 
     * @param rule
     * @return
     * @throws SQLException
     */
    public boolean createRule(NewRule rule) throws SQLException {
        Logger log = new Logger();
        log.out(Level.INFORMATIVE, "createRule", "Start making rules");
        Statement statement = this.getApexConnection().createStatement();

        statement.addBatch("DELETE RULE_COLUMN WHERE BUSINESSRULEID=" + rule.getBusinessrule());
        statement.addBatch("DELETE RULE_VALUE WHERE BUSINESSRULEID=" + rule.getBusinessrule());
        statement.addBatch("DELETE RULE_TABLE WHERE BUSINESSRULEID=" + rule.getBusinessrule());

        if (rule.getColumns() != null) {
            for (String column : rule.getColumns()) {
                statement.addBatch("INSERT INTO RULE_COLUMN (NAME, BUSINESSRULEID) VALUES ('" + column + "', '"
                        + rule.getBusinessrule() + "')");
            }
        } else {
            log.out(Level.ERROR, "createRule", "No columns found");
        }
        if (rule.getValues() != null) {
            for (String value : rule.getValues()) {
                statement.addBatch("INSERT INTO RULE_VALUE (NAME, BUSINESSRULEID) VALUES ('" + value + "', '"
                        + rule.getBusinessrule() + "')");
            }
        } else {
            log.out(Level.ERROR, "createRule", "No values found");
        }
        if (rule.getTables() != null) {
            for (String table : rule.getTables()) {
                statement.addBatch("INSERT INTO RULE_TABLE (NAME, BUSINESSRULEID) VALUES ('" + table + "', '"
                        + rule.getBusinessrule() + "')");
            }
        } else {
            log.out(Level.ERROR, "createRule", "No tables found");
        }
        statement.executeBatch();
        statement.close();
        return true;
    }
}