Add new API to check whether groups have device types or not

group-operation
Arshana 2 years ago committed by prathabanKavin
parent b797ed1f5b
commit 462c610bfd

@ -192,6 +192,13 @@ import java.util.List;
key = "perm:groups:device", key = "perm:groups:device",
roles = {"Internal/devicemgt-user"}, roles = {"Internal/devicemgt-user"},
permissions = {"/device-mgt/groups/devices/view"} permissions = {"/device-mgt/groups/devices/view"}
),
@Scope(
name = "View whether the groups has relevant device types",
description = "View whether the groups has relevant device types",
key = "perm:groups:devices-types",
roles = {"Internal/devicemgt-user"},
permissions = {"/device-mgt/groups/devices/types"}
) )
} }
) )
@ -1185,4 +1192,59 @@ public interface GroupManagementService {
@QueryParam("requireGroupProps") @QueryParam("requireGroupProps")
boolean requireGroupProps); boolean requireGroupProps);
@Path("/device-types")
@GET
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET,
value = "Getting Details whether the groups has relevant device type or not",
notes = "Getting Details whether the groups has relevant device type or not.",
tags = "Device Group Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:groups:devices-types")
})
},
nickname = "getGroupByGroupNameFilter"
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the device types of groups.",
response = DeviceGroup.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource has been modified the last time.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."),
@ApiResponse(
code = 404,
message = "Error occurred",
response = ErrorResponse.class),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the group details.",
response = ErrorResponse.class)
})
Response getGroupHasDeviceTypes(
@ApiParam(
name = "identifiers",
value = "list of identifiers.",
required = true)
@QueryParam("identifiers")
List<String> identifiers);
} }

@ -48,6 +48,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
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.DeviceGroupConstants;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceTypesOfGroups;
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.GroupNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException;
@ -429,4 +430,24 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
} }
@GET
@Path("/device-types")
@Override
public Response getGroupHasDeviceTypes(@QueryParam("identifiers") List<String> identifiers) {
try {
DeviceTypesOfGroups deviceTypesOfGroups = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getDeviceTypesOfGroups(identifiers);
return Response.status(Response.Status.OK).entity(deviceTypesOfGroups).build();
} catch (GroupManagementException e) {
String msg = "Only numbers can exists in a group ID or Invalid Group ID provided.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
} }

@ -0,0 +1,60 @@
/*
* Copyright (c) 2022, Entgra (pvt) Ltd. (https://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.mgt.common.group.mgt;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(value = "DeviceTypesOfGroups", description = "This class carries whether the groups has device type or not.")
public class DeviceTypesOfGroups implements Serializable {
private static final long serialVersionUID = 5562356373277828099L;
@ApiModelProperty(name = "hasAndroid", value = "groups has Android devices.")
private boolean hasAndroid;
@ApiModelProperty(name = "id", value = "groups has iOS devices.")
private boolean hasIos;
@ApiModelProperty(name = "hasAndroid", value = "groups has Windows devices.")
private boolean hasWindows;
public boolean isHasAndroid() {
return hasAndroid;
}
public void setHasAndroid(boolean hasAndroid) {
this.hasAndroid = hasAndroid;
}
public boolean isHasIos() {
return hasIos;
}
public void setHasIos(boolean hasIos) {
this.hasIos = hasIos;
}
public boolean isHasWindows() {
return hasWindows;
}
public void setHasWindows(boolean hasWindows) {
this.hasWindows = hasWindows;
}
}

@ -41,6 +41,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.DeviceTypesOfGroups;
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.GroupNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException;
@ -335,4 +336,5 @@ public interface GroupManagementProviderService {
*/ */
boolean isDeviceMappedToGroup(int groupId, DeviceIdentifier deviceIdentifier) throws GroupManagementException; boolean isDeviceMappedToGroup(int groupId, DeviceIdentifier deviceIdentifier) throws GroupManagementException;
DeviceTypesOfGroups getDeviceTypesOfGroups(List<String> identifiers) throws GroupManagementException;
} }

@ -43,6 +43,7 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
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.DeviceNotFoundException; 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;
@ -51,6 +52,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
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.DeviceGroupConstants;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceTypesOfGroups;
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.GroupNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException;
@ -79,6 +81,7 @@ import java.util.stream.Collectors;
public class GroupManagementProviderServiceImpl implements GroupManagementProviderService { public class GroupManagementProviderServiceImpl implements GroupManagementProviderService {
private static final Log log = LogFactory.getLog(GroupManagementProviderServiceImpl.class); private static final Log log = LogFactory.getLog(GroupManagementProviderServiceImpl.class);
private static final String DEVICE_STATUS_REMOVED = "REMOVED";
private final GroupDAO groupDAO; private final GroupDAO groupDAO;
private final DeviceDAO deviceDAO; private final DeviceDAO deviceDAO;
@ -1379,4 +1382,104 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
createGroupWithChildren(nextParentGroup, childrenGroups, requireGroupProps, tenantId, depth, counter); createGroupWithChildren(nextParentGroup, childrenGroups, requireGroupProps, tenantId, depth, counter);
} }
} }
@Override
public DeviceTypesOfGroups getDeviceTypesOfGroups(List<String> identifiers) throws GroupManagementException {
DeviceTypesOfGroups deviceTypesOfGroups = new DeviceTypesOfGroups();
List<Integer> groupsIDs = new ArrayList<>();
try {
for (String id : identifiers) {
groupsIDs.add(Integer.parseInt(id));
}
List<String> deviceIDs = new ArrayList<>();
List<Device> allDevices = new ArrayList<>();
for (Integer groupID : groupsIDs) {
DeviceGroup deviceGroup = getGroup(groupID, false);
if (deviceGroup == null) {
String errorMessage = "Invalid Group ID provided.";
log.error(errorMessage);
// throw new GroupManagementException(errorMessage, e);
throw new GroupManagementException(errorMessage);
}
List<Device> devices = getAllDevicesOfGroup(deviceGroup.getName(), false);
for (Device device : devices) {
if ( !DEVICE_STATUS_REMOVED.equals(device.getEnrolmentInfo().getStatus().toString())
&& !deviceIDs.contains(device.getDeviceIdentifier())) {
deviceIDs.add(device.getDeviceIdentifier());
allDevices.add(device);
}
}
}
for (Device device: allDevices){
if (DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID.equals(device.getType())){
deviceTypesOfGroups.setHasAndroid(true);
break;
}
}
for (Device device: allDevices){
if (DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_IOS.equals(device.getType())){
deviceTypesOfGroups.setHasIos(true);
break;
}
}
for (Device device: allDevices){
if (DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS.equals(device.getType())){
deviceTypesOfGroups.setHasWindows(true);
break;
}
}
} catch (NumberFormatException e) {
String errorMessage = "Only numbers can exists in a group ID";
log.error(errorMessage);
throw new GroupManagementException(errorMessage, e);
}
return deviceTypesOfGroups;
}
// DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID.equals(device.getType())
//
// public static List<Integer> convertStringToInteger(List<String> list) throws BadRequestException {
//
//
// return groupsIDs;
// }
//
// public static List<String> getDeviceIDsOfGroups(List<Integer> groupIDs) throws InvalidGroupException,
// GroupDeviceException {
//
// try {
// GroupManagementProviderService groupManagementProviderService = AndroidDeviceManagementDataHolder
// .getInstance().getGroupManagementProviderService();
//
// List<String> deviceIDs = new ArrayList<>();
// for (Integer groupID : groupIDs) {
// DeviceGroup deviceGroup = groupManagementProviderService.getGroup(groupID, false);
// if (deviceGroup == null) {
// String errorMessage = "Invalid Group ID provided.";
// log.error(errorMessage);
// throw new InvalidGroupException(errorMessage);
// }
// List<Device> devices = groupManagementProviderService.getAllDevicesOfGroup(deviceGroup.getName(), false);
// for (Device device : devices) {
// if (DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID.equals(device.getType())
// && !AndroidConstants.DEVICE_STATUS_REMOVED.equals(device.getEnrolmentInfo().getStatus().toString())
// && !deviceIDs.contains(device.getDeviceIdentifier())) {
// deviceIDs.add(device.getDeviceIdentifier());
// }
// }
// }
//
// return deviceIDs;
// } catch (GroupManagementException e) {
// String errorMessage = "Error occurred getting group or device IDs .";
// log.error(errorMessage, e);
// throw new GroupDeviceException(errorMessage, e);
// }
// }
} }

Loading…
Cancel
Save