diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java index 2281dd0749..deebdb2701 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java @@ -110,11 +110,63 @@ public class Application implements Serializable { } public boolean equals(Object o) { - if (!(o instanceof Application)) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Application that = (Application) o; + + if (id != that.id) { + return false; + } + if (appProperties != null ? !appProperties.equals(that.appProperties) : that.appProperties != null) { return false; } - Application target = (Application)o; - return packageName.equals(target.getPackageName()); + + if (category != null ? !category.equals(that.category) : that.category != null) { + return false; + } + if (imageUrl != null ? !imageUrl.equals(that.imageUrl) : that.imageUrl != null) { + return false; + } + if (locationUrl != null ? !locationUrl.equals(that.locationUrl) : that.locationUrl != null) { + return false; + } + if (name != null ? !name.equals(that.name) : that.name != null) { + return false; + } + if (packageName != null ? !packageName.equals(that.packageName) : that.packageName != null) { + return false; + } + if (platform != null ? !platform.equals(that.platform) : that.platform != null) { + return false; + } + if (type != null ? !type.equals(that.type) : that.type != null) { + return false; + } + if (version != null ? !version.equals(that.version) : that.version != null) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = id; + result = 31 * result + (packageName != null ? packageName.hashCode() : 0); + result = 31 * result + (platform != null ? platform.hashCode() : 0); + result = 31 * result + (category != null ? category.hashCode() : 0); + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (locationUrl != null ? locationUrl.hashCode() : 0); + result = 31 * result + (imageUrl != null ? imageUrl.hashCode() : 0); + result = 31 * result + (version != null ? version.hashCode() : 0); + result = 31 * result + (type != null ? type.hashCode() : 0); + result = 31 * result + (appProperties != null ? appProperties.hashCode() : 0); + return result; } public Properties getAppProperties() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/ApplicationManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/ApplicationManagementProviderService.java index 70ff9225f9..08851712dc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/ApplicationManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/ApplicationManagementProviderService.java @@ -12,4 +12,6 @@ public interface ApplicationManagementProviderService extends ApplicationManager public void updateApplicationListInstallInDevice(DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException; + public List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) + throws ApplicationManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 82529a25b1..bf5a32e06e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -182,6 +182,21 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } } + @Override + public List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) + throws ApplicationManagementException { + Device device = null; + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + device = deviceDAO.getDevice(deviceIdentifier, tenantId); + return applicationMappingDAO.getInstalledApplications(device.getId()); + }catch (DeviceManagementDAOException deviceDaoEx) { + String errorMsg = "Error occured while fetching the Application List of device : " + device.getId(); + log.error(errorMsg, deviceDaoEx); + throw new ApplicationManagementException(errorMsg, deviceDaoEx); + } + } + @Override public void registerDeviceManagementService(DeviceManagementService deviceManagementService) { try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java index aea92929b7..9d759e0e22 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java @@ -31,7 +31,4 @@ public interface ApplicationDAO { int removeApplication(String applicationName, int tenantId) throws DeviceManagementDAOException; Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException; - - List getInstalledApplications(int deviceId) throws DeviceManagementDAOException; - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java index 1060b9cc6c..21ad380c25 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java @@ -18,6 +18,8 @@ */ package org.wso2.carbon.device.mgt.core.dao; +import org.wso2.carbon.device.mgt.common.app.mgt.Application; + import java.util.List; public interface ApplicationMappingDAO { @@ -29,4 +31,6 @@ public interface ApplicationMappingDAO { int removeApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException; + List getInstalledApplications(int deviceId) throws DeviceManagementDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index 3568fc29e0..0b18948cde 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -168,43 +168,6 @@ public class ApplicationDAOImpl implements ApplicationDAO { } } - @Override - public List getInstalledApplications(int deviceId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - List applications = new ArrayList(); - Application application; - ByteArrayInputStream bais; - ObjectInputStream ois; - - try { - conn = this.getConnection(); - stmt = conn.prepareStatement( - "SELECT DEVICE_ID, APPLICATIONS FROM DM_DEVICE_APPLICATIONS WHERE DEVICE_ID = ?"); - stmt.setInt(1, deviceId); - ResultSet rs = stmt.executeQuery(); - - while (rs.next()) { - byte[] applicationDetails = rs.getBytes("APPLICATIONS"); - bais = new ByteArrayInputStream(applicationDetails); - ois = new ObjectInputStream(bais); - application = (Application) ois.readObject(); - applications.add(application); - } - } catch (IOException e) { - throw new DeviceManagementDAOException("IO Error occurred while de serialize the Application object", e); - } catch (ClassNotFoundException e) { - throw new DeviceManagementDAOException("Class not found error occurred while de serialize the " + - "Application object", e); - } catch (SQLException e) { - throw new DeviceManagementDAOException("SQL Error occurred while retrieving the list of Applications " + - "installed in device id '" + deviceId, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return applications; - } - private Connection getConnection() throws DeviceManagementDAOException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index 5bc7236fb4..4a5bac617f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -18,11 +18,15 @@ */ package org.wso2.carbon.device.mgt.core.dao.impl; +import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.core.dao.ApplicationMappingDAO; 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.util.DeviceManagementDAOUtil; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -121,4 +125,40 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { return DeviceManagementDAOFactory.getConnection(); } + @Override + public List getInstalledApplications(int deviceId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + List applications = new ArrayList(); + Application application; + ByteArrayInputStream bais; + ObjectInputStream ois; + + try { + conn = this.getConnection(); + stmt = conn.prepareStatement( + "SELECT DEVICE_ID, APPLICATIONS FROM DM_DEVICE_APPLICATIONS WHERE DEVICE_ID = ?"); + stmt.setInt(1, deviceId); + ResultSet rs = stmt.executeQuery(); + + while (rs.next()) { + byte[] applicationDetails = rs.getBytes("APPLICATIONS"); + bais = new ByteArrayInputStream(applicationDetails); + ois = new ObjectInputStream(bais); + application = (Application) ois.readObject(); + applications.add(application); + } + } catch (IOException e) { + throw new DeviceManagementDAOException("IO Error occurred while de serialize the Application object", e); + } catch (ClassNotFoundException e) { + throw new DeviceManagementDAOException("Class not found error occurred while de serialize the " + + "Application object", e); + } catch (SQLException e) { + throw new DeviceManagementDAOException("SQL Error occurred while retrieving the list of Applications " + + "installed in device id '" + deviceId, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return applications; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java index 6522191686..b91f267541 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java @@ -65,7 +65,8 @@ public class ApplicationPersistenceDAOTests extends BaseDeviceManagementDAOTest Assert.fail(msg, e); } - Assert.assertEquals(target, source, "Application added is not as same as what's retrieved"); + Assert.assertEquals(target.getPackageName(), source.getPackageName(), "Application added is not as same as what's " + + "retrieved"); } private Application getApplication(String packageName, int tenantId) throws DeviceManagementDAOException { diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index b4bfe487a2..3b7f858567 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -276,8 +276,6 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA ( ON UPDATE NO ACTION ); - - CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( ID INT NOT NULL AUTO_INCREMENT, POLICY_CRITERION_ID INT NOT NULL, @@ -292,6 +290,45 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( ON UPDATE NO ACTION ); +CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OWNER VARCHAR(50) NOT NULL, + OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + STATUS VARCHAR(50) NULL, + DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, + DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL, + TENANT_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_APPLICATION ( + ID INTEGER AUTO_INCREMENT NOT NULL, + NAME VARCHAR(50) NOT NULL, + PACKAGE_NAME VARCHAR(50) NOT NULL, + PLATFORM VARCHAR(50) NULL DEFAULT NULL, + CATEGORY VARCHAR(50) NULL, + VERSION VARCHAR(50) NULL, + TYPE VARCHAR(50) NULL, + LOCATION_URL VARCHAR(100) NULL DEFAULT NULL, + IMAGE_URL VARCHAR(100) NULL DEFAULT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + APPLICATION_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_application FOREIGN KEY (APPLICATION_ID) REFERENCES + DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -- POLICY RELATED TABLES FINISHED --