Merge branch 'application-mgt-new' of https://gitlab.com/entgra/carbon-device-mgt into application-mgt-new

feature/appm-store/pbac
Jayasanka 5 years ago
commit d75724563a

@ -1,4 +1,4 @@
package org.wso2.carbon.device.application.mgt.common;/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. /* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
* *
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License, * Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
@ -15,6 +15,8 @@ package org.wso2.carbon.device.application.mgt.common;/* Copyright (c) 2019, Ent
* under the License. * under the License.
*/ */
package org.wso2.carbon.device.application.mgt.common;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import java.sql.Timestamp; import java.sql.Timestamp;

@ -116,9 +116,9 @@ public interface SubscriptionManager {
* @param limitValue limit * @param limitValue limit
* @param appUUID application release UUID * @param appUUID application release UUID
* @return {@link PaginationResult} * @return {@link PaginationResult}
* @throws ApplicationManagementException if values are incorrect for offset and limit or couldn't find an * @throws ApplicationManagementException if offset or limit contains incorrect values, if it couldn't find an
* application release for given UUID or error occurred while getting device details of subscribed device ids or * application release for given UUID, if an error occurred while getting device details of subscribed device ids,
* getting subscription details of given application release UUID. * if an error occurred while getting subscription details of given application release UUID.
*/ */
PaginationResult getAppSubscriptionDetails(int offsetValue, int limitValue, String appUUID) PaginationResult getAppSubscriptionDetails(int offsetValue, int limitValue, String appUUID)
throws ApplicationManagementException; throws ApplicationManagementException;

@ -316,6 +316,15 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
} }
} }
/**
* THis method is responsible to validate application install or uninstall request.
*
* @param params params could be either list of {@link DeviceIdentifier} or list of username or list of group
* names or list or role names.
* @param subType Subscription type. i.e DEVICE or USER or ROLE or GROUP
* @param action performing action. i.e Install or Uninstall
* @throws BadRequestException if incompatible data is found with app install/uninstall request.
*/
private <T> void validateRequest(List<T> params, String subType, String action) throws BadRequestException { private <T> void validateRequest(List<T> params, String subType, String action) throws BadRequestException {
if (params.isEmpty()) { if (params.isEmpty()) {
String msg = "In order to install application release, you should provide list of subscribers. " String msg = "In order to install application release, you should provide list of subscribers. "
@ -339,7 +348,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
} }
} }
/*** /**
* This method perform given action (i.e APP INSTALL or APP UNINSTALL) on given set of devices. * This method perform given action (i.e APP INSTALL or APP UNINSTALL) on given set of devices.
* *
* @param deviceType Application supported device type. * @param deviceType Application supported device type.
@ -425,7 +434,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
return applicationInstallResponse; return applicationInstallResponse;
} }
/*** /**
* Filter given devices and davide given list of device into two sets, those are already application installed * Filter given devices and davide given list of device into two sets, those are already application installed
* devices and application installable devices. * devices and application installable devices.
* *
@ -470,6 +479,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
return subscribingDeviceIdHolder; return subscribingDeviceIdHolder;
} }
/**
* This method returns the application categories of a particular application
*
* @param id Application Id
* @return List of application categories.
* @throws ApplicationManagementException if error occurred while getting application categories from the DB.
*/
private List<String> getApplicationCategories(int id) throws ApplicationManagementException { private List<String> getApplicationCategories(int id) throws ApplicationManagementException {
List<String> categories; List<String> categories;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -529,6 +545,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
} }
} }
/**
* This method is responsible to update subscription data.
*
* @param applicationReleaseId Application release Id
* @param activities List of {@link Activity}
* @param subscribingDeviceIdHolder Subscribing device id holder.
* @param params subscribers. If subscription is performed via user, group or role, params is a list of
* {@link String}
* @param subType Subscription type. i.e USER, GROUP, ROLE or DEVICE
* @param action performing action. ie INSTALL or UNINSTALL>
* @throws ApplicationManagementException if error occurred while getting or updating subscription data.
*/
private void updateSubscriptions(int applicationReleaseId, List<Activity> activities, private void updateSubscriptions(int applicationReleaseId, List<Activity> activities,
SubscribingDeviceIdHolder subscribingDeviceIdHolder, List<String> params, String subType, SubscribingDeviceIdHolder subscribingDeviceIdHolder, List<String> params, String subType,
String action) throws ApplicationManagementException { String action) throws ApplicationManagementException {
@ -686,6 +714,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
} }
} }
/**
* This method constructs operation payload to install/uninstall an application.
*
* @param deviceType Device type
* @param application {@link Application} data.
* @param action Action is either ININSTALL or UNINSTALL
* @return {@link Operation}
* @throws ApplicationManagementException if unknown application type is found to generate operation payload or
* invalid action is found to generate operation payload.
*/
private Operation generateOperationPayloadByDeviceType(String deviceType, Application application, String action) private Operation generateOperationPayloadByDeviceType(String deviceType, Application application, String action)
throws ApplicationManagementException { throws ApplicationManagementException {
try { try {
@ -941,7 +979,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(appUUID, tenantId); ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(appUUID, tenantId);
if (applicationDTO == null) { if (applicationDTO == null) {
String msg = "Couldn't found an application with application release which has UUID " + appUUID; String msg = "Couldn't find an application with application release which has UUID " + appUUID;
log.error(msg); log.error(msg);
throw new NotFoundException(msg); throw new NotFoundException(msg);
} }

@ -67,8 +67,8 @@ import java.util.List;
@org.wso2.carbon.apimgt.annotations.api.Scope( @org.wso2.carbon.apimgt.annotations.api.Scope(
name = "View Application Subscriptions", name = "View Application Subscriptions",
description = "View Application Subscriptions.", description = "View Application Subscriptions.",
key = "perm:app:subscription:view", key = "perm:admin:app:subscription:view",
permissions = {"/app-mgt/store/subscription/view"} permissions = {"/app-mgt/store/admin/subscription/view"}
) )
} }
) )

@ -18,45 +18,24 @@
package org.wso2.carbon.device.application.mgt.store.api.services.impl.admin; package org.wso2.carbon.device.application.mgt.store.api.services.impl.admin;
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.application.mgt.common.ApplicationInstallResponse;
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.DeviceGroupList;
import org.wso2.carbon.device.application.mgt.common.DeviceList;
import org.wso2.carbon.device.application.mgt.common.ErrorResponse;
import org.wso2.carbon.device.application.mgt.common.RoleList;
import org.wso2.carbon.device.application.mgt.common.SubAction;
import org.wso2.carbon.device.application.mgt.common.SubscriptionType;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; 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.common.services.SubscriptionManager;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationOperationTaskException;
import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException;
import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.task.ScheduledAppSubscriptionTaskManager;
import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
import org.wso2.carbon.device.application.mgt.store.api.services.SubscriptionManagementAPI;
import org.wso2.carbon.device.application.mgt.store.api.services.admin.SubscriptionManagementAdminAPI; import org.wso2.carbon.device.application.mgt.store.api.services.admin.SubscriptionManagementAdminAPI;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import javax.validation.Valid;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/** /**
* Implementation of Subscription Management related APIs. * Implementation of Subscription Management related APIs.

@ -142,7 +142,13 @@ public interface GroupManagementAdminService {
value = "Provide how many device details you require from the starting pagination index/offset.", value = "Provide how many device details you require from the starting pagination index/offset.",
defaultValue = "5") defaultValue = "5")
@QueryParam("limit") @QueryParam("limit")
int limit); int limit,
@ApiParam(
name = "status",
value = "status of group to be retrieve.")
@QueryParam("status")
String status);
@Path("/count") @Path("/count")
@GET @GET
@ -190,6 +196,10 @@ public interface GroupManagementAdminService {
message = "Internal Server Error. \n Server error occurred while fetching the group count.", message = "Internal Server Error. \n Server error occurred while fetching the group count.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response getGroupCount(); Response getGroupCount(@ApiParam(
name = "status",
value = "status of groups of which count should be retrieved")
@QueryParam("status")
String status);
} }

@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException;
@ -100,6 +101,7 @@ public class GroupManagementServiceImpl implements GroupManagementService {
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} }
group.setOwner(owner); group.setOwner(owner);
group.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE);
try { try {
DeviceMgtAPIUtils.getGroupManagementProviderService().createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); DeviceMgtAPIUtils.getGroupManagementProviderService().createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS);
return Response.status(Response.Status.CREATED).build(); return Response.status(Response.Status.CREATED).build();

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin;
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.apache.solr.common.StringUtils;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
@ -36,7 +37,7 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class); private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class);
@Override @Override
public Response getGroups(String name, String owner, int offset, int limit) { public Response getGroups(String name, String owner, int offset, int limit, String status) {
try { try {
RequestValidationUtil.validatePaginationParameters(offset, limit); RequestValidationUtil.validatePaginationParameters(offset, limit);
GroupPaginationRequest request = new GroupPaginationRequest(offset, limit); GroupPaginationRequest request = new GroupPaginationRequest(offset, limit);
@ -46,6 +47,9 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
if (owner != null) { if (owner != null) {
request.setOwner(owner.toUpperCase()); request.setOwner(owner.toUpperCase());
} }
if (status != null && !status.isEmpty()) {
request.setStatus(status.toUpperCase());
}
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService() PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroups(request); .getGroups(request);
DeviceGroupList deviceGroupList = new DeviceGroupList(); DeviceGroupList deviceGroupList = new DeviceGroupList();
@ -65,9 +69,14 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
} }
@Override @Override
public Response getGroupCount() { public Response getGroupCount(String status) {
try { try {
int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(); int count;
if (status == null || status.isEmpty()) {
count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount();
} else {
count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCountByStatus(status);
}
return Response.status(Response.Status.OK).entity(count).build(); return Response.status(Response.Status.OK).entity(count).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
String msg = "ErrorResponse occurred while retrieving group count."; String msg = "ErrorResponse occurred while retrieving group count.";

@ -27,12 +27,21 @@ public class GroupPaginationRequest {
private int rowCount; private int rowCount;
private String owner; private String owner;
private String groupName; private String groupName;
private String status;
public GroupPaginationRequest(int start, int rowCount) { public GroupPaginationRequest(int start, int rowCount) {
this.startIndex = start; this.startIndex = start;
this.rowCount = rowCount; this.rowCount = rowCount;
} }
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getStartIndex() { public int getStartIndex() {
return startIndex; return startIndex;
} }

@ -44,6 +44,17 @@ public class DeviceGroup implements Serializable {
private String owner; private String owner;
@ApiModelProperty(name = "status", value = "The status of group that needs updating/retrieval.")
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
private Map<String,String> groupProperties; private Map<String,String> groupProperties;
public DeviceGroup() {} public DeviceGroup() {}

@ -65,6 +65,11 @@ public class DeviceGroupConstants {
public static final String DEFAULT_VIEW_EVENTS = "view-events"; public static final String DEFAULT_VIEW_EVENTS = "view-events";
} }
public static class GroupStatus {
public static final String ACTIVE = "ACTIVE";
public static final String INACTIVE = "INACTIVE";
}
public static class Permissions { public static class Permissions {
public static final String[] DEFAULT_ADMIN_PERMISSIONS = public static final String[] DEFAULT_ADMIN_PERMISSIONS =
{"/permission/device-mgt/groups", "/permission/device-mgt/user/groups"}; {"/permission/device-mgt/groups", "/permission/device-mgt/user/groups"};

@ -158,7 +158,7 @@ public interface GroupDAO {
* @return List of all Device Groups in tenant. * @return List of all Device Groups in tenant.
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
int getGroupCount(int tenantId) throws GroupManagementDAOException; int getGroupCount(int tenantId, String status) throws GroupManagementDAOException;
/** /**
* Get paginated count of Device Groups in tenant. * Get paginated count of Device Groups in tenant.

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.dao.impl; package org.wso2.carbon.device.mgt.core.dao.impl;
import org.apache.solr.common.StringUtils;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
@ -46,14 +47,25 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs; ResultSet rs;
int groupId = -1; int groupId = -1;
boolean hasStatus = false;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) VALUES (?, ?, ?, ?)"; String sql;
if(deviceGroup.getStatus() == null || deviceGroup.getStatus().isEmpty()){
sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) VALUES (?, ?, ?, ?)";
} else {
sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, STATUS) VALUES (?, ?, ?, ?, ?)";
hasStatus = true;
}
stmt = conn.prepareStatement(sql, new String[]{"ID"}); stmt = conn.prepareStatement(sql, new String[]{"ID"});
stmt.setString(1, deviceGroup.getDescription()); stmt.setString(1, deviceGroup.getDescription());
stmt.setString(2, deviceGroup.getName()); stmt.setString(2, deviceGroup.getName());
stmt.setString(3, deviceGroup.getOwner()); stmt.setString(3, deviceGroup.getOwner());
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
if(hasStatus) {
stmt.setString(5, deviceGroup.getStatus());
}
stmt.executeUpdate(); stmt.executeUpdate();
rs = stmt.getGeneratedKeys(); rs = stmt.getGeneratedKeys();
if (rs.next()) { if (rs.next()) {
@ -128,16 +140,26 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
public void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId) public void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
boolean hasStatus = false;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = String sql =
"UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ? WHERE ID = ? AND TENANT_ID = ?"; "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ? WHERE ID = ? AND TENANT_ID = ?";
if(deviceGroup.getStatus() != null && !deviceGroup.getStatus().isEmpty()){
sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, STATUS = ? WHERE ID = ? AND TENANT_ID = ?";
hasStatus = true;
}
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceGroup.getDescription()); int paramIndex = 1;
stmt.setString(2, deviceGroup.getName()); stmt.setString(paramIndex++, deviceGroup.getDescription());
stmt.setString(3, deviceGroup.getOwner()); stmt.setString(paramIndex++, deviceGroup.getName());
stmt.setInt(4, groupId); stmt.setString(paramIndex++, deviceGroup.getOwner());
stmt.setInt(5, tenantId); if(hasStatus) {
stmt.setString(paramIndex++, deviceGroup.getStatus());
}
stmt.setInt(paramIndex++, groupId);
stmt.setInt(paramIndex++, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" + throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" +
@ -213,8 +235,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
Map<String,String> properties = new HashMap<String, String>(); Map<String,String> properties = new HashMap<String, String>();
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
stmt = conn.prepareStatement( String sql = "SELECT PROPERTY_NAME, PROPERTY_VALUE FROM GROUP_PROPERTIES WHERE GROUP_ID = ? AND TENANT_ID = ?";
"SELECT PROPERTY_NAME, PROPERTY_VALUE FROM GROUP_PROPERTIES WHERE GROUP_ID = ? AND TENANT_ID = ?"); stmt = conn.prepareStatement(sql);
stmt.setInt(1, groupId); stmt.setInt(1, groupId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
@ -304,14 +326,22 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
@Override @Override
public int getGroupCount(int tenantId) throws GroupManagementDAOException { public int getGroupCount(int tenantId, String status) throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
boolean statusAvailable = false;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP WHERE TENANT_ID = ?"; String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP WHERE TENANT_ID = ?";
if(!StringUtils.isEmpty(status)){
sql += " AND STATUS = ?";
statusAvailable = true;
}
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
if(statusAvailable){
stmt.setString(2, status);
}
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
return resultSet.getInt("GROUP_COUNT"); return resultSet.getInt("GROUP_COUNT");
@ -335,6 +365,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
boolean hasGroupName = false; boolean hasGroupName = false;
String owner = request.getOwner(); String owner = request.getOwner();
boolean hasOwner = false; boolean hasOwner = false;
boolean hasStatus = false;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
@ -347,6 +378,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
sql += " AND OWNER LIKE ?"; sql += " AND OWNER LIKE ?";
hasOwner = true; hasOwner = true;
} }
if(!StringUtils.isEmpty(request.getStatus())){
sql += " AND STATUS = ?";
hasStatus = true;
}
int paramIndex = 1; int paramIndex = 1;
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -357,6 +392,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex, owner + "%"); stmt.setString(paramIndex, owner + "%");
} }
if (hasStatus) {
stmt.setString(paramIndex, request.getStatus());
}
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
return resultSet.getInt("GROUP_COUNT"); return resultSet.getInt("GROUP_COUNT");

@ -48,7 +48,9 @@ public class GenericGroupDAOImpl extends AbstractGroupDAOImpl {
String groupName = request.getGroupName(); String groupName = request.getGroupName();
boolean hasGroupName = false; boolean hasGroupName = false;
String owner = request.getOwner(); String owner = request.getOwner();
String status = request.getStatus();
boolean hasOwner = false; boolean hasOwner = false;
boolean hasStatus = false;
boolean hasLimit = request.getRowCount() != 0; boolean hasLimit = request.getRowCount() != 0;
try { try {
@ -62,6 +64,10 @@ public class GenericGroupDAOImpl extends AbstractGroupDAOImpl {
sql += " AND UPPER(OWNER) LIKE ?"; sql += " AND UPPER(OWNER) LIKE ?";
hasOwner = true; hasOwner = true;
} }
if (status != null && !status.isEmpty()) {
sql += " AND STATUS = ?";
hasStatus = true;
}
if (hasLimit) { if (hasLimit) {
sql += " LIMIT ?, ?"; sql += " LIMIT ?, ?";
} }
@ -75,6 +81,9 @@ public class GenericGroupDAOImpl extends AbstractGroupDAOImpl {
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex++, owner + "%"); stmt.setString(paramIndex++, owner + "%");
} }
if (hasStatus) {
stmt.setString(paramIndex++, status.toUpperCase());
}
if (hasLimit) { if (hasLimit) {
stmt.setInt(paramIndex++, request.getStartIndex()); stmt.setInt(paramIndex++, request.getStartIndex());
stmt.setInt(paramIndex, request.getRowCount()); stmt.setInt(paramIndex, request.getRowCount());

@ -49,7 +49,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
String groupName = request.getGroupName(); String groupName = request.getGroupName();
boolean hasGroupName = false; boolean hasGroupName = false;
String owner = request.getOwner(); String owner = request.getOwner();
String status = request.getStatus();
boolean hasOwner = false; boolean hasOwner = false;
boolean hasStatus = false;
boolean hasLimit = request.getRowCount() != 0; boolean hasLimit = request.getRowCount() != 0;
try { try {
@ -63,6 +65,10 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
sql += " AND OWNER LIKE ?"; sql += " AND OWNER LIKE ?";
hasOwner = true; hasOwner = true;
} }
if (status != null && !status.isEmpty()) {
sql += " AND STATUS = ?";
hasStatus = true;
}
if (hasLimit) { if (hasLimit) {
sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} }
@ -76,6 +82,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl {
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex++, owner + "%"); stmt.setString(paramIndex++, owner + "%");
} }
if (hasStatus) {
stmt.setString(paramIndex++, status.toUpperCase());
}
if (hasLimit) { if (hasLimit) {
stmt.setInt(paramIndex++, request.getStartIndex()); stmt.setInt(paramIndex++, request.getStartIndex());
stmt.setInt(paramIndex, request.getRowCount()); stmt.setInt(paramIndex, request.getRowCount());

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.dao.impl.group; package org.wso2.carbon.device.mgt.core.dao.impl.group;
import org.apache.solr.common.StringUtils;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
@ -44,15 +45,26 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs; ResultSet rs;
int groupId = -1; int groupId = -1;
boolean hasStatus = false;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) " + String sql;
"VALUES (?, ?, ?, ?) RETURNING ID"; if(StringUtils.isEmpty(deviceGroup.getStatus())) {
sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) " +
"VALUES (?, ?, ?, ?) RETURNING ID";
} else {
sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, STATUS) " +
"VALUES (?, ?, ?, ?, ?) RETURNING ID";
hasStatus = true;
}
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceGroup.getDescription()); stmt.setString(1, deviceGroup.getDescription());
stmt.setString(2, deviceGroup.getName()); stmt.setString(2, deviceGroup.getName());
stmt.setString(3, deviceGroup.getOwner()); stmt.setString(3, deviceGroup.getOwner());
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
if(hasStatus) {
stmt.setString(5, deviceGroup.getStatus());
}
stmt.execute(); stmt.execute();
rs = stmt.getGeneratedKeys(); rs = stmt.getGeneratedKeys();
if (rs.next()) { if (rs.next()) {
@ -77,7 +89,9 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl {
String groupName = request.getGroupName(); String groupName = request.getGroupName();
boolean hasGroupName = false; boolean hasGroupName = false;
String owner = request.getOwner(); String owner = request.getOwner();
String status = request.getStatus();
boolean hasOwner = false; boolean hasOwner = false;
boolean hasStatus = false;
boolean hasLimit = request.getRowCount() != 0; boolean hasLimit = request.getRowCount() != 0;
try { try {
@ -91,6 +105,10 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl {
sql += " AND OWNER LIKE ?"; sql += " AND OWNER LIKE ?";
hasOwner = true; hasOwner = true;
} }
if (status != null && !status.isEmpty()) {
sql += " AND STATUS = ?";
hasStatus = true;
}
if (hasLimit) { if (hasLimit) {
sql += " LIMIT ? OFFSET ?"; sql += " LIMIT ? OFFSET ?";
} }
@ -104,6 +122,9 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl {
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex++, owner + "%"); stmt.setString(paramIndex++, owner + "%");
} }
if (hasStatus) {
stmt.setString(paramIndex++, status.toUpperCase());
}
if (hasLimit) { if (hasLimit) {
stmt.setInt(paramIndex++, request.getRowCount()); stmt.setInt(paramIndex++, request.getRowCount());
stmt.setInt(paramIndex, request.getStartIndex()); stmt.setInt(paramIndex, request.getStartIndex());

@ -50,6 +50,8 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
boolean hasGroupName = false; boolean hasGroupName = false;
String owner = request.getOwner(); String owner = request.getOwner();
boolean hasOwner = false; boolean hasOwner = false;
String status = request.getStatus();
boolean hasStatus = false;
boolean hasLimit = request.getRowCount() != 0; boolean hasLimit = request.getRowCount() != 0;
try { try {
@ -63,6 +65,10 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
sql += " AND OWNER LIKE ?"; sql += " AND OWNER LIKE ?";
hasOwner = true; hasOwner = true;
} }
if (status != null && !status.isEmpty()) {
sql += " AND STATUS = ?";
hasStatus = true;
}
if (hasLimit) { if (hasLimit) {
sql += " ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; sql += " ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} }
@ -76,6 +82,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl {
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex++, owner + "%"); stmt.setString(paramIndex++, owner + "%");
} }
if (hasStatus) {
stmt.setString(paramIndex++, status.toUpperCase());
}
if (hasLimit) { if (hasLimit) {
stmt.setInt(paramIndex++, request.getStartIndex()); stmt.setInt(paramIndex++, request.getStartIndex());
stmt.setInt(paramIndex, request.getRowCount()); stmt.setInt(paramIndex, request.getRowCount());

@ -2794,6 +2794,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
defaultGroup = new DeviceGroup(groupName); defaultGroup = new DeviceGroup(groupName);
// Setting system level user (wso2.system.user) as the owner // Setting system level user (wso2.system.user) as the owner
defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME); defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME);
defaultGroup.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE);
defaultGroup.setDescription("Default system group for devices with " + groupName + " ownership."); defaultGroup.setDescription("Default system group for devices with " + groupName + " ownership.");
try { try {
service.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE, service.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE,

@ -127,6 +127,14 @@ public interface GroupManagementProviderService {
*/ */
int getGroupCount() throws GroupManagementException; int getGroupCount() throws GroupManagementException;
/**
* Get all device group count in tenant by group status
*
* @return group count
* @throws GroupManagementException
*/
int getGroupCountByStatus(String status) throws GroupManagementException;
/** /**
* Get device group count of user * Get device group count of user
* *

@ -472,7 +472,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
return groupDAO.getGroupCount(tenantId); return groupDAO.getGroupCount(tenantId, null);
} catch (GroupManagementDAOException | SQLException e) { } catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving all groups in tenant"; String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e); log.error(msg, e);
@ -485,6 +485,29 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
} }
@Override
public int getGroupCountByStatus(String status) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get groups count by Status");
}
int tenantId = -1;
try {
tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection();
return groupDAO.getGroupCount(tenantId, status);
} catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving all groups in tenant " + tenantId
+" by status : " + status;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException { private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -882,6 +905,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
DeviceGroup defaultGroup = this.getGroup(groupName); DeviceGroup defaultGroup = this.getGroup(groupName);
if (defaultGroup == null) { if (defaultGroup == null) {
defaultGroup = new DeviceGroup(groupName); defaultGroup = new DeviceGroup(groupName);
defaultGroup.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE);
// Setting system level user (wso2.system.user) as the owner // Setting system level user (wso2.system.user) as the owner
defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME); defaultGroup.setOwner(CarbonConstants.REGISTRY_SYSTEM_USERNAME);
defaultGroup.setDescription("Default system group for devices with " + groupName + " ownership."); defaultGroup.setDescription("Default system group for devices with " + groupName + " ownership.");

@ -11,6 +11,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
CREATE TABLE IF NOT EXISTS DM_GROUP ( CREATE TABLE IF NOT EXISTS DM_GROUP (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
GROUP_NAME VARCHAR(100) DEFAULT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL,
STATUS VARCHAR(50) DEFAULT NULL,
DESCRIPTION TEXT DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL,
OWNER VARCHAR(45) DEFAULT NULL, OWNER VARCHAR(45) DEFAULT NULL,
TENANT_ID INTEGER DEFAULT 0, TENANT_ID INTEGER DEFAULT 0,

@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
GROUP_NAME VARCHAR(100) DEFAULT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL,
DESCRIPTION TEXT DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL,
STATUS VARCHAR(50) DEFAULT NULL,
DATE_OF_CREATE BIGINT DEFAULT NULL, DATE_OF_CREATE BIGINT DEFAULT NULL,
DATE_OF_LAST_UPDATE BIGINT DEFAULT NULL, DATE_OF_LAST_UPDATE BIGINT DEFAULT NULL,
OWNER VARCHAR(45) DEFAULT NULL, OWNER VARCHAR(45) DEFAULT NULL,

@ -156,6 +156,7 @@
<Scope>perm:admin:app:review:view</Scope> <Scope>perm:admin:app:review:view</Scope>
<Scope>perm:admin:app:publisher:update</Scope> <Scope>perm:admin:app:publisher:update</Scope>
<Scope>perm:admin:app:review:update</Scope> <Scope>perm:admin:app:review:update</Scope>
<Scope>perm:admin:app:subscription:view</Scope>
<Scope>perm:device-types:types</Scope> <Scope>perm:device-types:types</Scope>
<Scope>perm:enterprise:modify</Scope> <Scope>perm:enterprise:modify</Scope>
<Scope>perm:enterprise:view</Scope> <Scope>perm:enterprise:view</Scope>
@ -255,7 +256,7 @@
<Scope>perm:device:enroll</Scope> <Scope>perm:device:enroll</Scope>
<Scope>perm:geo-service:analytics-view</Scope> <Scope>perm:geo-service:analytics-view</Scope>
<Scope>perm:geo-service:alerts-manage</Scope> <Scope>perm:geo-service:alerts-manage</Scope>
<Scope>appm:rea</Scope> <Scope>appm:read</Scope>
<Scope>perm:devices:permanent-delete</Scope> <Scope>perm:devices:permanent-delete</Scope>
</Scopes> </Scopes>
<SSOConfiguration> <SSOConfiguration>

@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE (
CREATE TABLE IF NOT EXISTS DM_GROUP ( CREATE TABLE IF NOT EXISTS DM_GROUP (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
GROUP_NAME VARCHAR(100) DEFAULT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL,
STATUS VARCHAR(50) DEFAULT NULL,
DESCRIPTION TEXT DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL,
OWNER VARCHAR(255) DEFAULT NULL, OWNER VARCHAR(255) DEFAULT NULL,
TENANT_ID INTEGER DEFAULT 0, TENANT_ID INTEGER DEFAULT 0,

@ -29,6 +29,7 @@ IF NOT EXISTS(SELECT *
CREATE TABLE DM_GROUP ( CREATE TABLE DM_GROUP (
ID INTEGER IDENTITY (1, 1) NOT NULL, ID INTEGER IDENTITY (1, 1) NOT NULL,
GROUP_NAME VARCHAR(100) DEFAULT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL,
STATUS VARCHAR(50) DEFAULT NULL,
DESCRIPTION VARCHAR(MAX) DEFAULT NULL, DESCRIPTION VARCHAR(MAX) DEFAULT NULL,
OWNER VARCHAR(255) DEFAULT NULL, OWNER VARCHAR(255) DEFAULT NULL,
TENANT_ID INTEGER DEFAULT 0, TENANT_ID INTEGER DEFAULT 0,

@ -24,6 +24,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE (
CREATE TABLE IF NOT EXISTS DM_GROUP ( CREATE TABLE IF NOT EXISTS DM_GROUP (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
GROUP_NAME VARCHAR(100) DEFAULT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL,
STATUS VARCHAR(50) DEFAULT NULL,
DESCRIPTION TEXT DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL,
OWNER VARCHAR(255) DEFAULT NULL, OWNER VARCHAR(255) DEFAULT NULL,
TENANT_ID INTEGER DEFAULT 0, TENANT_ID INTEGER DEFAULT 0,

@ -48,6 +48,7 @@ CREATE TABLE DM_GROUP (
ID NUMBER(10) NOT NULL, ID NUMBER(10) NOT NULL,
DESCRIPTION CLOB DEFAULT NULL, DESCRIPTION CLOB DEFAULT NULL,
GROUP_NAME VARCHAR2(100) DEFAULT NULL, GROUP_NAME VARCHAR2(100) DEFAULT NULL,
STATUS VARCHAR2(50) DEFAULT NULL,
OWNER VARCHAR2(255) DEFAULT NULL, OWNER VARCHAR2(255) DEFAULT NULL,
TENANT_ID NUMBER(10) DEFAULT 0, TENANT_ID NUMBER(10) DEFAULT 0,
CONSTRAINT PK_DM_GROUP PRIMARY KEY (ID) CONSTRAINT PK_DM_GROUP PRIMARY KEY (ID)

@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE (
CREATE TABLE IF NOT EXISTS DM_GROUP ( CREATE TABLE IF NOT EXISTS DM_GROUP (
ID BIGSERIAL NOT NULL PRIMARY KEY, ID BIGSERIAL NOT NULL PRIMARY KEY,
GROUP_NAME VARCHAR(100) DEFAULT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL,
STATUS VARCHAR(50) DEFAULT NULL,
DESCRIPTION TEXT DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL,
OWNER VARCHAR(255) DEFAULT NULL, OWNER VARCHAR(255) DEFAULT NULL,
TENANT_ID INTEGER DEFAULT 0 TENANT_ID INTEGER DEFAULT 0

Loading…
Cancel
Save