Search in sources :

Example 11 with OpenClinicaException

use of org.akaza.openclinica.exception.OpenClinicaException in project OpenClinica by OpenClinica.

the class NewCRFBean method deleteFromDB.

public void deleteFromDB() throws OpenClinicaException {
    Statement s = null;
    Connection con = null;
    ResultSet rs = null;
    ResultSet rs2 = null;
    ArrayList error = new ArrayList();
    int count = 0;
    try {
        con = ds.getConnection();
        CoreResources.setSchema(con);
        if (con.isClosed()) {
            String msg = "The connection to the database is not open.";
            error.add(msg);
            throw new OpenClinicaException("newCRFBean, deleteFromDB, connection not open", "1");
        }
        // commenting this out temporarily so that mistakes are not made
        con.setAutoCommit(false);
        // delete version and related info
        int last = deleteQueries.size();
        for (int th = 0; th < last; th++) {
            // it.next();
            String query = (String) deleteQueries.get(th);
            count = th;
            s = con.createStatement();
            // logger.info(query);
            s.executeUpdate(query);
            s.close();
            error.add(query);
        }
        /*
             * //now need to insert all items again-no need to do this
             * 04/12/2005 any more Set mySet = backupItemQueries.entrySet();
             * logger.info("---start of item generation here---"); for (Iterator
             * itvl = mySet.iterator(); itvl.hasNext(); ) { Map.Entry ment =
             * (Map.Entry)itvl.next(); String pQuery = (String)ment.getValue();
             * s = con.createStatement(); //logger.info(pQuery);
             * s.executeUpdate(pQuery); s.close(); } logger.info("---pause in
             * query generation, items---");
             */
        con.commit();
        logger.debug("---end of delete query generation, all queries committed---");
        con.setAutoCommit(true);
        logger.debug("---end of delete query generation, autocommit set to true---");
    } catch (SQLException se) {
        se.printStackTrace();
        try {
            con.rollback();
            logger.debug("Error detected, rollback " + se.getMessage());
            String msg2 = "The following error was returned from the database: " + se.getMessage() + " using the following query: " + deleteQueries.get(count);
            error.add(msg2);
            this.setDeleteErrors(error);
            con.setAutoCommit(true);
            throw new OpenClinicaException("", "");
        } catch (SQLException seq) {
            seq.printStackTrace();
            logger.debug("Error within rollback " + seq.getMessage());
            String msg2 = "The following error was returned from the database: " + seq.getMessage();
            error.add(msg2);
            this.setDeleteErrors(error);
            throw new OpenClinicaException("", "");
        }
    } catch (OpenClinicaException pe) {
        pe.printStackTrace();
        try {
            con.rollback();
            logger.debug("OpenClinica Error detected, rollback " + pe.getMessage());
            String msg2 = "The following error was returned from the application: " + pe.getMessage();
            error.add(msg2);
            this.setDeleteErrors(error);
            con.setAutoCommit(true);
            throw new OpenClinicaException("", "");
        } catch (SQLException seq) {
            seq.printStackTrace();
            logger.debug("OpenClinica Error within rollback " + seq.getMessage());
            String msg2 = "The following error was returned from the application: " + seq.getMessage();
            error.add(msg2);
            this.setDeleteErrors(error);
            throw new OpenClinicaException("", "");
        // catching OpenClinica exceptions in general is duplicating
        // effort, maybe we should nix it?
        }
    } finally {
        try {
            if (con != null)
                con.close();
            if (s != null)
                s.close();
            if (rs != null)
                rs.close();
            if (rs2 != null)
                rs2.close();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OpenClinicaException(e.getMessage(), "1");
        }
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException)

Example 12 with OpenClinicaException

use of org.akaza.openclinica.exception.OpenClinicaException in project OpenClinica by OpenClinica.

the class NewCRFBean method deleteInsertToDB.

/*
     * this function is used for replace CRF version only
     * it runs all delete and insert statements in one batch
     */
public synchronized void deleteInsertToDB() throws OpenClinicaException {
    Statement statement = null;
    Connection con = null;
    ResultSet rs = null;
    PreparedStatement prep_statement = null;
    ArrayList<String> error = new ArrayList<String>();
    String cur_query = null;
    try {
        con = ds.getConnection();
        CoreResources.setSchema(con);
        if (con.isClosed()) {
            error.add("The connection to the database is not open.");
            throw new OpenClinicaException("newCRFBean, deleteInsertToDB, connection not open", "1");
        }
        con.setAutoCommit(false);
        // delete version and related info
        for (String dQuery : (ArrayList<String>) deleteQueries) {
            logger.debug(dQuery);
            cur_query = dQuery;
            if (cur_query == null || cur_query.trim().length() < 1) {
                continue;
            }
            statement = con.createStatement();
            statement.executeUpdate(dQuery);
            statement.close();
        }
        logger.debug("deleteInsertToDB function ---end of delete query generation, all queries committed---");
        logger.debug("deleteInsertToDB function ---start of item query generation here---");
        for (String pQuery : (Collection<String>) itemQueries.values()) {
            logger.debug(pQuery);
            cur_query = pQuery;
            if (cur_query == null || cur_query.trim().length() < 1) {
                continue;
            }
            statement = con.createStatement();
            statement.executeUpdate(pQuery);
            statement.close();
        }
        logger.debug("deleteInsertToDB function  ---pause in query generation, items---");
        for (String crQuery : (ArrayList<String>) queries) {
            logger.debug(crQuery);
            cur_query = crQuery;
            if (cur_query == null || cur_query.trim().length() < 1) {
                continue;
            }
            statement = con.createStatement();
            statement.executeUpdate(crQuery);
            statement.close();
        }
        // the below lines are temporarily commented out for instrument
        // upload, tbh 8-13
        con.commit();
        logger.debug("---end of query generation, all queries committed---");
        con.setAutoCommit(true);
        // to be the active version, tbh, 8-29
        if (crfId != 0) {
            String sql = digester.getQuery("findDefaultVersion");
            logger.debug("findDefaultVersion [" + sql + "]");
            prep_statement = con.prepareStatement(sql);
            prep_statement.setInt(1, crfId);
            rs = prep_statement.executeQuery();
            if (rs.next()) {
                // first
                if (rs.wasNull()) {
                    String sql2 = digester.getQuery("updateDefaultVersion");
                    prep_statement = con.prepareStatement(sql2);
                    prep_statement.setInt(1, crfId);
                    prep_statement.setInt(2, crfId);
                    if (prep_statement.executeUpdate() != 1) {
                        throw new OpenClinicaException("error, updated more than one row, smart assigner part of insertToDB, NewCRFBean", "");
                    }
                }
            }
            rs.close();
            prep_statement.close();
        }
    } catch (SQLException se) {
        logger.error(se.getMessage());
        try {
            con.rollback();
            logger.error("Error detected, rollback " + se.getMessage());
            String msg2 = "The following error was returned from the database: " + se.getMessage() + " using the following query: " + cur_query;
            error.add(msg2);
            throw new OpenClinicaException("", "");
        } catch (SQLException seq) {
            error.add("The following error was returned from the database: " + seq.getMessage());
            logger.error("Error within rollback " + seq.getMessage());
            throw new OpenClinicaException("", "");
        }
    } catch (OpenClinicaException pe) {
        pe.printStackTrace();
        try {
            error.add("The following error was returned from the application: " + pe.getMessage());
            con.rollback();
            logger.error("OpenClinica Error detected, rollback " + pe.getMessage());
            throw new OpenClinicaException("", "");
        } catch (SQLException seq) {
            error.add("The following error was returned from the application: " + seq.getMessage());
            logger.error("OpenClinica Error within rollback " + seq.getMessage());
            throw new OpenClinicaException("", "");
        }
    } finally {
        try {
            if (con != null) {
                con.setAutoCommit(true);
            }
        } catch (SQLException sac) {
            ;
        }
        try {
            if (con != null)
                con.close();
            if (statement != null)
                statement.close();
            if (prep_statement != null)
                prep_statement.close();
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            error.add("The following error was returned from the application: " + e.getMessage());
            logger.error(e.getMessage());
            throw new OpenClinicaException(e.getMessage(), "1");
        }
        this.setErrors(error);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) Collection(java.util.Collection) PreparedStatement(java.sql.PreparedStatement) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException)

Example 13 with OpenClinicaException

use of org.akaza.openclinica.exception.OpenClinicaException in project OpenClinica by OpenClinica.

the class NewCRFBean method insertToDB.

public void insertToDB() throws OpenClinicaException {
    /*
         * insertToDB() will implement rollback functionality, throwing an error
         * will write the error to the setErrors and redirect to the error page,
         * rather than to the success page, tbh, 6-6-03
         */
    Statement s = null;
    PreparedStatement ps = null;
    Connection con = null;
    ResultSet rs = null;
    ResultSet rs2 = null;
    ArrayList error = new ArrayList();
    int count = 0;
    try {
        con = ds.getConnection();
        CoreResources.setSchema(con);
        if (con.isClosed()) {
            String msg = "The connection to the database is not open.";
            error.add(msg);
            throw new OpenClinicaException("newCRFBean, insertToDB, connection not open", "1");
        }
        // commenting this out temporarily so that mistakes are not made,
        // tbh 8-13
        con.setAutoCommit(false);
        Set mySet = itemQueries.entrySet();
        logger.debug("---start of item query generation here---");
        for (Iterator itvl = mySet.iterator(); itvl.hasNext(); ) {
            Map.Entry ment = (Map.Entry) itvl.next();
            String pQuery = (String) ment.getValue();
            s = con.createStatement();
            logger.debug(pQuery);
            s.executeUpdate(pQuery);
            s.close();
        // this might throw off the 'error' count, who can say?
        // of course, the queries are simple enough that maybe we'll
        // never throw
        // an error. Never say never though...
        }
        logger.debug("---pause in query generation, items---");
        // Iterator it = queries.iterator();
        // using iterator gets us out of order, and throws everything off.
        // try a for loop instead
        int last = queries.size();
        for (int th = 0; th < last; th++) {
            // it.next();
            String query = (String) queries.get(th);
            count = th;
            s = con.createStatement();
            s.executeUpdate(query);
            s.close();
            error.add(query);
        }
        // the below lines are temporarily commented out for instrument
        // upload, tbh 8-13
        con.commit();
        logger.debug("---end of query generation, all queries committed---");
        con.setAutoCommit(true);
        logger.debug("---end of query generation, autocommit set to true---");
        // to be the active version, tbh, 8-29
        if (crfId != 0) {
            String sql = digester.getQuery("findDefaultVersion");
            logger.debug("findDefaultVersion [" + sql + "]");
            ps = con.prepareStatement(sql);
            ps.setInt(1, crfId);
            rs = ps.executeQuery();
            if (rs.next()) {
                // do nothing on purpose? no, we check to see if it was null
                // first
                int test = rs.getInt(1);
                if (rs.wasNull()) {
                    String sql2 = digester.getQuery("updateDefaultVersion");
                    ps = con.prepareStatement(sql2);
                    ps.setInt(1, crfId);
                    ps.setInt(2, crfId);
                    if (ps.executeUpdate() != 1) {
                        throw new OpenClinicaException("error, updated more than one row, smart assigner part of insertToDB, NewCRFBean", "");
                    }
                // }
                // rs2.close();
                }
            }
            rs.close();
            ps.close();
        }
    // hairy code--but it will serve as a 'smart assigner' to set a
    // version active if
    // it's the first version entered, tbh, 8-29
    } catch (SQLException se) {
        se.printStackTrace();
        try {
            con.rollback();
            logger.debug("Error detected, rollback " + se.getMessage());
            String msg2 = "The following error was returned from the database: " + se.getMessage() + " using the following query: " + queries.get(count);
            error.add(msg2);
            this.setErrors(error);
            con.setAutoCommit(true);
            throw new OpenClinicaException("", "");
        } catch (SQLException seq) {
            seq.printStackTrace();
            logger.debug("Error within rollback " + seq.getMessage());
            String msg2 = "The following error was returned from the database: " + seq.getMessage();
            error.add(msg2);
            this.setErrors(error);
            throw new OpenClinicaException("", "");
        }
    } catch (OpenClinicaException pe) {
        pe.printStackTrace();
        try {
            con.rollback();
            logger.debug("OpenClinica Error detected, rollback " + pe.getMessage());
            String msg2 = "The following error was returned from the application: " + pe.getMessage();
            error.add(msg2);
            this.setErrors(error);
            con.setAutoCommit(true);
            throw new OpenClinicaException("", "");
        } catch (SQLException seq) {
            seq.printStackTrace();
            logger.debug("OpenClinica Error within rollback " + seq.getMessage());
            String msg2 = "The following error was returned from the application: " + seq.getMessage();
            error.add(msg2);
            this.setErrors(error);
            throw new OpenClinicaException("", "");
        // catching OpenClinica exceptions in general is duplicating
        // effort, maybe we should nix it?
        }
    } finally {
        try {
            if (con != null)
                con.close();
            if (s != null)
                s.close();
            if (ps != null)
                ps.close();
            if (rs != null)
                rs.close();
            if (rs2 != null)
                rs2.close();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OpenClinicaException(e.getMessage(), "1");
        }
    }
}
Also used : Set(java.util.Set) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with OpenClinicaException

use of org.akaza.openclinica.exception.OpenClinicaException in project OpenClinica by OpenClinica.

the class NewCRFBean method listVersionNames.

public HashMap listVersionNames() throws OpenClinicaException {
    /*
         * serves up a list of all crf names and versions, necessary for
         * checking for duplicates. Added by tbh, 7-25-03
         */
    PreparedStatement ps = null;
    Connection con = null;
    ResultSet rs = null;
    HashMap returnMe = new HashMap();
    String sql = digester.getQuery("findVersionNamesForCRF");
    try {
        con = ds.getConnection();
        CoreResources.setSchema(con);
        if (con.isClosed()) {
            throw new OpenClinicaException("Con is closed: NewCRFBean", "");
        }
        ps = con.prepareStatement(sql);
        ps.setInt(1, crfId);
        rs = ps.executeQuery();
        String key = null;
        String value = null;
        while (rs.next()) {
            key = rs.getString(1);
            value = rs.getString(2);
            returnMe.put(key, value);
        }
        return returnMe;
    } catch (SQLException se) {
        se.printStackTrace();
        throw new OpenClinicaException("SQLException: " + se.getMessage(), "");
    } catch (OpenClinicaException pe) {
        pe.printStackTrace();
        throw new OpenClinicaException("OpenClinicaException: " + pe.getMessage(), "");
    } finally {
        try {
            if (con != null)
                con.close();
            if (ps != null)
                ps.close();
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OpenClinicaException(e.getMessage(), "1");
        }
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException)

Example 15 with OpenClinicaException

use of org.akaza.openclinica.exception.OpenClinicaException in project OpenClinica by OpenClinica.

the class NewCRFBean method listItemNames.

public HashMap listItemNames(int crfId) throws OpenClinicaException {
    /*
         * serves up a list of all item names in a CRF, necessary for checking
         * for duplicates and creating new items. Added by tbh, 7-25-03
         */
    PreparedStatement ps = null;
    Connection con = null;
    ResultSet rs = null;
    HashMap returnMe = new HashMap();
    String sql = digester.getQuery("findItemNamesByCRF");
    logger.debug("crf id: *******" + crfId);
    try {
        con = ds.getConnection();
        CoreResources.setSchema(con);
        if (con.isClosed()) {
            throw new OpenClinicaException("Con is closed: NewCRFBean", "");
        }
        ps = con.prepareStatement(sql);
        ps.setInt(1, crfId);
        rs = ps.executeQuery();
        String key = null;
        String value = null;
        while (rs.next()) {
            key = rs.getString(1);
            value = "1";
            returnMe.put(key, value);
        }
        return returnMe;
    } catch (SQLException se) {
        se.printStackTrace();
        throw new OpenClinicaException("SQLException: " + se.getMessage(), "");
    } catch (OpenClinicaException pe) {
        pe.printStackTrace();
        throw new OpenClinicaException("OpenClinicaException: " + pe.getMessage(), "");
    } finally {
        try {
            if (con != null)
                con.close();
            if (ps != null)
                ps.close();
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new OpenClinicaException(e.getMessage(), "1");
        }
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException)

Aggregations

OpenClinicaException (org.akaza.openclinica.exception.OpenClinicaException)23 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)11 ItemBean (org.akaza.openclinica.bean.submit.ItemBean)8 DisplayItemBean (org.akaza.openclinica.bean.submit.DisplayItemBean)7 ItemFormMetadataBean (org.akaza.openclinica.bean.submit.ItemFormMetadataBean)7 ItemDAO (org.akaza.openclinica.dao.submit.ItemDAO)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)6 ItemFormMetadataDAO (org.akaza.openclinica.dao.submit.ItemFormMetadataDAO)6 MessageFormat (java.text.MessageFormat)5 EventCRFBean (org.akaza.openclinica.bean.submit.EventCRFBean)5 ItemGroupBean (org.akaza.openclinica.bean.submit.ItemGroupBean)5 StudyDAO (org.akaza.openclinica.dao.managestudy.StudyDAO)5 Iterator (java.util.Iterator)4 List (java.util.List)4 Status (org.akaza.openclinica.bean.core.Status)4