/**
   * @param applicationOperationDevice holds the information needs to retrieve device list.
   * @return List of devices
   * @throws MobileApplicationException
   */
  public List<Device> getDevices(ApplicationOperationDevice applicationOperationDevice)
      throws MobileApplicationException {

    List<Device> devices;
    try {
      List<org.wso2.carbon.device.mgt.common.Device> deviceList =
          MDMServiceAPIUtils.getDeviceManagementService(applicationOperationDevice.getTenantId())
              .getDevicesOfUser(applicationOperationDevice.getCurrentUser().getUsername());
      devices = new ArrayList<>(deviceList.size());
      if (log.isDebugEnabled()) {
        log.debug("device list got from mdm " + deviceList.toString());
      }
      for (org.wso2.carbon.device.mgt.common.Device commonDevice : deviceList) {
        if (MDMAppConstants.ACTIVE.equals(
            commonDevice.getEnrolmentInfo().getStatus().toString().toLowerCase())) {
          Device device = new Device();
          org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier deviceIdentifier =
              new org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier();
          deviceIdentifier.setId(commonDevice.getDeviceIdentifier());
          deviceIdentifier.setType(commonDevice.getType());
          device.setDeviceIdentifier(deviceIdentifier);
          device.setName(commonDevice.getName());
          device.setModel(commonDevice.getName());
          device.setType(MDMAppConstants.MOBILE_DEVICE);
          String imgUrl;
          if (MDMAppConstants.ANDROID.equalsIgnoreCase(commonDevice.getType())) {
            imgUrl =
                String.format(
                    applicationOperationDevice.getConfigParams().get(MDMAppConstants.IMAGE_URL),
                    MDMAppConstants.NEXUS);
          } else if (MDMAppConstants.IOS.equalsIgnoreCase(commonDevice.getType())) {
            imgUrl =
                String.format(
                    applicationOperationDevice.getConfigParams().get(MDMAppConstants.IMAGE_URL),
                    MDMAppConstants.IPHONE);
          } else {
            imgUrl =
                String.format(
                    applicationOperationDevice.getConfigParams().get(MDMAppConstants.IMAGE_URL),
                    MDMAppConstants.NONE);
          }
          device.setImage(imgUrl);
          device.setPlatform(commonDevice.getType());
          devices.add(device);
        }
      }
    } catch (DeviceManagementException e) {
      logError("Error While retrieving Device List.", e);
      throw new MobileApplicationException(e.getMessage());
    }
    return devices;
  }
  /**
   * Create a new device identifier from Device object.
   *
   * @param device device which is to be retrieved type and id
   * @return created device identifier
   */
  private static DeviceIdentifier getDeviceIdentifierByDevice(
      org.wso2.carbon.device.mgt.common.Device device) {
    DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
    deviceIdentifier.setId(device.getDeviceIdentifier());
    deviceIdentifier.setType(device.getType());

    return deviceIdentifier;
  }
  public PolicyEnforcementDelegatorImpl(List<Device> devices) {

    log.info("Policy re-enforcing stared due to change of the policies.");

    if (log.isDebugEnabled()) {
      for (Device device : devices) {
        log.debug(
            "Policy re-enforcing for device :"
                + device.getDeviceIdentifier()
                + " - Type : "
                + device.getType());
      }
    }
    this.devices = devices;
  }
  @Override
  public void delegate() throws PolicyDelegationException {

    for (Device device : devices) {
      DeviceIdentifier identifier = new DeviceIdentifier();
      identifier.setId(device.getDeviceIdentifier());
      identifier.setType(device.getType());

      Policy policy = this.getEffectivePolicy(identifier);

      if (policy != null) {
        List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
        deviceIdentifiers.add(identifier);
        this.addPolicyOperation(deviceIdentifiers, policy);
      }
    }
  }
  @Override
  public Activity installApplicationForUsers(Operation operation, List<String> userNameList)
      throws ApplicationManagementException {

    String userName = null;
    try {
      List<Device> deviceList;
      List<DeviceIdentifier> deviceIdentifierList = new ArrayList<>();
      DeviceIdentifier deviceIdentifier;

      for (String user : userNameList) {
        userName = user;
        deviceList =
            DeviceManagementDataHolder.getInstance()
                .getDeviceManagementProvider()
                .getDevicesOfUser(user);
        for (Device device : deviceList) {
          deviceIdentifier = new DeviceIdentifier();
          deviceIdentifier.setId(Integer.toString(device.getId()));
          deviceIdentifier.setType(device.getType());

          deviceIdentifierList.add(deviceIdentifier);
        }
      }
      // TODO: Fix this properly later adding device type to be passed in when the task manage
      // executes "addOperations()"
      String type = null;
      if (deviceIdentifierList.size() > 0) {
        type = deviceIdentifierList.get(0).getType();
      }

      return DeviceManagementDataHolder.getInstance()
          .getDeviceManagementProvider()
          .addOperation(type, operation, deviceIdentifierList);
    } catch (InvalidDeviceException e) {
      throw new ApplicationManagementException("Invalid DeviceIdentifiers found.", e);
    } catch (DeviceManagementException e) {
      throw new ApplicationManagementException(
          "Error in get devices for user: "******" in app installation", e);

    } catch (OperationManagementException e) {
      throw new ApplicationManagementException("Error in add operation at app installation", e);
    }
  }