diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Group.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Group.java index 4e1b9b68b7b..acd9e987141 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Group.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Group.java @@ -21,15 +21,15 @@ package org.wso2.carbon.device.mgt.jaxrs.api; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils; -import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; 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.GroupAlreadyEixistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -141,6 +141,26 @@ public class Group { } } + @Path("/all") + @GET + @Produces("application/json") + public Response getAllGroups() { + try { + GroupManagementProviderService groupManagementProviderService = DeviceMgtAPIUtils + .getGroupManagementProviderService(); + PaginationResult paginationResult = groupManagementProviderService + .getGroups(0, groupManagementProviderService.getGroupCount()); + if (paginationResult.getRecordsTotal() > 0) { + return Response.status(Response.Status.OK).entity(paginationResult.getData()).build(); + } else { + return Response.status(Response.Status.NOT_FOUND).build(); + } + } catch (GroupManagementException e) { + log.error(e.getMessage(), e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + } + } + @Path("/user/{user}") @GET @Produces("application/json") @@ -364,16 +384,18 @@ public class Group { } @GET - @Path("/owner/{owner}/name/{groupName}/devices/all") + @Path("/owner/{owner}/name/{groupName}/devices") @Produces("application/json") - public Response getDevices(@PathParam("groupName") String groupName, - @PathParam("owner") String owner) { + public Response getDevices(@PathParam("groupName") String groupName, @PathParam("owner") String owner, + @QueryParam("start") int startIdx, @QueryParam("length") int length) { try { - List devices = DeviceMgtAPIUtils.getGroupManagementProviderService().getDevices( - groupName, owner); - Device[] deviceArray = new Device[devices.size()]; - devices.toArray(deviceArray); - return Response.status(Response.Status.OK).entity(deviceArray).build(); + PaginationResult paginationResult = DeviceMgtAPIUtils + .getGroupManagementProviderService().getDevices(groupName, owner, startIdx, length); + if (paginationResult.getRecordsTotal() > 0) { + return Response.status(Response.Status.OK).entity(paginationResult).build(); + } else { + return Response.status(Response.Status.NOT_FOUND).build(); + } } catch (GroupManagementException e) { log.error(e.getMessage(), e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); @@ -394,15 +416,12 @@ public class Group { } } - @PUT - @Path("/owner/{owner}/name/{groupName}/devices/{deviceType}/{deviceId}") + @POST + @Path("/owner/{owner}/name/{groupName}/devices") @Produces("application/json") public Response addDevice(@PathParam("groupName") String groupName, - @PathParam("owner") String owner, @PathParam("deviceId") String deviceId, - @PathParam("deviceType") String deviceType, - @FormParam("userName") String userName) { + @PathParam("owner") String owner, DeviceIdentifier deviceIdentifier) { try { - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); boolean isAdded = DeviceMgtAPIUtils.getGroupManagementProviderService().addDevice( deviceIdentifier, groupName, owner); if (isAdded) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml index 1768fa91608..e00d1a93ac7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -959,12 +959,19 @@ - List All Groups + List All Groups with Pagination /device-mgt/admin/groups/list /groups GET + + List All Groups + /device-mgt/admin/groups/list + /groups/all + GET + + List Groups /device-mgt/user/groups/list @@ -1080,7 +1087,7 @@ List Group Devices /device-mgt/user/groups/devices/list - /groups/owner/*/name/*/devices/all + /groups/owner/*/name/*/devices GET @@ -1094,8 +1101,8 @@ Add Device to Group /device-mgt/user/groups/devices/add - /groups/owner/*/name/*/devices/*/* - PUT + /groups/owner/*/name/*/devices + POST diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java index b509f136aa6..1dc62b8046b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java @@ -382,9 +382,9 @@ public class GroupDAOImpl implements GroupDAO { ResultSet resultSet = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(gm.ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP gm, (SELECT ID as GROUP_ID " + + String sql = "SELECT COUNT(gm.ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP gm, (SELECT ID " + "FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) dg " + - "WHERE dm.GROUP_ID = dg.GROUP_ID AND dm.TENANT_ID = ?"; + "WHERE gm.GROUP_ID = dg.ID AND gm.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, groupName); stmt.setString(2, owner); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag new file mode 100644 index 00000000000..e64ad14d168 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/group-api.jag @@ -0,0 +1,83 @@ +<% +/* + * 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. + */ + +var uri = request.getRequestURI(); +var uriMatcher = new URIMatcher(String(uri)); + +var log = new Log("api/device-api.jag"); +var constants = require("/app/modules/constants.js"); +var utility = require("/app/modules/utility.js").utility; +var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); +var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker; + +var user = session.get(constants.USER_SESSION_KEY); +var result; + +response.contentType = 'application/json'; + +if (!user) { + response.sendRedirect("/devicemgt/login?#login-required"); + exit(); +} else { + if (uriMatcher.match("/{context}/api/groups")) { + var url = request.getParameter("url"); + var draw = request.getParameter("draw"); + var length = request.getParameter("length"); + var start = request.getParameter("start"); + var search = request.getParameter("search[value]"); + var groupName = request.getParameter("columns[1][search][value]"); + var owner = request.getParameter("columns[2][search][value]"); + var targetURL; + + function appendQueryParam(url, queryParam, value) { + if (url.indexOf("?") > 0) { + return url + "&" + queryParam + "=" + value; + } + return url + "?" + queryParam + "=" + value; + } + + targetURL = devicemgtProps.httpsURL + request.getParameter("url"); + targetURL = appendQueryParam(targetURL, "start", start); + targetURL = appendQueryParam(targetURL, "length", length); + + if (search && search !== "") { + targetURL = appendQueryParam(targetURL, "search", search); + } + + if (groupName && groupName !== "") { + targetURL = appendQueryParam(targetURL, "group-name", groupName); + } + + if (owner && owner !== "") { + targetURL = appendQueryParam(targetURL, "user", owner); + } + + serviceInvokers.XMLHttp.get( + targetURL, function (responsePayload) { + response.status = 200; + result = responsePayload; + }, + function (responsePayload) { + response.status = responsePayload.status; + result = responsePayload.responseText; + }); + } +} + +%> \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/stats-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/stats-api.jag deleted file mode 100644 index 5c8fd94355d..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/stats-api.jag +++ /dev/null @@ -1,124 +0,0 @@ -<% -/* - * Copyright (c) 2015, 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. - */ - -var uri = request.getRequestURI(); -var uriMatcher = new URIMatcher(String(uri)); - -var log = new Log("api/stats-api.jag"); - -var from = request.getParameter("from"); -var to = request.getParameter("to"); - -var constants = require("/app/modules/constants.js"); -var utility = require("/app/modules/utility.js").utility; -var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); - -var deviceCloudGroupService = devicemgtProps["httpsURL"] + "/common/group_manager"; -var deviceCloudDeviceService = devicemgtProps["httpsURL"] + "/common/device_manager"; -var deviceCloudStatsService = devicemgtProps["httpsURL"] + "/common/stats_manager"; - -var stats = {}; -var deviceId; -var deviceType; - -var responseProcessor = require('utils').response; -response.contentType = 'application/json'; - -var user = session.get(constants.USER_SESSION_KEY); - -if (!user) { - response = responseProcessor.buildErrorResponse(response, 401, "Unauthorized"); -} else { - if (uriMatcher.match("/{context}/api/stats")) { - deviceId = request.getParameter("deviceId"); - deviceType = request.getParameter("deviceType"); - - getDeviceData(deviceType, deviceId); - - } else if (uriMatcher.match("/{context}/api/stats/group")) { - var groupId = request.getParameter("groupId"); - - //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/device/all - var endPoint = deviceCloudGroupService + "/group/id/" + groupId + "/device/all"; - var data = {"username": user}; - var devices = get(endPoint, data, "json").data; - - for (var device in devices) { - deviceId = devices[device].deviceIdentifier; - deviceType = devices[device].type; - getDeviceData(deviceType, deviceId); - } - } - log.info(stats); - // returning the result. - if (stats) { - print(stats); - } -} - -function getDeviceData(deviceType, deviceId) { - //URL: GET https://localhost:9443/devicecloud/device_manager/device/type/{type}/identifier/{identifier} - var endPoint = deviceCloudDeviceService + "/device/type/" + deviceType + "/identifier/" + deviceId; - var data = {"username": user}; - var device = get(endPoint, data, "json").data; - log.info(device); - if (!device) { - return; - } - var uname = device.enrolmentInfo.owner; - - var analyticStreams = utility.getDeviceTypeConfig(deviceType)["analyticStreams"]; - - if (analyticStreams) { - var streamTableName; - for (var stream in analyticStreams) { - streamTableName = analyticStreams[stream]["table"]; - if (stats[streamTableName] == null) { - stats[streamTableName] = []; - } - stats[streamTableName].push({ - "device": device.name, - "stats": getSensorData(streamTableName, analyticStreams[stream]["ui_unit"]["data"][1]["column"]["name"], uname, device.type, device.deviceIdentifier, from, to), - "stream": analyticStreams[stream] - }); - } - } -} - -function getSensorData(table, column, user, type, deviceIdentifier, from, to) { - - var fetchedData = []; - - try { - ///stats/device/type/{type}/identifier/{identifier} - var endPoint = deviceCloudStatsService + "/stats/device/type/" + type + "/identifier/" + deviceIdentifier; - var query = "?table=" + encodeURIComponent(table) - + "&column=" + encodeURIComponent(column) - + "&username=" + encodeURIComponent(user) - + "&from=" + from - + "&to=" + to; - endPoint = endPoint + query; - fetchedData = get(endPoint, {}, "json").data; - return fetchedData; - } catch (error) { - log.error(error); - } -} - -%> diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js index 3d050b38a15..31fac7176d3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/group.js @@ -54,7 +54,7 @@ var groupModule = {}; }; groupModule.getGroupDeviceCount = function (groupName, owner) { - endPoint = groupServiceEndpoint + "/" + owner + "/" + groupName + "/devices/count"; + endPoint = groupServiceEndpoint + "/owner/" + owner + "/name/" + groupName + "/devices/count"; return serviceInvokers.XMLHttp.get( endPoint, function (responsePayload) { return responsePayload; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs index 2966fa09a2d..b800c8777e7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs @@ -55,6 +55,11 @@ {{/zone}} {{#zone "content"}} +
+
+ {{title}} +
+
@@ -275,7 +280,7 @@