diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index ebd34eb8c9..245cbfa361 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -122,6 +122,11 @@ public interface DeviceManagementService { value = "Enrollment status of devices to be fetched.", required = false) @QueryParam("status") String status, + @ApiParam( + name = "since", + value = "Last modified timestamp", + required = false) + @QueryParam("since") String since, @ApiParam( name = "If-Modified-Since", value = "Timestamp of the last modified date", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index e641efcac7..d927cf3c1d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -18,6 +18,7 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; +import io.swagger.annotations.ApiParam; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.*; @@ -49,6 +50,9 @@ import javax.validation.constraints.NotNull; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; @Path("/devices") @@ -66,7 +70,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("roleName") String roleName, @QueryParam("ownership") String ownership, @QueryParam("status") String status, - @HeaderParam("If-Modified-Since") String timestamp, + @QueryParam("since") String since, + @HeaderParam("If-Modified-Since") String ifModifiedSince, @QueryParam("offset") int offset, @QueryParam("limit") int limit) { try { @@ -78,26 +83,60 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (type != null) { request.setDeviceType(type); - result = dms.getDevicesByType(request); - } else if (user != null) { + } + if (user != null) { request.setOwner(user); - result = dms.getDevicesOfUser(request); - } else if (ownership != null) { + } + if (ownership != null) { RequestValidationUtil.validateOwnershipType(ownership); request.setOwnership(ownership); - result = dms.getDevicesByOwnership(request); - } else if (status != null) { + } + if (status != null) { RequestValidationUtil.validateStatus(status); request.setStatus(status); - result = dms.getDevicesByStatus(request); + } + + if (ifModifiedSince != null) { + Date sinceDate; + SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); + try { + sinceDate = format.parse(ifModifiedSince); + } catch (ParseException e) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Invalid date " + + "string is provided in 'If-Modified-Since' header").build()); + } + request.setSince(sinceDate); + result = dms.getAllDevices(request); + if (result == null || result.getData() == null || result.getData().size() <= 0) { + return Response.status(Response.Status.NOT_MODIFIED).entity("No device is modified " + + "after the timestamp provided in 'If-Modified-Since' header").build(); + } + } else if (since != null) { + Date sinceDate; + SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); + try { + sinceDate = format.parse(since); + } catch (ParseException e) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Invalid date " + + "string is provided in 'since' filter").build()); + } + request.setSince(sinceDate); + result = dms.getAllDevices(request); + if (result == null || result.getData() == null || result.getData().size() <= 0) { + return Response.status(Response.Status.OK).entity("No device is modified " + + "after the timestamp provided in 'since' filter").build(); + } } else { result = dms.getAllDevices(request); + if (result == null) { + throw new NotFoundException( + new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device is currently" + + " enrolled with the server").build()); + } } - if (result == null) { - throw new NotFoundException( - new ErrorResponse.ErrorResponseBuilder().setCode(404l).setMessage("No device is currently" + - " enrolled with the server").build()); - } + DeviceList devices = new DeviceList(); devices.setList((List) result.getData()); devices.setCount(result.getRecordsTotal()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java index 9934bcd61c..e39106cadb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java @@ -18,6 +18,8 @@ package org.wso2.carbon.device.mgt.common; +import java.util.Date; + /** * This class holds required parameters for a querying a paginated response. */ @@ -30,6 +32,7 @@ public class PaginationRequest { private String deviceType; private String deviceName; private String ownership; + private Date since; public PaginationRequest(int start, int rowCount) { this.startIndex = start; @@ -91,4 +94,13 @@ public class PaginationRequest { public void setOwnership(String ownership) { this.ownership = ownership; } + + public Date getSince() { + return since; + } + + public void setSince(Date since) { + this.since = since; + } + } 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 ae7fd00de2..d6653fa892 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 @@ -97,13 +97,12 @@ public interface DeviceDAO { /** * This method is used to update a given device. * - * @param typeId device type id. * @param device device object. * @param tenantId tenant id. * @return returns the id of updated device. * @throws DeviceManagementDAOException */ - boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; + boolean updateDevice(Device device, int tenantId) throws DeviceManagementDAOException; /** * This method is used to remove a device. @@ -194,6 +193,8 @@ public interface DeviceDAO { */ List getDevices(String type, int tenantId) throws DeviceManagementDAOException; + List getDevices(long timestamp, int tenantId) throws DeviceManagementDAOException; + /** * This method is used to retrieve devices of a given user. * 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 96dce585c2..30d177efc2 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 @@ -50,14 +50,16 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { int deviceId = -1; try { conn = this.getConnection(); - String sql = "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) " + - "VALUES (?, ?, ?, ?, ?)"; + String sql = "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, " + + "LAST_UPDATED_TIMESTAMP, TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, new String[] {"id"}); stmt.setString(1, device.getDescription()); stmt.setString(2, device.getName()); stmt.setInt(3, typeId); stmt.setString(4, device.getDeviceIdentifier()); - stmt.setInt(5, tenantId); + stmt.setTimestamp(5, new Timestamp(new Date().getTime())); + stmt.setInt(6, tenantId); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); @@ -74,26 +76,24 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } @Override - public boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { + public boolean updateDevice(Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; - boolean status = false; int rows; try { conn = this.getConnection(); - String sql = "UPDATE DM_DEVICE SET DESCRIPTION = ?, NAME = ? WHERE DEVICE_IDENTIFICATION = ? AND " + - "DEVICE_TYPE_ID = ? AND TENANT_ID = ?"; + String sql = "UPDATE DM_DEVICE SET DESCRIPTION = ?, LAST_UPDATED_TIMESTAMP = ? " + + "WHERE DEVICE_TYPE_ID = (SELECT ID FROM DM_DEVICE_TYPE WHERE NAME = ? AND PROVIDER_TENANT_ID = ?) " + + "AND DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql, new String[] {"id"}); stmt.setString(1, device.getDescription()); - stmt.setString(2, device.getName()); - stmt.setString(3, device.getDeviceIdentifier()); - stmt.setInt(4, typeId); - stmt.setInt(5, tenantId); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setString(3, device.getType()); + stmt.setInt(4, tenantId); + stmt.setString(5, device.getDeviceIdentifier()); + stmt.setInt(6, tenantId); rows = stmt.executeUpdate(); - if (rows > 0) { - status = true; - } - return status; + return (rows > 0); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while enrolling device '" + device.getName() + "'", e); @@ -875,4 +875,42 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return deviceTypes; } + /** + * Returns the collection of devices that has been updated after the time given in the timestamp passed in. + * + * @param timestamp Timestamp in long, after which the devices have been updated. + * @param tenantId Tenant id of the currently logged in user. + * @return A collection of devices that have been updated after the provided timestamp + * @throws DeviceManagementDAOException + */ + public List getDevices(long timestamp, int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = new ArrayList<>(); + try { + conn = this.getConnection(); + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID AS DEVICE_ID, " + + "d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE, d.LAST_UPDATED_TIMESTAMP FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? AND d.LAST_UPDATED_TIMESTAMP < CURRENT_TIMESTAMP) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setInt(2, tenantId); + rs = stmt.executeQuery(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return devices; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index d10b7e9d99..246a1bc1f3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -25,11 +25,9 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; +import java.util.Date; import java.util.List; /** @@ -54,6 +52,8 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { boolean isOwnershipProvided = false; String status = request.getStatus(); boolean isStatusProvided = false; + Date since = request.getSince(); + boolean isSinceProvided = false; try { conn = this.getConnection(); String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + @@ -73,6 +73,12 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { isDeviceNameProvided = true; } + //Add query for last updated timestamp + if (since != null) { + sql = sql + " AND d.LAST_UPDATED_TIMESTAMP > ?"; + isSinceProvided = true; + } + sql = sql + ") d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?"; //Add the query for ownership @@ -102,6 +108,9 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { if (isDeviceNameProvided) { stmt.setString(paramIdx++, request.getDeviceName() + "%"); } + if (isSinceProvided) { + stmt.setTimestamp(paramIdx++, new Timestamp(since.getTime())); + } stmt.setInt(paramIdx++, tenantId); if (isOwnershipProvided) { stmt.setString(paramIdx++, request.getOwnership()); 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 0df398c669..62fcd3bf82 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 @@ -21,9 +21,12 @@ package org.wso2.carbon.device.mgt.core.device.details.mgt.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; +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.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; @@ -40,9 +43,11 @@ import java.util.Map; public class DeviceInformationManagerImpl implements DeviceInformationManager { private DeviceDetailsDAO deviceDetailsDAO; + private DeviceDAO deviceDAO; private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class); public DeviceInformationManagerImpl() { + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.deviceDetailsDAO = DeviceManagementDAOFactory.getDeviceDetailsDAO(); } @@ -53,6 +58,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { getDeviceManagementProvider().getDevice(deviceId); DeviceManagementDAOFactory.beginTransaction(); + deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDetailsDAO.deleteDeviceInformation(device.getId()); deviceDetailsDAO.deleteDeviceProperties(device.getId()); deviceDetailsDAO.addDeviceInformation(device.getId(), deviceInfo); @@ -60,13 +66,17 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Transactional error occurred while adding the device information."); + 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."); + throw new DeviceDetailsMgtException("Error occurred while adding the device information.", e); } catch (DeviceManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Error occurred while retrieving the device information."); + throw new DeviceDetailsMgtException("Error occurred while retrieving the device information.", e); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceDetailsMgtException("Error occurred while updating the last update timestamp of the " + + "device", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -147,18 +157,23 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { getDeviceManagementProvider().getDevice(deviceLocation.getDeviceIdentifier()); deviceLocation.setDeviceId(device.getId()); DeviceManagementDAOFactory.beginTransaction(); + deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceDetailsDAO.deleteDeviceLocation(deviceLocation.getDeviceId()); deviceDetailsDAO.addDeviceLocation(deviceLocation); DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location information."); + throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location " + + "information.", e); } catch (DeviceDetailsMgtDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Error occurred while adding the device location information."); + throw new DeviceDetailsMgtException("Error occurred while adding the device location information.", e); } catch (DeviceManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Error occurred while getting the device information."); + throw new DeviceDetailsMgtException("Error occurred while getting the device information.", e); + } catch (DeviceManagementDAOException e) { + throw new DeviceDetailsMgtException("Error occurred while updating the last updated timestamp of " + + "the device", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -192,7 +207,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } @Override - public List getDeviceLocations(List deviceIdentifiers) throws DeviceDetailsMgtException { + public List getDeviceLocations( + List deviceIdentifiers) throws DeviceDetailsMgtException { try { List devices = DeviceManagementDataHolder.getInstance(). diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 4917af2a44..dd4ae3c212 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -252,7 +252,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.setId(currentDevice.getId()); device.getEnrolmentInfo().setId(currentDevice.getEnrolmentInfo().getId()); - deviceDAO.updateDevice(type.getId(), device, tenantId); + deviceDAO.updateDevice(device, tenantId); enrollmentDAO.updateEnrollment(device.getEnrolmentInfo()); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { @@ -317,7 +317,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); enrollmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId); - deviceDAO.updateDevice(deviceType.getId(), device, tenantId); + deviceDAO.updateDevice(device, tenantId); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { @@ -439,6 +439,73 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return devices; } + public List getDevices(Date since) throws DeviceManagementException { + List devices = new ArrayList<>(); + List allDevices; + try { + DeviceManagementDAOFactory.openConnection(); + allDevices = deviceDAO.getDevices(since.getTime(), this.getTenantId()); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + + "the current tenant", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + for (Device device : allDevices) { + DeviceInfo info = null; + try { + DeviceManagementDAOFactory.openConnection(); + info = deviceInfoDAO.getDeviceInformation(device.getId()); + DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); + if (info != null) { + info.setLocation(location); + } + } catch (DeviceDetailsMgtDAOException e) { + log.error("Error occurred while retrieving advance info of '" + device.getType() + + "' that carries the id '" + device.getDeviceIdentifier() + "'"); + } catch (SQLException e) { + log.error("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + device.setDeviceInfo(info); + + try { + DeviceManagementDAOFactory.openConnection(); + List applications = applicationDAO.getInstalledApplications(device.getId()); + device.setApplications(applications); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while retrieving the application list of '" + device.getType() + "', " + + "which carries the id '" + device.getId() + "'", e); + } catch (SQLException e) { + log.error("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + DeviceManager deviceManager = this.getDeviceManager(device.getType()); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + + "Therefore, not attempting method 'isEnrolled'"); + } + devices.add(device); + continue; + } + Device dmsDevice = + deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } + devices.add(device); + } + return devices; + } + @Override public PaginationResult getDevicesByType(PaginationRequest request) throws DeviceManagementException { PaginationResult paginationResult = new PaginationResult(); @@ -1463,10 +1530,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { try { DeviceManagementDAOFactory.beginTransaction(); - DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType(), this.getTenantId()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(status); - deviceDAO.updateDevice(deviceType.getId(), device, this.getTenantId()); + deviceDAO.updateDevice(device, this.getTenantId()); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); 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 7d6159a87f..f9062684ae 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 @@ -31,6 +31,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( NAME VARCHAR(100) DEFAULT NULL, DEVICE_TYPE_ID INT(11) DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_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 dbbcd4e092..fe3d2de042 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 @@ -32,6 +32,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( NAME VARCHAR(100) DEFAULT NULL, DEVICE_TYPE_ID INT(11) DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_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 f90b4eae79..b810963921 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 @@ -32,6 +32,7 @@ CREATE TABLE DM_DEVICE ( NAME VARCHAR(100) DEFAULT NULL, DEVICE_TYPE_ID INTEGER DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) 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 d3be894e74..651bb734e9 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 @@ -21,6 +21,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( NAME VARCHAR(100) DEFAULT NULL, DEVICE_TYPE_ID INT(11) DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID) 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 70dc8c3561..de8645df1f 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 @@ -71,6 +71,7 @@ CREATE TABLE DM_DEVICE ( NAME VARCHAR2(100) DEFAULT NULL, DEVICE_TYPE_ID NUMBER(10) DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR2(300) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, TENANT_ID NUMBER(10) DEFAULT 0, CONSTRAINT PK_DM_DEVICE PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) 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 51dac1c6c1..407c2cc155 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 @@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE ( NAME VARCHAR(100) DEFAULT NULL, DEVICE_TYPE_ID INTEGER DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, TENANT_ID INTEGER DEFAULT 0, CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION