Search in sources :

Example 26 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method addApplication.

/**
 * Add a new instance of an Application
 *
 * @param application The {@link Application} object to be added
 * @throws APIMgtDAOException   If failed to add application.
 */
@Override
public void addApplication(Application application) throws APIMgtDAOException {
    final String addAppQuery = "INSERT INTO AM_APPLICATION (UUID, NAME, APPLICATION_POLICY_ID, " + "DESCRIPTION, APPLICATION_STATUS, CREATED_BY, CREATED_TIME, UPDATED_BY, " + "LAST_UPDATED_TIME) VALUES (?, ?,?,?,?,?,?,?,?)";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(addAppQuery)) {
            ps.setString(1, application.getId());
            ps.setString(2, application.getName());
            ps.setString(3, application.getPolicy().getUuid());
            ps.setString(4, application.getDescription());
            if (APIMgtConstants.DEFAULT_APPLICATION_NAME.equals(application.getName())) {
                ps.setString(5, APIMgtConstants.ApplicationStatus.APPLICATION_APPROVED);
            } else {
                ps.setString(5, APIMgtConstants.ApplicationStatus.APPLICATION_CREATED);
            }
            ps.setString(6, application.getCreatedUser());
            ps.setTimestamp(7, Timestamp.valueOf(application.getCreatedTime()));
            ps.setString(8, application.getCreatedUser());
            ps.setTimestamp(9, Timestamp.valueOf(application.getCreatedTime()));
            ps.executeUpdate();
            addApplicationPermission(conn, application.getPermissionMap(), application.getId());
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding application: " + application.getId(), ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding application: " + application.getId(), ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 27 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method getApplication.

/**
 * Retrieve a given instance of an Application
 *
 * @param appId The UUID that uniquely identifies an Application
 * @return valid {@link Application} object or null
 * @throws APIMgtDAOException   If failed to retrieve application.
 */
@Override
public Application getApplication(String appId) throws APIMgtDAOException {
    final String completeGetAppQuery = GET_APPS_WITH_POLICY_QUERY + " AND APPLICATION.UUID = ?";
    Application application;
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(completeGetAppQuery)) {
        ps.setString(1, appId);
        try (ResultSet rs = ps.executeQuery()) {
            application = this.createApplicationFromResultSet(rs);
            if (application == null) {
                throw new APIMgtDAOException("Application is not available in the system.", ExceptionCodes.APPLICATION_NOT_FOUND);
            }
            application.setApplicationKeys(getApplicationKeys(appId));
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting application: " + appId, ex);
    }
    return application;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 28 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method getApplicationByName.

/**
 * Retrieve a given instance of an Application
 *
 * @param appName Name of the Application
 * @param ownerId ID of the application owner.
 * @return valid {@link Application} object or null
 * @throws APIMgtDAOException   If failed to retrieve application.
 */
@Override
public Application getApplicationByName(String appName, String ownerId) throws APIMgtDAOException {
    final String completeGetAppQuery = GET_APPS_WITH_POLICY_QUERY + "AND APPLICATION.NAME = ? AND APPLICATION" + ".CREATED_BY = ?";
    Application application;
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(completeGetAppQuery)) {
        ps.setString(1, appName);
        ps.setString(2, ownerId);
        try (ResultSet rs = ps.executeQuery()) {
            application = this.createApplicationFromResultSet(rs);
        }
        if (application == null) {
            throw new APIMgtDAOException("Application is not available in the system.", ExceptionCodes.APPLICATION_NOT_FOUND);
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting application(name: " + appName + ", " + "ownerId: " + ownerId + ")", ex);
    }
    return application;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 29 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method getAllApplications.

@Override
public List<Application> getAllApplications() throws APIMgtDAOException {
    List<Application> applicationList = new ArrayList<>();
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(GET_APPS_QUERY)) {
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                String createdUser = rs.getString("CREATED_BY");
                Application application = new Application(rs.getString("NAME"), createdUser);
                application.setId(rs.getString("UUID"));
                application.setStatus(rs.getString("APPLICATION_STATUS"));
                application.setPolicy(new ApplicationPolicy(rs.getString("APPLICATION_POLICY_ID"), ""));
                applicationList.add(application);
            }
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting all applications", ex);
    }
    return applicationList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 30 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method createApplicationFromResultSet.

private Application createApplicationFromResultSet(ResultSet rs) throws APIMgtDAOException, SQLException {
    Application application = null;
    if (rs.next()) {
        String createdUser = rs.getString("CREATED_BY");
        application = new Application(rs.getString("NAME"), createdUser);
        application.setId(rs.getString("UUID"));
        application.setDescription(rs.getString("DESCRIPTION"));
        application.setStatus(rs.getString("APPLICATION_STATUS"));
        application.setCreatedTime(rs.getTimestamp("CREATED_TIME").toLocalDateTime());
        application.setUpdatedUser(rs.getString("UPDATED_BY"));
        application.setUpdatedTime(rs.getTimestamp("LAST_UPDATED_TIME").toLocalDateTime());
        application.setPolicy(new ApplicationPolicy(rs.getString("APPLICATION_POLICY_NAME")));
    }
    return application;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) Application(org.wso2.carbon.apimgt.core.models.Application)

Aggregations

APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)333 SQLException (java.sql.SQLException)192 Connection (java.sql.Connection)146 PreparedStatement (java.sql.PreparedStatement)129 Test (org.testng.annotations.Test)84 ResultSet (java.sql.ResultSet)72 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)72 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)57 API (org.wso2.carbon.apimgt.core.models.API)57 ArrayList (java.util.ArrayList)50 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)35 Application (org.wso2.carbon.apimgt.core.models.Application)24 HashMap (java.util.HashMap)23 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)22 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)21 BeforeTest (org.testng.annotations.BeforeTest)21 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)20 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)20 IOException (java.io.IOException)19 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)17