get all devices when the username is provided

revert-70aa11f8
inosh-perera 10 years ago
parent c13b942f0e
commit 4754da70be

@ -55,4 +55,7 @@ public interface DeviceManager {
public OperationManager getOperationManager(String type) throws DeviceManagementException;
public List<Device> getDeviceListOfUser(String username)
throws DeviceManagementException, DeviceManagementDAOException;
}

@ -203,4 +203,21 @@ public class DeviceManagerImpl implements DeviceManager {
public DeviceManagementRepository getPluginRepository() {
return pluginRepository;
}
@Override
public List<Device> getDeviceListOfUser(String username)
throws DeviceManagementException, DeviceManagementDAOException {
List<org.wso2.carbon.device.mgt.core.dto.Device> devicesList = this.deviceDAO.getDeviceListOfUser(username);
List<Device> devicesOfUser=new ArrayList<Device>();
for(int x=0;x<devicesList.size();x++){
DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(
devicesList.get(x).getDeviceType().getName());
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(devicesList.get(x).getDeviceIdentificationId());
deviceIdentifier.setType(devicesList.get(x).getDeviceType().getName());
Device dv=dms.getDevice(deviceIdentifier);
devicesOfUser.add(dv);
}
return devicesOfUser;
}
}

@ -55,4 +55,12 @@ public interface DeviceDAO {
* @throws DeviceManagementDAOException
*/
List<Device> getDevices(Integer type) throws DeviceManagementDAOException;
/**
* Get the list of devices belongs to a user.
* @param username Requested user.
* @return List of devices of the user.
* @throws DeviceManagementDAOException
*/
List<Device> getDeviceListOfUser(String username) throws DeviceManagementDAOException;
}

@ -24,6 +24,7 @@ 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.util.DeviceManagementDAOUtil;
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 javax.sql.DataSource;
@ -76,6 +77,39 @@ public class DeviceDAOImpl implements DeviceDAO {
}
}
@Override public List<Device> getDeviceListOfUser(String username)
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, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION FROM DM_DEVICE, DM_DEVICE_TYPE WHERE DM_DEVICE.DEVICE_TYPE_ID =DM_DEVICE_TYPE.ID AND DM_DEVICE.OWNER =?");
stmt.setString(1, username);
ResultSet results = stmt.executeQuery();
while (results.next()) {
Device device = new Device();
DeviceType deviceType=new DeviceType();
int id=results.getInt(results.getInt(1));
deviceType.setId((long) id);
deviceType.setName(results.getString(2));
device.setId(results.getInt(3));
device.setDeviceType(deviceType);
device.setDeviceTypeId(results.getInt(4));
device.setDeviceIdentificationId(results.getString(5));
deviceList.add(device);
}
} catch (SQLException e) {
String msg = "Error occurred while fetching the list of devices belongs to " + username;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return deviceList;
}
@Override
public void updateDevice(Device device) throws DeviceManagementDAOException {

@ -34,6 +34,15 @@ public class Device implements Serializable {
private String ownerShip;
private int tenantId;
private Integer deviceTypeId;
private DeviceType deviceType;
public DeviceType getDeviceType() {
return deviceType;
}
public void setDeviceType(DeviceType deviceType) {
this.deviceType = deviceType;
}
public Integer getDeviceTypeId() {
return deviceTypeId;

@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.OperationManager;
import org.wso2.carbon.device.mgt.core.DeviceManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import java.util.List;
@ -93,4 +94,9 @@ public class DeviceManagementService implements DeviceManager {
getOperationManager(type);
}
@Override public List<Device> getDeviceListOfUser(String username) throws DeviceManagementException,DeviceManagementDAOException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().getDeviceListOfUser(username);
}
}

Loading…
Cancel
Save