Merge branch 'device-type-pagination-1' into 'master'

Introduce device type filtering and limit results in device types retrieve endpoint

See merge request entgra/carbon-device-mgt!430
feature/appm-store/pbac
Dharmakeerthi Lasantha 5 years ago
commit 51ca3c56f7

@ -167,7 +167,27 @@ public interface DeviceTypeManagementService {
"Example: Mon, 05 Jan 2014 15:10:00 +0200" "Example: Mon, 05 Jan 2014 15:10:00 +0200"
) )
@HeaderParam("If-Modified-Since") @HeaderParam("If-Modified-Since")
String ifModifiedSince); String ifModifiedSince,
@ApiParam(
name = "offset",
value = "The starting pagination index for the complete list of qualified items.",
required = false)
@QueryParam("offset")
int offset,
@ApiParam(
name = "limit",
value = "Provide how many device details you require from the starting " +
"pagination index/offset.",
required = false)
@QueryParam("limit")
int limit,
@ApiParam(
name = "filter",
value = "Provide criteria for filter device type name",
required = false)
@QueryParam("filter")
String filter
);
@GET @GET
@Path("/{type}") @Path("/{type}")

@ -38,6 +38,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException;
import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.common.Feature;
@ -49,6 +50,8 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceTypeManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceTypeManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
@ -68,9 +71,26 @@ public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementServ
@GET @GET
@Override @Override
public Response getDeviceTypes(@HeaderParam("If-Modified-Since") String ifModifiedSince) { public Response getDeviceTypes(@HeaderParam("If-Modified-Since") String ifModifiedSince,
@QueryParam("offset") int offset,
@QueryParam("limit") int limit,
@QueryParam("filter") String filter) {
try { try {
List<DeviceType> deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceTypes(); RequestValidationUtil.validatePaginationParameters(offset, limit);
List<DeviceType> deviceTypes;
if (offset == 0 && limit == 0 && StringUtils.isEmpty(filter)) {
deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService()
.getDeviceTypes();
} else {
PaginationRequest paginationRequest = new PaginationRequest(offset, limit);
if (!StringUtils.isEmpty(filter)) {
paginationRequest.setFilter(filter);
}
deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService()
.getDeviceTypes(paginationRequest);
}
List<DeviceType> filteredDeviceTypes = new ArrayList<>(); List<DeviceType> filteredDeviceTypes = new ArrayList<>();
for (DeviceType deviceType : deviceTypes) { for (DeviceType deviceType : deviceTypes) {
filteredDeviceTypes.add(clearMetaEntryInfo(deviceType)); filteredDeviceTypes.add(clearMetaEntryInfo(deviceType));
@ -80,7 +100,12 @@ public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementServ
String msg = "Error occurred at server side while fetching device type."; String msg = "Error occurred at server side while fetching device type.";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity(msg).build(); return Response.serverError().entity(msg).build();
} catch (InputValidationException e) {
String msg = "Invalid pagination parameters";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} }
} }
@Override @Override

@ -78,7 +78,8 @@ public class DeviceTypeManagementServiceTest {
public void testExistingDeviceType() throws Exception { public void testExistingDeviceType() throws Exception {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
Response response = this.deviceTypeManagementService.getDeviceTypes(""); Response response = this.deviceTypeManagementService
.getDeviceTypes("", 0, 0, null);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"The response states should be 200."); "The response states should be 200.");
@ -89,7 +90,8 @@ public class DeviceTypeManagementServiceTest {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
Mockito.when(this.deviceManagementProviderService.getDeviceTypes()).thenThrow(new DeviceManagementException()); Mockito.when(this.deviceManagementProviderService.getDeviceTypes()).thenThrow(new DeviceManagementException());
Response response = this.deviceTypeManagementService.getDeviceTypes(MODIFIED_SINCE); Response response = this.deviceTypeManagementService
.getDeviceTypes(MODIFIED_SINCE, 0, 0, null);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"The response status should be 500."); "The response status should be 500.");
@ -102,7 +104,8 @@ public class DeviceTypeManagementServiceTest {
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
Mockito.when(this.deviceManagementProviderService.getDeviceTypes()).thenThrow(new Mockito.when(this.deviceManagementProviderService.getDeviceTypes()).thenThrow(new
DeviceManagementException()); DeviceManagementException());
Response response = this.deviceTypeManagementService.getDeviceTypes(MODIFIED_SINCE); Response response = this.deviceTypeManagementService
.getDeviceTypes(MODIFIED_SINCE, 0, 0, null);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"The response status should be 500."); "The response status should be 500.");
@ -152,7 +155,8 @@ public class DeviceTypeManagementServiceTest {
public void testGetDeviceTypes() throws Exception { public void testGetDeviceTypes() throws Exception {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
Response response = this.deviceTypeManagementService.getDeviceTypes(MODIFIED_SINCE); Response response = this.deviceTypeManagementService
.getDeviceTypes(MODIFIED_SINCE, 0, 0, null);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"The response status should be 200."); "The response status should be 200.");
@ -164,7 +168,8 @@ public class DeviceTypeManagementServiceTest {
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
List<DeviceType> deviceTypes = DeviceMgtAPITestHelper.getDummyDeviceTypeList(5); List<DeviceType> deviceTypes = DeviceMgtAPITestHelper.getDummyDeviceTypeList(5);
Mockito.when(this.deviceManagementProviderService.getDeviceTypes()).thenReturn(deviceTypes); Mockito.when(this.deviceManagementProviderService.getDeviceTypes()).thenReturn(deviceTypes);
Response response = this.deviceTypeManagementService.getDeviceTypes(MODIFIED_SINCE); Response response = this.deviceTypeManagementService
.getDeviceTypes(MODIFIED_SINCE, 0, 0, null);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(),
"The response state should be 200"); "The response state should be 200");

@ -40,6 +40,7 @@ public class PaginationRequest {
private String ownerRole; private String ownerRole;
private Map<String, Object> property = new HashMap<>(); private Map<String, Object> property = new HashMap<>();
private Date since; private Date since;
private String filter;
public PaginationRequest(int start, int rowCount) { public PaginationRequest(int start, int rowCount) {
this.startIndex = start; this.startIndex = start;
@ -152,6 +153,14 @@ public class PaginationRequest {
return temp; return temp;
} }
public String getFilter() {
return filter;
}
public void setFilter(String filter) {
this.filter = filter;
}
@Override @Override
public String toString() { public String toString() {
return "Device type '" + this.deviceType + "' Device Name '" + this.deviceName + "' row count: " + this.rowCount return "Device type '" + this.deviceType + "' Device Name '" + this.deviceName + "' row count: " + this.rowCount

@ -34,6 +34,7 @@
*/ */
package org.wso2.carbon.device.mgt.core.dao; package org.wso2.carbon.device.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion;
@ -154,4 +155,13 @@ public interface DeviceTypeDAO {
* @throws DeviceManagementDAOException Might occur while executing database queries * @throws DeviceManagementDAOException Might occur while executing database queries
*/ */
void deleteDeviceType(int tenantId, int deviceTypeId) throws DeviceManagementDAOException; void deleteDeviceType(int tenantId, int deviceTypeId) throws DeviceManagementDAOException;
/**
*
* @param tenantId current tenant's ID
* @param paginationRequest request object for filter data. Using only offset limit and filter
* @return List of filtered device types
*/
List<DeviceType> getDeviceTypes(int tenantId, PaginationRequest paginationRequest)
throws DeviceManagementDAOException;
} }

@ -35,6 +35,10 @@
package org.wso2.carbon.device.mgt.core.dao.impl; package org.wso2.carbon.device.mgt.core.dao.impl;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition; import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -42,6 +46,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
@ -60,6 +65,8 @@ import java.util.List;
public class DeviceTypeDAOImpl implements DeviceTypeDAO { public class DeviceTypeDAOImpl implements DeviceTypeDAO {
private static Log log = LogFactory.getLog(DeviceTypeDAOImpl.class);
@Override @Override
public void addDeviceType(DeviceType deviceType, int providerTenantId, boolean isSharedWithAllTenants) public void addDeviceType(DeviceType deviceType, int providerTenantId, boolean isSharedWithAllTenants)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
@ -308,6 +315,64 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
} }
} }
@Override
public List<DeviceType> getDeviceTypes(int tenantId, PaginationRequest paginationRequest) throws
DeviceManagementDAOException {
List<DeviceType> deviceTypes = new ArrayList<>();
boolean isFilterProvided = false;
boolean isPaginationRangeProvided = false;
try {
Connection conn = this.getConnection();
String sql = "SELECT ID AS DEVICE_TYPE_ID, " +
"NAME AS DEVICE_TYPE_NAME, " +
"DEVICE_TYPE_META " +
"FROM DM_DEVICE_TYPE " +
"WHERE (PROVIDER_TENANT_ID =? OR " +
"SHARED_WITH_ALL_TENANTS = ?) ";
if (!StringUtils.isEmpty(paginationRequest.getFilter())) {
sql += "AND NAME LIKE ? ";
isFilterProvided = true;
}
if (paginationRequest.getStartIndex() >= 0 && paginationRequest.getRowCount() > 0) {
sql += "LIMIT ?, ?";
isPaginationRangeProvided = true;
}
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
int paramIndex = 1;
stmt.setInt(paramIndex++, tenantId);
stmt.setBoolean(paramIndex++, true);
if (isFilterProvided) {
stmt.setString(paramIndex++, paginationRequest.getFilter());
}
if (isPaginationRangeProvided) {
stmt.setInt(paramIndex++, paginationRequest.getStartIndex());
stmt.setInt(paramIndex, paginationRequest.getRowCount());
}
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceType deviceType = new DeviceType();
deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
deviceType.setName(rs.getString("DEVICE_TYPE_NAME"));
String devicetypeMeta = rs.getString("DEVICE_TYPE_META");
if (devicetypeMeta != null && devicetypeMeta.length() > 0) {
Gson gson = new Gson();
deviceType.setDeviceTypeMetaDefinition
(gson.fromJson(devicetypeMeta, DeviceTypeMetaDefinition.class));
}
deviceTypes.add(deviceType);
}
}
}
return deviceTypes;
} catch (SQLException e) {
String msg = "Error occurred while fetching device types";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override @Override
public void removeDeviceType(String type, int tenantId) throws DeviceManagementDAOException { public void removeDeviceType(String type, int tenantId) throws DeviceManagementDAOException {
@ -452,5 +517,4 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection(); return DeviceManagementDAOFactory.getConnection();
} }
} }

@ -722,12 +722,22 @@ public interface DeviceManagementProviderService {
*/ */
DeviceType getDeviceType(String deviceType) throws DeviceManagementException; DeviceType getDeviceType(String deviceType) throws DeviceManagementException;
/** /**
* This retrieves the device type info for the given type * This method will return device type list without filtering or limiting
* @throws DeviceManagementException * @return DeviceTypeList
* @throws DeviceManagementException will be thrown when Error in Backend occurs
*/ */
List<DeviceType> getDeviceTypes() throws DeviceManagementException; List<DeviceType> getDeviceTypes() throws DeviceManagementException;
/**
* This method will return device type list using limit and filtering provided
* @param paginationRequest request parameters to filter devices
* @return filterd device types
* @throws DeviceManagementException
*/
List<DeviceType> getDeviceTypes(PaginationRequest paginationRequest) throws DeviceManagementException;
/** /**
* This retrieves the device location histories * This retrieves the device location histories
* *

@ -2918,6 +2918,26 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
} }
@Override
public List<DeviceType> getDeviceTypes(PaginationRequest paginationRequest)
throws DeviceManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
DeviceManagementDAOFactory.openConnection();
return deviceTypeDAO.getDeviceTypes(tenantId, paginationRequest);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while obtaining the device types for tenant " + tenantId;
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Override @Override
public List<DeviceLocationHistory> getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, long to) public List<DeviceLocationHistory> getDeviceLocationInfo(DeviceIdentifier deviceIdentifier, long from, long to)
throws DeviceManagementException { throws DeviceManagementException {

Loading…
Cancel
Save