diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index b1f2ffb043..690fab2f46 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; @@ -30,6 +31,7 @@ import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Map; /** * This class represents the key operations associated with persisting device related information. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 2d0d877c03..f76dd5ae4d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -27,10 +27,16 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; +import java.io.ByteArrayOutputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -39,8 +45,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; -import java.util.Iterator; import java.util.List; +import java.util.Map; public abstract class AbstractDeviceDAOImpl implements DeviceDAO { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index f265509910..d990c0ab5a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -65,13 +65,27 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro try { Device device = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider().getDevice(deviceId, false); - + DeviceInfo newDeviceInfo = null; DeviceManagementDAOFactory.beginTransaction(); + DeviceInfo previousDeviceInfo = deviceDetailsDAO + .getDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); + Map previousDeviceProperties = deviceDetailsDAO + .getDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); + if (previousDeviceInfo != null && previousDeviceProperties != null) { + previousDeviceInfo.setDeviceDetailsMap(previousDeviceProperties); + newDeviceInfo = processDeviceInfo(previousDeviceInfo, deviceInfo); + } else if (previousDeviceInfo == null && previousDeviceProperties != null) { + previousDeviceInfo = new DeviceInfo(); + previousDeviceInfo.setDeviceDetailsMap(previousDeviceProperties); + newDeviceInfo = processDeviceInfo(previousDeviceInfo, deviceInfo); + } else { + newDeviceInfo = deviceInfo; + } deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDetailsDAO.deleteDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); deviceDetailsDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); - deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), deviceInfo); - deviceDetailsDAO.addDeviceProperties(deviceInfo.getDeviceDetailsMap(), device.getId(), + deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), newDeviceInfo); + deviceDetailsDAO.addDeviceProperties(newDeviceInfo.getDeviceDetailsMap(), device.getId(), device.getEnrolmentInfo().getId()); DeviceManagementDAOFactory.commitTransaction(); @@ -106,7 +120,8 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro } } catch (TransactionManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Transactional error occurred while adding the device information.", e); + throw new DeviceDetailsMgtException("Transactional error occurred while adding the device " + + "information.", e); } catch (DeviceDetailsMgtDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while adding the device information.", e); @@ -116,7 +131,7 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while updating the last update timestamp of the " + - "device", e); + "device", e); } catch (DataPublisherConfigurationException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e); @@ -125,6 +140,68 @@ public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) thro } } + private DeviceInfo processDeviceInfo(DeviceInfo previousDeviceInfo, DeviceInfo newDeviceInfo) { + if (newDeviceInfo.getDeviceModel().equals("")) { + newDeviceInfo.setDeviceModel(previousDeviceInfo.getDeviceModel()); + } + if (newDeviceInfo.getVendor().equals("")) { + newDeviceInfo.setVendor(previousDeviceInfo.getVendor()); + } + if (newDeviceInfo.getOsBuildDate().equals("")) { + newDeviceInfo.setOsBuildDate(previousDeviceInfo.getOsBuildDate()); + } + if (newDeviceInfo.getOsVersion().equals("")) { + newDeviceInfo.setOsVersion(previousDeviceInfo.getOsVersion()); + } + if (newDeviceInfo.getBatteryLevel() == -1D) { + newDeviceInfo.setBatteryLevel(previousDeviceInfo.getBatteryLevel()); + } + if (newDeviceInfo.getInternalTotalMemory() == -1D) { + newDeviceInfo.setInternalTotalMemory(previousDeviceInfo.getInternalTotalMemory()); + } + if (newDeviceInfo.getInternalAvailableMemory() == -1D) { + newDeviceInfo.setInternalAvailableMemory(previousDeviceInfo.getInternalAvailableMemory()); + } + if (newDeviceInfo.getExternalTotalMemory() == -1D) { + newDeviceInfo.setExternalTotalMemory(previousDeviceInfo.getExternalTotalMemory()); + } + if (newDeviceInfo.getExternalAvailableMemory() == -1D) { + newDeviceInfo.setExternalAvailableMemory(previousDeviceInfo.getExternalAvailableMemory()); + } + if (newDeviceInfo.getOperator().equals("")) { + newDeviceInfo.setOperator(previousDeviceInfo.getOperator()); + } + if (newDeviceInfo.getConnectionType().equals("")) { + newDeviceInfo.setConnectionType(previousDeviceInfo.getConnectionType()); + } + if (newDeviceInfo.getMobileSignalStrength() == 0.0) { + newDeviceInfo.setMobileSignalStrength(previousDeviceInfo.getMobileSignalStrength()); + } + if (newDeviceInfo.getSsid().equals("")) { + newDeviceInfo.setSsid(previousDeviceInfo.getSsid()); + } + if (newDeviceInfo.getCpuUsage() == 0.0) { + newDeviceInfo.setCpuUsage(previousDeviceInfo.getCpuUsage()); + } + if (newDeviceInfo.getTotalRAMMemory() == -1D) { + newDeviceInfo.setTotalRAMMemory(previousDeviceInfo.getTotalRAMMemory()); + } + if (newDeviceInfo.getAvailableRAMMemory() == -1D) { + newDeviceInfo.setAvailableRAMMemory(previousDeviceInfo.getAvailableRAMMemory()); + } + if (!newDeviceInfo.isPluggedIn()) { + newDeviceInfo.setPluggedIn(previousDeviceInfo.isPluggedIn()); + } + Map newDeviceDetailsMap = newDeviceInfo.getDeviceDetailsMap(); + Map previousDeviceDetailsMap = previousDeviceInfo.getDeviceDetailsMap(); + for (String eachKey : previousDeviceDetailsMap.keySet()) { + if (!newDeviceDetailsMap.containsKey(eachKey)) { + newDeviceDetailsMap.put(eachKey, previousDeviceDetailsMap.get(eachKey)); + } + } + return newDeviceInfo; + } + @Override public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { Device device = getDevice(deviceId); @@ -141,7 +218,7 @@ public DeviceInfo getDeviceInfo(DeviceIdentifier deviceId) throws DeviceDetailsM } catch (SQLException e) { throw new DeviceDetailsMgtException("SQL error occurred while retrieving device " + deviceId.toString() - + "'s info from database.", e); + + "'s info from database.", e); } catch (DeviceDetailsMgtDAOException e) { throw new DeviceDetailsMgtException("Exception occurred while retrieving device details.", e); } finally { @@ -256,7 +333,7 @@ private Device getDevice(DeviceIdentifier deviceId) throws DeviceDetailsMgtExcep if (device == null) { if (log.isDebugEnabled()) { log.debug("No device is found upon the device identifier '" + deviceId.getId() + - "' and type '" + deviceId.getType() + "'. Therefore returning null"); + "' and type '" + deviceId.getType() + "'. Therefore returning null"); } return null; } @@ -286,7 +363,7 @@ public List getDeviceLocations( throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e); } catch (DeviceDetailsMgtDAOException e) { throw new DeviceDetailsMgtException("Exception occurred while retrieving device locations.", e); - } finally{ + } finally { DeviceManagementDAOFactory.closeConnection(); } }