* Setup the datasource for the operation manager dao factory

* Implemented the get all devices and get devices for user
* Added support for tenant domain in get devices for user (This has to be added to all methods)
revert-70aa11f8
Dulitha Wijewantha 10 years ago
parent 4600968d60
commit fc86ccd86d

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core;
import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
@ -25,8 +26,7 @@ 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.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
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.dto.*;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl;
@ -35,6 +35,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import java.util.ArrayList;
import java.util.List;
@ -128,7 +129,29 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ
@Override
public List<Device> getAllDevices() throws DeviceManagementException {
return null;
List<Device> convertedDevicesList = new ArrayList<Device>();
try {
List<org.wso2.carbon.device.mgt.core.dto.Device> devicesList = this.deviceDAO.getDevices();
for (int x = 0; x < devicesList.size(); x++) {
org.wso2.carbon.device.mgt.core.dto.Device device = devicesList.get(x);
device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId()));
DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName());
DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId());
Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device, deviceType);
DeviceIdentifier deviceIdentifier =
DeviceManagementDAOUtil.createDeviceIdentifier(device, deviceType);
Device dmsDevice = dms.getDevice(deviceIdentifier);
if (dmsDevice != null) {
convertedDevice.setProperties(dmsDevice.getProperties());
convertedDevice.setFeatures(dmsDevice.getFeatures());
}
convertedDevicesList.add(convertedDevice);
}
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining devices all devices", e);
}
return convertedDevicesList;
}
@Override
@ -161,7 +184,31 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ
@Override
public List<Device> getDeviceListOfUser(String username) throws DeviceManagementException {
return null;
List<Device> devicesOfUser = new ArrayList<Device>();
try {
int tenantId = DeviceManagerUtil.getTenantId();
List<org.wso2.carbon.device.mgt.core.dto.Device> devicesList = this.deviceDAO.getDeviceListOfUser(username, tenantId);
for (int x = 0; x < devicesList.size(); x++) {
org.wso2.carbon.device.mgt.core.dto.Device device = devicesList.get(x);
device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId()));
DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName());
Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device, device.getDeviceType());
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(device.getDeviceIdentificationId());
deviceIdentifier.setType(device.getDeviceType().getName());
Device dmsDevice = dms.getDevice(deviceIdentifier);
if (dmsDevice != null) {
convertedDevice.setProperties(dmsDevice.getProperties());
convertedDevice.setFeatures(dmsDevice.getFeatures());
}
devicesOfUser.add(convertedDevice);
}
} catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining devices for user " +
"'" + username + "'", e);
}
return devicesOfUser;
}
@Override

@ -58,6 +58,6 @@ public interface DeviceDAO {
* @return List of devices of the user.
* @throws DeviceManagementDAOException
*/
List<Device> getDeviceListOfUser(String username) throws DeviceManagementDAOException;
List<Device> getDeviceListOfUser(String username , int tenantId) throws DeviceManagementDAOException;
}

@ -107,7 +107,7 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection();
String sql =
"SELECT d.ID, d.DESCRIPTION, d.NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, " +
"d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DEVICE_TYPE dt WHERE " +
"d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE dt WHERE " +
"dt.NAME = ? AND d.DEVICE_IDENTIFICATION = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceIdentifier.getType());
@ -140,7 +140,42 @@ public class DeviceDAOImpl implements DeviceDAO {
@Override
public List<Device> getDevices() throws DeviceManagementDAOException {
return null;
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 ";
stmt = conn.prepareStatement(selectDBQueryForType);
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 all devices for type ";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
return devicesList;
}
@Override
@ -178,9 +213,7 @@ public class DeviceDAOImpl implements DeviceDAO {
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 " +
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);
@ -212,9 +245,50 @@ public class DeviceDAOImpl implements DeviceDAO {
return devicesList;
}
@Override
public List<Device> getDeviceListOfUser(String username) throws DeviceManagementDAOException {
return null;
@Override public List<Device> getDeviceListOfUser(String username, 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.OWNER =? AND DM_DEVICE.TENANT_ID =?");
stmt.setString(1, username);
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 belongs to " + username;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
}
return deviceList;
}
private Connection getConnection() throws DeviceManagementDAOException {

@ -22,21 +22,22 @@ import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager;
import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.DeviceManagementRepository;
import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig;
import org.wso2.carbon.device.mgt.core.config.license.LicenseConfigurationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementServiceImpl;
import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer;
import org.wso2.carbon.registry.core.service.RegistryService;
@ -94,6 +95,8 @@ public class DeviceManagementServiceComponent {
DeviceManagementDataHolder.getInstance().setLicenseManager(licenseManager);
DeviceManagementDataHolder.getInstance().setLicenseConfig(licenseConfig);
OperationManagementDAOFactory.init(dsConfig);
/* If -Dsetup option enabled then create device management database schema */
String setupOption =
System.getProperty(DeviceManagementConstants.Common.PROPERTY_SETUP);

@ -59,6 +59,9 @@ public class OperationManagementDAOFactory {
public static void init(DataSource dtSource) {
dataSource = dtSource;
}
public static void init(DataSourceConfig config) {
dataSource = resolveDataSource(config);
}
public static void beginTransaction() throws OperationManagementDAOException {
try {

@ -33,6 +33,8 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager,
List<Device> getAllDevices(String type) throws DeviceManagementException;
List<Device> getAllDevices() throws DeviceManagementException;
List<Device> getDeviceListOfUser(String username) throws DeviceManagementException;
}

@ -70,7 +70,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@Override
public List<Device> getAllDevices() throws DeviceManagementException {
return null;
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getAllDevices();
}
@Override
@ -80,11 +80,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@Override
public List<Device> getDeviceListOfUser(String username) throws DeviceManagementException{
return null;
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDeviceListOfUser(username);
}
@Override
public org.wso2.carbon.device.mgt.common.Device getDevice(DeviceIdentifier deviceId)
@Override public org.wso2.carbon.device.mgt.common.Device getDevice(DeviceIdentifier deviceId)
throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId);
}

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
@ -139,4 +140,8 @@ public final class DeviceManagerUtil {
}
return propertiesMap;
}
public static int getTenantId(){
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
return ctx.getTenantId();
}
}

Loading…
Cancel
Save