Search in sources :

Example 11 with SqlLibrary

use of org.cerberus.crud.entity.SqlLibrary in project cerberus-source by cerberustesting.

the class SqlLibraryDAO method findSqlLibraryByKey.

/**
 * Short one line description.
 * <p/>
 * Longer description. If there were any, it would be here. <p> And even
 * more explanations to follow in consecutive paragraphs separated by HTML
 * paragraph breaks.
 *
 * @param name Description text text text.
 * @return Description text text text.
 */
@Override
public SqlLibrary findSqlLibraryByKey(String name) throws CerberusException {
    boolean throwEx = false;
    SqlLibrary result = null;
    final String query = "SELECT * FROM sqllibrary  WHERE NAME = ?";
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, name);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    String type = resultSet.getString("Type");
                    String database = resultSet.getString("Database");
                    String script = resultSet.getString("Script");
                    String description = resultSet.getString("Description");
                    result = factorySqlLib.create(name, type, database, script, description);
                } else {
                    throwEx = true;
                }
            } catch (SQLException exception) {
                LOG.warn("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.warn("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    if (throwEx) {
        throw new CerberusException(new MessageGeneral(MessageGeneralEnum.SQLLIB_NOT_FOUND));
    }
    return result;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) MessageGeneral(org.cerberus.engine.entity.MessageGeneral) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SqlLibrary(org.cerberus.crud.entity.SqlLibrary) IFactorySqlLibrary(org.cerberus.crud.factory.IFactorySqlLibrary) FactorySqlLibrary(org.cerberus.crud.factory.impl.FactorySqlLibrary) PreparedStatement(java.sql.PreparedStatement)

Example 12 with SqlLibrary

use of org.cerberus.crud.entity.SqlLibrary in project cerberus-source by cerberustesting.

the class SqlLibraryDAO method readByKey.

@Override
public AnswerItem readByKey(String key) {
    AnswerItem a = new AnswerItem();
    StringBuilder query = new StringBuilder();
    SqlLibrary p = new SqlLibrary();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    query.append("SELECT * FROM sqllibrary `sql` WHERE Name = ?");
    try (Connection connection = this.databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(query.toString())) {
        preStat.setString(1, key);
        try (ResultSet resultSet = preStat.executeQuery()) {
            // gets the data
            while (resultSet.next()) {
                p = this.loadFromResultSet(resultSet);
            }
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
            msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
        } catch (SQLException e) {
            LOG.error("Unable to execute query : " + e.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", e.toString()));
        }
    } catch (SQLException e) {
        LOG.error("Unable to execute query : " + e.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", e.toString()));
    }
    a.setResultMessage(msg);
    a.setItem(p);
    return a;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SqlLibrary(org.cerberus.crud.entity.SqlLibrary) IFactorySqlLibrary(org.cerberus.crud.factory.IFactorySqlLibrary) FactorySqlLibrary(org.cerberus.crud.factory.impl.FactorySqlLibrary) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 13 with SqlLibrary

use of org.cerberus.crud.entity.SqlLibrary in project cerberus-source by cerberustesting.

the class SqlLibraryService method updateSqlLibrary.

@Override
public void updateSqlLibrary(String name, String type, String database, String description, String script) {
    try {
        SqlLibrary s = sqlLibraryDao.findSqlLibraryByKey(name);
        s.setType(type);
        s.setDatabase(database);
        s.setDescription(description);
        s.setScript(script);
        sqlLibraryDao.updateSqlLibrary(s);
    } catch (CerberusException e) {
        LOG.warn("Unable to execute query : " + e.toString());
    }
}
Also used : CerberusException(org.cerberus.exception.CerberusException) SqlLibrary(org.cerberus.crud.entity.SqlLibrary)

Aggregations

SqlLibrary (org.cerberus.crud.entity.SqlLibrary)13 IFactorySqlLibrary (org.cerberus.crud.factory.IFactorySqlLibrary)7 ISqlLibraryService (org.cerberus.crud.service.ISqlLibraryService)6 JSONObject (org.json.JSONObject)6 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 FactorySqlLibrary (org.cerberus.crud.factory.impl.FactorySqlLibrary)5 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 AnswerItem (org.cerberus.util.answer.AnswerItem)5 ApplicationContext (org.springframework.context.ApplicationContext)4 ILogEventService (org.cerberus.crud.service.ILogEventService)3 CerberusException (org.cerberus.exception.CerberusException)3 Answer (org.cerberus.util.answer.Answer)3 ArrayList (java.util.ArrayList)2 SqlLibraryService (org.cerberus.crud.service.impl.SqlLibraryService)2 MessageGeneral (org.cerberus.engine.entity.MessageGeneral)2 AnswerList (org.cerberus.util.answer.AnswerList)2 PrintWriter (java.io.PrintWriter)1