Fixed EMM-1531

revert-70aa11f8
harshanl 8 years ago
parent a448959fe0
commit 78cab76101

@ -189,9 +189,25 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
Device device; Device device;
try { try {
RequestValidationUtil.validateDeviceIdentifier(type, id); RequestValidationUtil.validateDeviceIdentifier(type, id);
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
device = dms.getDevice(new DeviceIdentifier(id, type)); if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
Date sinceDate;
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
try {
sinceDate = format.parse(ifModifiedSince);
} catch (ParseException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage("Invalid date " +
"string is provided in 'If-Modified-Since' header").build()).build();
}
device = dms.getDevice(new DeviceIdentifier(id, type), sinceDate);
if (device == null) {
return Response.status(Response.Status.NOT_MODIFIED).entity("No device is modified " +
"after the timestamp provided in 'If-Modified-Since' header").build();
}
} else {
device = dms.getDevice(new DeviceIdentifier(id, type));
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while fetching the device information."; String msg = "Error occurred while fetching the device information.";
log.error(msg, e); log.error(msg, e);

@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -124,6 +125,18 @@ public interface DeviceDAO {
*/ */
Device getDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException; Device getDevice(DeviceIdentifier deviceIdentifier, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to retrieve a device of a given device-identifier and tenant-id.
*
* @param deviceIdentifier device id.
* @param ifModifiedSince last modified time.
* @param tenantId tenant id.
* @return returns the device object.
* @throws DeviceManagementDAOException
*/
Device getDevice(DeviceIdentifier deviceIdentifier, Date ifModifiedSince, int tenantId) throws
DeviceManagementDAOException;
/** /**
* This method is used to retrieve a device of a given device-identifier, enrollment status and tenant-id. * This method is used to retrieve a device of a given device-identifier, enrollment status and tenant-id.
* *

@ -128,7 +128,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
device = DeviceManagementDAOUtil.loadMatchingDevice(rs); device = DeviceManagementDAOUtil.loadMatchingDevice(rs, false);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing devices for type " + throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
@ -139,6 +139,41 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
return device; return device;
} }
@Override
public Device getDevice(DeviceIdentifier deviceIdentifier, Date since, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
Device device = null;
try {
conn = this.getConnection();
String sql = "SELECT d1.ID AS 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, d.DESCRIPTION, d.NAME, " +
"t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_DETAIL dt " +
"WHERE t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ? AND dt.DEVICE_ID = d.ID " +
"AND dt.UPDATE_TIMESTAMP > ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ?" ;
stmt = conn.prepareStatement(sql);
int paramIdx = 1;
stmt.setString(paramIdx++, deviceIdentifier.getType());
stmt.setString(paramIdx++, deviceIdentifier.getId());
stmt.setInt(paramIdx++, tenantId);
stmt.setLong(paramIdx++, since.getTime());
stmt.setInt(paramIdx, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
device = DeviceManagementDAOUtil.loadMatchingDevice(rs, true);
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while listing device for type " +
"'" + deviceIdentifier.getType() + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return device;
}
@Override @Override
public Device getDevice(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status status, int tenantId) throws public Device getDevice(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status status, int tenantId) throws
DeviceManagementDAOException { DeviceManagementDAOException {

@ -160,14 +160,21 @@ public final class DeviceManagementDAOUtil {
//This method will retrieve most appropriate device information when there are multiple device enrollments for //This method will retrieve most appropriate device information when there are multiple device enrollments for
//a single device. We'll give the highest priority to active devices. //a single device. We'll give the highest priority to active devices.
public static Device loadMatchingDevice(ResultSet rs) throws SQLException { public static Device loadMatchingDevice(ResultSet rs, boolean deviceInfoIncluded) throws SQLException {
Map<EnrolmentInfo.Status, Device> deviceMap = new HashMap<>(); Map<EnrolmentInfo.Status, Device> deviceMap = new HashMap<>();
Device device = loadDevice(rs); Device device = loadDevice(rs);
if (deviceInfoIncluded) {
device.setDeviceInfo(loadDeviceInfo(rs));
}
if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) {
return device; return device;
} }
while (rs.next()) { while (rs.next()) {
device = loadDevice(rs); device = loadDevice(rs);
if (deviceInfoIncluded) {
device.setDeviceInfo(loadDeviceInfo(rs));
}
if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) {
return device; return device;
} }

@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -205,6 +206,8 @@ public interface DeviceManagementProviderService {
Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException; Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
Device getDevice(DeviceIdentifier deviceId, Date since) throws DeviceManagementException;
Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException; Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException;
List<String> getAvailableDeviceTypes() throws DeviceManagementException; List<String> getAvailableDeviceTypes() throws DeviceManagementException;

@ -807,6 +807,54 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return device; return device;
} }
@Override
public Device getDevice(DeviceIdentifier deviceId, Date since) throws DeviceManagementException {
Device device;
try {
DeviceManagementDAOFactory.openConnection();
device = deviceDAO.getDevice(deviceId, since, this.getTenantId());
if (device == null) {
if (log.isDebugEnabled()) {
log.debug("No device is found upon the type '" + deviceId.getType() + "' and id '" +
deviceId.getId() + "'");
}
return null;
}
DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId());
if (device.getDeviceInfo() != null) {
device.getDeviceInfo().setLocation(location);
}
List<Application> applications = applicationDAO.getInstalledApplications(device.getId());
device.setApplications(applications);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the device for id " +
"'" + deviceId.getId() + "'", e);
} catch (SQLException e) {
throw new DeviceManagementException("Error occurred while opening a connection to the data source", e);
} catch (DeviceDetailsMgtDAOException e) {
throw new DeviceManagementException("Error occurred while fetching advanced device information", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
// The changes made here to prevent unit tests getting failed. They failed because when running the unit
// tests there is no osgi services. So getDeviceManager() returns a null.
DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
if (deviceManager == null) {
if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'getDevice'");
}
return device;
}
Device pluginSpecificInfo = deviceManager.getDevice(deviceId);
if (pluginSpecificInfo != null) {
device.setFeatures(pluginSpecificInfo.getFeatures());
device.setProperties(pluginSpecificInfo.getProperties());
}
return device;
}
@Override @Override
public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException { public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException {
Device device; Device device;

Loading…
Cancel
Save