added role query param to getdevices restapis

4.x.x
lakshani 8 years ago
parent c17bead1fc
commit 26f2e2d2ae

@ -205,6 +205,13 @@ public interface DeviceManagementService {
required = false) required = false)
@QueryParam("user") @QueryParam("user")
String user, String user,
@ApiParam(
name = "role",
value = "A role of device owners. Ex : store-admin",
required = false)
@QueryParam("role")
@Size(max = 45)
String role,
@ApiParam( @ApiParam(
name = "ownership", name = "ownership",
allowableValues = "BYOD, COPE", allowableValues = "BYOD, COPE",

@ -18,6 +18,7 @@
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.impl; package org.wso2.carbon.device.mgt.jaxrs.service.impl;
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.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
@ -84,6 +85,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("name") String name, @QueryParam("name") String name,
@QueryParam("type") String type, @QueryParam("type") String type,
@QueryParam("user") String user, @QueryParam("user") String user,
@QueryParam("role") String role,
@QueryParam("ownership") String ownership, @QueryParam("ownership") String ownership,
@QueryParam("status") String status, @QueryParam("status") String status,
@QueryParam("groupId") int groupId, @QueryParam("groupId") int groupId,
@ -92,6 +94,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("offset") int offset, @QueryParam("offset") int offset,
@QueryParam("limit") int limit) { @QueryParam("limit") int limit) {
try { try {
if (!StringUtils.isEmpty(name) && !StringUtils.isEmpty(role)) {
return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("Request contains both name and role " +
"parameters. Only one is allowed " +
"at once.").build()).build();
}
// RequestValidationUtil.validateSelectionCriteria(type, user, roleName, ownership, status); // RequestValidationUtil.validateSelectionCriteria(type, user, roleName, ownership, status);
RequestValidationUtil.validatePaginationParameters(offset, limit); RequestValidationUtil.validatePaginationParameters(offset, limit);
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
@ -126,6 +134,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
if (groupId != 0 ) { if (groupId != 0 ) {
request.setGroupId(groupId); request.setGroupId(groupId);
} }
if (role != null && !role.isEmpty()) {
request.setOwnerRole(role);
}
// this is the user who initiates the request // this is the user who initiates the request
String authorizedUser = MultitenantUtils.getTenantAwareUsername(CarbonContext.getThreadLocalCarbonContext().getUsername()); String authorizedUser = MultitenantUtils.getTenantAwareUsername(CarbonContext.getThreadLocalCarbonContext().getUsername());

@ -65,9 +65,10 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem
if (applicationWrapper.getDeviceIdentifiers() != null) { if (applicationWrapper.getDeviceIdentifiers() != null) {
for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) { for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) {
if (Platform.ANDROID.toString().equals(deviceIdentifier.getType())) { String deviceType = deviceIdentifier.getType().toUpperCase();
if (Platform.ANDROID.toString().equals(deviceType)) {
operation = MDMAndroidOperationUtil.createInstallAppOperation(mobileApp); operation = MDMAndroidOperationUtil.createInstallAppOperation(mobileApp);
} else if (Platform.IOS.toString().equals(deviceIdentifier.getType())) { } else if (Platform.IOS.toString().equals(deviceType)) {
operation = MDMIOSOperationUtil.createInstallAppOperation(mobileApp); operation = MDMIOSOperationUtil.createInstallAppOperation(mobileApp);
} }
} }
@ -114,9 +115,10 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem
if (applicationWrapper.getDeviceIdentifiers() != null) { if (applicationWrapper.getDeviceIdentifiers() != null) {
for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) { for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) {
if (Platform.ANDROID.toString().equals(deviceIdentifier.getType())) { String deviceType = deviceIdentifier.getType().toUpperCase();
if (Platform.ANDROID.toString().equals(deviceType)) {
operation = MDMAndroidOperationUtil.createAppUninstallOperation(mobileApp); operation = MDMAndroidOperationUtil.createAppUninstallOperation(mobileApp);
} else if (deviceIdentifier.getType().equals(Platform.IOS.toString())) { } else if (deviceType.equals(Platform.IOS.toString())) {
operation = MDMIOSOperationUtil.createAppUninstallOperation(mobileApp); operation = MDMIOSOperationUtil.createAppUninstallOperation(mobileApp);
} }
} }

@ -33,6 +33,7 @@ public class PaginationRequest {
private String deviceType; private String deviceType;
private String deviceName; private String deviceName;
private String ownership; private String ownership;
private String ownerRole;
private Date since; private Date since;
public PaginationRequest(int start, int rowCount) { public PaginationRequest(int start, int rowCount) {
@ -112,4 +113,11 @@ public class PaginationRequest {
this.since = since; this.since = since;
} }
public String getOwnerRole() {
return ownerRole;
}
public void setOwnerRole(String ownerRole) {
this.ownerRole = ownerRole;
}
} }

@ -17,6 +17,7 @@
*/ */
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
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.CarbonConstants; import org.wso2.carbon.CarbonConstants;
@ -607,16 +608,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public PaginationResult getAllDevices(PaginationRequest request) throws DeviceManagementException { public PaginationResult getAllDevices(PaginationRequest request) throws DeviceManagementException {
List<Device> devicesForRoles = null;
PaginationResult paginationResult = new PaginationResult(); PaginationResult paginationResult = new PaginationResult();
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<Device> allDevices = new ArrayList<>(); List<Device> allDevices = new ArrayList<>();
int count = 0; int count = 0;
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
request = DeviceManagerUtil.validateDeviceListPageSize(request); request = DeviceManagerUtil.validateDeviceListPageSize(request);
if (!StringUtils.isEmpty(request.getOwnerRole())) {
devicesForRoles = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.getAllDevicesOfRole(request.getOwnerRole());
}
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevices(request, tenantId); allDevices = deviceDAO.getDevices(request, tenantId);
count = deviceDAO.getDeviceCount(request, tenantId); count = deviceDAO.getDeviceCount(request, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
"the current tenant", e); "the current tenant", e);
@ -625,6 +632,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
devices = processDevices(devices, allDevices);
if (devicesForRoles != null) {
count += devicesForRoles.size();
devices = processDevices(devices, devicesForRoles);
}
paginationResult.setData(devices);
paginationResult.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count);
return paginationResult;
}
private List<Device> processDevices(List<Device> devices, List<Device> allDevices) throws DeviceManagementException {
for (Device device : allDevices) { for (Device device : allDevices) {
DeviceInfo info = null; DeviceInfo info = null;
try { try {
@ -637,7 +659,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
device.setDeviceInfo(info); device.setDeviceInfo(info);
} catch (DeviceDetailsMgtDAOException e) { } catch (DeviceDetailsMgtDAOException e) {
log.error("Error occurred while retrieving advance info of '" + device.getType() + log.error("Error occurred while retrieving advance info of '" + device.getType() +
"' that carries the id '" + device.getDeviceIdentifier() + "'"); "' that carries the id '" + device.getDeviceIdentifier() + "'");
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error occurred while opening a connection to the data source", e); log.error("Error occurred while opening a connection to the data source", e);
} finally { } finally {
@ -651,7 +673,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
device.setApplications(applications); device.setApplications(applications);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
log.error("Error occurred while retrieving the application list of '" + device.getType() + "', " + log.error("Error occurred while retrieving the application list of '" + device.getType() + "', " +
"which carries the id '" + device.getId() + "'", e); "which carries the id '" + device.getId() + "'", e);
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error occurred while opening a connection to the data source", e); log.error("Error occurred while opening a connection to the data source", e);
} finally { } finally {
@ -662,7 +684,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (deviceManager == null) { if (deviceManager == null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " +
"Therefore, not attempting method 'isEnrolled'"); "Therefore, not attempting method 'isEnrolled'");
} }
devices.add(device); devices.add(device);
continue; continue;
@ -675,12 +697,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
devices.add(device); devices.add(device);
} }
paginationResult.setData(devices); return devices;
paginationResult.setRecordsFiltered(count);
paginationResult.setRecordsTotal(count);
return paginationResult;
} }
@Override @Override
public List<Device> getAllDevices(String deviceType) throws DeviceManagementException { public List<Device> getAllDevices(String deviceType) throws DeviceManagementException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();

Loading…
Cancel
Save