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 bb1c421d3e..5201d72445 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,9 +21,27 @@ 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.*; +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 javax.ws.rs.*; +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.core.MediaType; import javax.ws.rs.core.Response; @@ -277,7 +295,7 @@ public interface DeviceOrganizationMgtService { * @return A response containing a list of leaf device organizations. */ @GET - @Path("/leafs") + @Path("leafs") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", @@ -345,7 +363,7 @@ public interface DeviceOrganizationMgtService { * @return A response containing a list of root device organizations. */ @GET - @Path("/roots") + @Path("roots") @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", 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 ace5afb3cc..5be5e1b16b 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 @@ -20,7 +20,7 @@ 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.DeviceNodeResult; 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; @@ -28,7 +28,15 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.D import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import javax.ws.rs.*; +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.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; @@ -74,9 +82,7 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe @QueryParam("includeDevice") boolean includeDevice) { try { DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); - DeviceNode deviceNode = new DeviceNode(); - deviceNode.setDeviceId(deviceId); - List children = deviceOrganizationService.getChildrenOfDeviceNode(deviceNode, maxDepth, includeDevice); + DeviceNodeResult children = deviceOrganizationService.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice); return Response.status(Response.Status.OK).entity(children).build(); } catch (DeviceOrganizationMgtPluginException e) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); @@ -92,9 +98,7 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe @QueryParam("includeDevice") boolean includeDevice) { try { DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); - DeviceNode deviceNode = new DeviceNode(); - deviceNode.setDeviceId(deviceId); - List parents = deviceOrganizationService.getParentsOfDeviceNode(deviceNode, maxDepth, includeDevice); + DeviceNodeResult parents = deviceOrganizationService.getParentsOfDeviceNode(deviceId, maxDepth, includeDevice); return Response.status(Response.Status.OK).entity(parents).build(); } catch (DeviceOrganizationMgtPluginException e) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).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 f5a0d93e44..20e6242d09 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 @@ -17,7 +17,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.DeviceNodeResult; 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; @@ -32,24 +32,24 @@ public interface DeviceOrganizationDAO { /** * Retrieves child devices per particular device ID * - * @param node The device node for which child devices are retrieved. + * @param deviceId The device ID for which child devices are retrieved. * @param maxDepth The maximum depth to traverse when fetching child devices. * @param includeDevice Flag to indicate whether to include the parent device in the result. * @return A list of child device nodes. * @throws DeviceOrganizationMgtDAOException If an error occurs while retrieving child devices. */ - List getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException; + DeviceNodeResult getChildrenOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException; /** * Retrieves parent devices for a given device node. * - * @param node The device node for which parent devices are retrieved. + * @param deviceId The device ID for which parent devices are retrieved. * @param maxDepth The maximum depth to traverse when fetching parent devices. * @param includeDevice Flag to indicate whether to include the current device node in the result. * @return A list of parent device nodes. * @throws DeviceOrganizationMgtDAOException If an error occurs while retrieving parent devices. */ - List getParentsOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException; + DeviceNodeResult getParentsOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException; /** * Retrieves all device organization records. @@ -61,6 +61,7 @@ public interface DeviceOrganizationDAO { /** * Retrieves device Organization Roots + * * @return A list of root device organization records. * @throws DeviceOrganizationMgtDAOException */ @@ -68,6 +69,7 @@ public interface DeviceOrganizationDAO { /** * Retrieves device Organization Leafs + * * @return A list of leaf device organization records. * @throws DeviceOrganizationMgtDAOException */ 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 606fd50976..0baa1672e7 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 @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao. import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO; 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.DeviceNodeResult; 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; @@ -53,63 +54,74 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public List getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) + public DeviceNodeResult getChildrenOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException { List childNodes = new ArrayList<>(); + Set organizations = new HashSet<>(); Set visited = new HashSet<>(); - Set twiseVisited = new HashSet<>(); + Set twiceVisited = new HashSet<>(); try { Connection conn = ConnectionManagerUtil.getDBConnection(); + DeviceNode deviceNode = getDeviceDetails(deviceId, conn); boolean parentAdded = false; // Flag to track whether the parent device has been added - getChildrenRecursive(node, maxDepth, + getChildrenRecursive( + deviceNode, + maxDepth, visited, - twiseVisited, - conn, childNodes, includeDevice - , parentAdded + twiceVisited, + conn, + childNodes, + includeDevice, + parentAdded, + organizations ); if (!includeDevice && !parentAdded ) { - childNodes.add(node); // Add the parent device if it hasn't been added and includeDevice is false. + childNodes.add(deviceNode); // Add the parent device if it hasn't been added and includeDevice is false. } - return childNodes; + return new DeviceNodeResult(childNodes, organizations); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining DB connection to retrieve all child devices for " + - "parent device ID " + node.getDeviceId(); + "parent device ID " + deviceId; log.error(msg); throw new DeviceOrganizationMgtDAOException(msg, e); } catch (SQLException e) { String msg = "Error occurred while processing SQL to retrieve all child devices for " + - "parent device ID " + node.getDeviceId(); + "parent device ID " + deviceId; log.error(msg); throw new DeviceOrganizationMgtDAOException(msg, e); } } - private void getChildrenRecursive(DeviceNode node, int maxDepth, + private void getChildrenRecursive(DeviceNode node, + int maxDepth, Set visited, - Set twiseVisited, + Set twiceVisited, Connection conn, - List childNodes, boolean includeDevice - , boolean parentAdded + List childNodes, + boolean includeDevice, + boolean parentAdded, + Set organizations ) throws SQLException { if (maxDepth <= 0) { return; } - if (twiseVisited.contains(node.getDeviceId())) { + if (twiceVisited.contains(node.getDeviceId())) { return; } if (visited.contains(node.getDeviceId())) { - twiseVisited.add(node.getDeviceId()); + twiceVisited.add(node.getDeviceId()); } visited.add(node.getDeviceId()); - String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " + - "FROM DM_DEVICE D " + + String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME, " + + "DO.ORGANIZATION_ID, DO.DEVICE_ID, DO.PARENT_DEVICE_ID, DO.DEVICE_ORGANIZATION_META ," + + "DO.LAST_UPDATED_TIMESTAMP FROM DM_DEVICE D " + "JOIN DM_DEVICE_ORGANIZATION DO ON D.ID = DO.DEVICE_ID " + "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " + "WHERE DO.PARENT_DEVICE_ID = ?"; @@ -128,11 +140,19 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { parentAdded = true; // Set the flag to true after adding the parent device. } - getChildrenRecursive(child, maxDepth - 1, + DeviceOrganization organization = loadDeviceOrganization(rs); + organizations.add(organization); + + getChildrenRecursive( + child, + (maxDepth - 1), visited, - twiseVisited, - conn, childNodes, includeDevice - , parentAdded + twiceVisited, + conn, + childNodes, + includeDevice, + parentAdded, + organizations ); } } @@ -143,56 +163,71 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public List getParentsOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) + public DeviceNodeResult getParentsOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException { List parentNodes = new ArrayList<>(); + Set organizations = new HashSet<>(); Set visited = new HashSet<>(); - Set twiseVisited = new HashSet<>(); + Set twiceVisited = new HashSet<>(); try { Connection conn = ConnectionManagerUtil.getDBConnection(); + DeviceNode deviceNode = getDeviceDetails(deviceId, conn); boolean childAdded = false; - getParentsRecursive(node, maxDepth, + getParentsRecursive( + deviceNode, + maxDepth, visited, - twiseVisited, - conn, parentNodes, includeDevice, childAdded); + twiceVisited, + conn, + parentNodes, + includeDevice, + childAdded, + organizations); if (!includeDevice && !childAdded) { - parentNodes.add(node); + parentNodes.add(deviceNode); } - return parentNodes; + + return new DeviceNodeResult(parentNodes, organizations); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining DB connection to retrieve parent devices for " + - "device ID " + node.getDeviceId(); + "device ID " + deviceId; log.error(msg); throw new DeviceOrganizationMgtDAOException(msg, e); } catch (SQLException e) { String msg = "Error occurred while processing SQL to retrieve parent devices for " + - "device ID " + node.getDeviceId(); + "device ID " + deviceId; log.error(msg); throw new DeviceOrganizationMgtDAOException(msg, e); } } - private void getParentsRecursive(DeviceNode node, int maxDepth, + private void getParentsRecursive(DeviceNode node, + int maxDepth, Set visited, - Set twiseVisited, + Set twiceVisited, Connection conn, - List parentNodes, boolean includeDevice, boolean childAdded) throws SQLException { + List parentNodes, + boolean includeDevice, + boolean childAdded, + Set organizations) + throws SQLException { if (maxDepth <= 0) { return; } - if (twiseVisited.contains(node.getDeviceId())) { + if (twiceVisited.contains(node.getDeviceId())) { return; } if (visited.contains(node.getDeviceId())) { - twiseVisited.add(node.getDeviceId()); + twiceVisited.add(node.getDeviceId()); } visited.add(node.getDeviceId()); - String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " + - "FROM DM_DEVICE D " + + String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME, " + + "DO.ORGANIZATION_ID, DO.DEVICE_ID, DO.PARENT_DEVICE_ID, DO.DEVICE_ORGANIZATION_META ," + + "DO.LAST_UPDATED_TIMESTAMP FROM DM_DEVICE D " + "JOIN DM_DEVICE_ORGANIZATION DO ON D.ID = DO.PARENT_DEVICE_ID " + "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " + "WHERE DO.DEVICE_ID = ?"; @@ -207,15 +242,42 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { parentNodes.add(node); childAdded = true; } - getParentsRecursive(parent, maxDepth - 1, + + DeviceOrganization organization = loadDeviceOrganization(rs); + organizations.add(organization); + + getParentsRecursive( + parent, + (maxDepth - 1), visited, - twiseVisited, - conn, parentNodes, includeDevice, childAdded); + twiceVisited, + conn, + parentNodes, + includeDevice, + childAdded, + organizations); } } } } + private DeviceNode getDeviceDetails(int deviceId, Connection conn) throws SQLException { + String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " + + "FROM DM_DEVICE D " + + "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " + + "WHERE D.ID = ?"; + DeviceNode node = new DeviceNode(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + node = getDeviceFromResultSet(rs); + } + } + } + return node; + } + /** * {@inheritDoc} */ @@ -286,8 +348,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { 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 OR ID IS NOT NULL) " + + String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE ORGANIZATION_ID NOT IN " + + "(SELECT DISTINCT PARENT_DEVICE_ID FROM DM_DEVICE_ORGANIZATION WHERE PARENT_DEVICE_ID IS NOT NULL " + + "OR ORGANIZATION_ID IS NOT NULL) " + "LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, request.getLimit()); @@ -463,7 +526,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { if (!Objects.equals(organization.getDeviceOrganizationMeta(), deviceOrganization.getDeviceOrganizationMeta())) { sql += "DEVICE_ORGANIZATION_META = ? ,"; } - sql += "LAST_UPDATED_TIMESTAMP = ? WHERE ID = ? "; + sql += "LAST_UPDATED_TIMESTAMP = ? WHERE ORGANIZATION_ID = ? "; Connection conn = ConnectionManagerUtil.getDBConnection(); Calendar calendar = Calendar.getInstance(); @@ -504,16 +567,16 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { @Override public DeviceOrganization getDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException { try { - String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION do WHERE do.ID = ? "; - Connection conn = ConnectionManagerUtil.getDBConnection(); + String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION do WHERE do.ORGANIZATION_ID = ? "; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, organizationId); try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { return loadDeviceOrganization(rs); } - log.info("No Device Organization found for retrieval for organizationID = " + organizationId); + log.info("No Device Organization found for retrieving for organizationID = " + organizationId); return null; } } @@ -538,7 +601,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { public boolean deleteDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException { try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String deleteOrganizationSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE ID = ?"; + String deleteOrganizationSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE ORGANIZATION_ID = ?"; try (PreparedStatement deleteOrgStmt = conn.prepareStatement(deleteOrganizationSql)) { 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/util/DeviceOrganizationDaoUtil.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/util/DeviceOrganizationDaoUtil.java index 9414ac22f4..c17bf50336 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/util/DeviceOrganizationDaoUtil.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/util/DeviceOrganizationDaoUtil.java @@ -26,7 +26,7 @@ public class DeviceOrganizationDaoUtil { */ public static DeviceOrganization loadDeviceOrganization(ResultSet rs) throws SQLException { DeviceOrganization deviceOrganization = new DeviceOrganization(); - deviceOrganization.setOrganizationId(rs.getInt("ID")); + deviceOrganization.setOrganizationId(rs.getInt("ORGANIZATION_ID")); deviceOrganization.setDeviceId(rs.getInt("DEVICE_ID")); if (rs.getInt("PARENT_DEVICE_ID") != 0) { deviceOrganization.setParentDeviceId(rs.getInt("PARENT_DEVICE_ID")); 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/DeviceNodeResult.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/DeviceNodeResult.java new file mode 100644 index 0000000000..9011821a12 --- /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/DeviceNodeResult.java @@ -0,0 +1,24 @@ +package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto; + +import java.util.List; +import java.util.Set; + +public class DeviceNodeResult { + + private List nodes; + private Set edges; + + public DeviceNodeResult(List nodes, Set edges) { + this.nodes = nodes; + this.edges = edges; + } + + public List getNodes() { + return nodes; + } + + public Set getEdges() { + return edges; + } + +} 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/DeviceOrganization.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/DeviceOrganization.java index 7d3a2212d8..7a875b91aa 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/dto/DeviceOrganization.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/DeviceOrganization.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto; import java.util.Date; +import java.util.Objects; /** * This abstract class represents a device organization entity used in DeviceOrganizationService. @@ -72,4 +73,22 @@ public class DeviceOrganization { public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DeviceOrganization that = (DeviceOrganization) o; + // Compare fields for equality + return Objects.equals(organizationId, that.organizationId) + && Objects.equals(deviceId, that.deviceId) + && Objects.equals(parentDeviceId, that.parentDeviceId) + && Objects.equals(deviceOrganizationMeta, that.deviceOrganizationMeta); + } + + @Override + public int hashCode() { + // Hash based on fields + return Objects.hash(organizationId, deviceId, parentDeviceId, deviceOrganizationMeta); + } } 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 a051a5661e..040e681889 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 @@ -20,7 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.impl import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAOFactory; 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.DeviceNodeResult; 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; @@ -47,27 +47,28 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService * {@inheritDoc} */ @Override - public List getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) + public DeviceNodeResult getChildrenOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException { - if (node == null || node.getDeviceId() <= 0 || maxDepth < 0) { + if (deviceId <= 0 || maxDepth < 0) { String msg = "Invalid input parameters for retrieving child devices : " + - "deviceID = " + (node != null ? node.getDeviceId() : null) + ", maxDepth = " + maxDepth + + "deviceID = " + deviceId + ", maxDepth = " + maxDepth + ", includeDevice = " + includeDevice; throw new BadRequestException(msg); } try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.getChildrenOfDeviceNode(node, maxDepth, includeDevice); + //set device details + return deviceOrganizationDao.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve child devices : " + - "deviceID = " + node.getDeviceId() + ", maxDepth = " + maxDepth + ", includeDevice = " + + "deviceID = " + deviceId + ", maxDepth = " + maxDepth + ", includeDevice = " + includeDevice; log.error(msg); throw new DeviceOrganizationMgtPluginException(msg, e); } catch (DeviceOrganizationMgtDAOException e) { String msg = "Error occurred in the database level while retrieving child devices : " + - "deviceID = " + node.getDeviceId() + ", maxDepth = " + maxDepth + ", includeDevice = " + + "deviceID = " + deviceId + ", maxDepth = " + maxDepth + ", includeDevice = " + includeDevice; log.error(msg); throw new DeviceOrganizationMgtPluginException(msg, e); @@ -81,27 +82,27 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService * {@inheritDoc} */ @Override - public List getParentsOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) + public DeviceNodeResult getParentsOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException { - if (node == null || node.getDeviceId() <= 0 || maxDepth <= 0) { + if (deviceId <= 0 || maxDepth <= 0) { String msg = "Invalid input parameters for retrieving parent devices. Params : " + - "deviceID = " + (node != null ? node.getDeviceId() : null) + ", maxDepth = " + maxDepth + + "deviceID = " + deviceId + ", maxDepth = " + maxDepth + ", includeDevice = " + includeDevice; throw new BadRequestException(msg); } try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.getParentsOfDeviceNode(node, maxDepth, includeDevice); + return deviceOrganizationDao.getParentsOfDeviceNode(deviceId, maxDepth, includeDevice); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve parent devices for : " + - "device ID = " + node.getDeviceId() + ", maxDepth = " + maxDepth + ", includeDevice = " + + "device ID = " + deviceId + ", maxDepth = " + maxDepth + ", includeDevice = " + includeDevice; log.error(msg); throw new DeviceOrganizationMgtPluginException(msg, e); } catch (DeviceOrganizationMgtDAOException e) { String msg = "Error occurred in the database level while retrieving parent devices for : " + - "device ID = " + node.getDeviceId() + ", maxDepth = " + maxDepth + ", includeDevice = " + + "device ID = " + deviceId + ", maxDepth = " + maxDepth + ", includeDevice = " + includeDevice; log.error(msg); throw new DeviceOrganizationMgtPluginException(msg, e); 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 940f5c2699..c00d834869 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 @@ -17,7 +17,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.DeviceNodeResult; 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; @@ -42,25 +42,25 @@ public interface DeviceOrganizationService { /** * Retrieves a list of child nodes of a given device node, up to a specified depth. * - * @param node The parent device node. + * @param deviceID The parent device ID. * @param maxDepth The maximum depth of child nodes to retrieve. * @param includeDevice Indicates whether to include device information in the retrieved nodes. * @return A list of child device nodes. * @throws DeviceOrganizationMgtPluginException If an error occurs during the operation. */ - List getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) + DeviceNodeResult getChildrenOfDeviceNode(int deviceID, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException; /** * Retrieves a list of parent nodes of a given device node, up to a specified depth. * - * @param node The child device node. + * @param deviceID The child device ID. * @param maxDepth The maximum depth of parent nodes to retrieve. * @param includeDevice Indicates whether to include device information in the retrieved nodes. * @return A list of parent device nodes. * @throws DeviceOrganizationMgtPluginException If an error occurs during the operation. */ - List getParentsOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice) + DeviceNodeResult getParentsOfDeviceNode(int deviceID, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException; /** @@ -73,6 +73,7 @@ public interface DeviceOrganizationService { /** * Retrieves device Organization Leafs + * * @return A list of leaf device organizations. * @throws DeviceOrganizationMgtPluginException */ @@ -80,6 +81,7 @@ public interface DeviceOrganizationService { /** * Retrieves device Organization Roots + * * @return A list of root device organizations. * @throws DeviceOrganizationMgtPluginException */ 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/DAOTest.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/DAOTest.java index 1cc75f2596..818a575181 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/DAOTest.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/DAOTest.java @@ -3,7 +3,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAOFactory; 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.DeviceNodeResult; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; 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; @@ -32,26 +32,22 @@ public class DAOTest extends BaseDeviceOrganizationTest { @Test(dependsOnMethods = "testAddDeviceOrganizationDAO") public void testGetChildrenOfDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException { ConnectionManagerUtil.openDBConnection(); - DeviceNode node = new DeviceNode(); - node.setDeviceId(2); + int deviceId = 2; int maxDepth = 4; boolean includeDevice = true; - List childrenList = deviceOrganizationDAO.getChildrenOfDeviceNode(node, maxDepth, includeDevice); + DeviceNodeResult childrenList = deviceOrganizationDAO.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice); ConnectionManagerUtil.closeDBConnection(); - log.info(childrenList.size()); Assert.assertNotNull(childrenList, "Cannot be null"); } @Test(dependsOnMethods = "testAddDeviceOrganizationDAO") public void testGetParentsOfDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException { ConnectionManagerUtil.openDBConnection(); - DeviceNode node = new DeviceNode(); - node.setDeviceId(4); + int deviceID = 4; int maxDepth = 4; boolean includeDevice = false; - List parentList = deviceOrganizationDAO.getParentsOfDeviceNode(node, maxDepth, includeDevice); + DeviceNodeResult parentList = deviceOrganizationDAO.getParentsOfDeviceNode(deviceID, maxDepth, includeDevice); ConnectionManagerUtil.closeDBConnection(); - log.info(parentList.size()); Assert.assertNotNull(parentList, "Cannot be null"); } 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/ServiceNegativeTest.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/ServiceNegativeTest.java index 8f04761255..40eb971bc9 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/ServiceNegativeTest.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/ServiceNegativeTest.java @@ -1,6 +1,5 @@ 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.exception.BadRequestException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; @@ -25,50 +24,50 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest { log.info("Service test initialized"); } - @Test(description = "This method tests Get Children Of method under negative circumstances with null data", + @Test(description = "This method tests Get Children Of method under negative circumstances with negative deviceId", expectedExceptions = {DeviceOrganizationMgtPluginException.class}) public void testGetChildrenOfWithInvalidInput() throws DeviceOrganizationMgtPluginException { - DeviceNode invalidNode = null; + int deviceId = -1; int maxDepth = -1; boolean includeDevice = true; - deviceOrganizationService.getChildrenOfDeviceNode(invalidNode, maxDepth, includeDevice); + deviceOrganizationService.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice); } - @Test(description = "This method tests Get Children Of method under negative circumstances with an invalid DeviceNode", + @Test(description = "This method tests Get Children Of method under negative circumstances with an invalid deviceId", expectedExceptions = {DeviceOrganizationMgtPluginException.class}) public void testGetChildrenOfWithInvalidDeviceNode() throws DeviceOrganizationMgtPluginException { - DeviceNode invalidNode = new DeviceNode(); // Provide an invalid DeviceNode + int deviceId = 0; int maxDepth = 2; boolean includeDevice = true; - deviceOrganizationService.getChildrenOfDeviceNode(invalidNode, maxDepth, includeDevice); + deviceOrganizationService.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice); } - @Test(description = "This method tests Get Parents Of method under negative circumstances with null data", + @Test(description = "This method tests Get Parents Of method under negative circumstances with invalid data", expectedExceptions = {DeviceOrganizationMgtPluginException.class}) public void testGetParentsOfWithInvalidInput() throws DeviceOrganizationMgtPluginException { - DeviceNode invalidNode = null; + int deviceID = 0; int maxDepth = -1; boolean includeDevice = true; - deviceOrganizationService.getParentsOfDeviceNode(invalidNode, maxDepth, includeDevice); + deviceOrganizationService.getParentsOfDeviceNode(deviceID, maxDepth, includeDevice); } - @Test(description = "This method tests Get Parents Of method under negative circumstances with an invalid DeviceNode" + @Test(description = "This method tests Get Parents Of method under negative circumstances with an invalid ID" , expectedExceptions = {DeviceOrganizationMgtPluginException.class}) public void testGetParentsOfWithInvalidDeviceNode() throws DeviceOrganizationMgtPluginException { - DeviceNode invalidNode = new DeviceNode(); // Provide an invalid DeviceNode + int deviceID = -2; int maxDepth = 2; boolean includeDevice = true; - deviceOrganizationService.getParentsOfDeviceNode(invalidNode, maxDepth, includeDevice); + deviceOrganizationService.getParentsOfDeviceNode(deviceID, maxDepth, includeDevice); } @Test(description = "This method tests Get Parents Of method under negative circumstances with an invalid DeviceNode" , expectedExceptions = {DeviceOrganizationMgtPluginException.class} ) public void testGetParentsOfWithNullDeviceNode() throws DeviceOrganizationMgtPluginException { - DeviceNode invalidNode = null; // Provide an invalid DeviceNode + int deviceID = -1; int maxDepth = 2; boolean includeDevice = true; - deviceOrganizationService.getParentsOfDeviceNode(invalidNode, maxDepth, includeDevice); + deviceOrganizationService.getParentsOfDeviceNode(deviceID, maxDepth, includeDevice); } 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 c7d4c77480..05514ade4b 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 @@ -1,6 +1,6 @@ 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.DeviceNodeResult; 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; @@ -31,12 +31,10 @@ public class ServiceTest extends BaseDeviceOrganizationTest { public void testGetChildrenOf() throws DeviceOrganizationMgtPluginException { boolean exists = deviceOrganizationService.isDeviceIdExist(17); if (exists) { - DeviceNode deviceNode = new DeviceNode(); - deviceNode.setDeviceId(17); + int deviceID = 17; int maxDepth = 10; - boolean includeDevice = true; - List childrenList = deviceOrganizationService.getChildrenOfDeviceNode(deviceNode, maxDepth, includeDevice); - + boolean includeDevice = false; + DeviceNodeResult childrenList = deviceOrganizationService.getChildrenOfDeviceNode(deviceID, maxDepth, includeDevice); Assert.assertNotNull(childrenList, "Cannot be null"); } } @@ -45,11 +43,10 @@ public class ServiceTest extends BaseDeviceOrganizationTest { public void testGetParentsOf() throws DeviceOrganizationMgtPluginException { boolean exists = deviceOrganizationService.isChildDeviceIdExist(20); if (exists) { - DeviceNode deviceNode = new DeviceNode(); - deviceNode.setDeviceId(20); + int deviceID = 20; int maxDepth = 3; boolean includeDevice = false; - List parentList = deviceOrganizationService.getParentsOfDeviceNode(deviceNode, maxDepth, includeDevice); + DeviceNodeResult parentList = deviceOrganizationService.getParentsOfDeviceNode(deviceID, maxDepth, includeDevice); Assert.assertNotNull(parentList, "Cannot be null"); } @@ -246,7 +243,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest { @Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent") public void testGetRootOrganizations() throws DeviceOrganizationMgtPluginException { - int offset = 0; + int offset = 0; int limit = 10; PaginationRequest request = new PaginationRequest(offset, limit); List organizations = deviceOrganizationService.getDeviceOrganizationRoots(request); @@ -263,7 +260,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest { @Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent") public void testGetLeafOrganizationsWithNullParents() throws DeviceOrganizationMgtPluginException { - int offset = 0; + int offset = 0; int limit = 10; PaginationRequest request = new PaginationRequest(offset, limit); List organizations = deviceOrganizationService.getDeviceOrganizationLeafs(request); @@ -279,7 +276,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest { @Test(dependsOnMethods = "testAddDeviceOrganization") public void testGetLeafOrganizations() throws DeviceOrganizationMgtPluginException { - int offset = 0; + int offset = 0; int limit = 10; PaginationRequest request = new PaginationRequest(offset, limit); List organizations = deviceOrganizationService.getDeviceOrganizationLeafs(request); diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index 928badb25d..1637374edc 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -856,12 +856,12 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INT NOT NULL AUTO_INCREMENT, + ORGANIZATION_ID INT NOT NULL AUTO_INCREMENT, DEVICE_ID INT(11) NOT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ORGANIZATION_ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_PARENT_DEVICE_DM_ID FOREIGN KEY (PARENT_DEVICE_ID) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql index c02493f4ab..a944be130a 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql @@ -20,12 +20,12 @@ -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INT NOT NULL AUTO_INCREMENT, + ORGANIZATION_ID INT NOT NULL AUTO_INCREMENT, DEVICE_ID INT(11) NOT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ORGANIZATION_ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_PARENT_DEVICE_DM_ID FOREIGN KEY (PARENT_DEVICE_ID) diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 0424d8315f..dd5e1884fd 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -843,12 +843,12 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INT NOT NULL AUTO_INCREMENT, + ORGANIZATION_ID INT NOT NULL AUTO_INCREMENT, DEVICE_ID INT(11) NOT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ORGANIZATION_ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_PARENT_DEVICE_DM_ID FOREIGN KEY (PARENT_DEVICE_ID) diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 4f06fc3611..65eebccc88 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -917,12 +917,12 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INT NOT NULL IDENTITY(1,1), + ORGANIZATION_ID INT NOT NULL IDENTITY(1,1), DEVICE_ID INT NOT NULL, PARENT_DEVICE_ID INT DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, LAST_UPDATED_TIMESTAMP BIGINT NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ORGANIZATION_ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_PARENT_DEVICE_DM_ID FOREIGN KEY (PARENT_DEVICE_ID) diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index ea8391bee9..e4ff1dc305 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -910,12 +910,12 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INT NOT NULL AUTO_INCREMENT, + ORGANIZATION_ID INT NOT NULL AUTO_INCREMENT, DEVICE_ID INT(11) NOT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ORGANIZATION_ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_PARENT_DEVICE_DM_ID FOREIGN KEY (PARENT_DEVICE_ID) diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 49430128cf..828435236d 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -1197,7 +1197,7 @@ CREATE SEQUENCE SUB_OPERATION_TEMPLATE_seq START WITH 1 INCREMENT BY 1; -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID NUMBER(10) NOT NULL, + ORGANIZATION_ID NUMBER(10) NOT NULL, DEVICE_ID NUMBER(10) NOT NULL, PARENT_DEVICE_ID NUMBER(10) DEFAULT NULL, DEVICE_ORGANIZATION_META CLOB DEFAULT NULL, @@ -1219,10 +1219,10 @@ BEFORE INSERT ON DM_DEVICE_ORGANIZATION REFERENCING NEW AS NEW FOR EACH ROW -WHEN (NEW.ID IS NULL) +WHEN (NEW.ORGANIZATION_ID IS NULL) BEGIN SELECT DM_DEVICE_ORGANIZATION_seq.NEXTVAL -INTO :NEW.ID +INTO :NEW.ORGANIZATION_ID FROM DUAL; END; / diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 57bfdbad62..52e20e467d 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -850,12 +850,12 @@ CREATE TABLE DM_DEVICE_CERTIFICATE ( CREATE SEQUENCE DM_DEVICE_ORGANIZATION_seq; CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INTEGER DEFAULT NEXTVAL ('DM_DEVICE_ORGANIZATION_seq') NOT NULL, + ORGANIZATION_ID INTEGER DEFAULT NEXTVAL ('DM_DEVICE_ORGANIZATION_seq') NOT NULL, DEVICE_ID INTEGER NOT NULL, PARENT_DEVICE_ID INTEGER DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP(0) NOT NULL NOT NULL, - PRIMARY KEY (ID), + PRIMARY KEY (ORGANIZATION_ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_PARENT_DEVICE_DM_ID FOREIGN KEY (PARENT_DEVICE_ID)