From 9b8894240ed7d56e41816f8e6956028d88c17826 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Mon, 3 May 2021 11:21:05 +0530 Subject: [PATCH 1/9] Add DAO fix for app schedule subscription (cherry picked from commit 0060083499a411ef79ada46d44c718d61218334d) --- .../core/dao/impl/subscription/GenericSubscriptionDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 7018ca662f..c6937a05a8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1037,7 +1037,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, ExecutionStatus.PENDING.toString()); stmt.setBoolean(2, false); - stmt.setTimestamp(3, new Timestamp(Calendar.getInstance().getTime().getTime())); + stmt.setLong(3, Calendar.getInstance().getTime().getTime() / 1000); try (ResultSet rs = stmt.executeQuery()) { return DAOUtil.loadScheduledSubscriptions(rs); } From 8793b413afaffc189ebccffd921c77528227a02f Mon Sep 17 00:00:00 2001 From: MalshaPiumini Date: Tue, 16 Mar 2021 14:39:02 +0530 Subject: [PATCH 2/9] Device search in store uninstall modal (cherry picked from commit 74c4f1004af175297feddb149aad76d24fed0636) --- .../common/services/SubscriptionManager.java | 6 ++-- .../core/impl/SubscriptionManagerImpl.java | 15 ++++---- .../services/SubscriptionManagementAPI.java | 23 ++++++++++++ .../impl/SubscriptionManagementAPIImpl.java | 17 +++++++-- .../carbon/device/mgt/core/dao/DeviceDAO.java | 13 ++++--- .../dao/impl/device/GenericDeviceDAOImpl.java | 36 ++++++++++++++++--- .../dao/impl/device/OracleDeviceDAOImpl.java | 34 ++++++++++++++++-- .../impl/device/PostgreSQLDeviceDAOImpl.java | 34 ++++++++++++++++-- .../impl/device/SQLServerDeviceDAOImpl.java | 32 +++++++++++++++-- .../DeviceManagementProviderService.java | 9 +++-- .../DeviceManagementProviderServiceImpl.java | 6 ++-- 11 files changed, 183 insertions(+), 42 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index 9d9e94c183..c1c0155087 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -22,6 +22,7 @@ import org.wso2.carbon.device.application.mgt.common.dto.ScheduledSubscriptionDT import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.SubscriptionManagementException; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; import java.util.List; @@ -127,13 +128,12 @@ public interface SubscriptionManager { * This method used to get the app id ,device ids and pass them to DM service method. * * @param appUUID UUID of the application release. - * @param offsetValue offset value for get paginated request. - * @param limitValue limit value for get paginated request. + * @param request paginated request object. * @param status status of the devices. * @return deviceDetails - device details for given application release. * @throws {@link ApplicationManagementException} Exception of the application management */ - PaginationResult getAppInstalledDevices(int offsetValue, int limitValue, String appUUID, + PaginationResult getAppInstalledDevices(PaginationRequest request, String appUUID, List status) throws ApplicationManagementException; /*** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index dcd641f102..d3b45b9971 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -65,10 +65,7 @@ import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.HelperUtil; import org.wso2.carbon.device.application.mgt.core.util.OAuthUtils; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo; -import org.wso2.carbon.device.mgt.common.MDMAppConstants; +import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.App; import org.wso2.carbon.device.mgt.common.app.mgt.MobileAppTypes; import org.wso2.carbon.device.mgt.common.app.mgt.android.CustomApplication; @@ -88,7 +85,6 @@ import org.wso2.carbon.device.mgt.core.util.MDMIOSOperationUtil; import org.wso2.carbon.device.mgt.core.util.MDMWindowsOperationUtil; import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.device.mgt.common.PaginationResult; import javax.ws.rs.core.MediaType; import java.io.BufferedReader; @@ -1223,7 +1219,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public PaginationResult getAppInstalledDevices(int offsetValue, int limitValue, String appUUID, List status) + public PaginationResult getAppInstalledDevices(PaginationRequest request, String appUUID, List status) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); DeviceManagementProviderService deviceManagementProviderService = HelperUtil @@ -1262,8 +1258,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } //pass the device id list to device manager service method try { - PaginationResult deviceDetails = deviceManagementProviderService - .getAppSubscribedDevices(offsetValue, limitValue, deviceIdList, status); + PaginationResult deviceDetails = deviceManagementProviderService.getAppSubscribedDevices + (request, deviceIdList, status); if (deviceDetails == null) { String msg = "Couldn't found an subscribed devices details for device ids: " + deviceIdList; @@ -1344,6 +1340,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { @Override public PaginationResult getAppSubscriptionDetails(int offsetValue, int limitValue, String appUUID) throws ApplicationManagementException { + PaginationRequest request = new PaginationRequest(offsetValue, limitValue); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); DeviceManagementProviderService deviceManagementProviderService = HelperUtil .getDeviceManagementProviderService(); @@ -1378,7 +1375,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { try { //pass the device id list to device manager service method PaginationResult paginationResult = deviceManagementProviderService - .getAppSubscribedDevices(offsetValue, limitValue, deviceIdList, null); + .getAppSubscribedDevices(request, deviceIdList, null); List deviceSubscriptionDataList = new ArrayList<>(); if (!paginationResult.getData().isEmpty()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java index 85485f1a08..be55b64639 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java @@ -32,6 +32,7 @@ import org.wso2.carbon.device.application.mgt.common.ErrorResponse; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import javax.validation.Valid; +import javax.validation.constraints.Size; import javax.ws.rs.Path; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; @@ -337,6 +338,28 @@ public interface SubscriptionManagementAPI { response = ErrorResponse.class) }) Response getAppInstalledDevices( + @ApiParam( + name = "name", + value = "The device name. For example, Nexus devices can have names, suhc as shamu, bullhead or angler.", + required = false) + @Size(max = 45) + String name, + @ApiParam( + name = "user", + value = "The username of the owner of the device.", + required = false) + @QueryParam("user") + String user, + @ApiParam( + name = "ownership", + allowableValues = "BYOD, COPE", + value = "Provide the ownership status of the device. The following values can be assigned:\n" + + "- BYOD: Bring Your Own Device\n" + + "- COPE: Corporate-Owned, Personally-Enabled", + required = false) + @QueryParam("ownership") + @Size(max = 45) + String ownership, @ApiParam( name="uuid", value="uuid of the application release.", diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index 4e71aa760c..b04adbca63 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -33,6 +33,7 @@ import org.wso2.carbon.device.application.mgt.common.BasicUserInfo; import org.wso2.carbon.device.application.mgt.common.BasicUserInfoList; import org.wso2.carbon.device.application.mgt.common.RoleList; import org.wso2.carbon.device.application.mgt.common.DeviceGroupList; +import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; @@ -273,6 +274,9 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @Produces("application/json") @Path("/{uuid}/devices") public Response getAppInstalledDevices( + @QueryParam("name") String name, + @QueryParam("user") String user, + @QueryParam("ownership") String ownership, @PathParam("uuid") String uuid, @DefaultValue("0") @QueryParam("offset") int offset, @@ -281,8 +285,17 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @QueryParam("status") List status) { try { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); - PaginationResult subscribedDeviceDetails = subscriptionManager - .getAppInstalledDevices(offset, limit, uuid, status); + PaginationRequest request = new PaginationRequest(offset, limit); + if (name != null && !name.isEmpty()) { + request.setDeviceName(name); + } + if (user != null && !user.isEmpty()) { + request.setOwner(user); + } + if (ownership != null && !ownership.isEmpty()) { + request.setOwnership(ownership); + } + PaginationResult subscribedDeviceDetails = subscriptionManager.getAppInstalledDevices(request, uuid, status); DeviceList devices = new DeviceList(); devices.setList((List) subscribedDeviceDetails.getData()); devices.setCount(subscribedDeviceDetails.getRecordsTotal()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 38ef50a923..620faf898f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -651,16 +651,15 @@ public interface DeviceDAO { /** * This method is used to get the details of subscribed devices. * - * @param deviceIds device ids of the subscribed devices. - * @param tenantId Id of the current tenant. - * @param offsetValue offset value for get paginated request. - * @param limitValue limit value for get paginated request. - * @param status status of the devices. + * @param deviceIds device ids of the subscribed devices. + * @param tenantId Id of the current tenant. + * @param request paginated request object. + * @param status status of the devices. * @return devices - subscribed device details list * @throws DeviceManagementDAOException if connections establishment fails. */ - List getSubscribedDevices(int offsetValue, int limitValue, List deviceIds, - int tenantId, List status) throws DeviceManagementDAOException; + List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, + List status) throws DeviceManagementDAOException; /** * @param deviceIds device ids of the subscribed devices. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index 83615eb056..65b83fcafb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -877,11 +877,15 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List getSubscribedDevices(int offsetValue, int limitValue, - List deviceIds, int tenantId, List status) + public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, + List status) throws DeviceManagementDAOException { Connection conn; - + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); + String name = request.getDeviceName(); + String user = request.getOwner(); + String ownership = request.getOwnership(); try { List devices = new ArrayList<>(); if (deviceIds.isEmpty()) { @@ -891,6 +895,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { int index = 1; boolean isStatusProvided = false; + boolean isDeviceNameProvided = false; + boolean isOwnerProvided = false; + boolean isOwnershipProvided = false; + StringJoiner joiner = new StringJoiner(",", "SELECT " + "DM_DEVICE.ID AS DEVICE_ID, " @@ -918,6 +926,18 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { deviceIds.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); + if (name != null && !name.isEmpty()) { + query += " AND DM_DEVICE.NAME LIKE ?"; + isDeviceNameProvided = true; + } + if (ownership != null && !ownership.isEmpty()) { + query += " AND e.OWNERSHIP = ?"; + isOwnershipProvided = true; + } + if (user != null && !user.isEmpty()) { + query += " AND e.OWNER = ?"; + isOwnerProvided = true; + } if (status != null && !status.isEmpty()) { query += buildStatusQuery(status); isStatusProvided = true; @@ -930,8 +950,16 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { for (Integer deviceId : deviceIds) { ps.setObject(index++, deviceId); } - ps.setInt(index++, tenantId); + if (isDeviceNameProvided) { + ps.setString(index++, name + "%"); + } + if (isOwnershipProvided) { + ps.setString(index++, ownership); + } + if (isOwnerProvided) { + ps.setString(index++, user); + } if (isStatusProvided) { for (String deviceStatus : status) { ps.setString(index++, deviceStatus); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index ed5a8c843c..a9f2fd9b6e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -847,11 +847,15 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List getSubscribedDevices(int offsetValue, int limitValue, - List deviceIds, int tenantId, List status) + public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, + List status) throws DeviceManagementDAOException { Connection conn; - + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); + String name = request.getDeviceName(); + String user = request.getOwner(); + String ownership = request.getOwnership(); try { List devices = new ArrayList<>(); if (deviceIds.isEmpty()) { @@ -861,6 +865,9 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { int index = 1; boolean isStatusProvided = false; + boolean isDeviceNameProvided = false; + boolean isOwnerProvided = false; + boolean isOwnershipProvided = false; StringJoiner joiner = new StringJoiner(",", "SELECT " + "DM_DEVICE.ID AS DEVICE_ID, " @@ -888,6 +895,18 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { deviceIds.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); + if (name != null && !name.isEmpty()) { + query += " AND DM_DEVICE.NAME LIKE ?"; + isDeviceNameProvided = true; + } + if (ownership != null && !ownership.isEmpty()) { + query += " AND e.OWNERSHIP = ?"; + isOwnershipProvided = true; + } + if (user != null && !user.isEmpty()) { + query += " AND e.OWNER = ?"; + isOwnerProvided = true; + } if (status != null && !status.isEmpty()) { query += buildStatusQuery(status); isStatusProvided = true; @@ -902,6 +921,15 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { } ps.setInt(index++, tenantId); + if (isDeviceNameProvided) { + ps.setString(index++, name + "%"); + } + if (isOwnershipProvided) { + ps.setString(index++, ownership); + } + if (isOwnerProvided) { + ps.setString(index++, user); + } if (isStatusProvided) { for (String deviceStatus : status) { ps.setString(index++, deviceStatus); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java index cfefbb8b3f..5d08b13838 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java @@ -827,11 +827,15 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List getSubscribedDevices(int offsetValue, int limitValue, - List deviceIds, int tenantId, List status) + public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, + List status) throws DeviceManagementDAOException { Connection conn; - + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); + String name = request.getDeviceName(); + String user = request.getOwner(); + String ownership = request.getOwnership(); try { List devices = new ArrayList<>(); if (deviceIds.isEmpty()) { @@ -841,6 +845,9 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { int index = 1; boolean isStatusProvided = false; + boolean isDeviceNameProvided = false; + boolean isOwnerProvided = false; + boolean isOwnershipProvided = false; StringJoiner joiner = new StringJoiner(",", "SELECT " + "DM_DEVICE.ID AS DEVICE_ID, " @@ -868,6 +875,18 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { deviceIds.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); + if (name != null && !name.isEmpty()) { + query += " AND DM_DEVICE.NAME LIKE ?"; + isDeviceNameProvided = true; + } + if (ownership != null && !ownership.isEmpty()) { + query += " AND e.OWNERSHIP = ?"; + isOwnershipProvided = true; + } + if (user != null && !user.isEmpty()) { + query += " AND e.OWNER = ?"; + isOwnerProvided = true; + } if (status != null && !status.isEmpty()) { query += buildStatusQuery(status); isStatusProvided = true; @@ -882,6 +901,15 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { } ps.setInt(index++, tenantId); + if (isDeviceNameProvided) { + ps.setString(index++, name + "%"); + } + if (isOwnershipProvided) { + ps.setString(index++, ownership); + } + if (isOwnerProvided) { + ps.setString(index++, user); + } if (isStatusProvided) { for (String deviceStatus : status) { ps.setString(index++, deviceStatus); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 755f882a46..315e9dc902 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -693,11 +693,15 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List getSubscribedDevices(int offsetValue, int limitValue, + public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, List status) throws DeviceManagementDAOException { Connection conn; - + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); + String name = request.getDeviceName(); + String user = request.getOwner(); + String ownership = request.getOwnership(); try { List devices = new ArrayList<>(); if (deviceIds.isEmpty()) { @@ -707,6 +711,9 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { int index = 1; boolean isStatusProvided = false; + boolean isDeviceNameProvided = false; + boolean isOwnerProvided = false; + boolean isOwnershipProvided = false; StringJoiner joiner = new StringJoiner(",", "SELECT " + "DM_DEVICE.ID AS DEVICE_ID, " @@ -734,6 +741,18 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { deviceIds.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); + if (name != null && !name.isEmpty()) { + query += " AND DM_DEVICE.NAME LIKE ?"; + isDeviceNameProvided = true; + } + if (ownership != null && !ownership.isEmpty()) { + query += " AND e.OWNERSHIP = ?"; + isOwnershipProvided = true; + } + if (user != null && !user.isEmpty()) { + query += " AND e.OWNER = ?"; + isOwnerProvided = true; + } if (status != null && !status.isEmpty()) { query += buildStatusQuery(status); isStatusProvided = true; @@ -748,6 +767,15 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { } ps.setInt(index++, tenantId); + if (isDeviceNameProvided) { + ps.setString(index++, name + "%"); + } + if (isOwnershipProvided) { + ps.setString(index++, ownership); + } + if (isOwnerProvided) { + ps.setString(index++, user); + } if (isStatusProvided) { for (String deviceStatus : status) { ps.setString(index++, deviceStatus); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index fb3dd19527..d5f31c1546 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -905,14 +905,13 @@ public interface DeviceManagementProviderService { * This method retrieves a list of subscribed devices. * * @param devicesIds devices ids of the subscribed devices. - * @param offsetValue offset value for get paginated request. - * @param limitValue limit value for get paginated request. - * @param status status of the devices. + * @param request paginated request object. + * @param status status of the devices. * @return {@link PaginationResult} * @throws DeviceManagementException if any service level or DAO level error occurs. */ - PaginationResult getAppSubscribedDevices(int offsetValue, int limitValue, - List devicesIds, List status) throws DeviceManagementException; + PaginationResult getAppSubscribedDevices(PaginationRequest request, List devicesIds, List status) + throws DeviceManagementException; /** * This method is used to get a list of applications installed in all enrolled devices diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 7456111973..343acf2602 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -4156,9 +4156,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public PaginationResult getAppSubscribedDevices(int offsetValue, int limitValue, List devicesIds, + public PaginationResult getAppSubscribedDevices(PaginationRequest request, List devicesIds, List status) throws DeviceManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (log.isDebugEnabled()) { log.debug("Getting all devices details for device ids: " + devicesIds); @@ -4167,8 +4166,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv List subscribedDeviceDetails; try { DeviceManagementDAOFactory.openConnection(); - subscribedDeviceDetails = deviceDAO - .getSubscribedDevices(offsetValue, limitValue, devicesIds, tenantId, status); + subscribedDeviceDetails = deviceDAO.getSubscribedDevices(request, devicesIds, tenantId, status); if (subscribedDeviceDetails.isEmpty()) { paginationResult.setData(new ArrayList<>()); paginationResult.setRecordsFiltered(0); From f3d1ad4f0df77f703125d97bfddd3bde7cfdab79 Mon Sep 17 00:00:00 2001 From: MalshaPiumini Date: Wed, 17 Mar 2021 15:25:02 +0530 Subject: [PATCH 3/9] Device search in store subscription details page (cherry picked from commit c2940a4568b55d0003f26b6d87447c92a9ee38a0) --- .../common/services/SubscriptionManager.java | 7 +-- .../mgt/core/dao/SubscriptionDAO.java | 2 +- .../GenericSubscriptionDAOImpl.java | 11 +++- .../mgt/core/impl/ApplicationManagerImpl.java | 4 +- .../core/impl/SubscriptionManagerImpl.java | 13 ++--- .../admin/SubscriptionManagementAdminAPI.java | 21 ++++++++ .../impl/SubscriptionManagementAPIImpl.java | 32 ++++++++++++ .../SubscriptionManagementAdminAPIImpl.java | 51 ++++++++++++++++++- 8 files changed, 127 insertions(+), 14 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index c1c0155087..1073015a73 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -152,14 +152,15 @@ public interface SubscriptionManager { /** * This method is responsible to provide application subscription data for given application release UUID. * - * @param offsetValue offset - * @param limitValue limit + * @param request paginated request object. + * @param status status of the devices. + * @param actionStatus status of the operation. * @param appUUID application release UUID * @return {@link PaginationResult} * @throws ApplicationManagementException if offset or limit contains incorrect values, if it couldn't find an * application release for given UUID, if an error occurred while getting device details of subscribed device ids, * if an error occurred while getting subscription details of given application release UUID. */ - PaginationResult getAppSubscriptionDetails(int offsetValue, int limitValue, String appUUID) + PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, List status,String actionStatus) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index 99b7395b1f..1a8dcedfa6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -81,7 +81,7 @@ public interface SubscriptionDAO { void addGroupSubscriptions(int tenantId, String subscribedBy, List groups, int releaseId, String action) throws ApplicationManagementDAOException; - List getDeviceSubscriptions(int appReleaseId, int tenantId) throws + List getDeviceSubscriptions(int appReleaseId, int tenantId, String actionStatus) throws ApplicationManagementDAOException; Map getDeviceSubscriptions(List deviceIds, int appReleaseId, int tenantId) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index c6937a05a8..c87ffb7ab7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -332,12 +332,13 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getDeviceSubscriptions(int appReleaseId, int tenantId) throws + public List getDeviceSubscriptions(int appReleaseId, int tenantId, String actionStatus) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " from the database"); } + boolean isActionStatusProvided = false; String sql = "SELECT " + "DS.ID AS ID, " + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " @@ -350,11 +351,19 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "DS.DM_DEVICE_ID AS DEVICE_ID " + "FROM AP_DEVICE_SUBSCRIPTION DS " + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.TENANT_ID=?"; + + if (actionStatus != null && !actionStatus.isEmpty()) { + sql += " AND DS.STATUS= ?"; + isActionStatusProvided = true; + } try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, appReleaseId); stmt.setInt(2, tenantId); + if(isActionStatusProvided){ + stmt.setString(3, actionStatus); + } try (ResultSet rs = stmt.executeQuery()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved device subscriptions for application release id " diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 2cebd98c61..80706aef41 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -1445,7 +1445,7 @@ ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new ForbiddenException(msg); } - if (!subscriptionDAO.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId).isEmpty()) { + if (!subscriptionDAO.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId, null).isEmpty()) { String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + " either subscribed to device/s or it had subscribed to device/s. Therefore you are not " + "permitted to delete the application release."; @@ -1580,7 +1580,7 @@ ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.beginDBTransaction(); List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); + .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId, null); if (!deviceSubscriptionDTOS.isEmpty()) { String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + " either subscribed to device/s or it had subscribed to device/s. Therefore you " diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index d3b45b9971..8b87b8a0d7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -377,7 +377,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (applicationDTO != null) { List deviceSubscriptionDTOS = this.subscriptionDAO .getDeviceSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), - tenantId); + tenantId, null); AtomicBoolean isAppSubscribable = new AtomicBoolean(true); for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { @@ -1231,7 +1231,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseId, tenantId); + .getDeviceSubscriptions(applicationReleaseId, tenantId, null); if (deviceSubscriptionDTOS.isEmpty()) { PaginationResult paginationResult = new PaginationResult(); paginationResult.setData(new ArrayList<>()); @@ -1338,9 +1338,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public PaginationResult getAppSubscriptionDetails(int offsetValue, int limitValue, String appUUID) + public PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, List status,String actionStatus) throws ApplicationManagementException { - PaginationRequest request = new PaginationRequest(offsetValue, limitValue); + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); DeviceManagementProviderService deviceManagementProviderService = HelperUtil .getDeviceManagementProviderService(); @@ -1362,7 +1363,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseId, tenantId); + .getDeviceSubscriptions(applicationReleaseId, tenantId, actionStatus); if (deviceSubscriptionDTOS.isEmpty()) { PaginationResult paginationResult = new PaginationResult(); paginationResult.setData(new ArrayList<>()); @@ -1375,7 +1376,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { try { //pass the device id list to device manager service method PaginationResult paginationResult = deviceManagementProviderService - .getAppSubscribedDevices(request, deviceIdList, null); + .getAppSubscribedDevices(request, deviceIdList, status); List deviceSubscriptionDataList = new ArrayList<>(); if (!paginationResult.getData().isEmpty()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java index 7c47b1f7cf..2cb9d63b21 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java @@ -31,6 +31,7 @@ import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.application.mgt.common.ErrorResponse; +import javax.validation.constraints.Size; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -111,6 +112,26 @@ public interface SubscriptionManagementAdminAPI { response = ErrorResponse.class) }) Response getAppInstalledDevices( + @ApiParam( + name = "name", + value = "The device name. For example, Nexus devices can have names, suhc as shamu, bullhead or angler.", + required = false) + @Size(max = 45) + String name, + @ApiParam( + name = "user", + value = "The username of the owner of the device.", + required = false) + @QueryParam("user") + String user, + @ApiParam( + name = "actionStatus", + value = "Provide the action status details") + @QueryParam("actionStatus") String actionStatus, + @ApiParam( + name = "status", + value = "Provide the device status details, such as active or inactive.") + @QueryParam("status") List status, @ApiParam( name = "uuid", value = "uuid of the application release.", diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index b04adbca63..afe3c922fd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -295,6 +295,38 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ if (ownership != null && !ownership.isEmpty()) { request.setOwnership(ownership); } +// if (status != null && !status.isEmpty()) { +// boolean isStatusEmpty = true; +// for (String statusString : status){ +// if (StringUtils.isNotBlank(statusString)){ +// isStatusEmpty = false; +// break; +// } +// } +// if (!isStatusEmpty) { +// for (String status_ : status) { +// switch (status_) { +// case "ACTIVE": +// case "INACTIVE": +// case "UNCLAIMED": +// case "UNREACHABLE": +// case "SUSPENDED": +// case "DISENROLLMENT_REQUESTED": +// case "REMOVED": +// case "BLOCKED": +// case "CREATED": +// break; +// default: +// String msg = "Invalid enrollment status type: " + status_ + ". \nValid status types are " + +// "ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + +// "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; +// log.error(msg); +// return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); +// } +// } +// request.setStatusList(status); +// } +// } PaginationResult subscribedDeviceDetails = subscriptionManager.getAppInstalledDevices(request, uuid, status); DeviceList devices = new DeviceList(); devices.setList((List) subscribedDeviceDetails.getData()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java index 2462556570..56c7e94646 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java @@ -18,16 +18,21 @@ package org.wso2.carbon.device.application.mgt.store.api.services.impl.admin; +import io.swagger.annotations.ApiParam; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.http.HttpStatus; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.services.admin.SubscriptionManagementAdminAPI; +import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; +import javax.validation.constraints.Size; import javax.ws.rs.Consumes; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; @@ -36,6 +41,7 @@ import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; +import java.util.List; /** * Implementation of Subscription Management related APIs. @@ -51,6 +57,10 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen @Produces("application/json") @Path("/{uuid}") public Response getAppInstalledDevices( + @QueryParam("name") String name, + @QueryParam("user") String user, + @QueryParam("actionStatus") String actionStatus, + @QueryParam("status") List status, @PathParam("uuid") String uuid, @DefaultValue("0") @QueryParam("offset") int offset, @@ -58,9 +68,48 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen @QueryParam("limit") int limit) { try { + PaginationRequest request = new PaginationRequest(offset, limit); + if (name != null && !name.isEmpty()) { + request.setDeviceName(name); + } + if (user != null && !user.isEmpty()) { + request.setOwner(user); + } +// if (status != null && !status.isEmpty()) { +// boolean isStatusEmpty = true; +// for (String statusString : status){ +// if (StringUtils.isNotBlank(statusString)){ +// isStatusEmpty = false; +// break; +// } +// } +// if (!isStatusEmpty) { +// for (String status_ : status) { +// switch (status_) { +// case "ACTIVE": +// case "INACTIVE": +// case "UNCLAIMED": +// case "UNREACHABLE": +// case "SUSPENDED": +// case "DISENROLLMENT_REQUESTED": +// case "REMOVED": +// case "BLOCKED": +// case "CREATED": +// break; +// default: +// String msg = "Invalid enrollment status type: " + status_ + ". \nValid status types are " + +// "ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + +// "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; +// log.error(msg); +// return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); +// } +// } +// request.setStatusList(status); +// } +// } SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); PaginationResult subscriptionData = subscriptionManager - .getAppSubscriptionDetails(offset, limit, uuid); + .getAppSubscriptionDetails(request, uuid, status, actionStatus); return Response.status(Response.Status.OK).entity(subscriptionData).build(); } catch (NotFoundException e) { String msg = "Application with application release UUID: " + uuid + " is not found"; From 82ebe79f2d4c1c6b602bc1334c2f88ac83f2ac4e Mon Sep 17 00:00:00 2001 From: MalshaPiumini Date: Wed, 17 Mar 2021 18:37:14 +0530 Subject: [PATCH 4/9] Code modified (cherry picked from commit 605168e770488c7b7890d63e3fbe772f2f382a48) --- .../common/services/SubscriptionManager.java | 6 +- .../core/impl/SubscriptionManagerImpl.java | 10 +-- .../impl/SubscriptionManagementAPIImpl.java | 66 +++++++------- .../SubscriptionManagementAdminAPIImpl.java | 89 ++++++++++++------- .../carbon/device/mgt/core/dao/DeviceDAO.java | 5 +- .../dao/impl/device/GenericDeviceDAOImpl.java | 4 +- .../dao/impl/device/OracleDeviceDAOImpl.java | 4 +- .../impl/device/PostgreSQLDeviceDAOImpl.java | 4 +- .../impl/device/SQLServerDeviceDAOImpl.java | 4 +- .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 7 +- 11 files changed, 109 insertions(+), 92 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index 1073015a73..5951433c14 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -133,8 +133,7 @@ public interface SubscriptionManager { * @return deviceDetails - device details for given application release. * @throws {@link ApplicationManagementException} Exception of the application management */ - PaginationResult getAppInstalledDevices(PaginationRequest request, String appUUID, - List status) throws ApplicationManagementException; + PaginationResult getAppInstalledDevices(PaginationRequest request, String appUUID) throws ApplicationManagementException; /*** * This method used to get category details. @@ -153,7 +152,6 @@ public interface SubscriptionManager { * This method is responsible to provide application subscription data for given application release UUID. * * @param request paginated request object. - * @param status status of the devices. * @param actionStatus status of the operation. * @param appUUID application release UUID * @return {@link PaginationResult} @@ -161,6 +159,6 @@ public interface SubscriptionManager { * application release for given UUID, if an error occurred while getting device details of subscribed device ids, * if an error occurred while getting subscription details of given application release UUID. */ - PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, List status,String actionStatus) + PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 8b87b8a0d7..b4dcdaa7d6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1219,7 +1219,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public PaginationResult getAppInstalledDevices(PaginationRequest request, String appUUID, List status) + public PaginationResult getAppInstalledDevices(PaginationRequest request, String appUUID) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); DeviceManagementProviderService deviceManagementProviderService = HelperUtil @@ -1259,7 +1259,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { //pass the device id list to device manager service method try { PaginationResult deviceDetails = deviceManagementProviderService.getAppSubscribedDevices - (request, deviceIdList, status); + (request, deviceIdList); if (deviceDetails == null) { String msg = "Couldn't found an subscribed devices details for device ids: " + deviceIdList; @@ -1338,7 +1338,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, List status,String actionStatus) + public PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus) throws ApplicationManagementException { int limitValue = request.getRowCount(); int offsetValue = request.getStartIndex(); @@ -1375,8 +1375,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { .collect(Collectors.toList()); try { //pass the device id list to device manager service method - PaginationResult paginationResult = deviceManagementProviderService - .getAppSubscribedDevices(request, deviceIdList, status); + PaginationResult paginationResult = deviceManagementProviderService.getAppSubscribedDevices + (request, deviceIdList); List deviceSubscriptionDataList = new ArrayList<>(); if (!paginationResult.getData().isEmpty()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index afe3c922fd..2dc964f4d6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -295,39 +295,39 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ if (ownership != null && !ownership.isEmpty()) { request.setOwnership(ownership); } -// if (status != null && !status.isEmpty()) { -// boolean isStatusEmpty = true; -// for (String statusString : status){ -// if (StringUtils.isNotBlank(statusString)){ -// isStatusEmpty = false; -// break; -// } -// } -// if (!isStatusEmpty) { -// for (String status_ : status) { -// switch (status_) { -// case "ACTIVE": -// case "INACTIVE": -// case "UNCLAIMED": -// case "UNREACHABLE": -// case "SUSPENDED": -// case "DISENROLLMENT_REQUESTED": -// case "REMOVED": -// case "BLOCKED": -// case "CREATED": -// break; -// default: -// String msg = "Invalid enrollment status type: " + status_ + ". \nValid status types are " + -// "ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + -// "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; -// log.error(msg); -// return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); -// } -// } -// request.setStatusList(status); -// } -// } - PaginationResult subscribedDeviceDetails = subscriptionManager.getAppInstalledDevices(request, uuid, status); + if (status != null && !status.isEmpty()) { + boolean isStatusEmpty = true; + for (String statusString : status) { + if (StringUtils.isNotBlank(statusString)) { + isStatusEmpty = false; + break; + } + } + if (!isStatusEmpty) { + for (String status_ : status) { + switch (status_) { + case "ACTIVE": + case "INACTIVE": + case "UNCLAIMED": + case "UNREACHABLE": + case "SUSPENDED": + case "DISENROLLMENT_REQUESTED": + case "REMOVED": + case "BLOCKED": + case "CREATED": + break; + default: + String msg = "Invalid enrollment status type: " + status_ + ". \nValid status types are " + + "ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + + "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } + } + request.setStatusList(status); + } + } + PaginationResult subscribedDeviceDetails = subscriptionManager.getAppInstalledDevices(request, uuid); DeviceList devices = new DeviceList(); devices.setList((List) subscribedDeviceDetails.getData()); devices.setCount(subscribedDeviceDetails.getRecordsTotal()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java index 56c7e94646..5536fc46d8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java @@ -75,41 +75,62 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen if (user != null && !user.isEmpty()) { request.setOwner(user); } -// if (status != null && !status.isEmpty()) { -// boolean isStatusEmpty = true; -// for (String statusString : status){ -// if (StringUtils.isNotBlank(statusString)){ -// isStatusEmpty = false; -// break; -// } -// } -// if (!isStatusEmpty) { -// for (String status_ : status) { -// switch (status_) { -// case "ACTIVE": -// case "INACTIVE": -// case "UNCLAIMED": -// case "UNREACHABLE": -// case "SUSPENDED": -// case "DISENROLLMENT_REQUESTED": -// case "REMOVED": -// case "BLOCKED": -// case "CREATED": -// break; -// default: -// String msg = "Invalid enrollment status type: " + status_ + ". \nValid status types are " + -// "ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + -// "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; -// log.error(msg); -// return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); -// } -// } -// request.setStatusList(status); -// } -// } + if (status != null && !status.isEmpty()) { + boolean isStatusEmpty = true; + for (String statusString : status) { + if (StringUtils.isNotBlank(statusString)) { + isStatusEmpty = false; + break; + } + } + if (!isStatusEmpty) { + for (String status_ : status) { + switch (status_) { + case "ACTIVE": + case "INACTIVE": + case "UNCLAIMED": + case "UNREACHABLE": + case "SUSPENDED": + case "DISENROLLMENT_REQUESTED": + case "REMOVED": + case "BLOCKED": + case "CREATED": + break; + default: + String msg = "Invalid enrollment status type: " + status_ + ". \nValid status types " + + "are ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + + "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } + } + request.setStatusList(status); + } + } + + if (actionStatus != null && !actionStatus.isEmpty()) { + if (StringUtils.isNotBlank(actionStatus)) { + switch (actionStatus) { + case "PENDING": + case "ERROR": + case "IN_PROGRESS": + case "NOTNOW": + case "COMPLETED": + case "REPEATED": + break; + default: + String msg = "Invalid enrollment action status type: " + actionStatus + ". " + + "\nValid action status types are PENDING | ERROR | IN_PROGRESS | NOTNOW | " + + "COMPLETED | REPEATED"; + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } + } + } + SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); - PaginationResult subscriptionData = subscriptionManager - .getAppSubscriptionDetails(request, uuid, status, actionStatus); + PaginationResult subscriptionData = subscriptionManager.getAppSubscriptionDetails + (request, uuid, actionStatus); return Response.status(Response.Status.OK).entity(subscriptionData).build(); } catch (NotFoundException e) { String msg = "Application with application release UUID: " + uuid + " is not found"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 620faf898f..82aded2490 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -654,12 +654,11 @@ public interface DeviceDAO { * @param deviceIds device ids of the subscribed devices. * @param tenantId Id of the current tenant. * @param request paginated request object. - * @param status status of the devices. * @return devices - subscribed device details list * @throws DeviceManagementDAOException if connections establishment fails. */ - List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, - List status) throws DeviceManagementDAOException; + List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId) + throws DeviceManagementDAOException; /** * @param deviceIds device ids of the subscribed devices. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index 65b83fcafb..a5959cc5f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -877,12 +877,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, - List status) + public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException { Connection conn; int limitValue = request.getRowCount(); int offsetValue = request.getStartIndex(); + List status = request.getStatusList(); String name = request.getDeviceName(); String user = request.getOwner(); String ownership = request.getOwnership(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index a9f2fd9b6e..109fac5507 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -847,12 +847,12 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, - List status) + public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException { Connection conn; int limitValue = request.getRowCount(); int offsetValue = request.getStartIndex(); + List status = request.getStatusList(); String name = request.getDeviceName(); String user = request.getOwner(); String ownership = request.getOwnership(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java index 5d08b13838..fdf81826ad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java @@ -827,12 +827,12 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId, - List status) + public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException { Connection conn; int limitValue = request.getRowCount(); int offsetValue = request.getStartIndex(); + List status = request.getStatusList(); String name = request.getDeviceName(); String user = request.getOwner(); String ownership = request.getOwnership(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 315e9dc902..673d63304d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -693,12 +693,12 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { } @Override - public List getSubscribedDevices(PaginationRequest request, - List deviceIds, int tenantId, List status) + public List getSubscribedDevices(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException { Connection conn; int limitValue = request.getRowCount(); int offsetValue = request.getStartIndex(); + List status = request.getStatusList(); String name = request.getDeviceName(); String user = request.getOwner(); String ownership = request.getOwnership(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index d5f31c1546..157fed7c5d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -910,7 +910,7 @@ public interface DeviceManagementProviderService { * @return {@link PaginationResult} * @throws DeviceManagementException if any service level or DAO level error occurs. */ - PaginationResult getAppSubscribedDevices(PaginationRequest request, List devicesIds, List status) + PaginationResult getAppSubscribedDevices(PaginationRequest request, List devicesIds) throws DeviceManagementException; /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 343acf2602..4959c282c4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -4156,8 +4156,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public PaginationResult getAppSubscribedDevices(PaginationRequest request, List devicesIds, - List status) throws DeviceManagementException { + public PaginationResult getAppSubscribedDevices(PaginationRequest request, List devicesIds) throws DeviceManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (log.isDebugEnabled()) { log.debug("Getting all devices details for device ids: " + devicesIds); @@ -4166,14 +4165,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv List subscribedDeviceDetails; try { DeviceManagementDAOFactory.openConnection(); - subscribedDeviceDetails = deviceDAO.getSubscribedDevices(request, devicesIds, tenantId, status); + subscribedDeviceDetails = deviceDAO.getSubscribedDevices(request, devicesIds, tenantId); if (subscribedDeviceDetails.isEmpty()) { paginationResult.setData(new ArrayList<>()); paginationResult.setRecordsFiltered(0); paginationResult.setRecordsTotal(0); return paginationResult; } - int count = deviceDAO.getSubscribedDeviceCount(devicesIds, tenantId, status); + int count = deviceDAO.getSubscribedDeviceCount(devicesIds, tenantId, request.getStatusList()); paginationResult.setRecordsFiltered(count); paginationResult.setRecordsTotal(count); } catch (DeviceManagementDAOException e) { From 4664bb24e5f86b5cfdd2ca01925fc0ed07e2839f Mon Sep 17 00:00:00 2001 From: MalshaPiumini Date: Thu, 18 Mar 2021 17:20:30 +0530 Subject: [PATCH 5/9] Added validations (cherry picked from commit 645fc43136ab2eb2cfee7d2f04e38dcc9aaf4df0) --- .../common/services/SubscriptionManager.java | 1 - .../impl/SubscriptionManagementAPIImpl.java | 26 +---- .../SubscriptionManagementAdminAPIImpl.java | 43 +------- .../impl/util/RequestValidationUtil.java | 102 ++++++++++++++++++ .../mgt/store/api/util/Constants.java | 101 +++++++++++++++++ 5 files changed, 211 insertions(+), 62 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/util/RequestValidationUtil.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/util/Constants.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index 5951433c14..12ca873701 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -129,7 +129,6 @@ public interface SubscriptionManager { * * @param appUUID UUID of the application release. * @param request paginated request object. - * @param status status of the devices. * @return deviceDetails - device details for given application release. * @throws {@link ApplicationManagementException} Exception of the application management */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index 2dc964f4d6..acf9662cb9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -33,6 +33,7 @@ import org.wso2.carbon.device.application.mgt.common.BasicUserInfo; import org.wso2.carbon.device.application.mgt.common.BasicUserInfoList; import org.wso2.carbon.device.application.mgt.common.RoleList; import org.wso2.carbon.device.application.mgt.common.DeviceGroupList; +import org.wso2.carbon.device.application.mgt.store.api.services.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; @@ -293,6 +294,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ request.setOwner(user); } if (ownership != null && !ownership.isEmpty()) { + RequestValidationUtil.validateOwnershipType(ownership); request.setOwnership(ownership); } if (status != null && !status.isEmpty()) { @@ -304,26 +306,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ } } if (!isStatusEmpty) { - for (String status_ : status) { - switch (status_) { - case "ACTIVE": - case "INACTIVE": - case "UNCLAIMED": - case "UNREACHABLE": - case "SUSPENDED": - case "DISENROLLMENT_REQUESTED": - case "REMOVED": - case "BLOCKED": - case "CREATED": - break; - default: - String msg = "Invalid enrollment status type: " + status_ + ". \nValid status types are " + - "ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + - "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } - } + RequestValidationUtil.validateStatus(status); request.setStatusList(status); } } @@ -337,8 +320,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (BadRequestException e) { - String msg = "Found invalid payload for getting application which has UUID: " + uuid - + ". Hence verify the payload"; + String msg = "User requested details are not valid"; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ForbiddenException e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java index 5536fc46d8..4474ddaa81 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.services.admin.SubscriptionManagementAdminAPI; +import org.wso2.carbon.device.application.mgt.store.api.services.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -84,50 +85,15 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen } } if (!isStatusEmpty) { - for (String status_ : status) { - switch (status_) { - case "ACTIVE": - case "INACTIVE": - case "UNCLAIMED": - case "UNREACHABLE": - case "SUSPENDED": - case "DISENROLLMENT_REQUESTED": - case "REMOVED": - case "BLOCKED": - case "CREATED": - break; - default: - String msg = "Invalid enrollment status type: " + status_ + ". \nValid status types " + - "are ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + - "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } - } + RequestValidationUtil.validateStatus(status); request.setStatusList(status); } } - if (actionStatus != null && !actionStatus.isEmpty()) { if (StringUtils.isNotBlank(actionStatus)) { - switch (actionStatus) { - case "PENDING": - case "ERROR": - case "IN_PROGRESS": - case "NOTNOW": - case "COMPLETED": - case "REPEATED": - break; - default: - String msg = "Invalid enrollment action status type: " + actionStatus + ". " + - "\nValid action status types are PENDING | ERROR | IN_PROGRESS | NOTNOW | " + - "COMPLETED | REPEATED"; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } + RequestValidationUtil.validateStatusFiltering(actionStatus); } } - SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); PaginationResult subscriptionData = subscriptionManager.getAppSubscriptionDetails (request, uuid, actionStatus); @@ -137,8 +103,7 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (BadRequestException e) { - String msg = "Found invalid payload for getting application which has UUID: " + uuid - + ". Hence verify the payload"; + String msg = "User requested details are not valid"; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ApplicationManagementException e) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/util/RequestValidationUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/util/RequestValidationUtil.java new file mode 100644 index 0000000000..f557579c77 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/util/RequestValidationUtil.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.store.api.services.impl.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; +import org.wso2.carbon.device.application.mgt.store.api.util.Constants; + +import java.util.List; + +public class RequestValidationUtil { + + private static final Log log = LogFactory.getLog(RequestValidationUtil.class); + + /** + * Checks if user requested status codes are valid. + * + * @param statusList status codes upon to filter operation logs using status + */ + public static void validateStatus(List statusList) throws BadRequestException { + for (String status : statusList) { + switch (status) { + case "ACTIVE": + case "INACTIVE": + case "UNCLAIMED": + case "UNREACHABLE": + case "SUSPENDED": + case "DISENROLLMENT_REQUESTED": + case "REMOVED": + case "BLOCKED": + case "CREATED": + break; + default: + String msg = "Invalid enrollment status type: " + status + ". \nValid status types " + + "are ACTIVE | INACTIVE | UNCLAIMED | UNREACHABLE | SUSPENDED | " + + "DISENROLLMENT_REQUESTED | REMOVED | BLOCKED | CREATED"; + log.error(msg); + throw new BadRequestException(msg); + } + } + } + + /** + * Checks if user requested ownerships are valid. + * + * @param ownership ownerships upon to filter devices using ownership + */ + public static void validateOwnershipType(String ownership) throws BadRequestException { + switch (ownership) { + case "BYOD": + case "COPE": + case "WORK_PROFILE": + case "GOOGLE_ENTERPRISE": + case "COSU": + case "FULLY_MANAGED": + case "DEDICATED_DEVICE": + break; + default: + String msg = "Invalid ownership type received.Valid ownership types are BYOD | COPE | WORK_PROFILE |" + + "GOOGLE_ENTERPRISE | COSU | FULLY_MANAGED | DEDICATED_DEVICE"; + log.error(msg); + throw new BadRequestException(msg); + } + } + + /** + * Checks if user requested Action status codes are valid. + * + * @param status status codes upon to filter operation logs using status + */ + public static void validateStatusFiltering(String status) throws BadRequestException { + if (Constants.OperationStatus.COMPLETED.toUpperCase().equals(status) + || Constants.OperationStatus.ERROR.toUpperCase().equals(status) + || Constants.OperationStatus.NOTNOW.toUpperCase().equals(status) + || Constants.OperationStatus.REPEATED.toUpperCase().equals(status) + || Constants.OperationStatus.PENDING.toUpperCase().equals(status) + || Constants.OperationStatus.IN_PROGRESS.toUpperCase().equals(status)) { + } else { + String msg = "Invalid status type: " + status + ". \nValid status types are COMPLETED | ERROR | " + + "IN_PROGRESS | NOTNOW | PENDING | REPEATED"; + log.error(msg); + throw new BadRequestException(msg); + } + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/util/Constants.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/util/Constants.java new file mode 100644 index 0000000000..a78c16077f --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/util/Constants.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * + * Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.application.mgt.store.api.util; + +/** + * Holds the constants used by DeviceImpl Management Admin web application. + */ +public class Constants { + + public static final String USER_CLAIM_EMAIL_ADDRESS = "http://wso2.org/claims/emailaddress"; + public static final String USER_CLAIM_FIRST_NAME = "http://wso2.org/claims/givenname"; + public static final String USER_CLAIM_LAST_NAME = "http://wso2.org/claims/lastname"; + public static final String USER_CLAIM_CREATED = "http://wso2.org/claims/created"; + public static final String USER_CLAIM_MODIFIED = "http://wso2.org/claims/modified"; + public static final String USER_CLAIM_DEVICES = "http://wso2.org/claims/devices"; + public static final String PRIMARY_USER_STORE = "PRIMARY"; + public static final String DEFAULT_STREAM_VERSION = "1.0.0"; + public static final String SCOPE = "scope"; + public static final String JDBC_USERSTOREMANAGER = "org.wso2.carbon.user.core.jdbc.JDBCUserStoreManager"; + public static final String DEFAULT_SIMPLE_DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; + public static final int DEFAULT_PAGE_LIMIT = 50; + public static final String FORWARD_SLASH = "/"; + public static final String ANDROID = "android"; + public static final String ANDROID_POLICY_VALIDATOR = "io.entgra.proprietary.platform.android." + + "core.polcy.AndroidPolicyPayloadValidator"; + public static final String IOS = "ios"; + public static final String WINDOWS = "windows"; + + + public final class OperationStatus { + private OperationStatus () { throw new AssertionError(); } + public static final String COMPLETED = "completed"; + public static final String ERROR = "error"; + public static final String IN_PROGRESS = "in_progress"; + public static final String PENDING = "pending"; + public static final String NOTNOW = "notnow"; + public static final String REPEATED = "repeated"; + } + public static final String DEVICES = "devices"; + public static final String ATTRIBUTE_DISPLAY_NAME = "DisplayName"; + public static final String ATTRIBUTE_DESCRIPTION = "Description"; + public static final String EXTERNAL_DEVICE_CLAIM_DISPLAY_NAME = "Devices"; + public static final String EXTERNAL_DEVICE_CLAIM_DESCRIPTION = "Device list"; + + public final class ErrorMessages { + private ErrorMessages () { throw new AssertionError(); } + + public static final String STATUS_BAD_REQUEST_MESSAGE_DEFAULT = "Bad Request"; + + } + + public final class DeviceConstants { + private DeviceConstants () { throw new AssertionError(); } + + public static final String APPLICATION_JSON = "application/json"; + public static final String HEADER_CONTENT_TYPE = "Content-Type"; + } + + public final class Permission { + private Permission() { throw new AssertionError(); } + + public static final String ADMIN = "/permission/admin"; + public static final String LOGIN = "/permission/admin/login"; + public static final String DEVICE_MGT = "/permission/admin/device-mgt"; + public static final String APP_MGT = "/permission/admin/app-mgt"; + } + +} From fc67e8b7aab953425399dc749f07653dee576d3f Mon Sep 17 00:00:00 2001 From: MalshaPiumini Date: Tue, 23 Mar 2021 14:50:36 +0530 Subject: [PATCH 6/9] Added new filter in subscription details page (cherry picked from commit dca6b53a67cb5e693f9691333c20655c6efb1842) --- .../common/services/SubscriptionManager.java | 3 ++- .../mgt/core/dao/SubscriptionDAO.java | 2 +- .../GenericSubscriptionDAOImpl.java | 23 +++++++++++++++---- .../mgt/core/impl/ApplicationManagerImpl.java | 4 ++-- .../core/impl/SubscriptionManagerImpl.java | 10 ++++---- .../admin/SubscriptionManagementAdminAPI.java | 6 +++++ .../SubscriptionManagementAdminAPIImpl.java | 11 +++++---- .../impl/util/RequestValidationUtil.java | 14 +++++++++++ .../DeviceManagementProviderService.java | 1 - 9 files changed, 54 insertions(+), 20 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index 12ca873701..a535ffd389 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -152,12 +152,13 @@ public interface SubscriptionManager { * * @param request paginated request object. * @param actionStatus status of the operation. + * @param action action related to the device. * @param appUUID application release UUID * @return {@link PaginationResult} * @throws ApplicationManagementException if offset or limit contains incorrect values, if it couldn't find an * application release for given UUID, if an error occurred while getting device details of subscribed device ids, * if an error occurred while getting subscription details of given application release UUID. */ - PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus) + PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, String action) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index 1a8dcedfa6..ce8780a6a2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -81,7 +81,7 @@ public interface SubscriptionDAO { void addGroupSubscriptions(int tenantId, String subscribedBy, List groups, int releaseId, String action) throws ApplicationManagementDAOException; - List getDeviceSubscriptions(int appReleaseId, int tenantId, String actionStatus) throws + List getDeviceSubscriptions(int appReleaseId, int tenantId, String actionStatus, String action) throws ApplicationManagementDAOException; Map getDeviceSubscriptions(List deviceIds, int appReleaseId, int tenantId) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index c87ffb7ab7..c2fd7f240a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -332,13 +332,15 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getDeviceSubscriptions(int appReleaseId, int tenantId, String actionStatus) throws + public List getDeviceSubscriptions(int appReleaseId, int tenantId, String actionStatus, String action) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " from the database"); } boolean isActionStatusProvided = false; + boolean isActionProvided = false; + int index = 1; String sql = "SELECT " + "DS.ID AS ID, " + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " @@ -356,13 +358,24 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc sql += " AND DS.STATUS= ?"; isActionStatusProvided = true; } + if (action != null && !action.isEmpty()) { + sql += " AND DS.UNSUBSCRIBED= ?"; + isActionProvided = true; + } try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, appReleaseId); - stmt.setInt(2, tenantId); - if(isActionStatusProvided){ - stmt.setString(3, actionStatus); + stmt.setInt(index++, appReleaseId); + stmt.setInt(index++, tenantId); + if (isActionStatusProvided) { + stmt.setString(index++, actionStatus); + } + if (isActionProvided) { + if (action.equals("SUBSCRIBED")) { + stmt.setString(index, "FALSE"); + } else { + stmt.setString(index, "TRUE"); + } } try (ResultSet rs = stmt.executeQuery()) { if (log.isDebugEnabled()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 80706aef41..05b757ba45 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -1445,7 +1445,7 @@ ApplicationManagerImpl implements ApplicationManager { log.error(msg); throw new ForbiddenException(msg); } - if (!subscriptionDAO.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId, null).isEmpty()) { + if (!subscriptionDAO.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId, null, null).isEmpty()) { String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + " either subscribed to device/s or it had subscribed to device/s. Therefore you are not " + "permitted to delete the application release."; @@ -1580,7 +1580,7 @@ ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.beginDBTransaction(); List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId, null); + .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId, null, null); if (!deviceSubscriptionDTOS.isEmpty()) { String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + " either subscribed to device/s or it had subscribed to device/s. Therefore you " diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index b4dcdaa7d6..5aa222ce13 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -377,7 +377,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (applicationDTO != null) { List deviceSubscriptionDTOS = this.subscriptionDAO .getDeviceSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), - tenantId, null); + tenantId, null, null); AtomicBoolean isAppSubscribable = new AtomicBoolean(true); for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { @@ -1231,7 +1231,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseId, tenantId, null); + .getDeviceSubscriptions(applicationReleaseId, tenantId, null, null); if (deviceSubscriptionDTOS.isEmpty()) { PaginationResult paginationResult = new PaginationResult(); paginationResult.setData(new ArrayList<>()); @@ -1338,8 +1338,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus) - throws ApplicationManagementException { + public PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, + String action) throws ApplicationManagementException { int limitValue = request.getRowCount(); int offsetValue = request.getStartIndex(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -1363,7 +1363,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseId, tenantId, actionStatus); + .getDeviceSubscriptions(applicationReleaseId, tenantId, actionStatus, action); if (deviceSubscriptionDTOS.isEmpty()) { PaginationResult paginationResult = new PaginationResult(); paginationResult.setData(new ArrayList<>()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java index 2cb9d63b21..97f6f6851e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java @@ -124,6 +124,12 @@ public interface SubscriptionManagementAdminAPI { required = false) @QueryParam("user") String user, + @ApiParam( + name = "action", + value = "The action, subscribed or unsubscribed.", + required = false) + @Size(max = 45) + @QueryParam("action") String action, @ApiParam( name = "actionStatus", value = "Provide the action status details") diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java index 4474ddaa81..681b3b5f38 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java @@ -18,11 +18,9 @@ package org.wso2.carbon.device.application.mgt.store.api.services.impl.admin; -import io.swagger.annotations.ApiParam; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.http.HttpStatus; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; @@ -33,7 +31,6 @@ import org.wso2.carbon.device.application.mgt.store.api.services.impl.util.Reque import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import javax.validation.constraints.Size; import javax.ws.rs.Consumes; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; @@ -60,6 +57,7 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen public Response getAppInstalledDevices( @QueryParam("name") String name, @QueryParam("user") String user, + @QueryParam("action") String action, @QueryParam("actionStatus") String actionStatus, @QueryParam("status") List status, @PathParam("uuid") String uuid, @@ -76,6 +74,9 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen if (user != null && !user.isEmpty()) { request.setOwner(user); } + if (action != null && !action.isEmpty()) { + RequestValidationUtil.validateAction(action); + } if (status != null && !status.isEmpty()) { boolean isStatusEmpty = true; for (String statusString : status) { @@ -96,7 +97,7 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen } SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); PaginationResult subscriptionData = subscriptionManager.getAppSubscriptionDetails - (request, uuid, actionStatus); + (request, uuid, actionStatus, action); return Response.status(Response.Status.OK).entity(subscriptionData).build(); } catch (NotFoundException e) { String msg = "Application with application release UUID: " + uuid + " is not found"; @@ -108,7 +109,7 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ApplicationManagementException e) { String msg = "Error occurred while getting app installed devices which has application release UUID of: " - + uuid; + + uuid; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/util/RequestValidationUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/util/RequestValidationUtil.java index f557579c77..2a59d220a4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/util/RequestValidationUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/util/RequestValidationUtil.java @@ -57,6 +57,20 @@ public class RequestValidationUtil { } } + /** + * Checks if user requested action is valid. + * + * @param action action upon to filter devices using action + */ + public static void validateAction(String action) throws BadRequestException { + if (action.equals("SUBSCRIBED") || action.equals("UNSUBSCRIBED")) { + } else { + String msg = "Invalid action type received.Valid action types are SUBSCRIBED | UNSUBSCRIBED"; + log.error(msg); + throw new BadRequestException(msg); + } + } + /** * Checks if user requested ownerships are valid. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 157fed7c5d..f084e35c3a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -906,7 +906,6 @@ public interface DeviceManagementProviderService { * * @param devicesIds devices ids of the subscribed devices. * @param request paginated request object. - * @param status status of the devices. * @return {@link PaginationResult} * @throws DeviceManagementException if any service level or DAO level error occurs. */ From d16a7a7fb8657499cd91a9d228f8548d0229ca75 Mon Sep 17 00:00:00 2001 From: MalshaPiumini Date: Wed, 21 Apr 2021 19:57:48 +0530 Subject: [PATCH 7/9] Added device filter using serial number. (cherry picked from commit b2691996ab23890566fa8542e6a31e83bd26eefe) --- .../service/api/DeviceManagementService.java | 6 +++ .../impl/DeviceManagementServiceImpl.java | 4 ++ .../impl/DeviceManagementServiceImplTest.java | 48 ++++++++++++------- .../device/mgt/common/PaginationRequest.java | 5 ++ .../dao/impl/device/GenericDeviceDAOImpl.java | 15 +++++- .../dao/impl/device/OracleDeviceDAOImpl.java | 15 +++++- .../impl/device/PostgreSQLDeviceDAOImpl.java | 17 +++++-- .../impl/device/SQLServerDeviceDAOImpl.java | 14 +++++- 8 files changed, 98 insertions(+), 26 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index f90a205c0a..8268fbaac6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -269,6 +269,12 @@ public interface DeviceManagementService { @QueryParam("ownership") @Size(max = 45) String ownership, + @ApiParam( + name = "serialNumber", + value = "The serialNumber of the device.", + required = false) + @QueryParam("serialNumber") + String serialNumber, @ApiParam( name = "status", value = "Provide the device status details, such as active or inactive.", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index b2f2d1ab11..1a151bddf9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -172,6 +172,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("userPattern") String userPattern, @QueryParam("role") String role, @QueryParam("ownership") String ownership, + @QueryParam("serialNumber") String serialNumber, @QueryParam("status") List status, @QueryParam("groupId") int groupId, @QueryParam("since") String since, @@ -205,6 +206,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { RequestValidationUtil.validateOwnershipType(ownership); request.setOwnership(ownership); } + if (serialNumber != null && !serialNumber.isEmpty()) { + request.setSerialNumber(serialNumber); + } if (status != null && !status.isEmpty()) { boolean isStatusEmpty = true; for (String statusString : status){ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java index bb9de05c1f..7b995660ad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImplTest.java @@ -175,7 +175,8 @@ public class DeviceManagementServiceImplTest { .toReturn(this.deviceAccessAuthorizationService); Response response = this.deviceManagementService .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 1, null, null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); } @@ -194,19 +195,23 @@ public class DeviceManagementServiceImplTest { Response response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 1, null, null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response = this.deviceManagementService .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, null, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 1, null, null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response = this.deviceManagementService .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, null, null, null, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 1, null, null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response = this.deviceManagementService .getDevices(TEST_DEVICE_NAME, TEST_DEVICE_TYPE, null, null, null, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 1, null, null, true, 10, 5); + null, DEFAULT_STATUS_LIST, 1, null, null, true, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); } @@ -319,7 +324,7 @@ public class DeviceManagementServiceImplTest { .toReturn(this.deviceManagementProviderService); Mockito.when(deviceAccessAuthorizationService.isDeviceAdminUser()).thenReturn(true); deviceManagementService.getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, - DEFAULT_ROLE, DEFAULT_OWNERSHIP, DEFAULT_STATUS_LIST, 1, + DEFAULT_ROLE, DEFAULT_OWNERSHIP, null, DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); } @@ -339,11 +344,11 @@ public class DeviceManagementServiceImplTest { Response response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP - , DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); + , null, DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, null, DEFAULT_USERNAME, DEFAULT_ROLE, DEFAULT_OWNERSHIP - , DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); + , null, DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); } @@ -365,7 +370,8 @@ public class DeviceManagementServiceImplTest { Response response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, "newuser", null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 0, null, null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 0, null, null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode()); Mockito.reset(this.deviceAccessAuthorizationService); } @@ -386,15 +392,18 @@ public class DeviceManagementServiceImplTest { Response response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 0, null, ifModifiedSince, false, 10, 5); + null, DEFAULT_STATUS_LIST, 0, null, ifModifiedSince, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode()); response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 0, null, ifModifiedSince, true, 10, 5); + null, DEFAULT_STATUS_LIST, 0, null, ifModifiedSince, true, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.NOT_MODIFIED.getStatusCode()); response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 0, null, "ErrorModifiedSince", false, 10, 5); + null, DEFAULT_STATUS_LIST, 0, null, "ErrorModifiedSince", + false, 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); } @@ -414,15 +423,18 @@ public class DeviceManagementServiceImplTest { Response response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 0, since, null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 0, since, null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 0, since, null, true, 10, 5); + null, DEFAULT_STATUS_LIST, 0, since, null, true, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 0, "ErrorSince", null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 0, "ErrorSince", null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode()); } @@ -444,7 +456,8 @@ public class DeviceManagementServiceImplTest { Response response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 1, null, null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); } @@ -466,7 +479,8 @@ public class DeviceManagementServiceImplTest { Response response = this.deviceManagementService .getDevices(null, TEST_DEVICE_TYPE, DEFAULT_USERNAME, null, DEFAULT_ROLE, DEFAULT_OWNERSHIP, - DEFAULT_STATUS_LIST, 1, null, null, false, 10, 5); + null, DEFAULT_STATUS_LIST, 1, null, null, false, + 10, 5); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); Mockito.reset(this.deviceAccessAuthorizationService); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java index 94cf5c5272..29bb7922e9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java @@ -40,6 +40,7 @@ public class PaginationRequest { private String ownerRole; private Date since; private String filter; + private String serialNumber; private Map property = new HashMap<>(); private List statusList = new ArrayList<>(); private OperationLogFilters operationLogFilters = new OperationLogFilters(); @@ -153,6 +154,10 @@ public class PaginationRequest { return this.property.get(key); } + public String getSerialNumber() { return serialNumber; } + + public void setSerialNumber(String serialNumber) { this.serialNumber = serialNumber; } + public Map getProperties() { Map temp = new HashMap<>(); temp.putAll(property); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index a5959cc5f9..69cc4e887b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -67,6 +67,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { boolean isStatusProvided = false; Date since = request.getSince(); boolean isSinceProvided = false; + String serial = request.getSerialNumber(); + boolean isSerialProvided = false; + try { Connection conn = getConnection(); @@ -88,9 +91,14 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t "; + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + + "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID AND i.KEY_FIELD='serial' "; - sql = sql + " WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + if (serial != null) { + sql = sql + "AND i.VALUE_FIELD = ? "; + isSerialProvided = true; + } + sql = sql + "AND d.TENANT_ID = ?"; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; @@ -128,6 +136,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; + if (isSerialProvided) { + stmt.setString(paramIdx++, serial); + } stmt.setInt(paramIdx++, tenantId); if (isSinceProvided) { stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index 109fac5507..301267e52e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -68,6 +68,8 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { boolean isStatusProvided = false; Date since = request.getSince(); boolean isSinceProvided = false; + String serial = request.getSerialNumber(); + boolean isSerialProvided = false; try { conn = getConnection(); @@ -89,8 +91,14 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + + "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID AND i.KEY_FIELD='serial' "; + + if (serial != null) { + sql = sql + "AND i.VALUE_FIELD = ? "; + isSerialProvided = true; + } + sql = sql + "AND d.TENANT_ID = ?"; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; @@ -128,6 +136,9 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; + if (isSerialProvided) { + stmt.setString(paramIdx++, serial); + } stmt.setInt(paramIdx++, tenantId); if (isSinceProvided) { stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java index fdf81826ad..f170d3cb71 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java @@ -67,6 +67,8 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { boolean isStatusProvided = false; Date since = request.getSince(); boolean isSinceProvided = false; + String serial = request.getSerialNumber(); + boolean isSerialProvided = false; try { conn = getConnection(); @@ -88,10 +90,14 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t " + - "WHERE DEVICE_TYPE_ID = t.ID " + - "AND d.TENANT_ID = ?"; + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + + "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID AND i.KEY_FIELD='serial' "; + + if (serial != null) { + sql = sql + "AND i.VALUE_FIELD = ? "; + isSerialProvided = true; + } + sql = sql + "AND d.TENANT_ID = ?"; //Add the query for device-type if (deviceType != null && !deviceType.isEmpty()) { sql = sql + " AND t.NAME = ?"; @@ -124,6 +130,9 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; + if (isSerialProvided) { + stmt.setString(paramIdx++, serial); + } stmt.setInt(paramIdx++, tenantId); if (isDeviceTypeProvided) { stmt.setString(paramIdx++, deviceType); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 673d63304d..7380d93a85 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -69,6 +69,8 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { boolean isStatusProvided = false; Date since = request.getSince(); boolean isSinceProvided = false; + String serial = request.getSerialNumber(); + boolean isSerialProvided = false; try { conn = getConnection(); @@ -90,7 +92,14 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + + "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID AND i.KEY_FIELD='serial' "; + + if (serial != null) { + sql = sql + "AND i.VALUE_FIELD = ? "; + isSerialProvided = true; + } + sql = sql + "AND d.TENANT_ID = ?"; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; @@ -128,6 +137,9 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIdx = 1; + if (isSerialProvided) { + stmt.setString(paramIdx++, serial); + } stmt.setInt(paramIdx++, tenantId); if (isSinceProvided) { stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); From 3b09a43d854b003cfe1440abb8d1ffb72a50c3da Mon Sep 17 00:00:00 2001 From: MalshaPiumini Date: Wed, 5 May 2021 14:37:06 +0530 Subject: [PATCH 8/9] Solved test case failure. (cherry picked from commit 60b59f455d3ba8beb2d37dd4caef2ce402f7b65d) --- .../core/dao/impl/device/GenericDeviceDAOImpl.java | 11 ++++++----- .../mgt/core/dao/impl/device/OracleDeviceDAOImpl.java | 11 ++++++----- .../core/dao/impl/device/PostgreSQLDeviceDAOImpl.java | 11 ++++++----- .../core/dao/impl/device/SQLServerDeviceDAOImpl.java | 11 ++++++----- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index 69cc4e887b..22f06c3471 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -90,15 +90,16 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.DESCRIPTION, " + "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + - "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID AND i.KEY_FIELD='serial' "; + "t.NAME AS DEVICE_TYPE "; if (serial != null) { - sql = sql + "AND i.VALUE_FIELD = ? "; + sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + + "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID " + + "AND i.KEY_FIELD='serial' AND i.VALUE_FIELD = ? AND d.TENANT_ID = ? "; isSerialProvided = true; + } else { + sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; } - sql = sql + "AND d.TENANT_ID = ?"; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index 301267e52e..044a681c9d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -90,15 +90,16 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.DESCRIPTION, " + "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + - "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID AND i.KEY_FIELD='serial' "; + "t.NAME AS DEVICE_TYPE "; if (serial != null) { - sql = sql + "AND i.VALUE_FIELD = ? "; + sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + + "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID " + + "AND i.KEY_FIELD='serial' AND i.VALUE_FIELD = ? AND d.TENANT_ID = ? "; isSerialProvided = true; + } else { + sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; } - sql = sql + "AND d.TENANT_ID = ?"; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java index f170d3cb71..5bcbbea8bc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java @@ -89,15 +89,16 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.DESCRIPTION, " + "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + - "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID AND i.KEY_FIELD='serial' "; + "t.NAME AS DEVICE_TYPE "; if (serial != null) { - sql = sql + "AND i.VALUE_FIELD = ? "; + sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + + "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID " + + "AND i.KEY_FIELD='serial' AND i.VALUE_FIELD = ? AND d.TENANT_ID = ? "; isSerialProvided = true; + } else { + sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; } - sql = sql + "AND d.TENANT_ID = ?"; //Add the query for device-type if (deviceType != null && !deviceType.isEmpty()) { sql = sql + " AND t.NAME = ?"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 7380d93a85..95a69d3642 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -91,15 +91,16 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.DESCRIPTION, " + "d.NAME, " + "d.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE " + - "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + - "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID AND i.KEY_FIELD='serial' "; + "t.NAME AS DEVICE_TYPE "; if (serial != null) { - sql = sql + "AND i.VALUE_FIELD = ? "; + sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + + "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID " + + "AND i.KEY_FIELD='serial' AND i.VALUE_FIELD = ? AND d.TENANT_ID = ? "; isSerialProvided = true; + } else { + sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; } - sql = sql + "AND d.TENANT_ID = ?"; //Add query for last updated timestamp if (since != null) { sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; From fd6947fffc94ea9b806680f25582a0a85ad48f42 Mon Sep 17 00:00:00 2001 From: MalshaPiumini Date: Thu, 6 May 2021 14:12:03 +0530 Subject: [PATCH 9/9] Formatting the SQL query. (cherry picked from commit 6f325862ec927ee3772fc811da401b205d07c2c3) --- .../mgt/jaxrs/service/api/DeviceManagementService.java | 2 +- .../jaxrs/service/impl/DeviceManagementServiceImpl.java | 2 +- .../mgt/core/dao/impl/device/GenericDeviceDAOImpl.java | 7 +++++-- .../mgt/core/dao/impl/device/OracleDeviceDAOImpl.java | 7 +++++-- .../mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java | 7 +++++-- .../mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java | 7 +++++-- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 8268fbaac6..01145b05ec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -271,7 +271,7 @@ public interface DeviceManagementService { String ownership, @ApiParam( name = "serialNumber", - value = "The serialNumber of the device.", + value = "The serial number of the device.", required = false) @QueryParam("serialNumber") String serialNumber, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 1a151bddf9..7648a102b8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -206,7 +206,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { RequestValidationUtil.validateOwnershipType(ownership); request.setOwnership(ownership); } - if (serialNumber != null && !serialNumber.isEmpty()) { + if (StringUtils.isNotBlank(serialNumber)) { request.setSerialNumber(serialNumber); } if (status != null && !status.isEmpty()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index 22f06c3471..eb3fa87f54 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -94,8 +94,11 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { if (serial != null) { sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + - "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID " + - "AND i.KEY_FIELD='serial' AND i.VALUE_FIELD = ? AND d.TENANT_ID = ? "; + "WHERE DEVICE_TYPE_ID = t.ID " + + "AND d.ID= i.DEVICE_ID " + + "AND i.KEY_FIELD = 'serial' " + + "AND i.VALUE_FIELD = ? " + + "AND d.TENANT_ID = ? "; isSerialProvided = true; } else { sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index 044a681c9d..4393a4546e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -94,8 +94,11 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { if (serial != null) { sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + - "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID " + - "AND i.KEY_FIELD='serial' AND i.VALUE_FIELD = ? AND d.TENANT_ID = ? "; + "WHERE DEVICE_TYPE_ID = t.ID " + + "AND d.ID= i.DEVICE_ID " + + "AND i.KEY_FIELD = 'serial' " + + "AND i.VALUE_FIELD = ? " + + "AND d.TENANT_ID = ? "; isSerialProvided = true; } else { sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java index 5bcbbea8bc..66313603cb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java @@ -93,8 +93,11 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { if (serial != null) { sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + - "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID " + - "AND i.KEY_FIELD='serial' AND i.VALUE_FIELD = ? AND d.TENANT_ID = ? "; + "WHERE DEVICE_TYPE_ID = t.ID " + + "AND d.ID= i.DEVICE_ID " + + "AND i.KEY_FIELD = 'serial' " + + "AND i.VALUE_FIELD = ? " + + "AND d.TENANT_ID = ? "; isSerialProvided = true; } else { sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 95a69d3642..4e87d8ab61 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -95,8 +95,11 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { if (serial != null) { sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_INFO i " + - "WHERE DEVICE_TYPE_ID = t.ID AND d.ID= i.DEVICE_ID " + - "AND i.KEY_FIELD='serial' AND i.VALUE_FIELD = ? AND d.TENANT_ID = ? "; + "WHERE DEVICE_TYPE_ID = t.ID " + + "AND d.ID= i.DEVICE_ID " + + "AND i.KEY_FIELD = 'serial' " + + "AND i.VALUE_FIELD = ? " + + "AND d.TENANT_ID = ? "; isSerialProvided = true; } else { sql = sql + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? ";