adding DAO methods for pplication management

merge-requests/7/head
lasantha 7 years ago
parent 81be917e10
commit 45c668ecce

@ -205,10 +205,10 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
public Response createApplication(@Valid Application application) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
application = applicationManager.createApplication(application);
Application application2 = applicationManager.createApplication(application);
if (application != null){
return Response.status(Response.Status.OK).entity(application).build();
return Response.status(Response.Status.CREATED).entity(application2).build();
}else{
String msg = "Given device type is not matched with existing device types";
log.error(msg);

@ -21,6 +21,7 @@ package org.wso2.carbon.device.application.mgt.core.dao;
import org.wso2.carbon.device.application.mgt.common.*;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import java.sql.Timestamp;
import java.util.List;
/**
@ -76,23 +77,24 @@ public interface ApplicationDAO {
/**
* To get the application with the given uuid
*
* @param uuid UUID of the application to be retrieved.
* @param appName name of the application to be retrieved.
* @param tenantId ID of the tenant.
* @param userName Name of the user.
* @param appType Type of the application.
* @return the application
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
Application getApplication(String uuid, int tenantId, String userName) throws ApplicationManagementDAOException;
Application getApplication(String appName, String appType, int tenantId) throws ApplicationManagementDAOException;
/**
* To get the application id of the application specified by the UUID
*
* @param uuid UUID of the application.
* @param appName name of the application.
* @param appType type of the application.
* @param tenantId ID of the tenant.
* @return ID of the Application.
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
int getApplicationId(String uuid, int tenantId) throws ApplicationManagementDAOException;
int getApplicationId(String appName, String appType, int tenantId) throws ApplicationManagementDAOException;
/**
* To edit the given application.
@ -161,6 +163,18 @@ public interface ApplicationDAO {
List<LifecycleStateTransition> getNextLifeCycleStates(String applicationUUID, int tenantId)
throws ApplicationManagementDAOException;
/**
* To get the next possible lifecycle states for the application.
*
* @param lifecycle lifecycle of the application.
* @param tenantId tenantId of the application useer.
* @param appReleaseId relesse id of the application.
* @param appId application id of the application.
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
public void addLifecycle(Lifecycle lifecycle, int tenantId, int appReleaseId, int appId)
throws ApplicationManagementDAOException;
/**
* To update the screen-shot count of a application.
*

@ -21,10 +21,7 @@ package org.wso2.carbon.device.application.mgt.core.dao.common;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.wso2.carbon.device.application.mgt.common.Application;
import org.wso2.carbon.device.application.mgt.common.Lifecycle;
import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.common.User;
import org.wso2.carbon.device.application.mgt.common.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -45,50 +42,105 @@ public class Util {
* To create application object from the result set retrieved from the Database.
*
* @param rs ResultSet
* @param rsProperties Properties resultset.
* @param rsTags Tags resultset
* @return Application that is retrieved from the Database.
* @return List of Applications that is retrieved from the Database.
* @throws SQLException SQL Exception
* @throws JSONException JSONException.
*/
public static Application loadApplication(ResultSet rs, ResultSet rsProperties, ResultSet rsTags)
throws SQLException, JSONException {
Application application = new Application();
application.setId(rs.getInt("ID"));
application.setName(rs.getString("NAME"));
application.setUuid(rs.getString("UUID"));
application.setShortDescription(rs.getString("SHORT_DESCRIPTION"));
application.setDescription(rs.getString("DESCRIPTION"));
application.setScreenShotCount(rs.getInt("SCREEN_SHOT_COUNT"));
application.setVideoName(rs.getString("VIDEO_NAME"));
application.setCreatedAt(rs.getDate("CREATED_AT"));
application.setModifiedAt(rs.getDate("MODIFIED_AT"));
application.setUser(new User(rs.getString("CREATED_BY"), rs.getInt("TENANT_ID")));
Map<String, String> properties = new HashMap<>();
while (rsProperties.next()) {
properties.put(rsProperties.getString("PROP_KEY"), rsProperties.getString("PROP_VAL"));
}
application.setProperties(properties);
List<String> tags = new ArrayList<>();
while ((rsTags.next())) {
tags.add(rsTags.getString("NAME"));
public static List<Application> loadApplications(ResultSet rs) throws SQLException, JSONException {
List<Application> applications = new ArrayList<>();
Application application = null ;
int applicatioId = -1;
while (rs.next()){
if (applicatioId != rs.getInt("APP_ID")){
if( application != null){
applications.add(application);
}
applicatioId = rs.getInt("APP_ID");
application = new Application();
application.setId(applicatioId);
application.setName(rs.getString("APP_NAME"));
application.setType(rs.getString("APP_TYPE"));
application.setAppCategory(rs.getString("APP_CATEGORY"));
application.setIsFree(rs.getInt("IS_FREE"));
application.setIsRestricted(rs.getInt("RESTRICTED"));
List<Tag> tags = new ArrayList<>();
Tag tag = new Tag();
tag.setTagName(rs.getString("APP_TAG"));
tags.add(tag);
application.setTags(tags);
LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setId(rs.getInt("LIFECYCLE_STATE_ID"));
lifecycleState.setName(rs.getString("LS_NAME"));
lifecycleState.setIdentifier(rs.getString("LS_IDENTIFIER"));
lifecycleState.setDescription(rs.getString("LS_DESCRIPTION"));
List<UnrestrictedRole> unrestrictedRoles = new ArrayList<>();
UnrestrictedRole unrestrictedRole = new UnrestrictedRole();
unrestrictedRole.setRole(rs.getString("ROLE"));
unrestrictedRoles.add(unrestrictedRole);
application.setUnrestrictedRoles(unrestrictedRoles);
}else{
Tag tag = new Tag();
tag.setTagName(rs.getString("APP_TAG"));
UnrestrictedRole unrestrictedRole = new UnrestrictedRole();
unrestrictedRole.setRole(rs.getString("ROLE"));
if (application != null && application.getTags().contains(tag)){
application.getTags().add(tag);
}
if (application != null && application.getUnrestrictedRoles().contains(unrestrictedRole)){
application.getUnrestrictedRoles().add(unrestrictedRole);
}
Lifecycle lifecycle = new Lifecycle();
lifecycle.setLifecycleState(lifecycleState);
application.setCurrentLifecycle(lifecycle);
return application;
}
if(rs.last()){
applications.add(application);
}
}
return applications;
}
/**
* To create application object from the result set retrieved from the Database.
*
* @param rs ResultSet
* @return Application that is retrieved from the Database.
* @throws SQLException SQL Exception
* @throws JSONException JSONException.
*/
public static Application loadApplication(ResultSet rs) throws SQLException, JSONException {
Application application = new Application();
int applicatioId = -1;
int iteration = 0;
while (rs.next()){
if (iteration == 0){
applicatioId = rs.getInt("APP_ID");
application.setId(applicatioId);
application.setName(rs.getString("APP_NAME"));
application.setType(rs.getString("APP_TYPE"));
application.setAppCategory(rs.getString("APP_CATEGORY"));
application.setIsFree(rs.getInt("IS_FREE"));
application.setIsRestricted(rs.getInt("RESTRICTED"));
}
Tag tag = new Tag();
tag.setTagName(rs.getString("APP_TAG"));
UnrestrictedRole unrestrictedRole = new UnrestrictedRole();
unrestrictedRole.setRole(rs.getString("ROLE"));
if (application.getTags().contains(tag)){
application.getTags().add(tag);
}
if (application.getUnrestrictedRoles().contains(unrestrictedRole)){
application.getUnrestrictedRoles().add(unrestrictedRole);
}
iteration++;
}
return application;
}
/**
* Cleans up the statement and resultset after executing the query
*

@ -42,6 +42,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
private static final Log log = LogFactory.getLog(GenericApplicationDAOImpl.class);
@Override
public int createApplication(Application application, int deviceId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to create an application");
@ -83,6 +84,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
@Override
public void addTags(List<Tag> tags, int applicationId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to add tags");
@ -114,6 +116,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
@Override
public void addUnrestrictedRoles(List<UnrestrictedRole> unrestrictedRoles, int applicationId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to add unrestricted roles");
@ -146,7 +149,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
@Override
public int isExistApplication(String appName, String type, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to verify whether the registering app is registered or not");
@ -185,94 +188,28 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
public ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting application data from the database");
log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset()));
log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset()));
}
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = "";
ApplicationList applicationList = new ApplicationList();
List<Application> applications = new ArrayList<>();
Pagination pagination = new Pagination();
int index = 0;
String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY"
+ " AS APP_CATEGORY, AP_APP.IS_FREE, AP_APP.RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLES.ROLE "
+ "AS APP_UNRESTRICTED_ROLES FROM ((AP_APP LEFT JOIN AP_APP_TAG ON AP_APP.ID = AP_APP_TAG.AP_APP_ID) "
+ "LEFT JOIN AP_UNRESTRICTED_ROLES ON AP_APP.ID = AP_UNRESTRICTED_ROLES.AP_APP_ID) "
+ "WHERE AP_APP.TENANT_ID = ?";
if (filter == null) {
throw new ApplicationManagementDAOException("Filter need to be instantiated");
} else {
pagination.setLimit(filter.getLimit());
pagination.setOffset(filter.getOffset());
}
try {
conn = this.getDBConnection();
stmt = this.generateGetApplicationsStatement(filter, conn, tenantId);
rs = stmt.executeQuery();
int length = 0;
while (rs.next()) {
//Getting properties
sql = "SELECT * FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, rs.getInt("ID"));
ResultSet rsProperties = stmt.executeQuery();
//Getting tags
sql = "SELECT * FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, rs.getInt("ID"));
ResultSet rsTags = stmt.executeQuery();
applications.add(Util.loadApplication(rs, rsProperties, rsTags));
Util.cleanupResources(null, rsProperties);
Util.cleanupResources(null, rsTags);
length++;
}
pagination.setSize(length);
pagination.setCount(this.getApplicationCount(filter));
applicationList.setApplications(applications);
applicationList.setPagination(pagination);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant"
+ " " + tenantId + ". While executing " + sql, e);
} catch (JSONException e) {
throw new ApplicationManagementDAOException("Error occurred while parsing JSON, while getting application"
+ " list for the tenant " + tenantId, e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection while "
+ "getting application list for the tenant " + tenantId, e);
} finally {
Util.cleanupResources(stmt, rs);
}
return applicationList;
}
/**
* This method is used to generate the statement that is used to get the applications with the given filter.
*
* @param filter Filter to filter out the applications.
* @param conn Database Connection.
* @param tenantId ID of the tenant to retrieve the applications.
* @return the statement for getting applications that are belong to a particular filter.
* @throws SQLException SQL Exception
*/
protected PreparedStatement generateGetApplicationsStatement(Filter filter, Connection conn,
int tenantId) throws SQLException {
int index = 0;
String sql = "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, CAT.ID AS CAT_ID, "
+ "CAT.NAME AS CAT_NAME, LS.NAME AS LS_NAME, LS.IDENTIFIER AS LS_IDENTIFIER, "
+ "LS.DESCRIPTION AS LS_DESCRIPTION FROM APPM_APPLICATION APP INNER JOIN APPM_PLATFORM APL "
+ "ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY CAT "
+ "ON APP.APPLICATION_CATEGORY_ID = CAT.ID INNER JOIN APPM_LIFECYCLE_STATE LS "
+ "ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE APP.TENANT_ID = ? ";
String userName = filter.getUserName();
if (!userName.equals("ALL")) {
sql += " AND APP.CREATED_BY = ? ";
}
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
sql += "AND LOWER (APP.NAME) ";
sql += " AND LOWER (AP_APP.NAME) ";
if (filter.isFullMatch()) {
sql += "= ?";
} else {
@ -280,14 +217,16 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
sql += "LIMIT ? OFFSET ?";
sql += " LIMIT ? OFFSET ?";
PreparedStatement stmt = conn.prepareStatement(sql);
pagination.setLimit(filter.getLimit());
pagination.setOffset(filter.getOffset());
try {
conn = this.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(++index, tenantId);
if (!userName.equals("ALL")) {
stmt.setString(++index, userName);
}
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
if (filter.isFullMatch()) {
stmt.setString(++index, filter.getSearchQuery().toLowerCase());
@ -298,9 +237,27 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.setInt(++index, filter.getLimit());
stmt.setInt(++index, filter.getOffset());
rs = stmt.executeQuery();
applicationList.setApplications(Util.loadApplications(rs));
pagination.setSize(filter.getOffset());
pagination.setCount(this.getApplicationCount(filter));
applicationList.setPagination(pagination);
return stmt;
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant"
+ " " + tenantId + ". While executing " + sql, e);
}
catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection while "
+ "getting application list for the tenant " + tenantId, e);
} catch (JSONException e) {
throw new ApplicationManagementDAOException("Error occurred while parsing JSON ", e);
} finally {
Util.cleanupResources(stmt, rs);
}
return applicationList;
}
@Override
public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException {
@ -321,13 +278,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try {
conn = this.getDBConnection();
sql += "SELECT COUNT(APP.ID) AS APP_COUNT ";
sql += "FROM APPM_APPLICATION AS APP ";
sql += "INNER JOIN APPM_PLATFORM AS APL ON APP.PLATFORM_ID = APL.ID ";
sql += "INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON APP.APPLICATION_CATEGORY_ID = CAT.ID ";
sql += "SELECT count(APP.ID) AS APP_COUNT FROM AP_APP AS APP WHERE TENANT_ID = ?";
if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) {
sql += "WHERE LOWER (APP.NAME) LIKE ? ";
sql += " AND LOWER (APP.NAME) LIKE ? ";
}
sql += ";";
@ -351,66 +305,38 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
@Override
public Application getApplication(String uuid, int tenantId, String userName) throws
public Application getApplication(String appName, String appType, int tenantId) throws
ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting application with the UUID(" + uuid + ") from the database");
if (log.isDebugEnabled()){
log.debug("Getting application with the type(" + appType + " and Name " + appName +
" ) from the database");
}
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = "";
Application application = null;
try {
conn = this.getDBConnection();
sql += "SELECT APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER, CAT.ID AS CAT_ID, "
+ "CAT.NAME AS CAT_NAME, LS.NAME AS LS_NAME, LS.IDENTIFIER AS LS_IDENTIFIER, "
+ "LS.DESCRIPTION AS LS_DESCRIPTION "
+ "FROM APPM_APPLICATION APP "
+ "INNER JOIN APPM_PLATFORM APL "
+ "ON APP.PLATFORM_ID = APL.ID "
+ "INNER JOIN APPM_APPLICATION_CATEGORY CAT "
+ "ON APP.APPLICATION_CATEGORY_ID = CAT.ID "
+ "INNER JOIN APPM_LIFECYCLE_STATE LS "
+ " ON APP.LIFECYCLE_STATE_ID = LS.ID "
+ "WHERE UUID = ? AND APP.TENANT_ID = ? ";
String sql = "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY "
+ "AS APP_CATEGORY, AP_APP.IS_FREE, AP_APP_TAG.TAG, AP_UNRESTRICTED_ROLES.ROLE AS RELESE_ID FROM "
+ "AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.NAME=? AND AP_APP.TYPE= ? "
+ "AND AP_APP.TENANT_ID=?;";
stmt = conn.prepareStatement(sql);
stmt.setString(1, uuid);
stmt.setInt(2, tenantId);
if (!userName.equals("ALL")) {
sql += "AND APP.CREATED_BY = ?";
stmt.setString(3, userName);
}
stmt.setString(1, appName);
stmt.setString(2, appType);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved basic details of the application with the UUID " + uuid);
log.debug("Successfully retrieved basic details of the application with the type "
+ appType +"and app name "+ appName);
}
if (rs.next()) {
//Getting properties
sql = "SELECT * FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, rs.getInt("ID"));
ResultSet rsProperties = stmt.executeQuery();
//Getting tags
sql = "SELECT * FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, rs.getInt("ID"));
ResultSet rsTags = stmt.executeQuery();
return Util.loadApplication(rs);
application = Util.loadApplication(rs, rsProperties, rsTags);
Util.cleanupResources(null, rsProperties);
Util.cleanupResources(null, rsTags);
}
return application;
} catch (SQLException e) {
throw new ApplicationManagementDAOException(
"Error occurred while getting application details with UUID " + uuid + " While executing query "
"Error occurred while getting application details with app name " + appName + " While executing query "
+ sql, e);
} catch (JSONException e) {
throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e);
@ -421,6 +347,36 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
}
@Override
public void addLifecycle(Lifecycle lifecycle, int tenantId, int appReleaseId, int appId)
throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Life cycle is created by" + lifecycle.getCreatedBy() + " at "
+ lifecycle.getCreatedAt());
}
Connection conn;
PreparedStatement stmt = null;
try {
conn = this.getDBConnection();
String sql = "INSERT INTO AP_APP_LIFECYCLE (CREATED_BY, CREATED_TIMESTAMP, TENANT_ID, AP_APP_RELEASE_ID, "
+ "AP_APP_ID) VALUES (?, ?, ?, ? ,?);";
stmt = conn.prepareStatement(sql);
stmt.setString(1, lifecycle.getCreatedBy());
stmt.setTimestamp(2, (Timestamp) lifecycle.getCreatedAt());
stmt.setInt(3, tenantId);
stmt.setInt(4, appReleaseId);
stmt.setInt(5, appId);
stmt.executeUpdate();
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while adding the lifecycle of application", e);
} finally {
Util.cleanupResources(stmt, null);
}
}
@Override
public void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String userName, int tenantId)
throws ApplicationManagementDAOException {
@ -719,7 +675,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
PreparedStatement stmt = null;
try {
conn = this.getDBConnection();
String sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?";
String sql = "DELETE FROM AP_APP_TAG WHERE ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationId);
stmt.executeUpdate();
@ -735,7 +691,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
@Override
public int getApplicationId(String uuid, int tenantId) throws ApplicationManagementDAOException {
public int getApplicationId(String appName, String appType, int tenantId) throws ApplicationManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
@ -743,10 +699,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
int id = -1;
try {
conn = this.getDBConnection();
sql = "SELECT ID FROM APPM_APPLICATION WHERE UUID = ? AND TENANT_ID = ?";
sql = "SELECT ID FROM AP_APP WHERE NAME = ? AND TYPE = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, uuid);
stmt.setInt(2, tenantId);
stmt.setString(1, appName);
stmt.setString(2, appType);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
id = rs.getInt(1);

Loading…
Cancel
Save