diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceInfo.java index dc6bcd886a..92af0d4544 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceInfo.java @@ -22,6 +22,7 @@ package org.wso2.carbon.device.mgt.common.device.details; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import java.io.Serializable; +import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -51,6 +52,7 @@ public class DeviceInfo implements Serializable { private Double totalRAMMemory; private Double availableRAMMemory; private boolean pluggedIn; + private Date updatedTime; private Map deviceDetailsMap = new HashMap<>(); @@ -290,6 +292,17 @@ public class DeviceInfo implements Serializable { this.pluggedIn = pluggedIn; } + public Date getUpdatedTime() { + if(updatedTime.equals(null)){ + updatedTime = new Date(); + } + return updatedTime; + } + + public void setUpdatedTime(Date updatedTime) { + this.updatedTime = updatedTime; + } + public void setDeviceDetailsMap(Map deviceDetailsMap) { this.deviceDetailsMap = deviceDetailsMap; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceLocation.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceLocation.java index 0b05406522..1368b92806 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceLocation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceLocation.java @@ -22,6 +22,7 @@ package org.wso2.carbon.device.mgt.common.device.details; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import java.io.Serializable; +import java.util.Date; public class DeviceLocation implements Serializable { @@ -39,6 +40,7 @@ public class DeviceLocation implements Serializable { private String state; private String zip; private String country; + private Date updatedTime; public int getDeviceId() { return deviceId; @@ -119,5 +121,16 @@ public class DeviceLocation implements Serializable { public void setCountry(String country) { this.country = country; } + + public Date getUpdatedTime() { + if(updatedTime.equals(null)){ + updatedTime = new Date(); + } + return updatedTime; + } + + public void setUpdatedTime(Date updatedTime) { + this.updatedTime = updatedTime; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index cd901b2238..3b392cc820 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -50,8 +50,8 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_DETAIL (DEVICE_ID, DEVICE_MODEL, " + "VENDOR, OS_VERSION, BATTERY_LEVEL, INTERNAL_TOTAL_MEMORY, INTERNAL_AVAILABLE_MEMORY, " + "EXTERNAL_TOTAL_MEMORY, EXTERNAL_AVAILABLE_MEMORY, CONNECTION_TYPE, " + - "SSID, CPU_USAGE, TOTAL_RAM_MEMORY, AVAILABLE_RAM_MEMORY, PLUGGED_IN) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + "SSID, CPU_USAGE, TOTAL_RAM_MEMORY, AVAILABLE_RAM_MEMORY, PLUGGED_IN, UPDATE_TIMESTAMP) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); stmt.setInt(1, deviceInfo.getDeviceId()); stmt.setString(2, deviceInfo.getDeviceModel()); @@ -68,6 +68,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { stmt.setDouble(13, deviceInfo.getTotalRAMMemory()); stmt.setDouble(14, deviceInfo.getAvailableRAMMemory()); stmt.setBoolean(15, deviceInfo.isPluggedIn()); + stmt.setLong(16, System.currentTimeMillis()); stmt.execute(); @@ -144,6 +145,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY")); deviceInfo.setAvailableRAMMemory(rs.getDouble("AVAILABLE_RAM_MEMORY")); deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN")); + deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); } deviceInfo.setDeviceId(deviceId); @@ -226,7 +228,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { try { conn = this.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_LOCATION (DEVICE_ID, LATITUDE, LONGITUDE, STREET1, " + - "STREET2, CITY, ZIP, STATE, COUNTRY) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); + "STREET2, CITY, ZIP, STATE, COUNTRY, UPDATE_TIMESTAMP) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); stmt.setInt(1, deviceLocation.getDeviceId()); stmt.setDouble(2, deviceLocation.getLatitude()); stmt.setDouble(3, deviceLocation.getLongitude()); @@ -236,6 +238,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { stmt.setString(7, deviceLocation.getZip()); stmt.setString(8, deviceLocation.getState()); stmt.setString(9, deviceLocation.getCountry()); + stmt.setLong(10, System.currentTimeMillis()); stmt.execute(); } catch (SQLException e) { throw new DeviceDetailsMgtDAOException("Error occurred while adding the device location to database.", e); @@ -268,6 +271,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { location.setZip(rs.getString("ZIP")); location.setState(rs.getString("STATE")); location.setCountry(rs.getString("COUNTRY")); + location.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); } location.setDeviceId(deviceId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 5281bed162..6ea7adbf59 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -613,7 +613,7 @@ public class OperationManagerImpl implements OperationManager { public Operation getOperation(int operationId) throws OperationManagementException { Operation operation; try { - OperationManagementDAOFactory.getConnection(); + OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. getOperation(operationId); if (dtoOperation == null) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/Processor.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/Processor.java index 88affab477..4c7058be02 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/Processor.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/Processor.java @@ -28,4 +28,6 @@ import java.util.List; public interface Processor { List execute(SearchContext searchContext) throws SearchMgtException; + + List getUpdatedDevices(long epochTime) throws SearchMgtException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryBuilder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryBuilder.java index db028edc19..c3e912fcb8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryBuilder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/QueryBuilder.java @@ -38,4 +38,6 @@ public interface QueryBuilder { List processORProperties(List conditions) throws InvalidOperatorException; + String processUpdatedDevices(long epochTime) throws InvalidOperatorException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/SearchManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/SearchManagerService.java index ad78ca171f..554ecfc0f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/SearchManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/SearchManagerService.java @@ -27,5 +27,7 @@ import java.util.List; public interface SearchManagerService { List search(SearchContext searchContext) throws SearchMgtException; + + List getUpdated(long epochTime) throws SearchMgtException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java index e80da38167..caf6f1929a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/dao/impl/SearchDAOImpl.java @@ -78,17 +78,14 @@ public class SearchDAOImpl implements SearchDAO { deviceInfo.setDeviceModel(rs.getString("DEVICE_MODEL")); deviceInfo.setExternalAvailableMemory(rs.getDouble("EXTERNAL_AVAILABLE_MEMORY")); deviceInfo.setExternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); -// deviceInfo.setIMEI(rs.getString("IMEI")); -// deviceInfo.setIMSI(rs.getString("IMSI")); deviceInfo.setInternalAvailableMemory(rs.getDouble("INTERNAL_AVAILABLE_MEMORY")); deviceInfo.setInternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); -// deviceInfo.setMobileSignalStrength(rs.getDouble("MOBILE_SIGNAL_STRENGTH")); -// deviceInfo.setOperator(rs.getString("OPERATOR")); deviceInfo.setOsVersion(rs.getString("OS_VERSION")); deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN")); deviceInfo.setSsid(rs.getString("SSID")); deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY")); deviceInfo.setVendor(rs.getString("VENDOR")); + deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); DeviceLocation deviceLocation = new DeviceLocation(); deviceLocation.setLatitude(rs.getDouble("LATITUDE")); @@ -100,6 +97,7 @@ public class SearchDAOImpl implements SearchDAO { deviceLocation.setZip(rs.getString("ZIP")); deviceLocation.setCountry(rs.getString("COUNTRY")); deviceLocation.setDeviceId(rs.getInt("ID")); + deviceLocation.setUpdatedTime(new java.util.Date(rs.getLong("DL_UPDATED_TIMESTAMP"))); DeviceWrapper wrapper = new DeviceWrapper(); wrapper.setDevice(device); @@ -160,17 +158,14 @@ public class SearchDAOImpl implements SearchDAO { deviceInfo.setDeviceModel(rs.getString("DEVICE_MODEL")); deviceInfo.setExternalAvailableMemory(rs.getDouble("EXTERNAL_AVAILABLE_MEMORY")); deviceInfo.setExternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); -// deviceInfo.setIMEI(rs.getString("IMEI")); -// deviceInfo.setIMSI(rs.getString("IMSI")); deviceInfo.setInternalAvailableMemory(rs.getDouble("INTERNAL_AVAILABLE_MEMORY")); deviceInfo.setInternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); -// deviceInfo.setMobileSignalStrength(rs.getDouble("MOBILE_SIGNAL_STRENGTH")); -// deviceInfo.setOperator(rs.getString("OPERATOR")); deviceInfo.setOsVersion(rs.getString("OS_VERSION")); deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN")); deviceInfo.setSsid(rs.getString("SSID")); deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY")); deviceInfo.setVendor(rs.getString("VENDOR")); + deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); DeviceLocation deviceLocation = new DeviceLocation(); deviceLocation.setLatitude(rs.getDouble("LATITUDE")); @@ -182,6 +177,7 @@ public class SearchDAOImpl implements SearchDAO { deviceLocation.setZip(rs.getString("ZIP")); deviceLocation.setCountry(rs.getString("COUNTRY")); deviceLocation.setDeviceId(rs.getInt("ID")); + deviceLocation.setUpdatedTime(new java.util.Date(rs.getLong("DL_UPDATED_TIMESTAMP"))); DeviceWrapper wrapper = new DeviceWrapper(); wrapper.setDevice(device); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java index 4a97f1fdbe..55b377d4f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java @@ -94,6 +94,28 @@ public class ProcessorImpl implements Processor { return aggregator.aggregate(deviceWrappers); } + @Override + public List getUpdatedDevices(long epochTime) throws SearchMgtException { + + if((1 + (int)Math.floor(Math.log10(epochTime))) <=10 ) { + epochTime = epochTime * 1000; + } + QueryBuilder queryBuilder = new QueryBuilderImpl(); + try { + String query = queryBuilder.processUpdatedDevices(epochTime); + DeviceManagementDAOFactory.openConnection(); + return searchDAO.searchDeviceDetailsTable(query); + } catch (InvalidOperatorException e) { + throw new SearchMgtException("Invalid operator was provided, so cannot execute the search.", e); + } catch (SQLException e) { + throw new SearchMgtException("Error occurred while managing database transactions.", e); + } catch (SearchDAOException e) { + throw new SearchMgtException("Error occurred while running the search operations for given time.", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + private List processANDSearch(List> deLists) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java index eaca48451f..60b46271c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/QueryBuilderImpl.java @@ -158,6 +158,12 @@ public class QueryBuilderImpl implements QueryBuilder { return this.getQueryList(conditions); } + @Override + public String processUpdatedDevices(long epochTime) throws InvalidOperatorException { + return this.getGenericQueryPart() + " AND ( DD.UPDATE_TIMESTAMP > " + epochTime + + " OR DL.UPDATE_TIMESTAMP > " + epochTime + " )"; + } + private List getQueryList(List conditions) { List queryList = new ArrayList<>(); for (Condition con : conditions) { @@ -189,8 +195,9 @@ public class QueryBuilderImpl implements QueryBuilder { "DD.OS_VERSION, DD.BATTERY_LEVEL, DD.INTERNAL_TOTAL_MEMORY, DD.INTERNAL_AVAILABLE_MEMORY,\n" + "DD.EXTERNAL_TOTAL_MEMORY, DD.EXTERNAL_AVAILABLE_MEMORY, DD.CONNECTION_TYPE, \n" + "DD.SSID, DD.CPU_USAGE, DD.TOTAL_RAM_MEMORY, DD.AVAILABLE_RAM_MEMORY, \n" + - "DD.PLUGGED_IN, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + - "DL.STATE, DL.COUNTRY FROM DM_DEVICE_DETAIL AS DD, DM_DEVICE AS D, DM_DEVICE_LOCATION AS DL, " + + "DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + + "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP " + + "FROM DM_DEVICE_DETAIL AS DD, DM_DEVICE AS D, DM_DEVICE_LOCATION AS DL, " + "DM_DEVICE_TYPE AS DT WHERE DEVICE_TYPE_ID=D.DEVICE_TYPE_ID AND D.TENANT_ID = " + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -205,8 +212,9 @@ public class QueryBuilderImpl implements QueryBuilder { "DD.OS_VERSION, DD.BATTERY_LEVEL, DD.INTERNAL_TOTAL_MEMORY, DD.INTERNAL_AVAILABLE_MEMORY,\n" + "DD.EXTERNAL_TOTAL_MEMORY, DD.EXTERNAL_AVAILABLE_MEMORY, DD.CONNECTION_TYPE, \n" + "DD.SSID, DD.CPU_USAGE, DD.TOTAL_RAM_MEMORY, DD.AVAILABLE_RAM_MEMORY, \n" + - "DD.PLUGGED_IN, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + - "DL.STATE, DL.COUNTRY, DI.KEY_FIELD, DI.VALUE_FIELD FROM DM_DEVICE_DETAIL AS DD, " + + "DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + + "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, " + + "DI.KEY_FIELD, DI.VALUE_FIELD FROM DM_DEVICE_DETAIL AS DD, " + "DM_DEVICE AS D, DM_DEVICE_LOCATION AS DL, \n" + "DM_DEVICE_INFO AS DI, DM_DEVICE_TYPE AS DT WHERE DEVICE_TYPE_ID=D.DEVICE_TYPE_ID AND D.TENANT_ID = " + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/SearchManagerServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/SearchManagerServiceImpl.java index 888f3edc36..cc9a51d919 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/SearchManagerServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/SearchManagerServiceImpl.java @@ -40,5 +40,10 @@ public class SearchManagerServiceImpl implements SearchManagerService { public List search(SearchContext searchContext) throws SearchMgtException { return processor.execute(searchContext); } + + @Override + public List getUpdated(long epochTime) throws SearchMgtException { + return processor.getUpdatedDevices(epochTime); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index 23a88af804..7d6159a87f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -451,6 +451,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( ZIP VARCHAR(10) NULL, STATE VARCHAR(45) NULL, COUNTRY VARCHAR(45) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_LOCATION_DEVICE FOREIGN KEY (DEVICE_ID) @@ -477,6 +478,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( TOTAL_RAM_MEMORY DECIMAL(30,3) NULL, AVAILABLE_RAM_MEMORY DECIMAL(30,3) NULL, PLUGGED_IN INT(1) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 23a88af804..7d6159a87f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -451,6 +451,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( ZIP VARCHAR(10) NULL, STATE VARCHAR(45) NULL, COUNTRY VARCHAR(45) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_LOCATION_DEVICE FOREIGN KEY (DEVICE_ID) @@ -477,6 +478,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( TOTAL_RAM_MEMORY DECIMAL(30,3) NULL, AVAILABLE_RAM_MEMORY DECIMAL(30,3) NULL, PLUGGED_IN INT(1) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 3067b30030..deabce4756 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -451,6 +451,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( ZIP VARCHAR(10) NULL, STATE VARCHAR(45) NULL, COUNTRY VARCHAR(45) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_LOCATION_DEVICE FOREIGN KEY (DEVICE_ID) @@ -477,6 +478,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( TOTAL_RAM_MEMORY DECIMAL(30,3) NULL, AVAILABLE_RAM_MEMORY DECIMAL(30,3) NULL, PLUGGED_IN INT(1) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index 69989f5168..6101c9e5a8 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -341,6 +341,24 @@ CREATE TABLE DM_POLICY_COMPLIANCE_FEATURES ( ON UPDATE NO ACTION ); +CREATE TABLE DM_DEVICE_GROUP_POLICY ( + ID INT NOT NULL IDENTITY, + DEVICE_GROUP_ID INT NOT NULL, + POLICY_ID INT NOT NULL, + TENANT_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_DEVICE_GROUP_POLICY + FOREIGN KEY (DEVICE_GROUP_ID) + REFERENCES DM_GROUP (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_DEVICE_GROUP_DM_POLICY + FOREIGN KEY (POLICY_ID , DEVICE_GROUP_ID) + REFERENCES DM_POLICY (ID , ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + CREATE TABLE DM_APPLICATION ( ID INTEGER IDENTITY NOT NULL, NAME VARCHAR(150) NOT NULL, @@ -390,13 +408,12 @@ CREATE TABLE DM_NOTIFICATION ( DROP TABLE IF EXISTS DM_DEVICE_INFO; -CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( - ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INTEGER NULL, +CREATE TABLE DM_DEVICE_INFO ( + ID INTEGER IDENTITY NOT NULL, + DEVICE_ID INT NULL, KEY_FIELD VARCHAR(45) NULL, VALUE_FIELD VARCHAR(100) NULL, PRIMARY KEY (ID), - INDEX DM_DEVICE_INFO_DEVICE_idx (DEVICE_ID ASC), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) @@ -404,23 +421,23 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( ON UPDATE NO ACTION ); - +CREATE INDEX DM_DEVICE_INFO_DEVICE_idx ON DM_DEVICE_INFO (DEVICE_ID ASC); DROP TABLE IF EXISTS DM_DEVICE_LOCATION; -CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( - ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INTEGER NULL, - LATITUDE DOUBLE NULL, - LONGITUDE DOUBLE NULL, +CREATE TABLE DM_DEVICE_LOCATION ( + ID INTEGER IDENTITY NOT NULL, + DEVICE_ID INT NULL, + LATITUDE FLOAT NULL, + LONGITUDE FLOAT NULL, STREET1 VARCHAR(45) NULL, STREET2 VARCHAR(45) NULL, CITY VARCHAR(45) NULL, ZIP VARCHAR(10) NULL, STATE VARCHAR(45) NULL, COUNTRY VARCHAR(45) NULL, + UPDATE_TIMESTAMP BIGINT NOT NULL, PRIMARY KEY (ID), - INDEX DM_DEVICE_LOCATION_DEVICE_idx (DEVICE_ID ASC), CONSTRAINT DM_DEVICE_LOCATION_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) @@ -428,11 +445,12 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( ON UPDATE NO ACTION ); +CREATE INDEX DM_DEVICE_LOCATION_DEVICE_idx ON DM_DEVICE_LOCATION (DEVICE_ID ASC); -DROP TABLE IF EXISTS DM_DEVICE_DETAIL ; +DROP TABLE IF EXISTS DM_DEVICE_DETAIL; -CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( - ID INT NOT NULL AUTO_INCREMENT, +CREATE TABLE DM_DEVICE_DETAIL ( + ID INT NOT NULL IDENTITY, DEVICE_ID INT NOT NULL, DEVICE_MODEL VARCHAR(45) NULL, VENDOR VARCHAR(45) NULL, @@ -447,9 +465,9 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( CPU_USAGE DECIMAL(5) NULL, TOTAL_RAM_MEMORY DECIMAL(30,3) NULL, AVAILABLE_RAM_MEMORY DECIMAL(30,3) NULL, - PLUGGED_IN INT(1) NULL, + PLUGGED_IN INT NULL, + UPDATE_TIMESTAMP BIGINT NOT NULL, PRIMARY KEY (ID), - INDEX FK_DM_DEVICE_DETAILS_DEVICE_idx (DEVICE_ID ASC), CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) @@ -457,3 +475,5 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ON UPDATE NO ACTION ); +CREATE INDEX FK_DM_DEVICE_DETAILS_DEVICE_idx ON DM_DEVICE_DETAIL (DEVICE_ID ASC); + diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql index 066e59c8c2..7d3eada22e 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -482,6 +482,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( ZIP VARCHAR(10) NULL, STATE VARCHAR(45) NULL, COUNTRY VARCHAR(45) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, PRIMARY KEY (ID), INDEX DM_DEVICE_LOCATION_DEVICE_idx (DEVICE_ID ASC), CONSTRAINT DM_DEVICE_LOCATION_DEVICE @@ -513,6 +514,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( TOTAL_RAM_MEMORY DECIMAL(30,3) NULL, AVAILABLE_RAM_MEMORY DECIMAL(30,3) NULL, PLUGGED_IN INT(1) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, PRIMARY KEY (ID), INDEX FK_DM_DEVICE_DETAILS_DEVICE_idx (DEVICE_ID ASC), CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index e47cd11440..fa9ac2e783 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -568,6 +568,34 @@ WHEN (NEW.ID IS NULL) END; / + +CREATE TABLE DM_DEVICE_GROUP_POLICY ( + ID NUMBER(10) NOT NULL, + DEVICE_GROUP_ID NUMBER(10) NOT NULL, + POLICY_ID NUMBER(10) NOT NULL, + TENANT_ID NUMBER(10) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_DEVICE_GROUP_POLICY + FOREIGN KEY (DEVICE_GROUP_ID) + REFERENCES DM_GROUP (ID) + , + CONSTRAINT FK_DM_DEVICE_GROUP_DM_POLICY + FOREIGN KEY (POLICY_ID , DEVICE_GROUP_ID) + REFERENCES DM_POLICY (ID , ID) +) ; + +-- Generate ID using sequence and trigger +CREATE SEQUENCE DM_DEVICE_GROUP_POLICY_seq START WITH 1 INCREMENT BY 1; + +CREATE OR REPLACE TRIGGER DM_DEVICE_GROUP_POLICY_seq_tr + BEFORE INSERT ON DM_DEVICE_GROUP_POLICY FOR EACH ROW + WHEN (NEW.ID IS NULL) +BEGIN + SELECT DM_DEVICE_GROUP_POLICY_seq.NEXTVAL INTO :NEW.ID FROM DUAL; +END; +/ + + CREATE TABLE DM_APPLICATION ( ID NUMBER(10) NOT NULL, NAME VARCHAR2(150) NOT NULL, @@ -714,6 +742,7 @@ CREATE TABLE DM_DEVICE_LOCATION ( ZIP VARCHAR2(10) NULL, STATE VARCHAR2(45) NULL, COUNTRY VARCHAR2(45) NULL, + UPDATE_TIMESTAMP NUMBER(19) NOT NULL, PRIMARY KEY (ID) , CONSTRAINT DM_DEVICE_LOCATION_DEVICE @@ -751,23 +780,22 @@ CREATE TABLE DM_DEVICE_DETAIL ( VENDOR VARCHAR2(45) NULL, OS_VERSION VARCHAR2(45) NULL, BATTERY_LEVEL NUMBER(4) NULL, - INTERNAL_TOTAL_MEMORY NUMBER(30) NULL, - INTERNAL_AVAILABLE_MEMORY NUMBER(30) NULL, - EXTERNAL_TOTAL_MEMORY NUMBER(30) NULL, - EXTERNAL_AVAILABLE_MEMORY NUMBER(30) NULL, + INTERNAL_TOTAL_MEMORY NUMBER(30,3) NULL, + INTERNAL_AVAILABLE_MEMORY NUMBER(30,3) NULL, + EXTERNAL_TOTAL_MEMORY NUMBER(30,3) NULL, + EXTERNAL_AVAILABLE_MEMORY NUMBER(30,3) NULL, CONNECTION_TYPE VARCHAR2(10) NULL, SSID VARCHAR2(45) NULL, CPU_USAGE NUMBER(5) NULL, - TOTAL_RAM_MEMORY NUMBER(30) NULL, - AVAILABLE_RAM_MEMORY NUMBER(30) NULL, + TOTAL_RAM_MEMORY NUMBER(30,3) NULL, + AVAILABLE_RAM_MEMORY NUMBER(30,3) NULL, PLUGGED_IN NUMBER(10) NULL, - PRIMARY KEY (ID) - , + UPDATE_TIMESTAMP NUMBER(19) NOT NULL, + PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) - ) - ; +); -- Generate ID using sequence and trigger CREATE SEQUENCE DM_DEVICE_DETAIL_seq START WITH 1 INCREMENT BY 1; @@ -782,4 +810,3 @@ END; CREATE INDEX FK_DM_DEVICE_DETAILS_DEVICE_idx ON DM_DEVICE_DETAIL (DEVICE_ID ASC); - diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 431fe0e40a..5fcf3f99ac 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -292,6 +292,26 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( ON UPDATE NO ACTION ); +CREATE SEQUENCE DM_DEVICE_GROUP_POLICY_seq; + +CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( + ID INT NOT NULL DEFAULT NEXTVAL ('DM_DEVICE_GROUP_POLICY_seq'), + DEVICE_GROUP_ID INT NOT NULL, + POLICY_ID INT NOT NULL, + TENANT_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_DEVICE_GROUP_POLICY + FOREIGN KEY (DEVICE_GROUP_ID) + REFERENCES DM_GROUP (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_DEVICE_GROUP_DM_POLICY + FOREIGN KEY (POLICY_ID , DEVICE_GROUP_ID) + REFERENCES DM_POLICY (ID , ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( ID BIGSERIAL NOT NULL PRIMARY KEY, DEVICE_ID INTEGER NOT NULL, @@ -379,6 +399,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( ZIP VARCHAR(10) NULL, STATE VARCHAR(45) NULL, COUNTRY VARCHAR(45) NULL, + UPDATE_TIMESTAMP BIGINT NOT NULL, PRIMARY KEY (ID) , CONSTRAINT DM_DEVICE_LOCATION_DEVICE FOREIGN KEY (DEVICE_ID) @@ -390,6 +411,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( CREATE INDEX DM_DEVICE_LOCATION_DEVICE_idx ON DM_DEVICE_LOCATION (DEVICE_ID ASC); +CREATE SEQUENCE DM_DEVICE_DETAIL_seq; + CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ID INT NOT NULL DEFAULT NEXTVAL ('DM_DEVICE_DETAIL_seq'), DEVICE_ID INT NOT NULL, @@ -407,7 +430,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( TOTAL_RAM_MEMORY DECIMAL(30,3) NULL, AVAILABLE_RAM_MEMORY DECIMAL(30,3) NULL, PLUGGED_IN INT NULL, - PRIMARY KEY (ID) , + UPDATE_TIMESTAMP BIGINT NOT NULL, + PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID)