Merge pull request #507 from charithag/release-2.0.x-Grouping-Fixes

Fixed following issues:
revert-70aa11f8
Rasika Perera 8 years ago committed by GitHub
commit f43202d8aa

@ -0,0 +1,56 @@
/*
* 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.
*
*/
package org.wso2.carbon.device.mgt.jaxrs.beans;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.jaxrs.beans.BasePaginatedResult;
import java.util.ArrayList;
import java.util.List;
public class DeviceToGroupsAssignment extends BasePaginatedResult {
@ApiModelProperty(value = "List of device group ids.")
@JsonProperty("deviceGroupIds")
private List<Integer> deviceGroupIds = new ArrayList<>();
@ApiModelProperty(value = "Device identifier of the device needed to be assigned with group")
@JsonProperty("deviceIdentifier")
private DeviceIdentifier deviceIdentifier;
public List<Integer> getDeviceGroupIds() {
return deviceGroupIds;
}
public void setDeviceGroupIds(List<Integer> deviceGroupIds) {
this.deviceGroupIds = deviceGroupIds;
}
public DeviceIdentifier getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
}

@ -37,6 +37,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceToGroupsAssignment;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList;
@ -774,4 +775,115 @@ public interface GroupManagementService {
required = true)
@Valid List<DeviceIdentifier> deviceIdentifiers);
@Path("/device/assign")
@POST
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_POST,
value = "Assign devices to groups",
notes = "Add existing device to device groups.",
tags = "Device Group Management",
authorizations = {
@Authorization(
value = "permission",
scopes = {@AuthorizationScope(scope = "/device-mgt/groups/devices/add",
description = "Add devices")}
)
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully assign the device to groups.",
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 = "No groups found.",
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 adding devices to the group.",
response = ErrorResponse.class)
})
Response updateDeviceAssigningToGroups(
@ApiParam(
name = "deviceToGroupsAssignment",
value = "Device to groups assignment",
required = true)
@Valid DeviceToGroupsAssignment deviceToGroupsAssignment);
@Path("/device")
@GET
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET,
value = "List of groups that have the device",
notes = "List of groups that have the device.",
tags = "Device Group Management",
authorizations = {
@Authorization(
value = "permission",
scopes = {@AuthorizationScope(scope = "/device-mgt/groups/devices/view",
description = "Add devices")}
)
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK.",
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 = "No groups found.",
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.",
response = ErrorResponse.class)
})
Response getGroups(
@ApiParam(
name = "deviceId",
value = "Id of the device.")
@QueryParam("deviceId") String deviceId,
@ApiParam(
name = "deviceType",
value = "Type of the device.")
@QueryParam("deviceType") String deviceType);
}

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
@ -35,12 +36,14 @@ import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceToGroupsAssignment;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList;
import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
public class GroupManagementServiceImpl implements GroupManagementService {
@ -243,7 +246,8 @@ public class GroupManagementServiceImpl implements GroupManagementService {
}
}
@Override public Response removeDevicesFromGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
@Override
public Response removeDevicesFromGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
try {
DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers);
return Response.status(Response.Status.OK).build();
@ -256,4 +260,45 @@ public class GroupManagementServiceImpl implements GroupManagementService {
}
}
@Override
public Response updateDeviceAssigningToGroups(DeviceToGroupsAssignment deviceToGroupsAssignment) {
try {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
deviceIdentifiers.add(deviceToGroupsAssignment.getDeviceIdentifier());
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
List<DeviceGroup> deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier());
for (DeviceGroup group : deviceGroups) {
Integer groupId = group.getGroupId();
if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) {
deviceToGroupsAssignment.getDeviceGroupIds().remove(groupId);
} else if (!CarbonConstants.REGISTRY_SYSTEM_USERNAME.equals(group.getOwner())) {
DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers);
}
}
for (int groupId : deviceToGroupsAssignment.getDeviceGroupIds()) {
DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers);
}
return Response.status(Response.Status.OK).build();
} catch (GroupManagementException e) {
String msg = "Error occurred while assigning device to groups.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (DeviceNotFoundException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
}
}
@Override
public Response getGroups(String deviceId, String deviceType) {
try {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
List<DeviceGroup> deviceGroups = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroups(deviceIdentifier);
return Response.status(Response.Status.OK).entity(deviceGroups).build();
} catch (GroupManagementException e) {
String msg = "Error occurred while removing devices from group.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
}

@ -233,17 +233,7 @@
<div class="modal-content">
<div class="row">
<div class="col-md-3 col-centered">
<h3>Device was successfully associated with group.</h3>
</div>
</div>
</div>
</div>
<div id="group-deassociate-device-200-content" class="hide">
<div class="modal-content">
<div class="row">
<div class="col-md-3 col-centered">
<h3>Device was successfully removed from group.</h3>
<h3>Device associations updated.</h3>
</div>
</div>
</div>

@ -556,6 +556,34 @@ function hidePopup() {
$('.modal-backdrop').remove();
}
function markAlreadyAssignedGroups(deviceId, deviceType) {
var successCallback = function (data, textStatus, xhr) {
data = JSON.parse(data);
if (xhr.status == 200) {
if (data.length > 0) {
for (var i = 0; i < data.length; i++) {
$('.groupCheckBoxes').each(
function () {
if (data[i].id == $(this).data('groupid')) {
$(this).attr('checked', true);
}
}
);
}
} else {
return;
}
} else {
displayErrors(xhr);
}
};
invokerUtil.get("/api/device-mgt/v1.0/groups/device?deviceId=" + deviceId + "&deviceType=" + deviceType,
successCallback, function (message) {
displayErrors(message);
});
}
/**
* Following functions should be triggered after AJAX request is made.
*/
@ -578,27 +606,42 @@ function attachDeviceEvents() {
var serviceURL;
if ($.hasPermission("LIST_ALL_GROUPS")) {
serviceURL = "/api/device-mgt/v1.0/groups";
serviceURL = "/api/device-mgt/v1.0/admin/groups?limit=100";
} else if ($.hasPermission("LIST_GROUPS")) {
//Get authenticated users groups
serviceURL = "/api/device-mgt/v1.0/groups/user/" + currentUser + "/all";
serviceURL = "/api/device-mgt/v1.0/groups?limit=100";
}
invokerUtil.get(serviceURL, function (data) {
var groups = JSON.parse(data);
var str = '<br /><select id="assign-group-selector" style="color:#3f3f3f;padding:5px;width:250px;">';
var html = '<br />';
for (var i = 0; i < groups.deviceGroups.length; i++) {
str += '<option value="' + groups.deviceGroups[i].id + '">' +
groups.deviceGroups[i].name + '</option>';
if (groups.deviceGroups[i].owner != "wso2.system.user") {
html += '<div class="wr-input-control"><label class="wr-input-control checkbox">' +
'<input class="groupCheckBoxes" type="checkbox" data-groupid="' + groups.deviceGroups[i].id + '" />' +
'<span class="helper" title="' + groups.deviceGroups[i].name + '">' + groups.deviceGroups[i].name +
'</span></label></div>';
}
}
str += '</select>';
$('#user-groups').html(str);
$('#user-groups').html(html);
markAlreadyAssignedGroups(deviceId, deviceType);
$("a#group-device-yes-link").show();
$("a#group-device-yes-link").click(function () {
var selectedGroup = $('#assign-group-selector').val();
serviceURL = "/api/device-mgt/v1.0/groups/id/" + selectedGroup + "/devices/add";
var deviceIdentifiers = [{"id": deviceId, "type": deviceType}];
invokerUtil.post(serviceURL, deviceIdentifiers, function (data) {
var deviceIdentifier = {"id": deviceId, "type": deviceType};
var deviceGroupIds = [];
$('.modal .groupCheckBoxes').each(
function () {
if ($(this).is(':checked')) {
deviceGroupIds.push($(this).data('groupid'));
}
}
);
var deviceToGroupsAssignment = {
deviceIdentifier: deviceIdentifier,
deviceGroupIds: deviceGroupIds
};
serviceURL = "/api/device-mgt/v1.0/groups/device/assign";
invokerUtil.post(serviceURL, deviceToGroupsAssignment, function (data) {
$(modalPopupContent).html($('#group-associate-device-200-content').html());
setTimeout(function () {
hidePopup();

@ -16,12 +16,12 @@
under the License.
}}
{{#if virtualDeviceTypesList}}
<div class="row wr-device-board">
<div class="col-lg-12 wr-secondary-bar">
<span class="page-sub-title">Device Types</span>
</div>
</div>
<span id="device-listing-status-msg"></span>
<div class="container-fluid">
<table class="table table-striped table-hover list-table no-operations display responsive nowrap data-table grid-view no-toolbar"
id="device-type-grid">
@ -36,6 +36,24 @@
</tbody>
</table>
</div>
{{else}}
<div class="ast-container list-view">
<div class="ctrl-info-panel col-centered text-center wr-login">
<h3 class="text-muted">
<i class="fw fw-mobile fw-3x"></i>
</h3>
<h3 class="text-muted">No device type is available to be displayed.</h3>
<a href="https://docs.wso2.com/display/IoTS100/Quick+Start+Guide" target="_blank"
class="btn-operations btn-default">
<span class="fw-stack">
<i class="fw fw-ring fw-stack-2x"></i>
<i class="fw fw-document fw-stack-1x"></i>
</span>
Quick Startup Guide
</a>
</div>
</div>
{{/if}}
<br class="c-both"/>
{{#if virtualDeviceTypesList}}

@ -29,6 +29,9 @@ function onRequest(context) {
if (typesListResponse["status"] == "success") {
var deviceTypes = typesListResponse.content.deviceTypes;
if (deviceTypes) {
if (deviceTypes.length > 0){
viewModel.hasDeviceTypes = true;
}
var deviceTypesList = [], virtualDeviceTypesList = [];
for (var i = 0; i < deviceTypes.length; i++) {
var deviceType = deviceTypes[i];

@ -142,13 +142,6 @@ function loadDevices(searchType, searchParam){
}
} else {
$('#device-type-grid').addClass('hidden');
$('#device-listing-status-msg').html(
'<div class="col-centered text-center"><h3 class="text-muted"><i class="fw fw-mobile fw-3x"></i>' +
'</h3><h3 class="text-muted">No device type is available to be displayed.</h3>' +
'<a href="https://docs.wso2.com/display/IoTS100/Quick+Start+Guide" target="_blank" ' +
'class="btn-operations btn-default"><span class="fw-stack">' +
'<i class="fw fw-ring fw-stack-2x"></i> <i class="fw fw-document fw-stack-1x"></i></span>' +
'Quick Startup Guide</a></div>');
}
$(".icon .text").res_text(0.2);

Loading…
Cancel
Save