@Override
 public List<Application> getApplicationListForDevice(DeviceIdentifier deviceId)
     throws ApplicationManagementException {
   Device device;
   try {
     int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
     DeviceManagementDAOFactory.openConnection();
     device = deviceDAO.getDevice(deviceId, tenantId);
     if (device == null) {
       if (log.isDebugEnabled()) {
         log.debug(
             "No device is found upon the device identifier '"
                 + deviceId.getId()
                 + "' and type '"
                 + deviceId.getType()
                 + "'. Therefore returning null");
       }
       return null;
     }
     return applicationDAO.getInstalledApplications(device.getId());
   } catch (DeviceManagementDAOException e) {
     throw new ApplicationManagementException(
         "Error occurred while fetching the Application List of '"
             + deviceId.getType()
             + "' device carrying the identifier'"
             + deviceId.getId(),
         e);
   } catch (SQLException e) {
     throw new ApplicationManagementException(
         "Error occurred while opening a connection to the data source", e);
   } finally {
     DeviceManagementDAOFactory.closeConnection();
   }
 }
 ApplicationManagerProviderServiceImpl() {
   this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
   this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
   this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO();
 }
  @Override
  public void updateApplicationListInstalledInDevice(
      DeviceIdentifier deviceIdentifier, List<Application> applications)
      throws ApplicationManagementException {
    List<Application> installedAppList = getApplicationListForDevice(deviceIdentifier);
    try {
      int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
      DeviceManagementDAOFactory.beginTransaction();
      Device device = deviceDAO.getDevice(deviceIdentifier, tenantId);

      if (log.isDebugEnabled()) {
        log.debug("Device:" + device.getId() + ":identifier:" + deviceIdentifier.getId());
      }

      if (log.isDebugEnabled()) {
        log.debug("num of apps installed:" + installedAppList.size());
      }
      List<Application> appsToAdd = new ArrayList<>();
      List<Integer> appIdsToRemove = new ArrayList<>(installedAppList.size());

      for (Application installedApp : installedAppList) {
        if (!applications.contains(installedApp)) {
          if (log.isDebugEnabled()) {
            log.debug("Remove app Id:" + installedApp.getId());
          }
          appIdsToRemove.add(installedApp.getId());
        }
      }
      applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove, tenantId);
      Application installedApp;
      List<Integer> applicationIds = new ArrayList<>();

      for (Application application : applications) {
        if (!installedAppList.contains(application)) {
          installedApp =
              applicationDAO.getApplication(
                  application.getApplicationIdentifier(), application.getVersion(), tenantId);
          if (installedApp == null) {
            appsToAdd.add(application);
          } else {
            applicationIds.add(installedApp.getId());
          }
        }
      }
      if (log.isDebugEnabled()) {
        log.debug("num of apps add:" + appsToAdd.size());
      }
      applicationIds.addAll(applicationDAO.addApplications(appsToAdd, tenantId));

      if (log.isDebugEnabled()) {
        log.debug("num of app Ids:" + applicationIds.size());
      }
      applicationMappingDAO.addApplicationMappings(device.getId(), applicationIds, tenantId);

      if (log.isDebugEnabled()) {
        log.debug("num of remove app Ids:" + appIdsToRemove.size());
      }

      DeviceManagementDAOFactory.commitTransaction();
    } catch (DeviceManagementDAOException e) {
      DeviceManagementDAOFactory.rollbackTransaction();
      throw new ApplicationManagementException(
          "Error occurred saving application list to the device", e);
    } catch (TransactionManagementException e) {
      throw new ApplicationManagementException("Error occurred while initializing transaction", e);
    } finally {
      DeviceManagementDAOFactory.closeConnection();
    }
  }
  @SuppressWarnings("unused")
  protected void activate(ComponentContext componentContext) {
    try {
      if (log.isDebugEnabled()) {
        log.debug("Initializing device management core bundle");
      }
      /* Initializing Device Management Configuration */
      DeviceConfigurationManager.getInstance().initConfig();
      DeviceManagementConfig config =
          DeviceConfigurationManager.getInstance().getDeviceManagementConfig();

      DataSourceConfig dsConfig =
          config.getDeviceManagementConfigRepository().getDataSourceConfig();

      APIManagerConfiguration apiManagerConfiguration = new APIManagerConfiguration();
      apiManagerConfiguration.load(APIM_CONFIGURATION_PATH);
      DeviceManagementDataHolder.getInstance().setApiManagerConfiguration(apiManagerConfiguration);

      DeviceManagementDAOFactory.init(dsConfig);
      GroupManagementDAOFactory.init(dsConfig);
      NotificationManagementDAOFactory.init(dsConfig);
      OperationManagementDAOFactory.init(dsConfig);

      String apiManagerDataSource = apiManagerConfiguration.getFirstProperty(DATA_SOURCE_NAME);
      ScopeManagementDAOFactory.init(apiManagerDataSource);

      /* Initialize Operation Manager */
      this.initOperationsManager();

      PushNotificationProviderRepository pushNotificationRepo =
          new PushNotificationProviderRepository();
      List<String> pushNotificationProviders = config.getPushNotificationProviders();
      if (pushNotificationProviders != null) {
        for (String pushNoteProvider : pushNotificationProviders) {
          pushNotificationRepo.addProvider(pushNoteProvider);
        }
      }
      DeviceManagementDataHolder.getInstance()
          .setPushNotificationProviderRepository(pushNotificationRepo);

      /* If -Dsetup option enabled then create device management database schema */
      String setupOption = System.getProperty(DeviceManagementConstants.Common.SETUP_PROPERTY);
      if (setupOption != null) {
        if (log.isDebugEnabled()) {
          log.debug(
              "-Dsetup is enabled. Device management repository schema initialization is about to "
                  + "begin");
        }
        this.setupDeviceManagementSchema(dsConfig);
      }

      /* Registering declarative service instances exposed by DeviceManagementServiceComponent */
      this.registerServices(componentContext);

      /* This is a workaround to initialize all Device Management Service Providers after the initialization
       * of Device Management Service component in order to avoid bundle start up order related complications */
      notifyStartupListeners();
      if (log.isDebugEnabled()) {
        log.debug("Device management core bundle has been successfully initialized");
      }
    } catch (Throwable e) {
      log.error("Error occurred while initializing device management core bundle", e);
    }
  }