Fix issues in GET /applications API

reporting
Amanda Randombage 5 years ago committed by Dharmakeerthi Lasantha
parent 41adb6bb7d
commit 1ac18c80b3

@ -21,6 +21,7 @@ import java.util.List;
/** /**
* Filter represents a criteria that can be used for searching applications. * Filter represents a criteria that can be used for searching applications.
* The default value for limit is 20
*/ */
public class Filter { public class Filter {
@ -87,8 +88,9 @@ public class Filter {
/** /**
* Limit of the applications * Limit of the applications
* default: 20
*/ */
private int limit; private int limit = 20;
/** /**
* Started from * Started from

@ -94,7 +94,13 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
@Override @Override
public List<ApplicationDTO> getApplications(Filter filter,int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { public List<ApplicationDTO> getApplications(Filter filter, int deviceTypeId, int tenantId) throws
ApplicationManagementDAOException {
if (filter == null) {
String msg = "Filter is not instantiated for tenant "+tenantId;
log.error(msg);
throw new ApplicationManagementDAOException(msg);
}
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Getting application data from the database"); log.debug("Getting application data from the database");
log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset())); log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset()));
@ -131,14 +137,12 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "FROM AP_APP " + "FROM AP_APP "
+ "INNER JOIN AP_APP_RELEASE ON " + "INNER JOIN AP_APP_RELEASE ON "
+ "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID " + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID "
+ "INNER JOIN (SELECT ID FROM AP_APP WHERE AP_APP.TENANT_ID = ? LIMIT ? OFFSET ? ) AS app_data ON app_data.ID = AP_APP.ID " + "INNER JOIN (SELECT AP_APP.ID FROM AP_APP ";
+ "WHERE AP_APP.TENANT_ID = ?"; if (!StringUtils.isEmpty(filter.getVersion()) || !StringUtils.isEmpty(filter.getAppReleaseState())
|| !StringUtils.isEmpty(filter.getAppReleaseType())) {
if (filter == null) { sql += "INNER JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID ";
String msg = "Filter is not instantiated.";
log.error(msg);
throw new ApplicationManagementDAOException(msg);
} }
sql += "WHERE AP_APP.TENANT_ID = ? ";
if (!StringUtils.isEmpty(filter.getAppType())) { if (!StringUtils.isEmpty(filter.getAppType())) {
sql += "AND AP_APP.TYPE = ? "; sql += "AND AP_APP.TYPE = ? ";
@ -169,36 +173,24 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (deviceTypeId != -1) { if (deviceTypeId != -1) {
sql += "AND AP_APP.DEVICE_TYPE_ID = ? "; sql += "AND AP_APP.DEVICE_TYPE_ID = ? ";
} }
sql += "GROUP BY AP_APP.ID ";
if (filter.getLimit() == -1) {
sql = sql.replace("LIMIT ? OFFSET ?", "");
}
String sortingOrder = "ASC";
if (!StringUtils.isEmpty(filter.getSortBy())) { if (!StringUtils.isEmpty(filter.getSortBy())) {
sortingOrder = filter.getSortBy(); sql += "ORDER BY ID " + filter.getSortBy() +" ";
} }
sql += " ORDER BY APP_ID " + sortingOrder; if (filter.getLimit() != -1) {
sql += " LIMIT ? OFFSET ? ";
}
sql += ") AS app_data ON app_data.ID = AP_APP.ID " +
"WHERE AP_APP.TENANT_ID = ?";
try { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1; int paramIndex = 1;
stmt.setInt(paramIndex++, tenantId); stmt.setInt(paramIndex++, tenantId);
if (filter.getLimit() != -1) { if (!StringUtils.isEmpty(filter.getAppType())) {
if (filter.getLimit() == 0) {
stmt.setInt(paramIndex++, 100);
} else {
stmt.setInt(paramIndex++, filter.getLimit());
}
stmt.setInt(paramIndex++, filter.getOffset());
}
stmt.setInt(paramIndex++, tenantId);
if (filter.getAppType() != null && !filter.getAppType().isEmpty()) {
stmt.setString(paramIndex++, filter.getAppType()); stmt.setString(paramIndex++, filter.getAppType());
} }
if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { if (!StringUtils.isEmpty(filter.getAppName())) {
if (filter.isFullMatch()) { if (filter.isFullMatch()) {
stmt.setString(paramIndex++, filter.getAppName().toLowerCase()); stmt.setString(paramIndex++, filter.getAppName().toLowerCase());
} else { } else {
@ -221,8 +213,13 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
stmt.setString(paramIndex++, filter.getAppReleaseState()); stmt.setString(paramIndex++, filter.getAppReleaseState());
} }
if (deviceTypeId > 0) { if (deviceTypeId > 0) {
stmt.setInt(paramIndex, deviceTypeId); stmt.setInt(paramIndex++, deviceTypeId);
}
if (filter.getLimit() != -1) {
stmt.setInt(paramIndex++, filter.getLimit());
stmt.setInt(paramIndex++, filter.getOffset());
} }
stmt.setInt(paramIndex, tenantId);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
return DAOUtil.loadApplications(rs); return DAOUtil.loadApplications(rs);
} }

@ -43,11 +43,15 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl {
@Override @Override
public List<ApplicationDTO> getApplications(Filter filter, int deviceTypeId, int tenantId) throws public List<ApplicationDTO> getApplications(Filter filter, int deviceTypeId, int tenantId) throws
ApplicationManagementDAOException { ApplicationManagementDAOException {
if (filter == null) {
String msg = "Filter is not instantiated for tenant " + tenantId;
log.error(msg);
throw new ApplicationManagementDAOException(msg);
}
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Getting application data from the database"); log.debug("Getting application data from the database");
log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset())); log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset()));
} }
int paramIndex = 1;
String sql = "SELECT " String sql = "SELECT "
+ "AP_APP.ID AS APP_ID, " + "AP_APP.ID AS APP_ID, "
+ "AP_APP.NAME AS APP_NAME, " + "AP_APP.NAME AS APP_NAME, "
@ -80,15 +84,11 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl {
+ "FROM AP_APP " + "FROM AP_APP "
+ "INNER JOIN AP_APP_RELEASE ON " + "INNER JOIN AP_APP_RELEASE ON "
+ "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID " + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID "
+ "INNER JOIN (SELECT ID FROM AP_APP ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) AS app_data ON app_data.ID = AP_APP.ID " + "INNER JOIN (SELECT AP_APP.ID FROM AP_APP ORDER BY ID ";
+ "WHERE AP_APP.TENANT_ID = ?"; if (!StringUtils.isEmpty(filter.getVersion()) || !StringUtils.isEmpty(filter.getAppReleaseState())
|| !StringUtils.isEmpty(filter.getAppReleaseType())) {
if (filter == null) { sql += "INNER JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID ";
String msg = "Filter is not instantiated.";
log.error(msg);
throw new ApplicationManagementDAOException(msg);
} }
if (!StringUtils.isEmpty(filter.getAppType())) { if (!StringUtils.isEmpty(filter.getAppType())) {
sql += "AND AP_APP.TYPE = ? "; sql += "AND AP_APP.TYPE = ? ";
} }
@ -118,35 +118,23 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl {
if (deviceTypeId != -1) { if (deviceTypeId != -1) {
sql += "AND AP_APP.DEVICE_TYPE_ID = ? "; sql += "AND AP_APP.DEVICE_TYPE_ID = ? ";
} }
sql += "GROUP BY AP_APP.ID ";
if (filter.getLimit() == -1) {
sql = sql.replace("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", "");
}
String sortingOrder = "ASC";
if (!StringUtils.isEmpty(filter.getSortBy())) { if (!StringUtils.isEmpty(filter.getSortBy())) {
sortingOrder = filter.getSortBy(); sql += "ORDER BY ID " + filter.getSortBy() + " ";
} }
sql += " ORDER BY APP_ID " + sortingOrder;
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql);
){
if (filter.getLimit() != -1) { if (filter.getLimit() != -1) {
stmt.setInt(paramIndex++, filter.getOffset()); sql += "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY ";
if (filter.getLimit() == 0) {
stmt.setInt(paramIndex++, 100);
} else {
stmt.setInt(paramIndex++, filter.getLimit());
} }
} sql += ") AS app_data ON app_data.ID = AP_APP.ID " +
stmt.setInt(paramIndex++, tenantId); "WHERE AP_APP.TENANT_ID = ?";
try {
if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
if (!StringUtils.isEmpty(filter.getAppType())) {
stmt.setString(paramIndex++, filter.getAppType()); stmt.setString(paramIndex++, filter.getAppType());
} }
if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { if (!StringUtils.isEmpty(filter.getAppName())) {
if (filter.isFullMatch()) { if (filter.isFullMatch()) {
stmt.setString(paramIndex++, filter.getAppName().toLowerCase()); stmt.setString(paramIndex++, filter.getAppName().toLowerCase());
} else { } else {
@ -169,8 +157,13 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl {
stmt.setString(paramIndex++, filter.getAppReleaseState()); stmt.setString(paramIndex++, filter.getAppReleaseState());
} }
if (deviceTypeId > 0) { if (deviceTypeId > 0) {
stmt.setInt(paramIndex, deviceTypeId); stmt.setInt(paramIndex++, deviceTypeId);
}
if (filter.getLimit() != -1) {
stmt.setInt(paramIndex++, filter.getOffset());
stmt.setInt(paramIndex++, filter.getLimit());
} }
stmt.setInt(paramIndex, tenantId);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
return DAOUtil.loadApplications(rs); return DAOUtil.loadApplications(rs);
} }

@ -42,11 +42,15 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl {
@Override @Override
public List<ApplicationDTO> getApplications(Filter filter, int deviceTypeId, int tenantId) throws public List<ApplicationDTO> getApplications(Filter filter, int deviceTypeId, int tenantId) throws
ApplicationManagementDAOException { ApplicationManagementDAOException {
if (filter == null) {
String msg = "Filter is not instantiated for tenant " + tenantId;
log.error(msg);
throw new ApplicationManagementDAOException(msg);
}
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Getting application data from the database"); log.debug("Getting application data from the database");
log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset())); log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset()));
} }
int paramIndex = 1;
String sql = "SELECT " String sql = "SELECT "
+ "AP_APP.ID AS APP_ID, " + "AP_APP.ID AS APP_ID, "
+ "AP_APP.NAME AS APP_NAME, " + "AP_APP.NAME AS APP_NAME, "
@ -79,15 +83,11 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl {
+ "FROM AP_APP " + "FROM AP_APP "
+ "INNER JOIN AP_APP_RELEASE ON " + "INNER JOIN AP_APP_RELEASE ON "
+ "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID " + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID "
+ "INNER JOIN (SELECT ID FROM AP_APP ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) AS app_data ON app_data.ID = AP_APP.ID " + "INNER JOIN (SELECT AP_APP.ID FROM AP_APP ";
+ "WHERE AP_APP.TENANT_ID = ?"; if (!StringUtils.isEmpty(filter.getVersion()) || !StringUtils.isEmpty(filter.getAppReleaseState())
|| !StringUtils.isEmpty(filter.getAppReleaseType())) {
if (filter == null) { sql += "INNER JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID ";
String msg = "Filter is not instantiated.";
log.error(msg);
throw new ApplicationManagementDAOException(msg);
} }
if (!StringUtils.isEmpty(filter.getAppType())) { if (!StringUtils.isEmpty(filter.getAppType())) {
sql += "AND AP_APP.TYPE = ? "; sql += "AND AP_APP.TYPE = ? ";
} }
@ -117,35 +117,23 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl {
if (deviceTypeId != -1) { if (deviceTypeId != -1) {
sql += "AND AP_APP.DEVICE_TYPE_ID = ? "; sql += "AND AP_APP.DEVICE_TYPE_ID = ? ";
} }
sql += "GROUP BY AP_APP.ID ";
if (filter.getLimit() == -1) {
sql = sql.replace("ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", "");
}
String sortingOrder = "ASC";
if (!StringUtils.isEmpty(filter.getSortBy())) { if (!StringUtils.isEmpty(filter.getSortBy())) {
sortingOrder = filter.getSortBy(); sql += "ORDER BY ID " + filter.getSortBy() + " ";
} }
sql += " ORDER BY APP_ID " + sortingOrder;
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql);
){
if (filter.getLimit() != -1) { if (filter.getLimit() != -1) {
stmt.setInt(paramIndex++, filter.getOffset()); sql += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY ";
if (filter.getLimit() == 0) {
stmt.setInt(paramIndex++, 100);
} else {
stmt.setInt(paramIndex++, filter.getLimit());
} }
} sql += ") AS app_data ON app_data.ID = AP_APP.ID " +
stmt.setInt(paramIndex++, tenantId); "WHERE AP_APP.TENANT_ID = ?";
try {
if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
if (!StringUtils.isEmpty(filter.getAppType())) {
stmt.setString(paramIndex++, filter.getAppType()); stmt.setString(paramIndex++, filter.getAppType());
} }
if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { if (!StringUtils.isEmpty(filter.getAppName())) {
if (filter.isFullMatch()) { if (filter.isFullMatch()) {
stmt.setString(paramIndex++, filter.getAppName().toLowerCase()); stmt.setString(paramIndex++, filter.getAppName().toLowerCase());
} else { } else {
@ -168,8 +156,13 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl {
stmt.setString(paramIndex++, filter.getAppReleaseState()); stmt.setString(paramIndex++, filter.getAppReleaseState());
} }
if (deviceTypeId > 0) { if (deviceTypeId > 0) {
stmt.setInt(paramIndex, deviceTypeId); stmt.setInt(paramIndex++, deviceTypeId);
}
if (filter.getLimit() != -1) {
stmt.setInt(paramIndex++, filter.getOffset());
stmt.setInt(paramIndex++, filter.getLimit());
} }
stmt.setInt(paramIndex, tenantId);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
return DAOUtil.loadApplications(rs); return DAOUtil.loadApplications(rs);
} }

@ -608,9 +608,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (!StringUtils.isEmpty(filter.getDeviceType())) { if (!StringUtils.isEmpty(filter.getDeviceType())) {
deviceType = APIUtil.getDeviceTypeData(filter.getDeviceType()); deviceType = APIUtil.getDeviceTypeData(filter.getDeviceType());
} }
if (filter.getLimit() == 0) {
filter.setLimit(20);
}
if (deviceType == null) { if (deviceType == null) {
deviceType = new DeviceType(); deviceType = new DeviceType();

Loading…
Cancel
Save