use of org.wso2.carbon.apimgt.api.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);
}
}
use of org.wso2.carbon.apimgt.api.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;
}
use of org.wso2.carbon.apimgt.api.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;
}
use of org.wso2.carbon.apimgt.api.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;
}
use of org.wso2.carbon.apimgt.api.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;
}
Aggregations