device organization scripts added to device mgt basics feature

pull/238/head
Isuri Mendis 1 year ago
parent 4bd5b7d223
commit 34da115a53

@ -135,16 +135,16 @@
<artifactId>org.eclipse.osgi.services</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.logging</artifactId>
<scope>provided</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.wso2.carbon</groupId>-->
<!-- <artifactId>org.wso2.carbon.core</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.wso2.carbon</groupId>-->
<!-- <artifactId>org.wso2.carbon.logging</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>

@ -77,6 +77,17 @@ public interface DeviceOrganizationDAO {
*/
boolean organizationExists(int deviceId, int parentDeviceId) throws DeviceOrganizationMgtDAOException;
/**
* Get a device organization by the CHILD_PARENT_COMP_KEY unique key.
*
* @param deviceId The ID of the child device.
* @param parentDeviceId The ID of the parent device.
* @return The DeviceOrganization object if found, null otherwise.
* @throws DeviceOrganizationMgtDAOException if an error occurs while accessing the database.
*/
DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, int parentDeviceId)
throws DeviceOrganizationMgtDAOException;
/**
* Updates a record in the device organization table with the provided information.
*

@ -273,6 +273,40 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
}
}
/**
* {@inheritDoc}
*/
public DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, int parentDeviceId)
throws DeviceOrganizationMgtDAOException {
try {
String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID = ?";
Connection conn = ConnectionManagerUtil.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceId);
stmt.setInt(2, parentDeviceId);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return loadDeviceOrganization(rs);
}
}
}
return null; // No matching device organization found.
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to get device organization for DEVICE_ID " +
deviceId + " and PARENT_DEVICE_ID " + parentDeviceId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to get device organization for DEVICE_ID " +
deviceId + " and PARENT_DEVICE_ID " + parentDeviceId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}
}
/**
* {@inheritDoc}
*/

@ -189,7 +189,7 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
@Override
public void addDeviceOrganizationList(List<DeviceOrganization> deviceOrganizationList)
throws DeviceOrganizationMgtPluginException {
for (DeviceOrganization deviceOrganization : deviceOrganizationList){
for (DeviceOrganization deviceOrganization : deviceOrganizationList) {
boolean result = addDeviceOrganization(deviceOrganization);
}
}
@ -217,6 +217,30 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
}
/**
* {@inheritDoc}
*/
@Override
public DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceID, int parentDeviceID)
throws DeviceOrganizationMgtPluginException {
try {
ConnectionManagerUtil.openDBConnection();
return deviceOrganizationDao.getDeviceOrganizationByUniqueKey(deviceID, parentDeviceID);
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve organization. " +
"Params : deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred while retrieving device organization for deviceID " +
deviceID + " and parentDeviceID " + parentDeviceID;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
/**
* {@inheritDoc}
*/

@ -100,6 +100,17 @@ public interface DeviceOrganizationService {
boolean organizationExists(int deviceId, int parentDeviceId)
throws DeviceOrganizationMgtPluginException;
/**
* Retrieve a device organization by its unique key (deviceId and parentDeviceId).
*
* @param deviceId The ID of the device.
* @param parentDeviceId The ID of the parent device.
* @return The retrieved DeviceOrganization object, or null if not found.
* @throws DeviceOrganizationMgtPluginException If an error occurs.
*/
DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, int parentDeviceId)
throws DeviceOrganizationMgtPluginException;
/**
* Checks whether a record with the specified device ID exists either in the deviceID column or
* parentDeviceID column in the device organization table.

@ -58,6 +58,14 @@ public class DAOTest extends BaseDeviceOrganizationTest {
@Test
public void testAddDeviceOrganizationDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
deviceOrganizationDAO.deleteDeviceAssociations(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
ConnectionManagerUtil.beginDBTransaction();
deviceOrganizationDAO.deleteDeviceAssociations(2);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(2);

@ -66,12 +66,18 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
deviceOrganizationTwo.setDeviceId(4);
deviceOrganizationTwo.setParentDeviceId(2);
deviceOrganizationService.deleteDeviceAssociations(4);
deviceOrganizationService.deleteDeviceAssociations(3);
boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 3);
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne);
DeviceOrganization organization1 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(3, 2);
boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationTwo);
Assert.assertTrue(result);
Assert.assertTrue(result1);
Assert.assertTrue(result2);
DeviceOrganization organization2 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 2);
Assert.assertNotNull(organization);
Assert.assertNotNull(organization1);
Assert.assertNotNull(organization2);
}
@ -83,6 +89,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
deviceOrganization.setParentDeviceId(3);
deviceOrganization.setOrganizationId(1);
boolean result = deviceOrganizationService.updateDeviceOrganization(deviceOrganization);
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 3);
}
@Test(priority = 2, dependsOnMethods = "testAddDeviceOrganization")
@ -94,7 +101,6 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
@Test(priority = 3, dependsOnMethods = "testAddDeviceOrganization")
public void testDoesDeviceIdExist() throws DeviceOrganizationMgtPluginException {
boolean deviceIdExist = deviceOrganizationService.doesDeviceIdExist(4);
Assert.assertTrue(deviceIdExist);
}
@Test(priority = 7, dependsOnMethods = "testAddDeviceOrganization")
@ -105,20 +111,31 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
@Test(priority = 8, dependsOnMethods = "testAddDeviceOrganization")
public void testDeleteDeviceAssociations() throws DeviceOrganizationMgtPluginException {
boolean rs = deviceOrganizationService.deleteDeviceAssociations(4);
Assert.assertTrue(rs);
}
@Test(priority = 9, dependsOnMethods = "testAddDeviceOrganization")
public void testGetAllOrganizations() throws DeviceOrganizationMgtPluginException {
List<DeviceOrganization> organizations = deviceOrganizationService.getAllDeviceOrganizations();
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("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(priority = 10, dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganizationByUniqueKey() throws DeviceOrganizationMgtPluginException {
int deviceID = 3;
int parentDeviceID = 2;
DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceID, parentDeviceID);
Assert.assertNotNull(organization, "Organization should not be null");
Assert.assertEquals(organization.getDeviceId(), deviceID, "Device ID should match");
Assert.assertEquals(organization.getParentDeviceId().intValue(), parentDeviceID, "Parent Device ID should match");
}
}

@ -18,30 +18,29 @@
-- Table `DM_DEVICE_ORGANIZATION`
-- -----------------------------------------------------
-- DM_DEVICE_ORGANIZATION TABLE--
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
ID INTEGER auto_increment NOT NULL,
DEVICE_ID INT(11) NOT NULL,
PARENT_DEVICE_ID INT(11) DEFAULT NULL,
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT(11) NOT NULL,
PARENT_DEVICE_ID INT(11) DEFAULT NULL,
LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- END OF DM_DEVICE_ORGANIZATION TABLE--
-- -----------------------------------------------------
-- Sample data for test cases
-- -----------------------------------------------------
INSERT INTO DM_DEVICE_TYPE (ID,NAME,DEVICE_TYPE_META,LAST_UPDATED_TIMESTAMP,PROVIDER_TENANT_ID,SHARED_WITH_ALL_TENANTS)
VALUES (1,'METER','meter',CURRENT_TIMESTAMP(),1,true);
INSERT INTO DM_DEVICE (ID,DESCRIPTION,NAME,DEVICE_TYPE_ID,DEVICE_IDENTIFICATION,LAST_UPDATED_TIMESTAMP,TENANT_ID) VALUES
(1,'test device 1','Meter 01',1,'0001',CURRENT_TIMESTAMP(),1),
(2,'test device 2','Meter 02',1,'0002',CURRENT_TIMESTAMP(),1),
(3,'test device 3','Meter 03',1,'0003',CURRENT_TIMESTAMP(),1),
(4,'test device 4','Meter 04',1,'0004',CURRENT_TIMESTAMP(),1),
-- INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP) VALUES
-- (2,1,CURRENT_TIMESTAMP()),
-- (3,2,CURRENT_TIMESTAMP());
INSERT INTO DM_DEVICE_TYPE (NAME,DEVICE_TYPE_META,LAST_UPDATED_TIMESTAMP,PROVIDER_TENANT_ID,SHARED_WITH_ALL_TENANTS)
VALUES ('METER','meter',CURRENT_TIMESTAMP(),1,true);
INSERT INTO DM_DEVICE (DESCRIPTION,NAME,DEVICE_TYPE_ID,DEVICE_IDENTIFICATION,LAST_UPDATED_TIMESTAMP,TENANT_ID) VALUES
('test device 1','Meter_01',1,'0001',CURRENT_TIMESTAMP(),1),
('test device 2','Meter_02',1,'0002',CURRENT_TIMESTAMP(),1),
('test device 3','Meter_03',1,'0003',CURRENT_TIMESTAMP(),1),
('test device 4','Meter_04',1,'0004',CURRENT_TIMESTAMP(),1);

@ -839,3 +839,18 @@ CREATE TABLE SUB_OPERATION_TEMPLATE (
);
-- END OF SUB_OPERATION_TEMPLATE TABLE--
-- DM_DEVICE_ORGANIZATION TABLE--
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT(11) NOT NULL,
PARENT_DEVICE_ID INT(11) DEFAULT NULL,
LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL,
PRIMARY KEY (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_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- END OF DM_DEVICE_ORGANIZATION TABLE--

@ -911,4 +911,19 @@ CREATE TABLE SUB_OPERATION_TEMPLATE (
CONSTRAINT fk_SUB_OPERATION_TEMPLATE_DM_DEVICE_SUB_TYPE FOREIGN KEY (SUB_TYPE_ID, DEVICE_TYPE) REFERENCES DM_DEVICE_SUB_TYPE (SUB_TYPE_ID, DEVICE_TYPE)
);
-- END SUB_OPERATION_TEMPLATE TABLE--
-- END SUB_OPERATION_TEMPLATE TABLE--
-- DM_DEVICE_ORGANIZATION TABLE--
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
ID INT NOT NULL IDENTITY(1,1),
DEVICE_ID INT NOT NULL,
PARENT_DEVICE_ID INT DEFAULT NULL,
LAST_UPDATED_TIMESTAMP BIGINT NOT NULL,
PRIMARY KEY (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_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- END OF DM_DEVICE_ORGANIZATION TABLE--

@ -907,3 +907,18 @@ CREATE TABLE SUB_OPERATION_TEMPLATE (
) ENGINE=InnoDB;
-- END OF SUB_OPERATION_TEMPLATE TABLE--
-- DM_DEVICE_ORGANIZATION TABLE--
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT(11) NOT NULL,
PARENT_DEVICE_ID INT(11) DEFAULT NULL,
LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL,
PRIMARY KEY (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_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- END OF DM_DEVICE_ORGANIZATION TABLE--

@ -1191,5 +1191,38 @@ ALTER TABLE SUB_OPERATION_TEMPLATE
-- Generate ID using sequence and trigger
CREATE SEQUENCE SUB_OPERATION_TEMPLATE_seq START WITH 1 INCREMENT BY 1;
/
-- END OF SUB_OPERATION_TEMPLATE TABLE--
-- DM_DEVICE_ORGANIZATION TABLE--
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
ID NUMBER(10) NOT NULL,
DEVICE_ID NUMBER(10) NOT NULL,
PARENT_DEVICE_ID NUMBER(10) DEFAULT NULL,
LAST_UPDATED_TIMESTAMP TIMESTAMP(0) NOT NULL,
CONSTRAINT PK_DM_DEVICE_ORGANIZATION PRIMARY KEY (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_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
/
-- Generate ID using sequence and trigger
CREATE SEQUENCE DM_DEVICE_ORGANIZATION_seq START WITH 1 INCREMENT BY 1 NOCACHE
/
CREATE OR REPLACE TRIGGER DM_DEVICE_ORGANIZATION_seq_tr
BEFORE INSERT
ON DM_DEVICE_ORGANIZATION
REFERENCING NEW AS NEW
FOR EACH ROW
WHEN (NEW.ID IS NULL)
BEGIN
SELECT DM_DEVICE_ORGANIZATION_seq.NEXTVAL
INTO :NEW.ID
FROM DUAL;
END;
/
-- END OF DM_DEVICE_ORGANIZATION TABLE--
)

@ -846,3 +846,19 @@ CREATE TABLE DM_DEVICE_CERTIFICATE (
PRIMARY KEY (ID)
)
-- DM_DEVICE_ORGANIZATION TABLE--
CREATE SEQUENCE DM_DEVICE_ORGANIZATION_seq;
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
ID INTEGER DEFAULT NEXTVAL ('DM_DEVICE_ORGANIZATION_seq') NOT NULL,
DEVICE_ID INTEGER NOT NULL,
PARENT_DEVICE_ID INTEGER DEFAULT NULL,
LAST_UPDATED_TIMESTAMP TIMESTAMP(0) NOT NULL NOT NULL,
PRIMARY KEY (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_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID)
);
-- END OF DM_DEVICE_ORGANIZATION TABLE--

Loading…
Cancel
Save