diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java index 4522014ba7..bb1c421d3e 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java @@ -21,27 +21,9 @@ import io.entgra.device.mgt.core.apimgt.annotations.Scope; import io.entgra.device.mgt.core.apimgt.annotations.Scopes; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.beans.ErrorResponse; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; -import io.swagger.annotations.Extension; -import io.swagger.annotations.ExtensionProperty; -import io.swagger.annotations.Info; -import io.swagger.annotations.ResponseHeader; -import io.swagger.annotations.SwaggerDefinition; -import io.swagger.annotations.Tag; +import io.swagger.annotations.*; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; +import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -290,16 +272,17 @@ public interface DeviceOrganizationMgtService { /** - * Retrieves a list of all device organizations. + * Retrieves a list of leaf device organizations. * - * @return A response containing a list of all device organizations. + * @return A response containing a list of leaf device organizations. */ @GET + @Path("/leafs") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", - value = "Retrieve All Device Organizations", - notes = "Get a list of all device organizations.", + value = "Retrieve leaf Device Organizations", + notes = "Get a list of leaf device organizations.", tags = "Device Organization Management", extensions = { @Extension(properties = { @@ -340,8 +323,89 @@ public interface DeviceOrganizationMgtService { "list of supported device types.", response = ErrorResponse.class) }) - Response getAllDeviceOrganizations(); + Response getDeviceOrganizationLeafs( + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items", + required = false, + defaultValue = "0") + @QueryParam("offset") + int offset, + @ApiParam( + name = "limit", + value = "Provide how many policy details you require from the starting pagination index/offset.", + required = false, + defaultValue = "5") + @QueryParam("limit") + int limit); + /** + * Retrieves a list of root device organizations. + * + * @return A response containing a list of root device organizations. + */ + @GET + @Path("/roots") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Retrieve leaf Device Organizations", + notes = "Get a list of leaf device organizations.", + tags = "Device Organization Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "dm:device-org:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the all devices.", + 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 was last modified.\n" + + "Used by caches, or in conditional requests."), + } + ), + @ApiResponse( + code = 400, + message = + "Bad Request. \n"), + @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 " + + "list of supported device types.", + response = ErrorResponse.class) + }) + Response getDeviceOrganizationRoots( + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items", + required = false, + defaultValue = "0") + @QueryParam("offset") + int offset, + @ApiParam( + name = "limit", + value = "Provide how many policy details you require from the starting pagination index/offset.", + required = false, + defaultValue = "5") + @QueryParam("limit") + int limit); /** * Retrieves a specific device organization by its organization ID. diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java index 333498a8ed..ace5afb3cc 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java @@ -19,22 +19,16 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api; import com.google.gson.Gson; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.util.DeviceOrgAPIUtils; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.util.RequestValidationUtil; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; +import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; @@ -109,10 +103,32 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe @GET @Override - public Response getAllDeviceOrganizations() { + @Path("/leafs") + public Response getDeviceOrganizationLeafs( + @QueryParam("offset") int offset, + @QueryParam("limit") int limit) { + RequestValidationUtil.validatePaginationParameters(offset, limit); try { DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); - List organizations = deviceOrganizationService.getAllDeviceOrganizations(); + PaginationRequest request = new PaginationRequest(offset, limit); + List organizations = deviceOrganizationService.getDeviceOrganizationLeafs(request); + return Response.status(Response.Status.OK).entity(organizations).build(); + } catch (DeviceOrganizationMgtPluginException e) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); + } + } + + @GET + @Path("/roots") + @Override + public Response getDeviceOrganizationRoots( + @QueryParam("offset") int offset, + @QueryParam("limit") int limit) { + RequestValidationUtil.validatePaginationParameters(offset, limit); + try { + DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); + PaginationRequest request = new PaginationRequest(offset, limit); + List organizations = deviceOrganizationService.getDeviceOrganizationRoots(request); return Response.status(Response.Status.OK).entity(organizations).build(); } catch (DeviceOrganizationMgtPluginException e) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); @@ -142,9 +158,9 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); boolean exists; if (parentDeviceId.equals("null")) { - exists = deviceOrganizationService.isDeviceOrganizationExist(deviceId, null); + exists = deviceOrganizationService.isDeviceOrganizationExist(deviceId, null); } else { - exists = deviceOrganizationService.isDeviceOrganizationExist(deviceId, Integer.valueOf(parentDeviceId)); + exists = deviceOrganizationService.isDeviceOrganizationExist(deviceId, Integer.valueOf(parentDeviceId)); } return Response.status(Response.Status.OK).entity(exists).build(); } catch (DeviceOrganizationMgtPluginException e) { @@ -163,9 +179,9 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); DeviceOrganization organization; if (parentDeviceId.equals("null")) { - organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceId, null); + organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceId, null); } else { - organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceId, Integer.valueOf(parentDeviceId)); + organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceId, Integer.valueOf(parentDeviceId)); } return Response.status(Response.Status.OK).entity(organization).build(); } catch (DeviceOrganizationMgtPluginException e) { diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/InputValidationException.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/InputValidationException.java new file mode 100644 index 0000000000..fa67ff3eaf --- /dev/null +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/InputValidationException.java @@ -0,0 +1,16 @@ +package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.util; + +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.beans.ErrorResponse; + +import javax.ws.rs.BadRequestException; +import javax.ws.rs.core.Response; +import java.io.Serializable; + +public class InputValidationException extends BadRequestException implements Serializable { + + private static final long serialVersionUID = 147843579458906890L; + + public InputValidationException(ErrorResponse error) { + super(Response.status(Response.Status.BAD_REQUEST).entity(error).build()); + } +} diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/RequestValidationUtil.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/RequestValidationUtil.java new file mode 100644 index 0000000000..664341ed9f --- /dev/null +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/RequestValidationUtil.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.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 io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.util; + +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.beans.ErrorResponse; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class RequestValidationUtil { + + private static final Log log = LogFactory.getLog(RequestValidationUtil.class); + + public static void validatePaginationParameters(int offset, int limit) { + if (offset < 0) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request parameter offset is s " + + "negative value.").build()); + } + if (limit < 0) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request parameter limit is a " + + "negative value.").build()); + } + if (limit > 100) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Request parameter limit should" + + " be less than or equal to 100.").build()); + } + + } +} diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java index 411243fa4c..f5a0d93e44 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; import java.util.List; @@ -58,6 +59,20 @@ public interface DeviceOrganizationDAO { */ List getAllDeviceOrganizations() throws DeviceOrganizationMgtDAOException; + /** + * Retrieves device Organization Roots + * @return A list of root device organization records. + * @throws DeviceOrganizationMgtDAOException + */ + public List getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtDAOException; + + /** + * Retrieves device Organization Leafs + * @return A list of leaf device organization records. + * @throws DeviceOrganizationMgtDAOException + */ + public List getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtDAOException; + /** * Adds a new record to the device organization table. * diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java index fe9d690438..3bad489e79 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java @@ -21,6 +21,7 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.D import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; import org.apache.commons.logging.Log; @@ -244,6 +245,73 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { } } + /** + * {@inheritDoc} + */ + @Override + public List getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtDAOException { + List deviceOrganizations = new ArrayList<>(); + try { + Connection conn = ConnectionManagerUtil.getDBConnection(); + String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE PARENT_DEVICE_ID IS NULL " + + "LIMIT ? OFFSET ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, request.getLimit()); + stmt.setInt(2, request.getOffSet()); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + DeviceOrganization deviceOrganization = loadDeviceOrganization(rs); + deviceOrganizations.add(deviceOrganization); + } + } + } + return deviceOrganizations; + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to retrieving device organization root details."; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while processing SQL to retrieving device organization roots."; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } + } + + + /** + * {@inheritDoc} + */ + @Override + public List getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtDAOException { + List deviceOrganizations = new ArrayList<>(); + try { + Connection conn = ConnectionManagerUtil.getDBConnection(); + String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE ID NOT IN (SELECT DISTINCT PARENT_DEVICE_ID " + + "FROM DM_DEVICE_ORGANIZATION WHERE PARENT_DEVICE_ID IS NOT NULL) " + + "LIMIT ? OFFSET ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, request.getLimit()); + stmt.setInt(2, request.getOffSet()); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + DeviceOrganization deviceOrganization = loadDeviceOrganization(rs); + deviceOrganizations.add(deviceOrganization); + } + } + } + return deviceOrganizations; + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to retrieving all device organizations details."; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while processing SQL to retrieving all device organizations."; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } + } + + /** * {@inheritDoc} */ diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/PaginationRequest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/PaginationRequest.java new file mode 100644 index 0000000000..18db134545 --- /dev/null +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/PaginationRequest.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.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 io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto; + +/** + * This class holds required parameters for a querying a paginated device response. + */ +public class PaginationRequest { + + private int offSet; + private int limit; + + public PaginationRequest(int start, int limit) { + this.offSet = start; + this.limit = limit; + } + + public int getOffSet() { + return offSet; + } + + public void setOffSet(int offSet) { + this.offSet = offSet; + } + + public int getLimit() { + return limit; + } + + public void setLimit(int limit) { + this.limit = limit; + } + + public boolean validatePaginationRequest(int offSet, int limit) { + if (offSet < 0) { + throw new IllegalArgumentException("off set value can't be negative"); + } else if (limit < 0) { + throw new IllegalArgumentException("limit value can't be negative"); + } else { + return true; + } + } + + @Override + public String toString() { + return "Off Set'" + this.offSet + "' row count '" + this.limit; + } +} diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java index e3e1d5b5d2..a051a5661e 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java @@ -22,6 +22,7 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.D import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.BadRequestException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; @@ -133,6 +134,52 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } } + /** + * {@inheritDoc} + */ + @Override + public List getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtPluginException { + try { + // Open a database connection + ConnectionManagerUtil.openDBConnection(); + return deviceOrganizationDao.getDeviceOrganizationRoots(request); + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the database connection to retrieve all device organizations."; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } catch (DeviceOrganizationMgtDAOException e) { + String msg = "Error occurred in the database level while retrieving all device organizations."; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } finally { + // Close the database connection + ConnectionManagerUtil.closeDBConnection(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public List getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtPluginException { + try { + // Open a database connection + ConnectionManagerUtil.openDBConnection(); + return deviceOrganizationDao.getDeviceOrganizationLeafs(request); + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the database connection to retrieve all device organizations."; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } catch (DeviceOrganizationMgtDAOException e) { + String msg = "Error occurred in the database level while retrieving all device organizations."; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } finally { + // Close the database connection + ConnectionManagerUtil.closeDBConnection(); + } + } + /** * {@inheritDoc} */ diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java index 01fd8c1fea..940f5c2699 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; import java.util.List; @@ -70,6 +71,20 @@ public interface DeviceOrganizationService { */ List getAllDeviceOrganizations() throws DeviceOrganizationMgtPluginException; + /** + * Retrieves device Organization Leafs + * @return A list of leaf device organizations. + * @throws DeviceOrganizationMgtPluginException + */ + List getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtPluginException; + + /** + * Retrieves device Organization Roots + * @return A list of root device organizations. + * @throws DeviceOrganizationMgtPluginException + */ + List getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtPluginException; + /** * Retrieves a specific device organization by its organization ID. * diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java index 7ff38038d3..566e0e9147 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java @@ -2,6 +2,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.impl.DeviceOrganizationServiceImpl; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest; @@ -243,6 +244,40 @@ public class ServiceTest extends BaseDeviceOrganizationTest { Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty"); } + @Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent") + public void testGetRootOrganizations() throws DeviceOrganizationMgtPluginException { + int offset = 0; + int limit = 10; + PaginationRequest request = new PaginationRequest(offset, limit); + List organizations = deviceOrganizationService.getDeviceOrganizationRoots(request); + for (DeviceOrganization organization : organizations) { + log.info("organizationID = " + organization.getOrganizationId()); + log.info("deviceID = " + organization.getDeviceId()); + log.info("parentDeviceID = " + organization.getParentDeviceId()); + log.info("updateTime = " + organization.getUpdateTime()); + log.info("----------------------------------------------"); + } + Assert.assertNotNull(organizations, "List of organizations cannot be null"); + Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty"); + } + + @Test(dependsOnMethods = "testAddDeviceOrganization") + public void testGetLeafOrganizations() throws DeviceOrganizationMgtPluginException { + int offset = 0; + int limit = 10; + PaginationRequest request = new PaginationRequest(offset, limit); + List organizations = deviceOrganizationService.getDeviceOrganizationLeafs(request); + for (DeviceOrganization organization : organizations) { + log.info("organizationID = " + organization.getOrganizationId()); + log.info("deviceID = " + organization.getDeviceId()); + log.info("parentDeviceID = " + organization.getParentDeviceId()); + log.info("updateTime = " + organization.getUpdateTime()); + log.info("----------------------------------------------"); + } + Assert.assertNotNull(organizations, "List of organizations cannot be null"); + Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty"); + } + @Test(dependsOnMethods = "testAddMultipleDeviceOrganizations") public void testGetDeviceOrganizationByUniqueKey() throws DeviceOrganizationMgtPluginException { int deviceID = 20;