diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/pom.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/pom.xml index 339c19eb70f..9dd8104a5d3 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/pom.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/pom.xml @@ -97,7 +97,7 @@ org.apache.synapse, org.apache.synapse.core.axis2, org.apache.synapse.rest, - org.wso2.carbon.certificate.mgt.core.impl + org.wso2.carbon.certificate.mgt.core.* diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandler.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandler.java index 06d344a5140..c9873629b71 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandler.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandler.java @@ -30,6 +30,8 @@ import org.wso2.carbon.apimgt.handlers.invoker.RESTInvoker; import org.wso2.carbon.apimgt.handlers.invoker.RESTResponse; import org.wso2.carbon.apimgt.handlers.utils.AuthConstants; import org.wso2.carbon.apimgt.handlers.utils.Utils; +import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse; +import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.impl.CertificateGenerator; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -57,6 +59,7 @@ public class AuthenticationHandler extends AbstractHandler { private static final String AUTHORIZATION = "Authorization"; private static final String BEARER = "Basic "; private static final String CONTENT_TYPE = "Content-Type"; + private static final boolean USE_INTERNAL_CERT_VERIFIER = true; private IOTServerConfiguration iotServerConfiguration; @@ -125,19 +128,29 @@ public class AuthenticationHandler extends AbstractHandler { log.debug("Verify subject DN: " + subjectDN); } - String deviceType = this.getDeviceType(messageContext.getTo().getAddress().trim()); - URI certVerifyUrl = new URI(iotServerConfiguration.getVerificationEndpoint() + deviceType); - Map certVerifyHeaders = this.setHeaders(); - Certificate certificate = new Certificate(); - certificate.setPem(subjectDN); - certificate.setTenantId(tenantId); - certificate.setSerial(AuthConstants.PROXY_MUTUAL_AUTH_HEADER); - - Gson gson = new Gson(); - String certVerifyContent = gson.toJson(certificate); - response = restInvoker.invokePOST(certVerifyUrl, certVerifyHeaders, certVerifyContent); - if (log.isDebugEnabled()) { - log.debug("Verify response:" + response.getContent()); + if (USE_INTERNAL_CERT_VERIFIER) { + CertificateResponse certificateResponse = Utils.getCertificateManagementService() + .verifySubjectDN(subjectDN); + if (certificateResponse != null && certificateResponse.getCommonName() != null + && !certificateResponse.getCommonName().isEmpty()) { + return true; + } + } else { + String deviceType = this.getDeviceType(messageContext.getTo().getAddress().trim()); + URI certVerifyUrl = new URI(iotServerConfiguration.getVerificationEndpoint() + deviceType); + Map certVerifyHeaders = this.setHeaders(); + + Certificate certificate = new Certificate(); + certificate.setPem(subjectDN); + certificate.setTenantId(tenantId); + certificate.setSerial(AuthConstants.PROXY_MUTUAL_AUTH_HEADER); + + Gson gson = new Gson(); + String certVerifyContent = gson.toJson(certificate); + response = restInvoker.invokePOST(certVerifyUrl, certVerifyHeaders, certVerifyContent); + if (log.isDebugEnabled()) { + log.debug("Verify response:" + response.getContent()); + } } } else if (headers.containsKey(AuthConstants.MUTUAL_AUTH_HEADER)) { javax.security.cert.X509Certificate[] certs = @@ -190,6 +203,9 @@ public class AuthenticationHandler extends AbstractHandler { } catch (CertificateEncodingException e) { log.error("Error while attempting to encode certificate.", e); return false; + } catch (KeystoreException e) { + log.error("Error while attempting to validate certificate.", e); + return false; } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/Utils.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/Utils.java index 5be2c187054..30f07f9e68c 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/Utils.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/main/java/org/wso2/carbon/apimgt/handlers/utils/Utils.java @@ -30,6 +30,8 @@ import org.wso2.carbon.apimgt.handlers.beans.DCR; import org.wso2.carbon.apimgt.handlers.config.IOTServerConfiguration; import org.wso2.carbon.apimgt.handlers.invoker.RESTInvoker; import org.wso2.carbon.apimgt.handlers.invoker.RESTResponse; +import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.utils.CarbonUtils; import javax.xml.XMLConstants; @@ -184,5 +186,20 @@ public class Utils { } + public static CertificateManagementService getCertificateManagementService() { + + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + CertificateManagementService certificateManagementService = (CertificateManagementService) + ctx.getOSGiService(CertificateManagementService.class, null); + + if (certificateManagementService == null) { + String msg = "CertificateManagementAdminServiceImpl Management service not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + + return certificateManagementService; + } + } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandlerTest.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandlerTest.java index 23e6b251fcf..5a9370a56ef 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandlerTest.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.handlers/src/test/java/org/wso2/carbon/apimgt/handlers/AuthenticationHandlerTest.java @@ -99,21 +99,8 @@ public class AuthenticationHandlerTest extends BaseAPIHandlerTest { this.mockClient.reset(); } - @Test(description = "Handle request with device type URI with Proxy Mutual Auth Header", - dependsOnMethods = "testHandleSuccessfulRequestMDMCertificate") - public void testHandleSuccessRequestProxyMutualAuthHeader() throws Exception { - HashMap transportHeaders = new HashMap<>(); - transportHeaders.put(AuthConstants.PROXY_MUTUAL_AUTH_HEADER, "Test Header"); - setMockClient(); - this.mockClient.setResponse(getValidationResponse()); - boolean response = this.handler.handleRequest(createSynapseMessageContext("", this.synapseConfiguration, - transportHeaders, "https://test.com/testservice/device-mgt/testdevice")); - Assert.assertTrue(response); - this.mockClient.reset(); - } - @Test(description = "Handle request with device type URI with Mutual Auth Header", - dependsOnMethods = "testHandleSuccessRequestProxyMutualAuthHeader") + dependsOnMethods = "testHandleSuccessfulRequestMDMCertificate") public void testHandleSuccessRequestMutualAuthHeader() throws Exception { HashMap transportHeaders = new HashMap<>(); transportHeaders.put(AuthConstants.MUTUAL_AUTH_HEADER, "Test Header"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceTest.java index 5b63d2de9b3..f183f2e10ba 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceTest.java @@ -759,7 +759,7 @@ public class DeviceAgentServiceTest { List deviceTypes = new ArrayList<>(); deviceTypes.add(TEST_DEVICE_TYPE); Mockito.when(this.deviceManagementProviderService.getAvailableDeviceTypes()).thenReturn(deviceTypes); - Mockito.when(this.deviceManagementProviderService.getPendingOperations(Mockito.any())).thenThrow(new + Mockito.when(this.deviceManagementProviderService.getPendingOperations(Mockito.any(DeviceIdentifier.class))).thenThrow(new OperationManagementException()); Response response = this.deviceAgentService.getPendingOperations(TEST_DEVICE_TYPE, TEST_DEVICE_IDENTIFIER); Assert.assertNotNull(response, "Response should not be null"); @@ -973,7 +973,7 @@ public class DeviceAgentServiceTest { deviceTypes.add(TEST_DEVICE_TYPE); Mockito.when(this.deviceManagementProviderService.getAvailableDeviceTypes()).thenReturn(deviceTypes); Mockito.doThrow(new OperationManagementException()).when(this.deviceManagementProviderService) - .updateOperation(Mockito.any(), Mockito.any()); + .updateOperation(Mockito.any(DeviceIdentifier.class), Mockito.any()); Response response = this.deviceAgentService.updateOperation(TEST_DEVICE_TYPE, TEST_DEVICE_IDENTIFIER, operation); Assert.assertNotNull(response, "The response should not be null"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java index 720383952e8..c46c56adf3d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.mgt.common.operation.mgt; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; @@ -74,8 +75,11 @@ public interface OperationManager { * @throws OperationManagementException If some unusual behaviour is observed while fetching the * operation list. */ + @Deprecated List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException; + List getPendingOperations(Device device) throws OperationManagementException; + Operation getNextPendingOperation(DeviceIdentifier deviceId, long notNowOperationFrequency) throws OperationManagementException; @@ -83,6 +87,8 @@ public interface OperationManager { void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException; + void updateOperation(int enrolmentId, Operation operation) throws OperationManagementException; + Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderService.java index 0cf17d53a2a..6ef83d83fb2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderService.java @@ -15,6 +15,7 @@ */ package org.wso2.carbon.device.mgt.core.app.mgt; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; @@ -24,10 +25,17 @@ import java.util.List; public interface ApplicationManagementProviderService extends ApplicationManager{ - void updateApplicationListInstalledInDevice(DeviceIdentifier deviceIdentifier, - List applications) throws ApplicationManagementException; + @Deprecated + void updateApplicationListInstalledInDevice(DeviceIdentifier deviceIdentifier, List applications) + throws ApplicationManagementException; + + void updateApplicationListInstalledInDevice(Device device, List applications) + throws ApplicationManagementException; + @Deprecated List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws ApplicationManagementException; + List getApplicationListForDevice(Device device) + 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 60209c0c13d..e9cc0583ff5 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 @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.app.mgt; +import com.google.gson.Gson; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -35,38 +36,31 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig; import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO; -import org.wso2.carbon.device.mgt.core.dao.ApplicationMappingDAO; -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.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Implements Application Manager interface */ public class ApplicationManagerProviderServiceImpl implements ApplicationManagementProviderService { - private DeviceDAO deviceDAO; private ApplicationDAO applicationDAO; - private ApplicationMappingDAO applicationMappingDAO; - private static final String GET_APP_LIST_URL = "store/apis/assets/mobileapp?domain=carbon.super&page=1"; private static final Log log = LogFactory.getLog(ApplicationManagerProviderServiceImpl.class); public ApplicationManagerProviderServiceImpl(AppManagementConfig appManagementConfig) { - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); - this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO(); } ApplicationManagerProviderServiceImpl() { - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); - this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO(); } @Override @@ -146,10 +140,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } catch (DeviceManagementException e) { throw new ApplicationManagementException("Error in get devices for user: " + userName + " in app installation", e); - } catch (OperationManagementException e) { throw new ApplicationManagementException("Error in add operation at app installation", e); - } } @@ -190,103 +182,114 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } catch (OperationManagementException e) { throw new ApplicationManagementException("Error in add operation at app installation", e); - } } @Override - public void updateApplicationListInstalledInDevice( - DeviceIdentifier deviceIdentifier, + public void updateApplicationListInstalledInDevice(DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException { if (log.isDebugEnabled()) { log.debug("Updating application list for device: " + deviceIdentifier.toString()); } - List installedAppList = getApplicationListForDevice(deviceIdentifier); try { - Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier, - false); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getDevice(deviceIdentifier, false); + updateApplicationListInstalledInDevice(device, applications); + } catch (DeviceManagementException e) { + String msg = "Error occurred obtaining the device object for device " + deviceIdentifier.toString(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + } + + @Override + public void updateApplicationListInstalledInDevice(Device device, List newApplications) + throws ApplicationManagementException { + if (log.isDebugEnabled()) { + log.debug("Updating application list for device: " + device.getDeviceIdentifier()); + log.debug("Apps in device: " + new Gson().toJson(newApplications)); + } + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceManagementDAOFactory.beginTransaction(); + List installedAppList = applicationDAO + .getInstalledApplications(device.getId(), device.getEnrolmentInfo().getId(), tenantId); if (log.isDebugEnabled()) { - log.debug("Number of apps installed:" + installedAppList.size()); + log.debug("Previous app list: " + new Gson().toJson(installedAppList)); } - List appsToAdd = new ArrayList<>(); - List appIdsToRemove = new ArrayList<>(installedAppList.size()); - for (Application installedApp : installedAppList) { - if (!applications.contains(installedApp)) { - if (log.isDebugEnabled()) { - log.debug("Remove app Id:" + installedApp.getId()); + Map appsToRemove = new HashMap<>(); + Map appsToUpdate = new HashMap<>(); + Map appsToInsert = new HashMap<>(); + + Map installedApps = new HashMap<>(); + boolean removable; + for (Application installedApp: installedAppList) { + removable = true; + for (Application newApp : newApplications) { + if (newApp.getApplicationIdentifier().equals(installedApp.getApplicationIdentifier())) { + removable = false; + break; } - appIdsToRemove.add(installedApp.getId()); + } + if (removable) { + appsToRemove.put(installedApp.getApplicationIdentifier(), installedApp); + } else { + installedApps.put(installedApp.getApplicationIdentifier(), installedApp); } } - DeviceManagementDAOFactory.beginTransaction(); - applicationMappingDAO.removeApplicationMapping(device.getId(), device.getEnrolmentInfo().getId(), - appIdsToRemove, tenantId); - Application installedApp; - List applicationIds = new ArrayList<>(); - List applicationsToMap = new ArrayList<>(); - - for (Application application : applications) { - // Adding N/A if application doesn't have a version. Also truncating the application version, - // if length of the version is greater than maximum allowed length. - if (application.getVersion() == null) { - application.setVersion("N/A"); - } else if (application.getVersion().length() > - DeviceManagementConstants.OperationAttributes.APPLIST_VERSION_MAX_LENGTH) { - application.setVersion(StringUtils.abbreviate(application.getVersion(), + + for (Application newApp : newApplications) { + if (newApp.getVersion() == null) { + newApp.setVersion("N/A"); + } else if (newApp.getVersion().length() + > DeviceManagementConstants.OperationAttributes.APPLIST_VERSION_MAX_LENGTH) { + newApp.setVersion(StringUtils.abbreviate(newApp.getVersion(), DeviceManagementConstants.OperationAttributes.APPLIST_VERSION_MAX_LENGTH)); } - if (!installedAppList.contains(application)) { - installedApp = applicationDAO.getApplication(application.getApplicationIdentifier(), - application.getVersion(), tenantId); - if (installedApp == null) { - appsToAdd.add(application); - } else { - application.setId(installedApp.getId()); - applicationsToMap.add(application); + if (installedApps.containsKey(newApp.getApplicationIdentifier())) { + Application oldApp = installedApps.get(newApp.getApplicationIdentifier()); + if (oldApp.isActive() != newApp.isActive() || oldApp.getMemoryUsage() != newApp.getMemoryUsage() + || !newApp.getVersion().equals(oldApp.getVersion())) { + newApp.setId(oldApp.getId()); + appsToUpdate.put(newApp.getApplicationIdentifier(), newApp); } + } else { + appsToInsert.put(newApp.getApplicationIdentifier(), newApp); } } if (log.isDebugEnabled()) { - log.debug("num of apps add:" + appsToAdd.size()); + log.debug("Apps to remove: " + new Gson().toJson(appsToRemove.values())); + log.debug("Apps to update: " + new Gson().toJson(appsToUpdate.values())); + log.debug("Apps to insert: " + new Gson().toJson(appsToInsert.values())); } - applicationIds.addAll(applicationDAO.addApplications(appsToAdd, tenantId)); - // Getting the applications ids for the second time - for (Application application : appsToAdd) { - installedApp = applicationDAO.getApplication(application.getApplicationIdentifier(), - application.getVersion(), tenantId); - application.setId(installedApp.getId()); - applicationsToMap.add(application); + if (!appsToRemove.isEmpty()) { + applicationDAO.removeApplications(new ArrayList<>(appsToRemove.values()), device.getId(), + device.getEnrolmentInfo().getId(), tenantId); } - - if (log.isDebugEnabled()) { - log.debug("num of app Ids:" + applicationIds.size()); + if (!appsToUpdate.isEmpty()) { + applicationDAO.updateApplications(new ArrayList<>(appsToUpdate.values()), device.getId(), + device.getEnrolmentInfo().getId(), tenantId); } - applicationMappingDAO.addApplicationMappingsWithApps(device.getId(), device.getEnrolmentInfo().getId(), - applicationsToMap, tenantId); - - if (log.isDebugEnabled()) { - log.debug("num of remove app Ids:" + appIdsToRemove.size()); + if (!appsToInsert.isEmpty()) { + applicationDAO.addApplications(new ArrayList<>(appsToInsert.values()), device.getId(), + device.getEnrolmentInfo().getId(), tenantId); } DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred saving application list of the device " + deviceIdentifier.toString(); + String msg = "Error occurred saving application list of the device " + device.getDeviceIdentifier(); log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (TransactionManagementException e) { - String msg = "Error occurred while initializing transaction for saving application list to the device " - + deviceIdentifier.toString(); - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DeviceManagementException e) { - String msg = "Error occurred obtaining the device object for device " + deviceIdentifier.toString(); + String msg = + "Error occurred while initializing transaction for saving application list to the device " + device + .getDeviceIdentifier(); log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (Exception e) { - String msg = "Exception occurred saving application list of the device " + deviceIdentifier.toString(); + String msg = "Exception occurred saving application list of the device " + device.getDeviceIdentifier(); log.error(msg, e); throw new ApplicationManagementException(msg, e); } finally { @@ -312,20 +315,26 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } return new ArrayList<>(); } + return getApplicationListForDevice(device); + } + + @Override + public List getApplicationListForDevice(Device device) throws ApplicationManagementException { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { DeviceManagementDAOFactory.openConnection(); - return applicationDAO.getInstalledApplications(device.getId(), device.getEnrolmentInfo().getId()); + return applicationDAO.getInstalledApplications(device.getId(), device.getEnrolmentInfo().getId(), tenantId); } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while fetching the Application List of device " + deviceId.toString(); + String msg = "Error occurred while fetching the Application List of device " + device.getDeviceIdentifier(); log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source to get application " + - "list of the device " + deviceId.toString(); + "list of the device " + device.getDeviceIdentifier(); log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (Exception e) { - String msg = "Exception occurred getting application list of the device " + deviceId.toString(); + String msg = "Exception occurred getting application list of the device " + device.getDeviceIdentifier(); log.error(msg, e); throw new ApplicationManagementException(msg, e); } finally { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/ArchivalServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/ArchivalServiceImpl.java index 33dffa1c8f2..e41152d47d1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/ArchivalServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/ArchivalServiceImpl.java @@ -171,8 +171,6 @@ public class ArchivalServiceImpl implements ArchivalService { openConnection(); operationResponses = archivalDAO.selectOperationResponses(); notification = archivalDAO.selectNotifications(); - commandOperations = archivalDAO.selectCommandOperations(); - profileOperations = archivalDAO.selectProfileOperations(); enrollmentMapping = archivalDAO.selectEnrolmentMappings(); operations = archivalDAO.selectOperations(); @@ -199,18 +197,6 @@ public class ArchivalServiceImpl implements ArchivalService { } archivalDAO.moveNotifications(notification); - //Purge the command operations table, DM_COMMAND_OPERATION - if (log.isDebugEnabled()) { - log.debug("## Archiving command operations"); - } - archivalDAO.moveCommandOperations(commandOperations); - - //Purge the profile operation table, DM_PROFILE_OPERATION - if (log.isDebugEnabled()) { - log.debug("## Archiving profile operations"); - } - archivalDAO.moveProfileOperations(profileOperations); - //Purge the enrolment mappings table, DM_ENROLMENT_OP_MAPPING if (log.isDebugEnabled()) { log.debug("## Archiving enrolment mappings"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/beans/ArchiveOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/beans/ArchiveOperation.java index d62fcddceb6..1826d7089d8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/beans/ArchiveOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/beans/ArchiveOperation.java @@ -29,6 +29,10 @@ public class ArchiveOperation { private Timestamp recievedTimeStamp; private String operationCode; + private Object operationDetails; + private String initiatedBy; + private boolean enabled; + public int getId() { return id; } @@ -68,5 +72,29 @@ public class ArchiveOperation { public void setOperationCode(String operationCode) { this.operationCode = operationCode; } + + public Object getOperationDetails() { + return operationDetails; + } + + public void setOperationDetails(Object operationDetails) { + this.operationDetails = operationDetails; + } + + public String getInitiatedBy() { + return initiatedBy; + } + + public void setInitiatedBy(String initiatedBy) { + this.initiatedBy = initiatedBy; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/dao/ArchivalDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/dao/ArchivalDAO.java index a360a8cfe52..8fc598d1696 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/dao/ArchivalDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/dao/ArchivalDAO.java @@ -46,12 +46,8 @@ public interface ArchivalDAO { List selectCommandOperations() throws ArchivalDAOException; - void moveCommandOperations(List rs) throws ArchivalDAOException; - List selectProfileOperations() throws ArchivalDAOException; - void moveProfileOperations(List rs) throws ArchivalDAOException; - List selectEnrolmentMappings() throws ArchivalDAOException; void moveEnrolmentMappings(List rs) throws ArchivalDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/dao/impl/ArchivalDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/dao/impl/ArchivalDAOImpl.java index 8763f866e0f..ed89a0a30e8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/dao/impl/ArchivalDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/archival/dao/impl/ArchivalDAOImpl.java @@ -398,56 +398,6 @@ public class ArchivalDAOImpl implements ArchivalDAO { return commandOperations; } - @Override - public void moveCommandOperations(List commandOperations) throws ArchivalDAOException { - Statement stmt = null; - PreparedStatement stmt2 = null; - Statement stmt3 = null; - try { - Connection conn = ArchivalSourceDAOFactory.getConnection(); - Connection conn2 = ArchivalDestinationDAOFactory.getConnection(); - - String sql = "INSERT INTO DM_COMMAND_OPERATION_ARCH VALUES(?,?,?)"; - stmt2 = conn2.prepareStatement(sql); - - int count = 0; - for (ArchiveCommandOperation rs : commandOperations) { - stmt2.setInt(1, rs.getOperationId()); - stmt2.setInt(2, rs.getEnabled()); - stmt2.setTimestamp(3, this.currentTimestamp); - stmt2.addBatch(); - - if (++count % batchSize == 0) { - stmt2.executeBatch(); - if (log.isDebugEnabled()) { - log.debug("Executing Command Operations batch " + count); - } - } - } - stmt2.executeBatch(); - if (log.isDebugEnabled()) { - log.debug(count + " [COMMAND_OPERATION] Records copied to the archival table. Starting deletion"); - } - sql = "DELETE o.* FROM DM_COMMAND_OPERATION o\n" + - " INNER JOIN\n" + - " DM_ARCHIVED_OPERATIONS da ON o.OPERATION_ID = da.ID \n" + - "WHERE\n" + - " o.OPERATION_ID = da.ID;"; - stmt3 = conn.createStatement(); - int affected = stmt3.executeUpdate(sql); - if (log.isDebugEnabled()) { - log.debug(affected + " Rows deleted"); - } - } catch (SQLException e) { - String msg = "Error occurred while archiving the command operation"; - log.error(msg, e); - throw new ArchivalDAOException(msg, e); - } finally { - ArchivalDAOUtil.cleanupResources(stmt2); - ArchivalDAOUtil.cleanupResources(stmt3); - } - } - @Override public List selectProfileOperations() throws ArchivalDAOException { Statement stmt = null; @@ -487,57 +437,6 @@ public class ArchivalDAOImpl implements ArchivalDAO { return profileOperations; } - @Override - public void moveProfileOperations(List profileOperations) throws ArchivalDAOException { - Statement stmt = null; - PreparedStatement stmt2 = null; - Statement stmt3 = null; - try { - Connection conn = ArchivalSourceDAOFactory.getConnection(); - - Connection conn2 = ArchivalDestinationDAOFactory.getConnection(); - - String sql = "INSERT INTO DM_PROFILE_OPERATION_ARCH VALUES(?, ?, ?, ?)"; - stmt2 = conn2.prepareStatement(sql); - - int count = 0; - for (ArchiveProfileOperation rs : profileOperations) { - stmt2.setInt(1, rs.getOperationId()); - stmt2.setInt(2, rs.getEnabled()); - stmt2.setBytes(3, (byte[]) rs.getOperationDetails()); - stmt2.setTimestamp(4, this.currentTimestamp); - stmt2.addBatch(); - - if (++count % batchSize == 0) { - stmt2.executeBatch(); - if (log.isDebugEnabled()) { - log.debug("Executing Profile Operations batch " + count); - } - } - } - stmt2.executeBatch(); - if (log.isDebugEnabled()) { - log.debug(count + " [PROFILE_OPERATION] Records copied to the archival table. Starting deletion"); - } - sql = "DELETE o.* FROM DM_PROFILE_OPERATION o\n" + - " INNER JOIN\n" + - " DM_ARCHIVED_OPERATIONS da ON o.OPERATION_ID = da.ID \n" + - "WHERE\n" + - " o.OPERATION_ID = da.ID;"; - stmt3 = conn.createStatement(); - int affected = stmt3.executeUpdate(sql); - if (log.isDebugEnabled()) { - log.debug(affected + " Rows deleted"); - } - } catch (SQLException e) { - String msg = "Error occurred while archiving the profile operation"; - log.error(msg, e); - throw new ArchivalDAOException(msg, e); - } finally { - ArchivalDAOUtil.cleanupResources(stmt2); - ArchivalDAOUtil.cleanupResources(stmt3); - } - } @Override public List selectEnrolmentMappings() throws ArchivalDAOException { @@ -652,7 +551,10 @@ public class ArchivalDAOImpl implements ArchivalDAO { " o.TYPE,\n" + " o.CREATED_TIMESTAMP,\n" + " o.RECEIVED_TIMESTAMP,\n" + - " o.OPERATION_CODE\n" + + " o.OPERATION_CODE,\n" + + " o.INITIATED_BY,\n" + + " o.OPERATION_DETAILS,\n" + + " o.ENABLED \n" + "FROM\n" + " DM_OPERATION o\n" + " INNER JOIN\n" + @@ -668,6 +570,9 @@ public class ArchivalDAOImpl implements ArchivalDAO { op.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP")); op.setRecievedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP")); op.setOperationCode(rs.getString("OPERATION_CODE")); + op.setInitiatedBy(rs.getString("INITIATED_BY")); + op.setOperationDetails(rs.getObject("OPERATION_DETAILS")); + op.setEnabled(rs.getBoolean("ENABLED")); operations.add(op); @@ -694,7 +599,7 @@ public class ArchivalDAOImpl implements ArchivalDAO { try { Connection conn = ArchivalSourceDAOFactory.getConnection(); Connection conn2 = ArchivalDestinationDAOFactory.getConnection(); - String sql = "INSERT INTO DM_OPERATION_ARCH VALUES(?, ?, ?, ?, ?, ?)"; + String sql = "INSERT INTO DM_OPERATION_ARCH VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; stmt2 = conn2.prepareStatement(sql); int count = 0; @@ -704,7 +609,11 @@ public class ArchivalDAOImpl implements ArchivalDAO { stmt2.setTimestamp(3, rs.getCreatedTimeStamp()); stmt2.setTimestamp(4, rs.getRecievedTimeStamp()); stmt2.setString(5, rs.getOperationCode()); - stmt2.setTimestamp(6, this.currentTimestamp); + stmt2.setString(6, rs.getInitiatedBy()); + stmt2.setBytes(7, (byte[]) rs.getOperationDetails()); + stmt2.setBoolean(8, rs.isEnabled()); + + stmt2.setTimestamp(9, this.currentTimestamp); stmt2.addBatch(); if (++count % batchSize == 0) { 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 a84d4099584..78cf35a2280 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 @@ -26,11 +26,14 @@ import java.util.List; public interface ApplicationDAO { - int addApplication(Application application, int tenantId) throws DeviceManagementDAOException; + void addApplications(List applications, int deviceId, int enrolmentId, int tenantId) + throws DeviceManagementDAOException; - List addApplications(List applications, int tenantId) throws DeviceManagementDAOException; + void updateApplications(List applications, int deviceId, int enrolmentId, int tenantId) + throws DeviceManagementDAOException; - List removeApplications(List apps, int tenantId) throws DeviceManagementDAOException; + void removeApplications(List apps, int deviceId, int enrolmentId, int tenantId) + throws DeviceManagementDAOException; Application getApplication(String identifier, int tenantId) throws DeviceManagementDAOException; @@ -39,7 +42,7 @@ public interface ApplicationDAO { Application getApplication(String identifier, String version, int deviceId, int enrolmentId, int tenantId) throws DeviceManagementDAOException; - List getInstalledApplications(int deviceId, int enrolmentId) throws DeviceManagementDAOException; + List getInstalledApplications(int deviceId, int enrolmentId, int tenantId) throws DeviceManagementDAOException; /** * This method is used to get a list of applications installed in all enrolled devices 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 deleted file mode 100644 index eb96a6185a3..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -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 { - - int addApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException; - - void addApplicationMappings(int deviceId, List applicationIds, int tenantId) - throws DeviceManagementDAOException; - - void addApplicationMappingsWithApps(int deviceId, int enrolmentId, List applications, int tenantId) - throws DeviceManagementDAOException; - - void removeApplicationMapping(int deviceId, int enrolmentId, List appIdList, int tenantId) - throws DeviceManagementDAOException; -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index 5b5a0bb80d8..78632c3e65d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -26,11 +26,9 @@ import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementExcepti import org.wso2.carbon.device.mgt.common.exceptions.UnsupportedDatabaseEngineException; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; -import org.wso2.carbon.device.mgt.core.dao.impl.ApplicationMappingDAOImpl; +import org.wso2.carbon.device.mgt.core.dao.impl.ApplicationDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.DeviceTypeDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.EnrollmentDAOImpl; -import org.wso2.carbon.device.mgt.core.dao.impl.GenericApplicationDAOImpl; -import org.wso2.carbon.device.mgt.core.dao.impl.PostgreSQLApplicationDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.device.GenericDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.device.OracleDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.impl.device.PostgreSQLDeviceDAOImpl; @@ -131,12 +129,11 @@ public class DeviceManagementDAOFactory { if (databaseEngine != null) { switch (databaseEngine) { case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: - return new PostgreSQLApplicationDAOImpl(); case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: - return new GenericApplicationDAOImpl(); + return new ApplicationDAOImpl(); default: throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } @@ -144,10 +141,6 @@ public class DeviceManagementDAOFactory { throw new IllegalStateException("Database engine has not initialized properly."); } - public static ApplicationMappingDAO getApplicationMappingDAO() { - return new ApplicationMappingDAOImpl(); - } - public static DeviceDetailsDAO getDeviceDetailsDAO() { return new DeviceDetailsDAOImpl(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java similarity index 73% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index 31a81e6ca0b..9c3af9e0b92 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -41,95 +41,101 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; -public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { +public class ApplicationDAOImpl implements ApplicationDAO { - private static final Log log = LogFactory.getLog(AbstractApplicationDAOImpl.class); + private static final Log log = LogFactory.getLog(ApplicationDAOImpl.class); @Override - public int addApplication(Application application, int tenantId) throws DeviceManagementDAOException { + public void addApplications(List applications, int deviceId, int enrolmentId, + int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; - ResultSet rs = null; - ByteArrayOutputStream bao = null; - ObjectOutputStream oos = null; - int applicationId = -1; try { conn = this.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, CATEGORY, " + - "VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID, APP_PROPERTIES, APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); - - stmt.setString(1, application.getName()); - stmt.setString(2, application.getPlatform()); - stmt.setString(3, application.getCategory()); - stmt.setString(4, application.getVersion()); - stmt.setString(5, application.getType()); - stmt.setString(6, application.getLocationUrl()); - stmt.setString(7, application.getImageUrl()); - stmt.setInt(8, tenantId); - - bao = new ByteArrayOutputStream(); - oos = new ObjectOutputStream(bao); - oos.writeObject(application.getAppProperties()); - stmt.setBytes(9, bao.toByteArray()); - - stmt.setString(10, application.getApplicationIdentifier()); - stmt.setInt(11, application.getMemoryUsage()); - stmt.setBoolean(12, application.isActive()); - stmt.execute(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - applicationId = rs.getInt(1); + stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " + + "CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID, " + + "APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE, DEVICE_ID, ENROLMENT_ID) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + + for (Application application : applications) { + stmt.setString(1, application.getName()); + stmt.setString(2, application.getPlatform()); + stmt.setString(3, application.getCategory()); + stmt.setString(4, application.getVersion()); + stmt.setString(5, application.getType()); + stmt.setString(6, application.getLocationUrl()); + stmt.setString(7, application.getImageUrl()); + stmt.setInt(8, tenantId); + stmt.setString(9, application.getApplicationIdentifier()); + stmt.setInt(10, application.getMemoryUsage()); + stmt.setBoolean(11, application.isActive()); + stmt.setInt(12, deviceId); + stmt.setInt(13, enrolmentId); + stmt.addBatch(); } - return applicationId; + stmt.executeBatch(); } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while adding application '" + - application.getName() + "'", e); - } catch (IOException e) { - throw new DeviceManagementDAOException("Error occurred while serializing application properties object", e); + throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e); } finally { - if (bao != null) { - try { - bao.close(); - } catch (IOException e) { - log.error("Error occurred while closing ByteArrayOutputStream", e); - } - } - if (oos != null) { - try { - oos.close(); - } catch (IOException e) { - log.error("Error occurred while closing ObjectOutputStream", e); - } + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void updateApplications(List applications, int deviceId, int enrolmentId, + int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + stmt = conn.prepareStatement("UPDATE DM_APPLICATION SET NAME = ?, PLATFORM = ?, CATEGORY = ?, " + + "VERSION = ?, TYPE = ?, LOCATION_URL = ?, IMAGE_URL = ?, MEMORY_USAGE = ?, IS_ACTIVE = ? " + + "WHERE APP_IDENTIFIER = ? AND DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ?"); + + for (Application application : applications) { + stmt.setString(1, application.getName()); + stmt.setString(2, application.getPlatform()); + stmt.setString(3, application.getCategory()); + stmt.setString(4, application.getVersion()); + stmt.setString(5, application.getType()); + stmt.setString(6, application.getLocationUrl()); + stmt.setString(7, application.getImageUrl()); + stmt.setInt(8, application.getMemoryUsage()); + stmt.setBoolean(9, application.isActive()); + stmt.setString(10, application.getApplicationIdentifier()); + stmt.setInt(11, deviceId); + stmt.setInt(12, enrolmentId); + stmt.setInt(13, tenantId); + stmt.addBatch(); } - DeviceManagementDAOUtil.cleanupResources(stmt, rs); + stmt.executeBatch(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); } } @Override - public List removeApplications(List apps, int tenantId) throws DeviceManagementDAOException { + public void removeApplications(List apps, int deviceId, int enrolmentId, int tenantId) + throws DeviceManagementDAOException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; - List applicationIds = new ArrayList<>(); try { conn = this.getConnection(); conn.setAutoCommit(false); - stmt = conn.prepareStatement("DELETE DM_APPLICATION WHERE APP_IDENTIFIER = ? AND TENANT_ID = ?", - new String[]{"id"}); + stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION WHERE APP_IDENTIFIER = ? AND DEVICE_ID = ? " + + "AND ENROLMENT_ID = ? AND TENANT_ID = ?"); for (Application app : apps) { stmt.setString(1, app.getApplicationIdentifier()); - stmt.setInt(2, tenantId); + stmt.setInt(2, deviceId); + stmt.setInt(3, enrolmentId); + stmt.setInt(4, tenantId); stmt.addBatch(); } stmt.executeBatch(); - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - applicationIds.add(rs.getInt(1)); - } - return applicationIds; } catch (SQLException e) { try { if (conn != null) { @@ -210,11 +216,9 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { try { conn = this.getConnection(); stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + - "LOCATION_URL, IMAGE_URL, appmap.APP_PROPERTIES, appmap.MEMORY_USAGE, appmap.IS_ACTIVE, TENANT_ID " + - "FROM DM_APPLICATION app INNER JOIN (SELECT APPLICATION_ID, APP_PROPERTIES, MEMORY_USAGE, " + - "IS_ACTIVE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?) appmap " + - "WHERE app.APP_IDENTIFIER = ? AND app.VERSION = ? AND " + - "appmap.APPLICATION_ID = app.id AND TENANT_ID = ?"); + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID " + + "FROM DM_APPLICATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND APP_IDENTIFIER = ? AND " + + "VERSION = ? AND TENANT_ID = ?"); stmt.setInt(1, deviceId); stmt.setInt(2, enrolmentId); stmt.setString(3, identifier); @@ -239,7 +243,8 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { } @Override - public List getInstalledApplications(int deviceId, int enrolmentId) throws DeviceManagementDAOException { + public List getInstalledApplications(int deviceId, int enrolmentId, int tenantId) + throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; List applications = new ArrayList<>(); @@ -247,16 +252,13 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { ResultSet rs = null; try { conn = this.getConnection(); - stmt = conn.prepareStatement("Select ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + - "LOCATION_URL, IMAGE_URL, APPMAP.APP_PROPERTIES, APPMAP.MEMORY_USAGE, APPMAP.IS_ACTIVE, " + - "TENANT_ID From DM_APPLICATION app INNER JOIN " + - "(Select APPLICATION_ID, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE" + - " From DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID=? AND ENROLMENT_ID = ?) APPMAP " + - "ON " + - "app.ID = APPMAP.APPLICATION_ID "); + stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID FROM DM_APPLICATION " + + "WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ?"); stmt.setInt(1, deviceId); stmt.setInt(2, enrolmentId); + stmt.setInt(3, tenantId); rs = stmt.executeQuery(); while (rs.next()) { 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 deleted file mode 100644 index 8642620ccbb..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.wso2.carbon.device.mgt.core.dao.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -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.*; -import java.sql.*; -import java.util.List; - -public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { - - private static final Log log = LogFactory.getLog(ApplicationMappingDAOImpl.class); - - @Override - public int addApplicationMapping(int deviceId, int applicationId, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - int mappingId = -1; - try { - conn = this.getConnection(); - String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + - "TENANT_ID) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[]{"id"}); - stmt.setInt(1, deviceId); - stmt.setInt(2, applicationId); - stmt.setInt(3, tenantId); - stmt.execute(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - mappingId = rs.getInt(1); - } - return mappingId; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public void addApplicationMappings(int deviceId, List applicationIds, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - - try { - conn = this.getConnection(); - String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + - "TENANT_ID) VALUES (?, ?, ?)"; - - conn.setAutoCommit(false); - stmt = conn.prepareStatement(sql); - - for (int applicationId : applicationIds) { - stmt.setInt(1, deviceId); - stmt.setInt(2, applicationId); - stmt.setInt(3, tenantId); - stmt.addBatch(); - } - stmt.executeBatch(); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - } - - @Override - public void addApplicationMappingsWithApps(int deviceId, int enrolmentId, List applications, int tenantId) - throws DeviceManagementDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - ByteArrayOutputStream bao = null; - ObjectOutputStream oos = null; - - try { - conn = this.getConnection(); - String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, ENROLMENT_ID, APPLICATION_ID, " + - "APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?, ?)"; - - conn.setAutoCommit(false); - stmt = conn.prepareStatement(sql); - - for (Application application : applications) { - stmt.setInt(1, deviceId); - stmt.setInt(2, enrolmentId); - stmt.setInt(3, application.getId()); - - bao = new ByteArrayOutputStream(); - oos = new ObjectOutputStream(bao); - oos.writeObject(application.getAppProperties()); - stmt.setBytes(4, bao.toByteArray()); - - stmt.setInt(5, application.getMemoryUsage()); - stmt.setBoolean(6, application.isActive()); - - stmt.setInt(7, tenantId); - stmt.addBatch(); - } - stmt.executeBatch(); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e); - } catch (IOException e) { - throw new DeviceManagementDAOException("Error occurred while serializing application properties object", e); - } finally { - if (bao != null) { - try { - bao.close(); - } catch (IOException e) { - log.error("Error occurred while closing ByteArrayOutputStream", e); - } - } - if (oos != null) { - try { - oos.close(); - } catch (IOException e) { - log.error("Error occurred while closing ObjectOutputStream", e); - } - } - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - - } - - @Override - public void removeApplicationMapping(int deviceId, int enrolmentId, List appIdList, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - try { - String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " + - "APPLICATION_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; - - conn = this.getConnection(); - for (int appId : appIdList) { - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setInt(2, appId); - stmt.setInt(3, tenantId); - stmt.setInt(4, enrolmentId); - stmt.execute(); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while removing device application mapping", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - } - - private Connection getConnection() throws SQLException { - 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/GenericApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericApplicationDAOImpl.java deleted file mode 100644 index 367f71f612e..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericApplicationDAOImpl.java +++ /dev/null @@ -1,91 +0,0 @@ -/* -* Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ - -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.DeviceManagementDAOException; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -/** - * Generic DAO implementation for Application Management Operations - */ -public class GenericApplicationDAOImpl extends AbstractApplicationDAOImpl{ - - @Override - public List addApplications(List applications, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs; - List applicationIds = new ArrayList<>(); - try { - conn = this.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " + - "CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES, " + - "APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new String[]{"id"}); - - for (Application application : applications) { - - stmt.setString(1, application.getName()); - stmt.setString(2, application.getPlatform()); - stmt.setString(3, application.getCategory()); - stmt.setString(4, application.getVersion()); - stmt.setString(5, application.getType()); - stmt.setString(6, application.getLocationUrl()); - stmt.setString(7, application.getImageUrl()); - stmt.setInt(8, tenantId); - - // Removing the application properties saving from the application table. - stmt.setBigDecimal(9, null); - - stmt.setString(10, application.getApplicationIdentifier()); - - // Removing the application memory - stmt.setInt(11, 0); - stmt.setBoolean(12, true); - - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - applicationIds.add(rs.getInt(1)); - } - } - return applicationIds; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - } - - private Connection getConnection() throws SQLException { - 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/PostgreSQLApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLApplicationDAOImpl.java deleted file mode 100644 index a6a50783235..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLApplicationDAOImpl.java +++ /dev/null @@ -1,90 +0,0 @@ -/* -* Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ - -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.DeviceManagementDAOException; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -/** - * PosgreSQL specific DAO implementation for Application Management Operations - */ -public class PostgreSQLApplicationDAOImpl extends AbstractApplicationDAOImpl{ - - @Override - public List addApplications(List applications, - int tenantId) throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs; - List applicationIds = new ArrayList<>(); - try { - conn = this.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_APPLICATION (NAME, PLATFORM, " + - "CATEGORY, VERSION, TYPE, LOCATION_URL, IMAGE_URL, TENANT_ID,APP_PROPERTIES, " + - "APP_IDENTIFIER, MEMORY_USAGE, IS_ACTIVE) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new String[]{"id"}); - - for (Application application : applications) { - - stmt.setString(1, application.getName()); - stmt.setString(2, application.getPlatform()); - stmt.setString(3, application.getCategory()); - stmt.setString(4, application.getVersion()); - stmt.setString(5, application.getType()); - stmt.setString(6, application.getLocationUrl()); - stmt.setString(7, application.getImageUrl()); - stmt.setInt(8, tenantId); - - // Removing the application properties saving from the application table. - stmt.setBytes(9, null); - - stmt.setString(10, application.getApplicationIdentifier()); - - // Removing the application memory - stmt.setInt(11, 0); - stmt.setBoolean(12, true); - - stmt.executeUpdate(); - - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - applicationIds.add(rs.getInt(1)); - } - } - return applicationIds; - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - } - - private Connection getConnection() throws SQLException { - return DeviceManagementDAOFactory.getConnection(); - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java index f9cc83fbab3..44e5278cfad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java @@ -42,9 +42,16 @@ public interface DeviceInformationManager { * @param deviceInfo - Device info object. * @throws DeviceDetailsMgtException */ - //void addDeviceInfo(DeviceInfo deviceInfo) throws DeviceDetailsMgtException; + @Deprecated void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) throws DeviceDetailsMgtException; + /** + * This method will manage the storing of the device information as key value pairs. + * @param deviceInfo - Device info object. + * @throws DeviceDetailsMgtException + */ + void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException; + /** * This method will return the device information. * @param deviceIdentifier - Device identifier, device type. @@ -68,6 +75,8 @@ public interface DeviceInformationManager { */ void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtException; + void addDeviceLocation(Device device, DeviceLocation deviceLocation) throws DeviceDetailsMgtException; + /** * This method will return the device location with latitude, longitude, address etc.. * @param deviceIdentifier - Device identifier, device type. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java index bdc44ec831d..a97b541b078 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java @@ -50,6 +50,9 @@ public interface DeviceDetailsDAO { void addDeviceProperties(Map propertyMap, int deviceId, int enrolmentId) throws DeviceDetailsMgtDAOException; + void updateDeviceProperties(Map propertyMap, int deviceId, int enrolmentId) + throws DeviceDetailsMgtDAOException; + /** * This method will return the device information when device id is provided. * @param deviceId - device Id @@ -112,6 +115,10 @@ public interface DeviceDetailsDAO { void addDeviceLocationInfo(Device device, DeviceLocation deviceLocation, int tenantId) throws DeviceDetailsMgtDAOException; + void updateDeviceInformation(int deviceId, int enrollmentId, DeviceInfo newDeviceInfo) throws DeviceDetailsMgtDAOException; + + void updateDeviceLocation(DeviceLocation deviceLocation, int enrollmentId) throws DeviceDetailsMgtDAOException; + // /** // * This method will add device application to database. // * @param deviceApplication - Device application diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index e9d592b47d8..db865b1c5cf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -119,6 +119,38 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } + @Override + public void updateDeviceProperties(Map propertyMap, int deviceId, int enrolmentId) + throws DeviceDetailsMgtDAOException { + + if (propertyMap.isEmpty()) { + if(log.isDebugEnabled()) { + log.debug("Property map of device id :" + deviceId + " is empty."); + } + return; + } + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + stmt = conn.prepareStatement("UPDATE DM_DEVICE_INFO SET VALUE_FIELD = ? WHERE DEVICE_ID = ?" + + " AND KEY_FIELD = ? AND ENROLMENT_ID = ?"); + + for (Map.Entry entry : propertyMap.entrySet()) { + stmt.setString(1, entry.getValue()); + stmt.setInt(2, deviceId); + stmt.setString(3, entry.getKey()); + stmt.setInt(4, enrolmentId); + stmt.addBatch(); + } + stmt.executeBatch(); + } catch (SQLException e) { + throw new DeviceDetailsMgtDAOException("Error occurred while updating device properties to database.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + @Override public DeviceInfo getDeviceInformation(int deviceId, int enrolmentId) throws DeviceDetailsMgtDAOException { Connection conn; @@ -267,6 +299,36 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } } + @Override + public void updateDeviceLocation(DeviceLocation deviceLocation, int enrollmentId) + throws DeviceDetailsMgtDAOException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + stmt = conn.prepareStatement("UPDATE DM_DEVICE_LOCATION SET LATITUDE = ?, LONGITUDE = ?, " + + "STREET1 = ?, STREET2 = ?, CITY = ?, ZIP = ?, STATE = ?, COUNTRY = ?, GEO_HASH = ?, " + + "UPDATE_TIMESTAMP = ? WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"); + stmt.setDouble(1, deviceLocation.getLatitude()); + stmt.setDouble(2, deviceLocation.getLongitude()); + stmt.setString(3, deviceLocation.getStreet1()); + stmt.setString(4, deviceLocation.getStreet2()); + stmt.setString(5, deviceLocation.getCity()); + stmt.setString(6, deviceLocation.getZip()); + stmt.setString(7, deviceLocation.getState()); + stmt.setString(8, deviceLocation.getCountry()); + stmt.setString(9, GeoHashGenerator.encodeGeohash(deviceLocation)); + stmt.setLong(10, System.currentTimeMillis()); + stmt.setInt(11, deviceLocation.getDeviceId()); + stmt.setInt(12, enrollmentId); + stmt.execute(); + } catch (SQLException e) { + throw new DeviceDetailsMgtDAOException("Error occurred while adding the device location to database.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + @Override public DeviceLocation getDeviceLocation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException { @@ -363,6 +425,47 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } } + @Override + public void updateDeviceInformation(int deviceId, int enrollmentId, DeviceInfo newDeviceInfo) throws DeviceDetailsMgtDAOException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + + stmt = conn.prepareStatement("UPDATE DM_DEVICE_DETAIL SET DEVICE_MODEL = ?, VENDOR = ?, " + + "OS_VERSION = ?, OS_BUILD_DATE = ?, BATTERY_LEVEL = ?, INTERNAL_TOTAL_MEMORY = ?, " + + "INTERNAL_AVAILABLE_MEMORY = ?, EXTERNAL_TOTAL_MEMORY = ?, EXTERNAL_AVAILABLE_MEMORY = ?, " + + "CONNECTION_TYPE = ?, SSID = ?, CPU_USAGE = ?, TOTAL_RAM_MEMORY = ?, AVAILABLE_RAM_MEMORY = ?, " + + "PLUGGED_IN = ?, UPDATE_TIMESTAMP = ? WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"); + + stmt.setString(1, newDeviceInfo.getDeviceModel()); + stmt.setString(2, newDeviceInfo.getVendor()); + stmt.setString(3, newDeviceInfo.getOsVersion()); + stmt.setString(4, newDeviceInfo.getOsBuildDate()); + stmt.setDouble(5, newDeviceInfo.getBatteryLevel()); + stmt.setDouble(6, newDeviceInfo.getInternalTotalMemory()); + stmt.setDouble(7, newDeviceInfo.getInternalAvailableMemory()); + stmt.setDouble(8, newDeviceInfo.getExternalTotalMemory()); + stmt.setDouble(9, newDeviceInfo.getExternalAvailableMemory()); + stmt.setString(10, newDeviceInfo.getConnectionType()); + stmt.setString(11, newDeviceInfo.getSsid()); + stmt.setDouble(12, newDeviceInfo.getCpuUsage()); + stmt.setDouble(13, newDeviceInfo.getTotalRAMMemory()); + stmt.setDouble(14, newDeviceInfo.getAvailableRAMMemory()); + stmt.setBoolean(15, newDeviceInfo.isPluggedIn()); + stmt.setLong(16, System.currentTimeMillis()); + stmt.setInt(17, deviceId); + stmt.setInt(18, enrollmentId); + + stmt.execute(); + + } catch (SQLException e) { + throw new DeviceDetailsMgtDAOException("Error occurred while updating device details.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index cdb995a60fb..a9a5dbe77aa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -16,7 +16,6 @@ * under the License. */ - package org.wso2.carbon.device.mgt.core.device.details.mgt.impl; import org.apache.commons.logging.Log; @@ -68,30 +67,50 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { try { Device device = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider().getDevice(deviceId, false); + addDeviceInfo(device, deviceInfo); + } catch (DeviceManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceDetailsMgtException("Error occurred while retrieving the device information.", e); + } + } + @Override + public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException { + try { DeviceManagementDAOFactory.beginTransaction(); DeviceInfo newDeviceInfo; DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); Map previousDeviceProperties = deviceDetailsDAO.getDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); - if (previousDeviceInfo != null && previousDeviceProperties != null) { - previousDeviceInfo.setDeviceDetailsMap(previousDeviceProperties); - newDeviceInfo = processDeviceInfo(previousDeviceInfo, deviceInfo); - } else if (previousDeviceInfo == null && previousDeviceProperties != null) { - previousDeviceInfo = new DeviceInfo(); - previousDeviceInfo.setDeviceDetailsMap(previousDeviceProperties); + if (previousDeviceInfo != null) { + previousDeviceInfo.setDeviceDetailsMap(new HashMap<>()); newDeviceInfo = processDeviceInfo(previousDeviceInfo, deviceInfo); + deviceDetailsDAO.updateDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), + newDeviceInfo); } else { + deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), deviceInfo); newDeviceInfo = deviceInfo; } - + if (previousDeviceProperties == null) { + deviceDetailsDAO.addDeviceProperties(newDeviceInfo.getDeviceDetailsMap(), device.getId(), + device.getEnrolmentInfo().getId()); + } else { + Map updatableProps = new HashMap<>(); + Map injectableProps = new HashMap<>(); + for (String key : newDeviceInfo.getDeviceDetailsMap().keySet()) { + if (previousDeviceProperties.containsKey(key)) { + updatableProps.put(key, newDeviceInfo.getDeviceDetailsMap().get(key)); + } else { + injectableProps.put(key, newDeviceInfo.getDeviceDetailsMap().get(key)); + } + } + deviceDetailsDAO.updateDeviceProperties(updatableProps, device.getId(), + device.getEnrolmentInfo().getId()); + deviceDetailsDAO.addDeviceProperties(injectableProps, device.getId(), + device.getEnrolmentInfo().getId()); + } deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - deviceDetailsDAO.deleteDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); - deviceDetailsDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); - deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), newDeviceInfo); - deviceDetailsDAO.addDeviceProperties(newDeviceInfo.getDeviceDetailsMap(), device.getId(), - device.getEnrolmentInfo().getId()); DeviceManagementDAOFactory.commitTransaction(); String reportingHost = System.getProperty(DeviceManagementConstants.Report @@ -223,17 +242,28 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { @Override public void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtException { - try { Device device = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider().getDevice(deviceLocation.getDeviceIdentifier(), false); + addDeviceLocation(device, deviceLocation); + } catch (DeviceManagementException e) { + throw new DeviceDetailsMgtException("Error occurred while updating the last updated timestamp of " + + "the device", e); + } + } + + @Override + public void addDeviceLocation(Device device, DeviceLocation deviceLocation) throws DeviceDetailsMgtException { + try { deviceLocation.setDeviceId(device.getId()); DeviceManagementDAOFactory.beginTransaction(); - deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - deviceDetailsDAO.addDeviceLocationInfo(device, deviceLocation, - CarbonContext.getThreadLocalCarbonContext().getTenantId()); - deviceDetailsDAO.deleteDeviceLocation(deviceLocation.getDeviceId(), device.getEnrolmentInfo().getId()); - deviceDetailsDAO.addDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); + DeviceLocation previousLocation = deviceDetailsDAO.getDeviceLocation(device.getId(), + device.getEnrolmentInfo().getId()); + if (previousLocation == null) { + deviceDetailsDAO.addDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); + } else { + deviceDetailsDAO.updateDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); + } if (DeviceManagerUtil.isPublishLocationResponseEnabled()) { Object[] metaData = {device.getDeviceIdentifier(), device.getEnrolmentInfo().getOwner(), device.getType()}; Object[] payload = new Object[]{ @@ -259,10 +289,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } catch (DeviceManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while getting the device information.", e); - } catch (DeviceManagementDAOException e) { - DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceDetailsMgtException("Error occurred while updating the last updated timestamp of " + - "the device", e); } catch (DataPublisherConfigurationException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e); @@ -384,9 +410,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { if (newDeviceInfo.getAvailableRAMMemory() == -1D) { newDeviceInfo.setAvailableRAMMemory(previousDeviceInfo.getAvailableRAMMemory()); } - if (!newDeviceInfo.isPluggedIn()) { - newDeviceInfo.setPluggedIn(previousDeviceInfo.isPluggedIn()); - } Map newDeviceDetailsMap = newDeviceInfo.getDeviceDetailsMap(); Map previousDeviceDetailsMap = previousDeviceInfo.getDeviceDetailsMap(); for (String eachKey : previousDeviceDetailsMap.keySet()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 28526736be0..5fff1c77da6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -196,7 +196,6 @@ public class OperationManagerImpl implements OperationManager { operation.setInitiatedBy(initiatedBy); } - OperationManagementDAOFactory.beginTransaction(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = OperationDAOUtil .convertOperation(operation); int enrolmentId; @@ -209,6 +208,7 @@ public class OperationManagerImpl implements OperationManager { authorizedDevices.add(device); } + OperationManagementDAOFactory.beginTransaction(); if (operationDto.getControl() == org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { int existingOperationID; @@ -253,14 +253,14 @@ public class OperationManagerImpl implements OperationManager { isScheduled = notificationStrategy.getConfig().isScheduled(); } - //TODO have to create a sql to load device details from deviceDAO using single query. + List enrolmentIds = new ArrayList<>(); for (Device device : authorizedDevices) { enrolmentId = device.getEnrolmentInfo().getId(); - //Do not repeat the task operations - operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled); + enrolmentIds.add(enrolmentId); } + operationMappingDAO.addOperationMapping(operationId, enrolmentIds, isScheduled); OperationManagementDAOFactory.commitTransaction(); - if (!isScheduled) { + if (!isScheduled && notificationStrategy != null) { for (Device device : authorizedDevices) { this.sendNotification(operation, device); } @@ -319,7 +319,7 @@ public class OperationManagerImpl implements OperationManager { .device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED); OperationManagementDAOFactory.commitTransaction(); } catch (OperationManagementDAOException ex) { - // Not throwing this exception in order to keep sending remaining notifications if any. + // Not throwing this exception in order to keep scheduling remaining notifications if any. log.error("Error occurred while setting push notification status to SCHEDULED.", ex); OperationManagementDAOFactory.rollbackTransaction(); } @@ -501,13 +501,6 @@ public class OperationManagerImpl implements OperationManager { List operations = new ArrayList<>(); List dtoOperationList = new ArrayList<>(); - if (!isActionAuthorized(deviceId)) { - throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "'"); - } - - // EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + @@ -552,6 +545,28 @@ public class OperationManagerImpl implements OperationManager { return operations; } + @Override + public List getPendingOperations(Device device) throws OperationManagementException { + EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo(); + if (enrolmentInfo == null) { + throw new OperationManagementException("Device not found for the given device Identifier:" + + device.getId() + " and given type:" + + device.getType()); + } + int enrolmentId = enrolmentInfo.getId(); + //Changing the enrollment status & attempt count if the device is marked as inactive or unreachable + switch (enrolmentInfo.getStatus()) { + case INACTIVE: + case UNREACHABLE: + this.setEnrolmentStatus(enrolmentId, EnrolmentInfo.Status.ACTIVE); + break; + } + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setType(device.getType()); + deviceIdentifier.setId(device.getDeviceIdentifier()); + return getOperations(deviceIdentifier, Operation.Status.PENDING, enrolmentId); + } + @Override public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { // setting notNowOperationFrequency to -1 to avoid picking notnow operations @@ -648,15 +663,8 @@ public class OperationManagerImpl implements OperationManager { @Override public void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException { - int operationId = operation.getId(); if (log.isDebugEnabled()) { - log.debug("operation Id:" + operationId + " status:" + operation.getStatus()); - } - - if (!isActionAuthorized(deviceId)) { - throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + - deviceId.getId() + "'"); + log.debug("operation Id:" + operation.getId() + " status:" + operation.getStatus()); } EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); @@ -665,9 +673,13 @@ public class OperationManagerImpl implements OperationManager { "Device not found for device id:" + deviceId.getId() + " " + "type:" + deviceId.getType()); } + updateOperation(enrolmentInfo.getId(), operation); + } + @Override + public void updateOperation(int enrolmentId, Operation operation) throws OperationManagementException { + int operationId = operation.getId(); try { - int enrolmentId = enrolmentInfo.getId(); OperationManagementDAOFactory.beginTransaction(); if (operation.getStatus() != null) { operationDAO.updateOperationStatus(enrolmentId, operationId, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java index aa5d9b7bea9..5077e152117 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java @@ -18,7 +18,6 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationEnrolmentMapping; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; @@ -28,7 +27,11 @@ import java.util.Map; public interface OperationMappingDAO { - void addOperationMapping(int operationId, Integer deviceId, boolean isScheduled) throws OperationManagementDAOException; + void addOperationMapping(int operationId, int enrollmentId, boolean isScheduled) + throws OperationManagementDAOException; + + void addOperationMapping(int operationId, List enrollmentIds, boolean isScheduled) + throws OperationManagementDAOException; void removeOperationMapping(int operationId, Integer deviceId) throws OperationManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index ca5c5a46ea7..80bcf8e1a4a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -28,29 +28,41 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class CommandOperationDAOImpl extends GenericOperationDAOImpl { @Override public int addOperation(Operation operation) throws OperationManagementDAOException { - int operationId; - CommandOperation commandOp = (CommandOperation) operation; PreparedStatement stmt = null; + ResultSet rs = null; try { - operationId = super.addOperation(operation); - Connection conn = OperationManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)"); - stmt.setInt(1, operationId); - stmt.setBoolean(2, commandOp.isEnabled()); + Connection connection = OperationManagementDAOFactory.getConnection(); + String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE, " + + "INITIATED_BY, ENABLED) VALUES (?, ?, ?, ?, ?, ?)"; + stmt = connection.prepareStatement(sql, new String[]{"id"}); + stmt.setString(1, operation.getType().toString()); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setTimestamp(3, null); + stmt.setString(4, operation.getCode()); + stmt.setString(5, operation.getInitiatedBy()); + stmt.setBoolean(6, operation.isEnabled()); stmt.executeUpdate(); + + rs = stmt.getGeneratedKeys(); + int id = -1; + if (rs.next()) { + id = rs.getInt(1); + } + return id; } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding command operation", e); } finally { - OperationManagementDAOUtil.cleanupResources(stmt); + OperationManagementDAOUtil.cleanupResources(stmt, rs); } - return operationId; } public CommandOperation getOperation(int id) throws OperationManagementDAOException { @@ -59,13 +71,14 @@ public class CommandOperationDAOImpl extends GenericOperationDAOImpl { CommandOperation commandOperation = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT OPERATION_ID, ENABLED FROM DM_COMMAND_OPERATION WHERE OPERATION_ID = ?"; + String sql = "SELECT ID, ENABLED FROM DM_OPERATION WHERE ID = ? AND TYPE='COMMAND'"; stmt = conn.prepareStatement(sql); stmt.setInt(1, id); rs = stmt.executeQuery(); if (rs.next()) { commandOperation = new CommandOperation(); + commandOperation.setId(rs.getInt("ID")); commandOperation.setEnabled(rs.getBoolean("ENABLED")); } } catch (SQLException e) { @@ -87,11 +100,11 @@ public class CommandOperationDAOImpl extends GenericOperationDAOImpl { List commandOperations = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT o.ID, co1.ENABLED, co1.STATUS, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " + - "o.OPERATION_CODE FROM (SELECT co.OPERATION_ID, co.ENABLED, dm.STATUS " + - "FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS " + + String sql = "SELECT co1.ID, co1.ENABLED, co1.STATUS, co1.TYPE, co1.CREATED_TIMESTAMP, co1.RECEIVED_TIMESTAMP, " + + "co1.OPERATION_CODE FROM (SELECT co.ID, co.TYPE, co.CREATED_TIMESTAMP, co.RECEIVED_TIMESTAMP, co.OPERATION_CODE, co.ENABLED, dm.STATUS " + + "FROM DM_OPERATION co INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS " + "FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ? AND STATUS = ?) dm " + - "ON dm.OPERATION_ID = co.OPERATION_ID) co1 INNER JOIN DM_OPERATION o ON co1.OPERATION_ID = o.ID"; + "ON dm.OPERATION_ID = co.ID and co.TYPE='COMMAND') co1"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java index 6b08254c4d9..397485a7449 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java @@ -34,6 +34,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class ConfigOperationDAOImpl extends GenericOperationDAOImpl { @@ -42,22 +43,33 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl { @Override public int addOperation(Operation operation) throws OperationManagementDAOException { - int operationId; PreparedStatement stmt = null; + ResultSet rs = null; try { - operationId = super.addOperation(operation); operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); - Connection conn = OperationManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID, OPERATION_CONFIG) VALUES(?, ?)"); - stmt.setInt(1, operationId); - stmt.setObject(2, operation); + Connection connection = OperationManagementDAOFactory.getConnection(); + String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE, " + + "INITIATED_BY, OPERATION_DETAILS) VALUES (?, ?, ?, ?, ?, ?)"; + stmt = connection.prepareStatement(sql, new String[]{"id"}); + stmt.setString(1, operation.getType().toString()); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setTimestamp(3, null); + stmt.setString(4, operation.getCode()); + stmt.setString(5, operation.getInitiatedBy()); + stmt.setObject(6, operation); stmt.executeUpdate(); + + rs = stmt.getGeneratedKeys(); + int id = -1; + if (rs.next()) { + id = rs.getInt(1); + } + return id; } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding command operation", e); } finally { - OperationManagementDAOUtil.cleanupResources(stmt); + OperationManagementDAOUtil.cleanupResources(stmt, rs); } - return operationId; } @Override @@ -70,17 +82,17 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl { ObjectInputStream ois; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_CONFIG FROM DM_CONFIG_OPERATION WHERE OPERATION_ID = ?"; + String sql = "SELECT ID, ENABLED, OPERATION_DETAILS FROM DM_OPERATION WHERE ID = ? AND TYPE='CONFIG'"; stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); rs = stmt.executeQuery(); if (rs.next()) { - byte[] operationDetails = rs.getBytes("OPERATION_CONFIG"); + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); configOperation = (ConfigOperation) ois.readObject(); - configOperation.setId(rs.getInt("OPERATION_ID")); + configOperation.setId(rs.getInt("ID")); configOperation.setEnabled(rs.getBoolean("ENABLED")); } } catch (IOException e) { @@ -111,9 +123,9 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl { ObjectInputStream ois = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT co.OPERATION_ID, co.OPERATION_CONFIG FROM DM_CONFIG_OPERATION co " + + String sql = "SELECT co.ID, co.OPERATION_DETAILS FROM DM_OPERATION co " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ? " + - "AND STATUS = ?) dm ON dm.OPERATION_ID = co.OPERATION_ID"; + "AND STATUS = ?) dm ON dm.OPERATION_ID = co.ID WHERE co.TYPE = 'CONFIG'"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); @@ -121,12 +133,12 @@ public class ConfigOperationDAOImpl extends GenericOperationDAOImpl { rs = stmt.executeQuery(); while (rs.next()) { - byte[] operationDetails = rs.getBytes("OPERATION_CONFIG"); + byte[] operationDetails = rs.getBytes("OPERATION_DETAILS"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); configOperation = (ConfigOperation) ois.readObject(); configOperation.setStatus(status); - configOperation.setId(rs.getInt("OPERATION_ID")); + configOperation.setId(rs.getInt("ID")); operations.add(configOperation); } } catch (IOException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 646f6a9051d..3edc27791d2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -64,13 +64,14 @@ public class GenericOperationDAOImpl implements OperationDAO { try { Connection connection = OperationManagementDAOFactory.getConnection(); String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE, " + - "INITIATED_BY) VALUES (?, ?, ?, ?, ?)"; + "INITIATED_BY, OPERATION_DETAILS) VALUES (?, ?, ?, ?, ?, ?)"; stmt = connection.prepareStatement(sql, new String[]{"id"}); stmt.setString(1, operation.getType().toString()); stmt.setTimestamp(2, new Timestamp(new Date().getTime())); stmt.setTimestamp(3, null); stmt.setString(4, operation.getCode()); stmt.setString(5, operation.getInitiatedBy()); + stmt.setObject(6, operation); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java index 9399b40b467..dfe523f414b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java @@ -39,7 +39,7 @@ import java.util.Map; public class OperationMappingDAOImpl implements OperationMappingDAO { @Override - public void addOperationMapping(int operationId, Integer deviceId, boolean isScheduled) throws + public void addOperationMapping(int operationId, int enrollmentId, boolean isScheduled) throws OperationManagementDAOException { PreparedStatement stmt = null; try { @@ -48,7 +48,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { String sql = "INSERT INTO DM_ENROLMENT_OP_MAPPING(ENROLMENT_ID, OPERATION_ID, STATUS, " + "PUSH_NOTIFICATION_STATUS, CREATED_TIMESTAMP, UPDATED_TIMESTAMP) VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); + stmt.setInt(1, enrollmentId); stmt.setInt(2, operationId); stmt.setString(3, Operation.Status.PENDING.toString()); if (isScheduled) { @@ -66,6 +66,37 @@ public class OperationMappingDAOImpl implements OperationMappingDAO { } } + @Override + public void addOperationMapping(int operationId, List enrollmentIds, boolean isScheduled) throws + OperationManagementDAOException { + PreparedStatement stmt = null; + try { + long time = System.currentTimeMillis() / 1000; + Connection conn = OperationManagementDAOFactory.getConnection(); + String sql = "INSERT INTO DM_ENROLMENT_OP_MAPPING(ENROLMENT_ID, OPERATION_ID, STATUS, " + + "PUSH_NOTIFICATION_STATUS, CREATED_TIMESTAMP, UPDATED_TIMESTAMP) VALUES (?, ?, ?, ?, ?, ?)"; + stmt = conn.prepareStatement(sql); + for (int enrollmentId : enrollmentIds) { + stmt.setInt(1, enrollmentId); + stmt.setInt(2, operationId); + stmt.setString(3, Operation.Status.PENDING.toString()); + if (isScheduled) { + stmt.setString(4, Operation.PushNotificationStatus.SCHEDULED.toString()); + } else { + stmt.setString(4, Operation.PushNotificationStatus.COMPLETED.toString()); + } + stmt.setLong(5, time); + stmt.setLong(6, time); + stmt.addBatch(); + } + stmt.executeBatch(); + } catch (SQLException e) { + throw new OperationManagementDAOException("Error occurred while persisting device operation mappings", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, null); + } + } + @Override public void removeOperationMapping(int operationId, Integer deviceId) throws OperationManagementDAOException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java index 7f142b7d04c..8801921c1f0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU import java.io.*; import java.sql.*; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { @@ -37,27 +38,38 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { @Override public int addOperation(Operation operation) throws OperationManagementDAOException { - int operationId; PreparedStatement stmt = null; + ResultSet rs = null; ByteArrayOutputStream bao = null; ObjectOutputStream oos = null; + int operationId = -1; try { - operationId = super.addOperation(operation); + operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); - operation.setId(operationId); operation.setEnabled(true); - PolicyOperation policyOperation = (PolicyOperation) operation; - Connection conn = OperationManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + - "VALUES(?, ?)"); + Connection connection = OperationManagementDAOFactory.getConnection(); + String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE, " + + "INITIATED_BY, OPERATION_DETAILS, ENABLED) VALUES (?, ?, ?, ?, ?, ?, ?)"; + stmt = connection.prepareStatement(sql, new String[]{"id"}); + stmt.setString(1, operation.getType().toString()); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setTimestamp(3, null); + stmt.setString(4, operation.getCode()); + stmt.setString(5, operation.getInitiatedBy()); bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(operation); - stmt.setInt(1, operationId); - stmt.setBytes(2, bao.toByteArray()); + stmt.setObject(6, operation); + stmt.setBoolean(7, operation.isEnabled()); stmt.executeUpdate(); + + rs = stmt.getGeneratedKeys(); + if (rs.next()) { + operationId = rs.getInt(1); + } + return operationId; } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding policy operation", e); } catch (IOException e) { @@ -77,9 +89,8 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { log.warn("Error occurred while closing ObjectOutputStream", e); } } - OperationManagementDAOUtil.cleanupResources(stmt); + OperationManagementDAOUtil.cleanupResources(stmt, rs); } - return operationId; } @Override @@ -92,7 +103,7 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { ObjectInputStream ois; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION WHERE OPERATION_ID=?"; + String sql = "SELECT ID, ENABLED, OPERATION_DETAILS FROM DM_OPERATION WHERE ID=? AND TYPE='POLICY'"; stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); rs = stmt.executeQuery(); @@ -102,6 +113,7 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); policyOperation = (PolicyOperation) ois.readObject(); + policyOperation.setId(rs.getInt("ID")); } } catch (IOException e) { throw new OperationManagementDAOException("IO Error occurred while de serialize the policy operation " + @@ -130,9 +142,9 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { ObjectInputStream ois = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT po.OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION po " + + String sql = "SELECT po.ID, ENABLED, OPERATION_DETAILS FROM DM_OPERATION po " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ? " + - "AND STATUS = ?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; + "AND STATUS = ?) dm ON dm.OPERATION_ID = po.ID WHERE po.TYPE='POLICY'"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); @@ -145,6 +157,7 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { ois = new ObjectInputStream(bais); policyOperation = (PolicyOperation) ois.readObject(); policyOperation.setStatus(status); + policyOperation.setId(rs.getInt("ID")); operations.add(policyOperation); } } catch (IOException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java index 025c871efb7..9f07007e3b0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU import java.io.*; import java.sql.*; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { @@ -37,27 +38,36 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { public int addOperation(Operation operation) throws OperationManagementDAOException { PreparedStatement stmt = null; + ResultSet rs = null; ByteArrayOutputStream bao = null; ObjectOutputStream oos = null; - - int operationId; try { - operationId = super.addOperation(operation); operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); - operation.setId(operationId); operation.setEnabled(true); - //ProfileOperation profileOp = (ProfileOperation) operation; - Connection conn = OperationManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + - "VALUES(?, ?)"); + Connection connection = OperationManagementDAOFactory.getConnection(); + String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE, " + + "INITIATED_BY, OPERATION_DETAILS, ENABLED) VALUES (?, ?, ?, ?, ?, ?, ?)"; + stmt = connection.prepareStatement(sql, new String[]{"id"}); + stmt.setString(1, operation.getType().toString()); + stmt.setTimestamp(2, new Timestamp(new Date().getTime())); + stmt.setTimestamp(3, null); + stmt.setString(4, operation.getCode()); + stmt.setString(5, operation.getInitiatedBy()); bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(operation.getPayLoad()); - stmt.setInt(1, operationId); - stmt.setBytes(2, bao.toByteArray()); + stmt.setBytes(6, bao.toByteArray()); + stmt.setBoolean(7, operation.isEnabled()); stmt.executeUpdate(); + + rs = stmt.getGeneratedKeys(); + int id = -1; + if (rs.next()) { + id = rs.getInt(1); + } + return id; } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding profile operation", e); } catch (IOException e) { @@ -79,7 +89,6 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { } OperationManagementDAOUtil.cleanupResources(stmt); } - return operationId; } public Operation getOperation(int id) throws OperationManagementDAOException { @@ -91,9 +100,8 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { ObjectInputStream ois; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT o.ID, po.ENABLED, po.OPERATION_DETAILS, o.CREATED_TIMESTAMP, o.OPERATION_CODE " + - "FROM DM_PROFILE_OPERATION po INNER JOIN DM_OPERATION o ON po.OPERATION_ID = o.ID WHERE po" + - ".OPERATION_ID=?"; + String sql = "SELECT po.ID, po.ENABLED, po.OPERATION_DETAILS, po.CREATED_TIMESTAMP, po.OPERATION_CODE " + + "FROM DM_OPERATION po WHERE po.ID=? AND po.TYPE='PROFILE'"; stmt = conn.prepareStatement(sql); stmt.setInt(1, id); @@ -114,6 +122,9 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { profileOperation.setPayLoad(obj); } else { profileOperation = (ProfileOperation) obj; + profileOperation.setCode(rs.getString("OPERATION_CODE")); + profileOperation.setId(rs.getInt("ID")); + profileOperation.setCreatedTimeStamp(rs.getString("CREATED_TIMESTAMP")); } } } catch (IOException e) { @@ -145,12 +156,11 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT o.ID, po1.ENABLED, po1.STATUS, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " + - "o.OPERATION_CODE, po1.OPERATION_DETAILS " + - "FROM (SELECT po.OPERATION_ID, po.ENABLED, po.OPERATION_DETAILS, dm.STATUS " + - "FROM DM_PROFILE_OPERATION po INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS FROM DM_ENROLMENT_OP_MAPPING " + - "WHERE ENROLMENT_ID = ? AND STATUS = ?) dm ON dm.OPERATION_ID = po.OPERATION_ID) po1 " + - "INNER JOIN DM_OPERATION o ON po1.OPERATION_ID = o.ID "; + String sql = "SELECT po1.ID, po1.ENABLED, po1.STATUS, po1.TYPE, po1.CREATED_TIMESTAMP, po1.RECEIVED_TIMESTAMP, " + + "po1.OPERATION_CODE, po1.OPERATION_DETAILS " + + "FROM (SELECT po.ID, po.ENABLED, po.OPERATION_DETAILS, po.TYPE, po.OPERATION_CODE, po.CREATED_TIMESTAMP, po.RECEIVED_TIMESTAMP, dm.STATUS " + + "FROM DM_OPERATION po INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS FROM DM_ENROLMENT_OP_MAPPING " + + "WHERE ENROLMENT_ID = ? AND STATUS = ?) dm ON dm.OPERATION_ID = po.ID WHERE po.TYPE='PROFILE') po1"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); @@ -174,6 +184,9 @@ public class ProfileOperationDAOImpl extends GenericOperationDAOImpl { } else { profileOperation = (ProfileOperation) obj; profileOperation.setStatus(status); + profileOperation.setCode(rs.getString("OPERATION_CODE")); + profileOperation.setId(rs.getInt("ID")); + profileOperation.setCreatedTimeStamp(rs.getString("CREATED_TIMESTAMP")); operationList.add(profileOperation); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java index 0aa25c8c84b..e5d8fc36ce3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/operation/MySQLOperationDAOImpl.java @@ -43,40 +43,6 @@ import java.util.List; */ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl { - @Override - public boolean updateOperationStatus(int enrolmentId, int operationId, Operation.Status status) - throws OperationManagementDAOException { - PreparedStatement stmt = null; - boolean isUpdated = false; - try { - long time = System.currentTimeMillis() / 1000; - Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("SELECT STATUS, UPDATED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING " + - "WHERE ENROLMENT_ID=? and OPERATION_ID=? FOR UPDATE"); - stmt.setString(1, status.toString()); - stmt.setLong(2, time); - if (stmt.execute()) { - OperationManagementDAOUtil.cleanupResources(stmt); - stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS=?, UPDATED_TIMESTAMP=? " + - "WHERE ENROLMENT_ID=? and OPERATION_ID=?"); - stmt.setString(1, status.toString()); - stmt.setLong(2, time); - stmt.setInt(3, enrolmentId); - stmt.setInt(4, operationId); - int numOfRecordsUpdated = stmt.executeUpdate(); - if (numOfRecordsUpdated != 0) { - isUpdated = true; - } - } - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + - "metadata", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); - } - return isUpdated; - } - @Override public List getActivityList(List activityIds) throws OperationManagementDAOException { PreparedStatement stmt = null; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java index 5510247082a..fc0e31b3e50 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.search.mgt.impl; 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.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; @@ -210,9 +211,10 @@ public class ProcessorImpl implements Processor { private void setApplicationListOfDevices(List devices) throws SearchMgtException { try { DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); for (Device device : devices) { device.setApplications(applicationDAO.getInstalledApplications(device.getId(), - device.getEnrolmentInfo().getId())); + device.getEnrolmentInfo().getId(), tenantId)); } } catch (DeviceManagementDAOException e) { throw new SearchMgtException("Error occurred while fetching the Application List of devices ", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 15667409f74..74d06fa7c8b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -656,16 +656,23 @@ public interface DeviceManagementProviderService { PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request) throws OperationManagementException; + @Deprecated List getPendingOperations( DeviceIdentifier deviceId) throws OperationManagementException; + List getPendingOperations( + Device device) throws OperationManagementException; + Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException; Operation getNextPendingOperation(DeviceIdentifier deviceId, long notNowOperationFrequency) throws OperationManagementException; + @Deprecated void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException; + void updateOperation(Device device, Operation operation) throws OperationManagementException; + boolean updateProperties(DeviceIdentifier deviceId, List properties) throws DeviceManagementException; Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 59d4bfcd0ee..95213403d1c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1817,6 +1817,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv .getPendingOperations(deviceId); } + @Override + public List getPendingOperations(Device device) throws OperationManagementException { + return pluginRepository.getOperationManager(device.getType(), this.getTenantId()) + .getPendingOperations(device); + } + @Override public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { // // setting notNowOperationFrequency to -1 to avoid picking notnow operations @@ -1864,6 +1870,46 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @Override + public void updateOperation(Device device, Operation operation) throws OperationManagementException { + EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo(); + if (enrolmentInfo == null) { + throw new OperationManagementException( + "Device not found for device id:" + device.getDeviceIdentifier() + " " + "type:" + + device.getType()); + } + pluginRepository.getOperationManager(device.getType(), this.getTenantId()) + .updateOperation(enrolmentInfo.getId(), operation); + try { + if (DeviceManagerUtil.isPublishOperationResponseEnabled()) { + List permittedOperations = DeviceManagerUtil.getEnabledOperationsForResponsePublish(); + if (permittedOperations.contains(operation.getCode()) + || permittedOperations.contains("*")) { + Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; + Object[] payload = new Object[]{ + Calendar.getInstance().getTimeInMillis(), + operation.getId(), + operation.getCode(), + operation.getType() != null ? operation.getType().toString() : null, + operation.getStatus() != null ? operation.getStatus().toString() : null, + operation.getOperationResponse() + }; + DeviceManagerUtil.getEventPublisherService().publishEvent( + OPERATION_RESPONSE_EVENT_STREAM_DEFINITION, "1.0.0", metaData, new Object[0], payload + ); + } + } + } catch (DeviceManagementException e) { + String msg = "Error occurred while reading configs."; + log.error(msg, e); + throw new OperationManagementException(msg, e); + } catch (DataPublisherConfigurationException e) { + String msg = "Error occurred while publishing event."; + log.error(msg, e); + throw new OperationManagementException(msg, e); + } + } + @Override public boolean updateProperties(DeviceIdentifier deviceId, List properties) throws DeviceManagementException { @@ -3062,7 +3108,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv List applications; try { DeviceManagementDAOFactory.openConnection(); - applications = applicationDAO.getInstalledApplications(device.getId(), device.getEnrolmentInfo().getId()); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + applications = applicationDAO.getInstalledApplications(device.getId(), + device.getEnrolmentInfo().getId(), tenantId); device.setApplications(applications); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving the application list of '" + device.getType() + "', " + @@ -3367,7 +3415,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceLocation.setSpeed(Float.parseFloat(speed)); deviceLocation.setBearing(Float.parseFloat(bearing)); DeviceInformationManager deviceInformationManager = new DeviceInformationManagerImpl(); - deviceInformationManager.addDeviceLocation(deviceLocation); + deviceInformationManager.addDeviceLocation(device, deviceLocation); } catch (Exception e) { //We are not failing the execution since this is not critical for the functionality. But logging as // a warning for reference. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java index 12ce068b747..6620774df6b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java @@ -119,12 +119,18 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider(); try { - List devices; - List operations; + //list operations for device type + List operations = this.getValidOperationNames(); + if (operations.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("No operations are available."); + } + return; + } List validDeviceIdentifiers; List startupOperations; - operations = this.getValidOperationNames(); //list operations for each device type - devices = deviceManagementProviderService.getAllDevices(deviceType, false);//list devices for each type + //list devices of device type + List devices = deviceManagementProviderService.getAllDevices(deviceType, false); if (!devices.isEmpty()) { if (log.isDebugEnabled() && deviceType != null) { @@ -150,7 +156,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { } } else { if (log.isDebugEnabled()) { - log.debug("No operations are available."); + log.debug("No valid devices are available."); } } } else { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java deleted file mode 100644 index 732adf791d9..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - */ -package org.wso2.carbon.device.mgt.core.dao; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.testng.Assert; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; -import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; -import org.wso2.carbon.device.mgt.core.common.TestDataHolder; - -import java.sql.SQLException; - -public class ApplicationPersistenceTests extends BaseDeviceManagementTest { - - private static final Log log = LogFactory.getLog(ApplicationPersistenceTests.class); - private ApplicationDAO applicationDAO = null; - - @Test - public void testAddApplication() { - /* Adding dummy application to the application store */ - String testAppIdentifier = "test sample1"; - try { - DeviceManagementDAOFactory.openConnection(); - applicationDAO.addApplication(TestDataHolder.generateApplicationDummyData(testAppIdentifier), -1234); - } catch (DeviceManagementDAOException | SQLException e) { - log.error("Error occurred while adding application test sample1", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - /* Retrieving the application by its name */ - Application target = null; - try { - target = this.getApplication(testAppIdentifier, -1234); - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while retrieving application info"; - log.error(msg, e); - Assert.fail(msg, e); - } - - if (!isMock()) { - Assert.assertEquals(target.getApplicationIdentifier(), testAppIdentifier, - "Application added is not as same as what's retrieved"); - } - } - - private Application getApplication(String appIdentifier, int tenantId) throws DeviceManagementDAOException { - Application application = null; - try { - DeviceManagementDAOFactory.openConnection(); - application = applicationDAO.getApplication(appIdentifier, tenantId); - } catch (SQLException e) { - log.error("Error occurred while metadata corresponding to the application '" + appIdentifier + "'", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - return application; - } - - @BeforeClass - @Override - public void init() throws Exception { - this.initDataSource(); - applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java index 098903e32b2..a5526ceb8c4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/operation/OperationManagementTests.java @@ -266,19 +266,6 @@ public class OperationManagementTests extends BaseDeviceManagementTest { } } - @Test(dependsOnMethods = "getOperations", expectedExceptions = OperationManagementException.class) - public void getPendingOperationsAsNonAdmin() throws DeviceManagementException, OperationManagementException, - InvalidDeviceException { - try { - startTenantFlowAsNonAdmin(); - for (DeviceIdentifier deviceIdentifier : deviceIds) { - this.operationMgtService.getPendingOperations(deviceIdentifier); - } - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - } - @Test(dependsOnMethods = "getPendingOperations") public void getPaginatedRequestAsAdmin() throws OperationManagementException { PrivilegedCarbonContext.startTenantFlow(); @@ -339,7 +326,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { Assert.assertEquals(pendingOperations.size(), 3); } - @Test(dependsOnMethods = "updateOperation", expectedExceptions = OperationManagementException.class) + @Test(dependsOnMethods = "updateOperation") public void updateOperationAsNonAdmin() throws OperationManagementException { //This is required to introduce a delay for the update operation of the device. try { @@ -356,7 +343,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { operation.setOperationResponse("The operation is successfully completed, and updated by non admin!"); this.operationMgtService.updateOperation(deviceIdentifier, operation); List pendingOperations = this.operationMgtService.getPendingOperations(deviceIdentifier); - Assert.assertEquals(pendingOperations.size(), 3); + Assert.assertEquals(pendingOperations.size(), 2); } finally { PrivilegedCarbonContext.endTenantFlow(); } @@ -442,7 +429,7 @@ public class OperationManagementTests extends BaseDeviceManagementTest { DeviceIdentifier deviceIdentifier = this.deviceIds.get(0); List operation = this.operationMgtService.getOperationsByDeviceAndStatus(deviceIdentifier, Operation.Status.PENDING); - Assert.assertEquals(operation.size(), 2); + Assert.assertEquals(operation.size(), 1); } @Test(dependsOnMethods = "getOperationByDeviceAndOperationId", expectedExceptions = OperationManagementException.class) @@ -515,8 +502,8 @@ public class OperationManagementTests extends BaseDeviceManagementTest { public void getActivityCountUpdatedAfter() throws OperationManagementException, ParseException { int activityCount = this.operationMgtService.getActivityCountUpdatedAfter (this.commandActivityBeforeUpdatedTimestamp / 1000); - Assert.assertTrue(activityCount == 2, - "The activities updated after the created should be 2"); + Assert.assertEquals(activityCount, 3, + "The activities updated after the created should be 3"); } @Test diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mssql-testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mssql-testng.xml index 8f0a9de7138..b91a4ffe76e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mssql-testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mssql-testng.xml @@ -31,7 +31,6 @@ - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mysql-testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mysql-testng.xml index 4240faeb80b..c464f8deb1c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mysql-testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/mysql-testng.xml @@ -31,7 +31,6 @@ - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/oracle-testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/oracle-testng.xml index 9ec34795d40..a9eeb70a4a0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/oracle-testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/oracle-testng.xml @@ -31,7 +31,6 @@ - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/postgre-testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/postgre-testng.xml index fbaf2fd8a79..25c022dc799 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/postgre-testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/postgre-testng.xml @@ -31,7 +31,6 @@ - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index 80ac0335bed..f13715cedd4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -78,42 +78,9 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( RECEIVED_TIMESTAMP TIMESTAMP NULL, OPERATION_CODE VARCHAR(1000) NOT NULL, INITIATED_BY VARCHAR(100) NULL, - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - OPERATION_CONFIG BLOB DEFAULT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, OPERATION_DETAILS BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_policy FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, - OPERATION_DETAILS BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (ID) ); CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( @@ -369,19 +336,8 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( APP_PROPERTIES BLOB NULL, MEMORY_USAGE INTEGER(10) NULL, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, - 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, ENROLMENT_ID INTEGER NOT NULL, - APPLICATION_ID INTEGER NOT NULL, - APP_PROPERTIES BLOB NULL, - MEMORY_USAGE INTEGER(10) NULL, - IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device @@ -389,11 +345,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( 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, CONSTRAINT FK_DM_APP_MAP_DM_ENROL FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT (ID) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index c0fd2360d2b..889a6238b71 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -31,7 +31,6 @@ - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java index 1b0b1fb8e90..520c81cf44a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java @@ -183,7 +183,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { PlatformConfiguration deviceTypeConfig = deviceManager.getConfiguration(); if (deviceTypeConfig != null) { List configuration = deviceTypeConfig.getConfiguration(); - if (!configuration.isEmpty()) { + if (configuration != null && !configuration.isEmpty()) { Map properties = this.getConfigProperty(configuration); String notifierValue = properties.get(NOTIFIER_PROPERTY); String enabledNotifierType = notifierType; diff --git a/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluationImpl.java b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluationImpl.java index 9d2a997b740..58b5b26737d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluationImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluationImpl.java @@ -34,7 +34,7 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { private static final Log log = LogFactory.getLog(SimpleEvaluationImpl.class); private PolicyManagerService policyManagerService; - private List policyList = new ArrayList(); + private volatile List policyList = new ArrayList(); @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { @@ -71,7 +71,7 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { } @Override - public void sortPolicies() throws PolicyEvaluationException { + public synchronized void sortPolicies() throws PolicyEvaluationException { Collections.sort(policyList); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index e6b1f3300e6..8bacda908c8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -85,45 +85,9 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( RECEIVED_TIMESTAMP TIMESTAMP NULL, OPERATION_CODE VARCHAR(1000) NOT NULL, INITIATED_BY VARCHAR(100) NULL, - PRIMARY KEY (ID) -); - -DROP TABLE IF EXISTS DM_CONFIG_OPERATION; -CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - OPERATION_CONFIG BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -DROP TABLE IF EXISTS DM_COMMAND_OPERATION; -CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -DROP TABLE IF EXISTS DM_POLICY_OPERATION; -CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, - OPERATION_DETAILS BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_policy FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -DROP TABLE IF EXISTS DM_PROFILE_OPERATION; -CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, OPERATION_DETAILS BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (ID) ); DROP TABLE IF EXISTS DM_ENROLMENT; @@ -433,20 +397,8 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( APP_PROPERTIES BLOB NULL, MEMORY_USAGE INTEGER(10) NULL, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, - TENANT_ID INTEGER NOT NULL, - PRIMARY KEY (ID) -); - -DROP TABLE IF EXISTS DM_DEVICE_APPLICATION_MAPPING; - -CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( - ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL, - APPLICATION_ID INTEGER NOT NULL, - APP_PROPERTIES BLOB NULL, - MEMORY_USAGE INTEGER(10) NULL, - IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device @@ -454,11 +406,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( 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, CONSTRAINT FK_DM_APP_MAP_DM_ENROL FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT (ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/archival/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/archival/mysql.sql index 630cb495dc5..85a9e381962 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/archival/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/archival/mysql.sql @@ -5,6 +5,9 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION_ARCH ( CREATED_TIMESTAMP TIMESTAMP NOT NULL, RECEIVED_TIMESTAMP TIMESTAMP NULL, OPERATION_CODE VARCHAR(50) NOT NULL, + INITIATED_BY VARCHAR(100) NULL, + OPERATION_DETAILS BLOB DEFAULT NULL, + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, ARCHIVED_AT TIMESTAMP DEFAULT NOW(), PRIMARY KEY (ID) )ENGINE = InnoDB; @@ -15,19 +18,16 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OP_MAPPING_ARCH ( ENROLMENT_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, STATUS VARCHAR(50) NULL, - PUSH_NOTIFICATION_STATUS VARCHAR(50) NULL, CREATED_TIMESTAMP INTEGER NOT NULL, UPDATED_TIMESTAMP INTEGER NOT NULL, ARCHIVED_AT TIMESTAMP DEFAULT NOW(), PRIMARY KEY (ID) )ENGINE = InnoDB; - CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE_ARCH ( ID INT(11) NOT NULL, ENROLMENT_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL, - EN_OP_MAP_ID INTEGER NOT NULL, OPERATION_RESPONSE LONGBLOB DEFAULT NULL, RECEIVED_TIMESTAMP TIMESTAMP NULL, ARCHIVED_AT TIMESTAMP DEFAULT NOW(), @@ -44,26 +44,3 @@ CREATE TABLE IF NOT EXISTS DM_NOTIFICATION_ARCH ( ARCHIVED_AT TIMESTAMP DEFAULT NOW(), PRIMARY KEY (NOTIFICATION_ID) )ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION_ARCH ( - OPERATION_ID INTEGER NOT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - ARCHIVED_AT TIMESTAMP DEFAULT NOW(), - PRIMARY KEY (OPERATION_ID) -)ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION_ARCH ( - OPERATION_ID INTEGER NOT NULL, - OPERATION_CONFIG BLOB DEFAULT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - ARCHIVED_AT TIMESTAMP DEFAULT NOW(), - PRIMARY KEY (OPERATION_ID) -)ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION_ARCH ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, - OPERATION_DETAILS BLOB DEFAULT NULL, - ARCHIVED_AT TIMESTAMP DEFAULT NOW(), - PRIMARY KEY (OPERATION_ID) -)ENGINE = InnoDB; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index a3658e474aa..9b502de09a6 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -87,42 +87,9 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( RECEIVED_TIMESTAMP TIMESTAMP NULL, OPERATION_CODE VARCHAR(1000) NOT NULL, INITIATED_BY VARCHAR(100) NULL, - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - OPERATION_CONFIG BLOB DEFAULT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, - OPERATION_DETAILS BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_policy FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, OPERATION_DETAILS BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (ID) ); CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( @@ -392,18 +359,8 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( APP_PROPERTIES BLOB NULL, MEMORY_USAGE INTEGER(10) NULL, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, - 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, ENROLMENT_ID INTEGER NOT NULL, - APPLICATION_ID INTEGER NOT NULL, - APP_PROPERTIES BLOB NULL, - MEMORY_USAGE INTEGER(10) NULL, - IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device @@ -411,11 +368,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( 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, CONSTRAINT FK_DM_APP_MAP_DM_ENROL FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT (ID) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index cb6df2f7625..c9366d1fe99 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -102,42 +102,9 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION ( RECEIVED_TIMESTAMP TIMESTAMP NULL, OPERATION_CODE VARCHAR(50) NOT NULL, INITIATED_BY VARCHAR(100) NULL, - PRIMARY KEY (ID) -)ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - OPERATION_CONFIG BLOB DEFAULT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT FK_DM_OPERATION_CONFIG FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -)ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT FK_DM_OPERATION_COMMAND FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -)ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, OPERATION_DETAILS BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT FK_DM_OPERATION_POLICY FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -)ENGINE = InnoDB; - -CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( - OPERATION_ID INTEGER NOT NULL, - ENABLED INTEGER NOT NULL DEFAULT 0, - OPERATION_DETAILS BLOB DEFAULT NULL, - PRIMARY KEY (OPERATION_ID), - CONSTRAINT FK_DM_OPERATION_PROFILE FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (ID) )ENGINE = InnoDB; CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( @@ -699,4 +666,4 @@ CREATE TABLE IF NOT EXISTS DM_ARCHIVED_OPERATIONS ( ID INTEGER NOT NULL, CREATED_TIMESTAMP TIMESTAMP NOT NULL, PRIMARY KEY (ID) -)ENGINE = InnoDB; \ No newline at end of file +)ENGINE = InnoDB;