Added a function to retrieve device list from device name

merge-requests/7/head
mharindu 10 years ago
parent 3c5a3adc2f
commit de8914fdcb

@ -596,7 +596,48 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ
int deviceCount = this.deviceDAO.getDeviceCount();
return deviceCount;
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining devices all devices", e);
log.error("Error occurred while counting devices", e);
throw new DeviceManagementException("Error occurred while counting devices", e);
}
}
@Override
public List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException {
List<Device> devicesOfUser = new ArrayList<Device>();
List<org.wso2.carbon.device.mgt.core.dto.Device> devicesList;
Device convertedDevice;
DeviceIdentifier deviceIdentifier;
DeviceManager dms;
Device dmsDevice;
org.wso2.carbon.device.mgt.core.dto.Device device;
try {
devicesList = this.getDeviceDAO().getDevicesByName(deviceName, tenantId);
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '"
+ deviceName + "'", e);
}
for (int x = 0; x < devicesList.size(); x++) {
device = devicesList.get(x);
try {
device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId()));
dms = this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName());
convertedDevice = DeviceManagementDAOUtil.convertDevice(device, device.getDeviceType());
deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(device.getDeviceIdentificationId());
deviceIdentifier.setType(device.getDeviceType().getName());
dmsDevice = dms.getDevice(deviceIdentifier);
if (dmsDevice != null) {
convertedDevice.setProperties(dmsDevice.getProperties());
convertedDevice.setFeatures(dmsDevice.getFeatures());
}
devicesOfUser.add(convertedDevice);
} catch (DeviceManagementDAOException e) {
log.error("Error occurred while obtaining the device type of DeviceTypeId '" +
device.getDeviceTypeId() + "'", e);
}
}
return devicesOfUser;
}
}

@ -62,8 +62,18 @@ public interface DeviceDAO {
/**
* Get the count of devices
*
* @return device count
* @throws DeviceManagementDAOException
*/
int getDeviceCount() throws DeviceManagementDAOException;
/**
* Get the list of devices that matches with the given device name.
*
* @param deviceName Name of the device
* @return List of devices that matches with the given device name.
* @throws DeviceManagementDAOException
*/
List<Device> getDevicesByName(String deviceName , int tenantId) throws DeviceManagementDAOException;
}

@ -330,4 +330,59 @@ public class DeviceDAOImpl implements DeviceDAO {
return deviceCount;
}
/**
* Get the list of devices that matches with the given device name.
*
* @param deviceName Name of the device.
* @param tenantId
* @return device list
* @throws DeviceManagementDAOException
*/
@Override
public List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException {
Connection conn = this.getConnection();
PreparedStatement stmt = null;
List<Device> deviceList = new ArrayList<Device>();
try {
stmt = conn.prepareStatement(
"SELECT DM_DEVICE_TYPE.ID, DM_DEVICE_TYPE.NAME, DM_DEVICE.ID, DM_DEVICE.DESCRIPTION, " +
"DM_DEVICE.NAME, DM_DEVICE.DATE_OF_ENROLLMENT, DM_DEVICE.DATE_OF_LAST_UPDATE, " +
"DM_DEVICE.OWNERSHIP, DM_DEVICE.STATUS, DM_DEVICE.DEVICE_TYPE_ID, " +
"DM_DEVICE.DEVICE_IDENTIFICATION, DM_DEVICE.OWNER, DM_DEVICE.TENANT_ID FROM " +
"DM_DEVICE, DM_DEVICE_TYPE WHERE DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID " +
"AND DM_DEVICE.NAME LIKE %?% AND DM_DEVICE.TENANT_ID =?");
stmt.setString(1, deviceName);
stmt.setInt(2, tenantId);
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
Device device = new Device();
DeviceType deviceType = new DeviceType();
int id = resultSet.getInt(resultSet.getInt(1));
deviceType.setId(id);
deviceType.setName(resultSet.getString(2));
device.setId(resultSet.getInt(3));
device.setDescription(resultSet.getString(4));
device.setName(resultSet.getString(5));
device.setDateOfEnrollment(resultSet.getLong(6));
device.setDateOfLastUpdate(resultSet.getLong(7));
//TODO:- Ownership is not a enum in DeviceDAO
device.setOwnerShip(resultSet.getString(8));
device.setStatus(Status.valueOf(resultSet.getString(9)));
device.setDeviceTypeId(resultSet.getInt(10));
device.setDeviceIdentificationId(resultSet.getString(11));
device.setOwnerId(resultSet.getString(12));
device.setTenantId(resultSet.getInt(13));
deviceList.add(device);
}
} catch (SQLException e) {
String msg = "Error occurred while fetching the list of devices that matches to '" + deviceName + "'";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return deviceList;
}
}

@ -73,7 +73,18 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager,
/**
* Method to get the count of all types of devices.
* @return device count
* @throws DeviceManagementException
* @throws DeviceManagementException If some unusual behaviour is observed while counting
* the devices
*/
int getDeviceCount() throws DeviceManagementException;
/**
* Method to get the list of devices that matches with the given device name.
*
* @param deviceName name of the device
* @return List of devices that matches with the given device name.
* @throws DeviceManagementException If some unusual behaviour is observed while fetching the
* device list
*/
List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException;
}

@ -211,4 +211,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.getDeviceCount();
}
@Override
public List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.getDevicesByName(deviceName, tenantId);
}
}

Loading…
Cancel
Save