Improve the subscribed devices and category details APIs

feature/appm-store/pbac
nipunnadeen 5 years ago committed by nipun
parent e3b29ee531
commit c08e631c93

@ -22,9 +22,6 @@ import io.swagger.annotations.ApiModelProperty;
public class BasePaginatedResult { public class BasePaginatedResult {
/**
* Number of Resources returned.
*/
@ApiModelProperty( @ApiModelProperty(
value = "Number of total resources.", value = "Number of total resources.",
example = "1") example = "1")

@ -33,12 +33,13 @@ public interface SubscriptionManager {
/*** /***
* This method used to get the app id ,device ids and pass them to DM service method * This method used to get the app id ,device ids and pass them to DM service method
* @param appUUID uuid * @param appUUID UUID of the application release
* @param offsetValue offsetValue * @param offsetValue offset value for get paginated result
* @param limitValue limitValue * @param limitValue limit value for get paginated result
* @param status status * @param status status of the devices
* @return deviceDetails * @return deviceDetails - device details for given application release.
* @throws ApplicationManagementException Exception of the application management * @throws ApplicationManagementException throws {@link ApplicationManagementException} Exception
* of the application management
*/ */
PaginationResult getAppInstalledDevices(int offsetValue, int limitValue, String appUUID, PaginationResult getAppInstalledDevices(int offsetValue, int limitValue, String appUUID,
String status) String status)
@ -46,12 +47,13 @@ public interface SubscriptionManager {
/*** /***
* This method used to get category details * This method used to get category details
* @param appUUID uuid * @param appUUID UUID of the application release
* @param subType subType * @param subType subscription type of the application
* @param offsetValue offsetValue * @param offsetValue offset value for get paginated result
* @param limitValue limitValue * @param limitValue limit value for get paginated result
* @return paginationResult * @return {@link PaginationResult} pagination result of the category details.
* @throws ApplicationManagementException Exception of the application management * @throws ApplicationManagementException throws {@link ApplicationManagementException} Exception
* of the application management
*/ */
PaginationResult getAppInstalledCategories(int offsetValue, int limitValue, String appUUID, PaginationResult getAppInstalledCategories(int offsetValue, int limitValue, String appUUID,
String subType) String subType)

@ -87,11 +87,11 @@ public interface SubscriptionDAO {
/** /**
* this method is used to get the details of users * this method is used to get the details of users
* @param tenantId tenant id * @param tenantId id of the current tenant
* @param offsetValue offsetValue * @param offsetValue offset value for get paginated result
* @param limitValue limitValue * @param limitValue limit value for get paginated result
* @param appReleaseId appReleaseId * @param appReleaseId id of the application release.
* @return subscribedUsers * @return subscribedUsers - list of app subscribed users.
* @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if * @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if
* connections establishment fails. * connections establishment fails.
*/ */
@ -101,11 +101,11 @@ public interface SubscriptionDAO {
/** /**
* this method is used to get the details of roles * this method is used to get the details of roles
* @param tenantId tenant id * @param tenantId id of the current tenant
* @param offsetValue offsetValue * @param offsetValue offset value for get paginated result
* @param limitValue limitValue * @param limitValue limit value for get paginated result
* @param appReleaseId appReleaseId * @param appReleaseId id of the application release.
* @return subscribedRoles * @return subscribedRoles - list of app subscribed roles.
* @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if * @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if
* connections establishment fails. * connections establishment fails.
*/ */
@ -114,12 +114,12 @@ public interface SubscriptionDAO {
throws ApplicationManagementDAOException; throws ApplicationManagementDAOException;
/** /**
* this method is used to get the details of groups * this method is used to get the details of subscribed groups
* @param tenantId tenant id * @param tenantId id of the current tenant
* @param offsetValue offsetValue * @param offsetValue offset value for get paginated result
* @param limitValue limitValue * @param limitValue limit value for get paginated result
* @param appReleaseId appReleaseId * @param appReleaseId id of the application release.
* @return subscribedUsers * @return subscribedGroups - list of app subscribed groups.
* @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if * @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if
* connections establishment fails. * connections establishment fails.
*/ */

@ -708,7 +708,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
try { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
int index = 1;
List<String> subscribedUsers = new ArrayList<>(); List<String> subscribedUsers = new ArrayList<>();
String sql = "SELECT " String sql = "SELECT "
+ "US.USER_NAME AS USER " + "US.USER_NAME AS USER "
@ -716,10 +715,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "WHERE " + "WHERE "
+ "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?"; + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(index++, appReleaseId); stmt.setInt(1, appReleaseId);
stmt.setInt(index++, tenantId); stmt.setInt(2, tenantId);
stmt.setInt(index++, offsetValue); stmt.setInt(3, offsetValue);
stmt.setInt(index, limitValue); stmt.setInt(4, limitValue);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {
subscribedUsers.add(rs.getString("USER")); subscribedUsers.add(rs.getString("USER"));
@ -749,7 +748,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
try { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
int index = 1;
List<String> subscribedRoles = new ArrayList<>(); List<String> subscribedRoles = new ArrayList<>();
String sql = "SELECT " String sql = "SELECT "
+ "US.ROLE_NAME AS ROLE " + "US.ROLE_NAME AS ROLE "
@ -757,10 +755,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "WHERE " + "WHERE "
+ "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?"; + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?";
try (PreparedStatement ps = conn.prepareStatement(sql)) { try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(index++, appReleaseId); ps.setInt(1, appReleaseId);
ps.setInt(index++, tenantId); ps.setInt(2, tenantId);
ps.setInt(index++, offsetValue); ps.setInt(3, offsetValue);
ps.setInt(index, limitValue); ps.setInt(4, limitValue);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
subscribedRoles.add(rs.getString("ROLE")); subscribedRoles.add(rs.getString("ROLE"));
@ -790,7 +788,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
} }
try { try {
Connection conn = this.getDBConnection(); Connection conn = this.getDBConnection();
int index = 1;
List<String> subscribedGroups = new ArrayList<>(); List<String> subscribedGroups = new ArrayList<>();
String sql = "SELECT " String sql = "SELECT "
+ "GS.GROUP_NAME AS GROUPS " + "GS.GROUP_NAME AS GROUPS "
@ -798,10 +795,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "WHERE " + "WHERE "
+ "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?"; + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?";
try (PreparedStatement ps = conn.prepareStatement(sql)) { try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(index++, appReleaseId); ps.setInt(1, appReleaseId);
ps.setInt(index++, tenantId); ps.setInt(2, tenantId);
ps.setInt(index++, offsetValue); ps.setInt(3, offsetValue);
ps.setInt(index, limitValue); ps.setInt(4, limitValue);
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) { while (rs.next()) {
subscribedGroups.add(rs.getString("GROUPS")); subscribedGroups.add(rs.getString("GROUPS"));

@ -571,7 +571,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(appUUID, tenantId); ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(appUUID, tenantId);
int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId();
@ -580,14 +579,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if (deviceSubscriptionDTOS.isEmpty()) { if (deviceSubscriptionDTOS.isEmpty()) {
String msg = "Couldn't found an subscribed devices for application release id: " String msg = "Couldn't found an subscribed devices for application release id: "
+ applicationReleaseId; + applicationReleaseId;
log.error(msg); log.info(msg);
} }
List<Integer> deviceIdList = new ArrayList<>(); List<Integer> deviceIdList = new ArrayList<>();
for (DeviceSubscriptionDTO deviceIds : deviceSubscriptionDTOS) { for (DeviceSubscriptionDTO deviceIds : deviceSubscriptionDTOS) {
deviceIdList.add(deviceIds.getDeviceId()); deviceIdList.add(deviceIds.getDeviceId());
} }
//pass the device id list to device manager service layer method //pass the device id list to device manager service method
try { try {
PaginationResult deviceDetails = deviceManagementProviderService PaginationResult deviceDetails = deviceManagementProviderService
.getAppSubscribedDevices(offsetValue ,limitValue, deviceIdList, status); .getAppSubscribedDevices(offsetValue ,limitValue, deviceIdList, status);
@ -630,7 +628,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
PaginationResult paginationResult = new PaginationResult(); PaginationResult paginationResult = new PaginationResult();
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
ApplicationDTO applicationDTO = this.applicationDAO ApplicationDTO applicationDTO = this.applicationDAO
.getAppWithRelatedRelease(appUUID, tenantId); .getAppWithRelatedRelease(appUUID, tenantId);
int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId();

@ -552,13 +552,13 @@ public interface DeviceDAO {
String toDate) throws DeviceManagementDAOException; String toDate) throws DeviceManagementDAOException;
/** /**
* this method is used to get the details of devices * this method is used to get the details of subscribed devices.
* @param deviceIds device ids * @param deviceIds device ids of the subscribed devices.
* @param tenantId tenant id * @param tenantId Id of the current tenant.
* @param offsetValue offsetValue * @param offsetValue offset value for get paginated result.
* @param limitValue limitValue * @param limitValue limit value for get paginated result.
* @param status status * @param status status of the devices.
* @return subscribed device details list * @return devices - subscribed device details list
* @throws DeviceManagementDAOException throws {@link DeviceManagementDAOException} if connections * @throws DeviceManagementDAOException throws {@link DeviceManagementDAOException} if connections
* establishment fails. * establishment fails.
*/ */

@ -614,8 +614,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving information " + String msg = "Error occurred while retrieving information of all registered devices " +
"of all registered devices according to device ids and the limit area", e); "according to device ids and the limit area.";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} }
} }

@ -37,6 +37,7 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.StringJoiner;
/** /**
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
@ -571,7 +572,61 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl {
public List<Device> getSubscribedDevices(int offsetValue, int limitValue, public List<Device> getSubscribedDevices(int offsetValue, int limitValue,
List<Integer> deviceIds, int tenantId, String status) List<Integer> deviceIds, int tenantId, String status)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
return null; Connection conn;
try {
conn = this.getConnection();
int index = 1;
boolean isStatusProvided = false;
StringJoiner joiner = new StringJoiner(",",
"SELECT " +
"f.ID AS DEVICE_ID, f.NAME AS DEVICE_NAME, f.DESCRIPTION AS DESCRIPTION, " +
"f.DEVICE_TYPE_ID, f.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " +
"e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " +
"e.DATE_OF_LAST_UPDATE, e.STATUS, t.NAME AS DEVICE_TYPE " +
"FROM DM_ENROLMENT AS e,DM_DEVICE AS f, DM_DEVICE_TYPE t " +
"WHERE " +
"e.DEVICE_ID=f.ID AND " +
"e.DEVICE_ID IN (", ") AND e.TENANT_ID=?");
deviceIds.stream().map(ignored -> "?").forEach(joiner::add);
String query = joiner.toString();
if (status != null && !status.isEmpty()) {
query = query + " AND e.STATUS=?";
isStatusProvided = true;
}
query = query + " ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
try (PreparedStatement ps = conn.prepareStatement(query)) {
for (Integer deviceId : deviceIds) {
ps.setObject(index++, deviceId);
}
ps.setInt(index++, tenantId);
if (isStatusProvided) {
ps.setString(index++, status);
}
ps.setInt(index++, offsetValue);
ps.setInt(index, limitValue);
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
while (rs.next()) {
devices.add(DeviceManagementDAOUtil.loadDevice(rs));
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving information of all registered devices " +
"according to device ids and the limit area.";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
} }
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {

@ -34,6 +34,7 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.StringJoiner;
/** /**
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
@ -546,7 +547,61 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl {
public List<Device> getSubscribedDevices(int offsetValue, int limitValue, public List<Device> getSubscribedDevices(int offsetValue, int limitValue,
List<Integer> deviceIds, int tenantId, String status) List<Integer> deviceIds, int tenantId, String status)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
return null; Connection conn;
try {
conn = this.getConnection();
int index = 1;
boolean isStatusProvided = false;
StringJoiner joiner = new StringJoiner(",",
"SELECT " +
"f.ID AS DEVICE_ID, f.NAME AS DEVICE_NAME, f.DESCRIPTION AS DESCRIPTION, " +
"f.DEVICE_TYPE_ID, f.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " +
"e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " +
"e.DATE_OF_LAST_UPDATE, e.STATUS, t.NAME AS DEVICE_TYPE " +
"FROM DM_ENROLMENT AS e,DM_DEVICE AS f, DM_DEVICE_TYPE t "+
"WHERE " +
"e.DEVICE_ID=f.ID AND " +
"e.DEVICE_ID IN (", ") AND e.TENANT_ID=?");
deviceIds.stream().map(ignored -> "?").forEach(joiner::add);
String query = joiner.toString();
if (status != null && !status.isEmpty()) {
query = query + " AND e.STATUS=?";
isStatusProvided = true;
}
query = query + " LIMIT ? OFFSET ?";
try (PreparedStatement ps = conn.prepareStatement(query)) {
for (Integer deviceId : deviceIds) {
ps.setObject(index++, deviceId);
}
ps.setInt(index++, tenantId);
if (isStatusProvided) {
ps.setString(index++, status);
}
ps.setInt(index++, offsetValue);
ps.setInt(index, limitValue);
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
while (rs.next()) {
devices.add(DeviceManagementDAOUtil.loadDevice(rs));
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving information of all registered devices " +
"according to device ids and the limit area.";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
} }
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {

@ -34,6 +34,7 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.StringJoiner;
/** /**
* This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax.
@ -501,7 +502,61 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl {
public List<Device> getSubscribedDevices(int offsetValue, int limitValue, public List<Device> getSubscribedDevices(int offsetValue, int limitValue,
List<Integer> deviceIds, int tenantId, String status) List<Integer> deviceIds, int tenantId, String status)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
return null; Connection conn;
try {
conn = this.getConnection();
int index = 1;
boolean isStatusProvided = false;
StringJoiner joiner = new StringJoiner(",",
"SELECT " +
"f.ID AS DEVICE_ID, f.NAME AS DEVICE_NAME, f.DESCRIPTION AS DESCRIPTION, " +
"f.DEVICE_TYPE_ID, f.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " +
"e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " +
"e.DATE_OF_LAST_UPDATE, e.STATUS, t.NAME AS DEVICE_TYPE " +
"FROM DM_ENROLMENT AS e,DM_DEVICE AS f, DM_DEVICE_TYPE t "+
"WHERE " +
"e.DEVICE_ID=f.ID AND " +
"e.DEVICE_ID IN (", ") AND e.TENANT_ID=?");
deviceIds.stream().map(ignored -> "?").forEach(joiner::add);
String query = joiner.toString();
if (status != null && !status.isEmpty()) {
query = query + " AND e.STATUS=?";
isStatusProvided = true;
}
query = query + " LIMIT ?,?";
try (PreparedStatement ps = conn.prepareStatement(query)) {
for (Integer deviceId : deviceIds) {
ps.setObject(index++, deviceId);
}
ps.setInt(index++, tenantId);
if (isStatusProvided) {
ps.setString(index++, status);
}
ps.setInt(index++, offsetValue);
ps.setInt(index, limitValue);
try (ResultSet rs = ps.executeQuery()) {
List<Device> devices = new ArrayList<>();
while (rs.next()) {
devices.add(DeviceManagementDAOUtil.loadDevice(rs));
}
return devices;
}
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving information of all registered devices " +
"according to device ids and the limit area.";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
} }
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {

@ -771,12 +771,13 @@ public interface DeviceManagementProviderService {
/** /**
* This method retrieves a list of subscribed devices. * This method retrieves a list of subscribed devices.
* @param devicesIds devices ids * @param devicesIds devices ids of the subscribed devices
* @param offsetValue offsetValue * @param offsetValue offset value for get paginated result
* @param limitValue limitValue * @param limitValue limit value for get paginated result
* @param status status * @param status status of the devices
* @return {@link PaginationResult} * @return {@link PaginationResult}
* @throws DeviceManagementException if any service level or DAO level error occurs * @throws DeviceManagementException throws {@link DeviceManagementException} if any service
* level or DAO level error occurs
*/ */
PaginationResult getAppSubscribedDevices(int offsetValue, int limitValue, PaginationResult getAppSubscribedDevices(int offsetValue, int limitValue,
List<Integer> devicesIds, String status) List<Integer> devicesIds, String status)

Loading…
Cancel
Save