leaf and root device organization retrieval

backup
Isuri Mendis 1 year ago
parent c0fe39d926
commit abdb1efd63

@ -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.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.api.beans.ErrorResponse;
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.DeviceOrganization;
import io.swagger.annotations.Api; import io.swagger.annotations.*;
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 javax.ws.rs.Consumes; import javax.ws.rs.*;
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.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; 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 @GET
@Path("/leafs")
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "GET", httpMethod = "GET",
value = "Retrieve All Device Organizations", value = "Retrieve leaf Device Organizations",
notes = "Get a list of all device organizations.", notes = "Get a list of leaf device organizations.",
tags = "Device Organization Management", tags = "Device Organization Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -340,8 +323,89 @@ public interface DeviceOrganizationMgtService {
"list of supported device types.", "list of supported device types.",
response = ErrorResponse.class) 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. * Retrieves a specific device organization by its organization ID.

@ -19,22 +19,16 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api;
import com.google.gson.Gson; 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.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.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.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.exception.DeviceOrganizationMgtPluginException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService;
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 javax.ws.rs.Consumes; import javax.ws.rs.*;
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.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
@ -109,10 +103,32 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe
@GET @GET
@Override @Override
public Response getAllDeviceOrganizations() { @Path("/leafs")
public Response getDeviceOrganizationLeafs(
@QueryParam("offset") int offset,
@QueryParam("limit") int limit) {
RequestValidationUtil.validatePaginationParameters(offset, limit);
try { try {
DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService();
List<DeviceOrganization> organizations = deviceOrganizationService.getAllDeviceOrganizations(); PaginationRequest request = new PaginationRequest(offset, limit);
List<DeviceOrganization> 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<DeviceOrganization> organizations = deviceOrganizationService.getDeviceOrganizationRoots(request);
return Response.status(Response.Status.OK).entity(organizations).build(); return Response.status(Response.Status.OK).entity(organizations).build();
} catch (DeviceOrganizationMgtPluginException e) { } catch (DeviceOrganizationMgtPluginException e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); 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(); DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService();
boolean exists; boolean exists;
if (parentDeviceId.equals("null")) { if (parentDeviceId.equals("null")) {
exists = deviceOrganizationService.isDeviceOrganizationExist(deviceId, null); exists = deviceOrganizationService.isDeviceOrganizationExist(deviceId, null);
} else { } else {
exists = deviceOrganizationService.isDeviceOrganizationExist(deviceId, Integer.valueOf(parentDeviceId)); exists = deviceOrganizationService.isDeviceOrganizationExist(deviceId, Integer.valueOf(parentDeviceId));
} }
return Response.status(Response.Status.OK).entity(exists).build(); return Response.status(Response.Status.OK).entity(exists).build();
} catch (DeviceOrganizationMgtPluginException e) { } catch (DeviceOrganizationMgtPluginException e) {
@ -163,9 +179,9 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe
DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService();
DeviceOrganization organization; DeviceOrganization organization;
if (parentDeviceId.equals("null")) { if (parentDeviceId.equals("null")) {
organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceId, null); organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceId, null);
} else { } else {
organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceId, Integer.valueOf(parentDeviceId)); organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceId, Integer.valueOf(parentDeviceId));
} }
return Response.status(Response.Status.OK).entity(organization).build(); return Response.status(Response.Status.OK).entity(organization).build();
} catch (DeviceOrganizationMgtPluginException e) { } catch (DeviceOrganizationMgtPluginException e) {

@ -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());
}
}

@ -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());
}
}
}

@ -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.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.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 io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
import java.util.List; import java.util.List;
@ -58,6 +59,20 @@ public interface DeviceOrganizationDAO {
*/ */
List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtDAOException; List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtDAOException;
/**
* Retrieves device Organization Roots
* @return A list of root device organization records.
* @throws DeviceOrganizationMgtDAOException
*/
public List<DeviceOrganization> getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtDAOException;
/**
* Retrieves device Organization Leafs
* @return A list of leaf device organization records.
* @throws DeviceOrganizationMgtDAOException
*/
public List<DeviceOrganization> getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtDAOException;
/** /**
* Adds a new record to the device organization table. * Adds a new record to the device organization table.
* *

@ -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.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.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.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.DBConnectionException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -244,6 +245,73 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
} }
} }
/**
* {@inheritDoc}
*/
@Override
public List<DeviceOrganization> getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtDAOException {
List<DeviceOrganization> 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<DeviceOrganization> getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtDAOException {
List<DeviceOrganization> 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} * {@inheritDoc}
*/ */

@ -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;
}
}

@ -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.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.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.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.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.DBConnectionException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; 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<DeviceOrganization> 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<DeviceOrganization> 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} * {@inheritDoc}
*/ */

@ -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.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.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.exception.DeviceOrganizationMgtPluginException;
import java.util.List; import java.util.List;
@ -70,6 +71,20 @@ public interface DeviceOrganizationService {
*/ */
List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtPluginException; List<DeviceOrganization> getAllDeviceOrganizations() throws DeviceOrganizationMgtPluginException;
/**
* Retrieves device Organization Leafs
* @return A list of leaf device organizations.
* @throws DeviceOrganizationMgtPluginException
*/
List<DeviceOrganization> getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtPluginException;
/**
* Retrieves device Organization Roots
* @return A list of root device organizations.
* @throws DeviceOrganizationMgtPluginException
*/
List<DeviceOrganization> getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtPluginException;
/** /**
* Retrieves a specific device organization by its organization ID. * Retrieves a specific device organization by its organization ID.
* *

@ -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.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.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.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.impl.DeviceOrganizationServiceImpl;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest; 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"); 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<DeviceOrganization> 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<DeviceOrganization> 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") @Test(dependsOnMethods = "testAddMultipleDeviceOrganizations")
public void testGetDeviceOrganizationByUniqueKey() throws DeviceOrganizationMgtPluginException { public void testGetDeviceOrganizationByUniqueKey() throws DeviceOrganizationMgtPluginException {
int deviceID = 20; int deviceID = 20;

Loading…
Cancel
Save