get parent and children fixes

pull/238/head
Isuri Mendis 12 months ago
parent 1010baf33a
commit 02f4b4faf3

@ -35,6 +35,7 @@ import io.swagger.annotations.Tag;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
@ -61,7 +62,7 @@ import javax.ws.rs.core.Response;
} }
), ),
tags = { tags = {
@Tag(name = "device_organization_management, device_management", description = "Device organization " + @Tag(name = "device_organization_management", description = "Device organization " +
"management related REST-API. This can be used to manipulate device organization related details.") "management related REST-API. This can be used to manipulate device organization related details.")
} }
) )
@ -347,14 +348,14 @@ public interface DeviceOrganizationMgtService {
value = "The starting pagination index for the complete list of qualified items", value = "The starting pagination index for the complete list of qualified items",
required = false, required = false,
defaultValue = "0") defaultValue = "0")
@QueryParam("offset") @DefaultValue("0") @QueryParam("offset")
int offset, int offset,
@ApiParam( @ApiParam(
name = "limit", name = "limit",
value = "Provide how many policy details you require from the starting pagination index/offset.", value = "Provide how many policy details you require from the starting pagination index/offset.",
required = false, required = false,
defaultValue = "5") defaultValue = "5")
@QueryParam("limit") @DefaultValue("20") @QueryParam("limit")
int limit); int limit);
/** /**
@ -415,14 +416,14 @@ public interface DeviceOrganizationMgtService {
value = "The starting pagination index for the complete list of qualified items", value = "The starting pagination index for the complete list of qualified items",
required = false, required = false,
defaultValue = "0") defaultValue = "0")
@QueryParam("offset") @DefaultValue("0") @QueryParam("offset")
int offset, int offset,
@ApiParam( @ApiParam(
name = "limit", name = "limit",
value = "Provide how many policy details you require from the starting pagination index/offset.", value = "Provide how many policy details you require from the starting pagination index/offset.",
required = false, required = false,
defaultValue = "5") defaultValue = "5")
@QueryParam("limit") @DefaultValue("20") @QueryParam("limit")
int limit); int limit);
/** /**

@ -30,6 +30,7 @@ import org.apache.commons.logging.LogFactory;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
@ -107,10 +108,10 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe
@GET @GET
@Override @Override
@Path("/leafs") @Path("leafs")
public Response getDeviceOrganizationLeafs( public Response getDeviceOrganizationLeafs(
@QueryParam("offset") int offset, @DefaultValue("0") @QueryParam("offset") int offset,
@QueryParam("limit") int limit) { @DefaultValue("20") @QueryParam("limit") int limit) {
RequestValidationUtil.validatePaginationParameters(offset, limit); RequestValidationUtil.validatePaginationParameters(offset, limit);
try { try {
DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService();
@ -123,11 +124,11 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe
} }
@GET @GET
@Path("/roots") @Path("roots")
@Override @Override
public Response getDeviceOrganizationRoots( public Response getDeviceOrganizationRoots(
@QueryParam("offset") int offset, @DefaultValue("0") @QueryParam("offset") int offset,
@QueryParam("limit") int limit) { @DefaultValue("20") @QueryParam("limit") int limit) {
RequestValidationUtil.validatePaginationParameters(offset, limit); RequestValidationUtil.validatePaginationParameters(offset, limit);
try { try {
DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService();

@ -126,6 +126,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
"JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " + "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " +
"WHERE DO.PARENT_DEVICE_ID = ?"; "WHERE DO.PARENT_DEVICE_ID = ?";
boolean hasChildren = false;
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, node.getDeviceId()); stmt.setInt(1, node.getDeviceId());
@ -133,6 +134,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
while (rs.next()) { while (rs.next()) {
DeviceNode child = getDeviceFromResultSet(rs); DeviceNode child = getDeviceFromResultSet(rs);
node.getChildren().add(child); node.getChildren().add(child);
hasChildren = true;
if (includeDevice if (includeDevice
&& !parentAdded && !parentAdded
) { ) {
@ -157,6 +159,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
} }
} }
} }
// Add the parent node if it doesn't have children and includeDevice is true
if (!hasChildren && includeDevice && !parentAdded) {
childNodes.add(node);
parentAdded = true;
}
} }
/** /**

@ -33,7 +33,19 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
if (exists) { if (exists) {
int deviceID = 17; int deviceID = 17;
int maxDepth = 10; int maxDepth = 10;
boolean includeDevice = false; boolean includeDevice = true;
DeviceNodeResult childrenList = deviceOrganizationService.getChildrenOfDeviceNode(deviceID, maxDepth, includeDevice);
Assert.assertNotNull(childrenList, "Cannot be null");
}
}
@Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent")
public void testGetChildrenOfWithOneParent() throws DeviceOrganizationMgtPluginException {
boolean exists = deviceOrganizationService.isDeviceIdExist(3);
if (exists) {
int deviceID = 3;
int maxDepth = 4;
boolean includeDevice = true;
DeviceNodeResult childrenList = deviceOrganizationService.getChildrenOfDeviceNode(deviceID, maxDepth, includeDevice); DeviceNodeResult childrenList = deviceOrganizationService.getChildrenOfDeviceNode(deviceID, maxDepth, includeDevice);
Assert.assertNotNull(childrenList, "Cannot be null"); Assert.assertNotNull(childrenList, "Cannot be null");
} }

@ -45,6 +45,7 @@
<module>io.entgra.device.mgt.core.device.mgt.extensions.stateengine</module> <module>io.entgra.device.mgt.core.device.mgt.extensions.stateengine</module>
<module>io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper</module> <module>io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper</module>
<module>io.entgra.device.mgt.core.device.mgt.extensions.device.organization</module> <module>io.entgra.device.mgt.core.device.mgt.extensions.device.organization</module>
<module>io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api</module>
</modules> </modules>
</project> </project>

Loading…
Cancel
Save