Refactored the device id to integer.

Created method to convert dto.device to common.device
Created a bulk method for conversion
Implemented getAllDevices
revert-dabc3590
Dulitha Wijewantha 10 years ago
parent 250c48a258
commit adebf34ff2

@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import java.util.ArrayList;
import java.util.List;
public class DeviceManagerImpl implements DeviceManager {
@ -51,10 +52,9 @@ public class DeviceManagerImpl implements DeviceManager {
boolean status = dms.enrollDevice(device);
try {
this.getDeviceTypeDAO().getDeviceType();
org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device);
Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType());
deviceDto.setDeviceType(deviceTypeId);
deviceDto.setDeviceTypeId(deviceTypeId);
this.getDeviceDAO().addDevice(deviceDto);
} catch (DeviceManagementDAOException e) {
@ -107,7 +107,27 @@ public class DeviceManagerImpl implements DeviceManager {
public List<Device> getAllDevices(String type) throws DeviceManagementException {
DeviceManagerService dms =
this.getPluginRepository().getDeviceManagementProvider(type);
return dms.getAllDevices();
List<Device> devicesList = new ArrayList<Device>();
try {
Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(type);
List<org.wso2.carbon.device.mgt.core.dto.Device> devices =
this.getDeviceDAO().getDevices(deviceTypeId);
for (org.wso2.carbon.device.mgt.core.dto.Device device : devices) {
Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device);
DeviceIdentifier deviceIdentifier = DeviceManagementDAOUtil
.createDeviceIdentifier(device, this.deviceTypeDAO
.getDeviceType(device.getDeviceTypeId()));
Device dmsDevice = dms.getDevice(deviceIdentifier);
convertedDevice.setProperties(dmsDevice.getProperties());
convertedDevice.setFeatures(dmsDevice.getFeatures());
devicesList.add(convertedDevice);
}
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the device for type '" + type + "'",
e);
}
return devicesList;
}
@Override

@ -39,4 +39,11 @@ public interface DeviceDAO {
Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException;
List<Device> getDevices() throws DeviceManagementDAOException;
/**
* @param type - The device type id.
* @return a list of devices based on the type id.
* @throws DeviceManagementDAOException
*/
List<Device> getDevices(Integer type) throws DeviceManagementDAOException;
}

@ -31,6 +31,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -62,7 +63,7 @@ public class DeviceDAOImpl implements DeviceDAO {
stmt.setLong(4, new Date().getTime());
stmt.setString(5, device.getOwnerShip());
stmt.setString(6, device.getStatus().toString());
stmt.setInt(7, device.getDeviceType());
stmt.setInt(7, device.getDeviceTypeId());
stmt.setString(8, device.getDeviceIdentificationId());
stmt.setString(9, device.getOwnerId());
stmt.setInt(10, device.getTenantId());
@ -102,6 +103,47 @@ public class DeviceDAOImpl implements DeviceDAO {
return null;
}
@Override public List<Device> getDevices(Integer type) throws DeviceManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Device> devicesList = null;
try {
conn = this.getConnection();
String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " +
"DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " +
"DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " +
"WHERE DM_DEVICE.DEVICE_TYPE_ID=?";
stmt = conn.prepareStatement(selectDBQueryForType);
stmt.setInt(1, type);
resultSet = stmt.executeQuery();
devicesList = new ArrayList<Device>();
while (resultSet.next()) {
Device device = new Device();
device.setId(resultSet.getInt(1));
device.setDescription(resultSet.getString(2));
device.setName(resultSet.getString(3));
device.setDateOfEnrollment(resultSet.getLong(4));
device.setDateOfLastUpdate(resultSet.getLong(5));
//TODO:- Ownership is not a enum in DeviceDAO
device.setOwnerShip(resultSet.getString(6));
device.setStatus(Status.valueOf(resultSet.getString(7)));
device.setDeviceTypeId(resultSet.getInt(8));
device.setDeviceIdentificationId(resultSet.getString(9));
device.setOwnerId(resultSet.getString(10));
device.setTenantId(resultSet.getInt(11));
devicesList.add(device);
}
} catch (SQLException e) {
String msg = "Error occurred while listing devices for type '" + type + "'";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
return devicesList;
}
private Connection getConnection() throws DeviceManagementDAOException {
try {
return dataSource.getConnection();

@ -18,8 +18,10 @@ package org.wso2.carbon.device.mgt.core.dao.util;
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.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.Status;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.user.api.UserStoreException;
@ -32,7 +34,9 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
public final class DeviceManagementDAOUtil {
@ -100,6 +104,44 @@ public final class DeviceManagementDAOUtil {
}
}
/**
* @param device - The DTO device object.
* @return A Business Object.
*/
public static org.wso2.carbon.device.mgt.common.Device convertDevice(Device device){
org.wso2.carbon.device.mgt.common.Device deviceBO =
new org.wso2.carbon.device.mgt.common.Device();
deviceBO.setDateOfEnrolment(device.getDateOfEnrollment());
deviceBO.setDateOfLastUpdate(device.getDateOfLastUpdate());
deviceBO.setDescription(device.getDescription());
deviceBO.setDeviceIdentifier(device.getDeviceIdentificationId());
deviceBO.setDeviceTypeId(device.getDeviceTypeId());
deviceBO.setName(device.getName());
deviceBO.setId(device.getId());
deviceBO.setOwner(device.getOwnerId());
deviceBO.setOwnership(device.getOwnerShip());
if (device.getStatus() == Status.ACTIVE) {
deviceBO.setStatus(true);
} else if (device.getStatus() == Status.INACTIVE) {
deviceBO.setStatus(false);
}
return null;
}
/**
* @param devices - DTO Device Object list.
* @return converted Business Object list.
*/
public static List<org.wso2.carbon.device.mgt.common.Device> convertDevices(
List<Device> devices) {
List<org.wso2.carbon.device.mgt.common.Device> deviceBOList =
new ArrayList<org.wso2.carbon.device.mgt.common.Device>();
for (Device device : devices) {
deviceBOList.add(convertDevice(device));
}
return deviceBOList;
}
public static Device convertDevice(org.wso2.carbon.device.mgt.common.Device
device) throws DeviceManagementDAOException {
Device deviceBO = new Device();
@ -120,4 +162,10 @@ public final class DeviceManagementDAOUtil {
return deviceBO;
}
public static DeviceIdentifier createDeviceIdentifier(Device device, DeviceType deviceType) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setType(deviceType.getName());
deviceIdentifier.setId(device.getDeviceIdentificationId());
return deviceIdentifier;
}
}

@ -23,7 +23,7 @@ import java.io.Serializable;
public class Device implements Serializable {
private static final long serialVersionUID = -8101106997837486245L;
private String id;
private Integer id;
private String description;
private String name;
private Long dateOfEnrollment;
@ -33,21 +33,21 @@ public class Device implements Serializable {
private String ownerId;
private String ownerShip;
private int tenantId;
private Integer deviceType;
private Integer deviceTypeId;
public Integer getDeviceType() {
return deviceType;
public Integer getDeviceTypeId() {
return deviceTypeId;
}
public void setDeviceType(Integer deviceType) {
this.deviceType = deviceType;
public void setDeviceTypeId(Integer deviceTypeId) {
this.deviceTypeId = deviceTypeId;
}
public String getId() {
public Integer getId() {
return id;
}
public void setId(String id) {
public void setId(Integer id) {
this.id = id;
}

Loading…
Cancel
Save