@Override
  public void setSchedule(
      ConnectorManager connectorManager, String connectorName, Schedule schedule) {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement(ServletUtil.XMLTAG_CONNECTOR_SCHEDULES);
    root.addElement(ServletUtil.XMLTAG_CONNECTOR_NAME).addText(connectorName);
    if (schedule.isDisabled()) {
      root.addElement(ServletUtil.XMLTAG_DISABLED).addText(Boolean.toString(schedule.isDisabled()));
    }
    root.addElement(ServletUtil.XMLTAG_LOAD).addText(Integer.toString(schedule.getLoad()));
    root.addElement(ServletUtil.XMLTAG_DELAY)
        .addText(Integer.toString(schedule.getRetryDelayMillis()));
    root.addElement(ServletUtil.XMLTAG_TIME_INTERVALS).addText(schedule.getTimeIntervalsAsString());
    ConnectorManagerRequestUtils.sendPost(connectorManager, "/setSchedule", document);

    String connectorType = getConnectorType(connectorManager, connectorName);
    BackupServices backupServices = ConstellioSpringUtils.getBackupServices();
    backupServices.backupConfig(connectorName, connectorType);
  }
  private Element setConnectorConfig(
      ConnectorManager connectorManager,
      String connectorName,
      String connectorType,
      Map<String, String[]> requestParams,
      boolean update,
      Locale locale) {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement(ServletUtil.XMLTAG_CONNECTOR_CONFIG);

    root.addElement(ServletUtil.QUERY_PARAM_LANG).addText(locale.getLanguage());
    root.addElement(ServletUtil.XMLTAG_CONNECTOR_NAME).addText(connectorName);
    root.addElement(ServletUtil.XMLTAG_CONNECTOR_TYPE).addText(connectorType);
    root.addElement(ServletUtil.XMLTAG_UPDATE_CONNECTOR).addText(Boolean.toString(update));

    for (String paramName : requestParams.keySet()) {
      if (!paramName.startsWith("wicket:")) {
        String[] paramValues = requestParams.get(paramName);
        for (String paramValue : paramValues) {
          Element paramElement = root.addElement(ServletUtil.XMLTAG_PARAMETERS);
          paramElement.addAttribute("name", paramName);
          paramElement.addAttribute("value", paramValue);
        }
      }
    }

    Element response =
        ConnectorManagerRequestUtils.sendPost(connectorManager, "/setConnectorConfig", document);
    Element statusIdElement = response.element(ServletUtil.XMLTAG_STATUSID);
    if (statusIdElement != null) {
      String statusId = statusIdElement.getTextTrim();
      if (!statusId.equals("" + ConnectorMessageCode.SUCCESS)) {
        return response;
      } else {
        BackupServices backupServices = ConstellioSpringUtils.getBackupServices();
        backupServices.backupConfig(connectorName, connectorType);
        return null;
      }
    } else {
      return null;
    }
  }
  @SuppressWarnings("unchecked")
  @Override
  public List<Record> authorizeByConnector(
      List<Record> records,
      Collection<UserCredentials> userCredentialsList,
      ConnectorManager connectorManager) {
    List<Record> authorizedRecords = new ArrayList<Record>();

    Document document = DocumentHelper.createDocument();
    Element root = document.addElement(ServletUtil.XMLTAG_AUTHZ_QUERY);
    Element connectorQueryElement = root.addElement(ServletUtil.XMLTAG_CONNECTOR_QUERY);

    Map<ConnectorInstance, UserCredentials> credentialsMap =
        new HashMap<ConnectorInstance, UserCredentials>();
    Set<ConnectorInstance> connectorsWithoutCredentials = new HashSet<ConnectorInstance>();
    Map<String, Record> recordsByURLMap = new HashMap<String, Record>();
    boolean recordToValidate = false;
    for (Record record : records) {
      // Use to accelerate the matching between response urls and actual entities
      recordsByURLMap.put(record.getUrl(), record);
      ConnectorInstance connectorInstance = record.getConnectorInstance();
      UserCredentials connectorCredentials = credentialsMap.get(connectorInstance);
      if (connectorCredentials == null
          && !connectorsWithoutCredentials.contains(connectorInstance)) {
        RecordCollection collection = connectorInstance.getRecordCollection();
        for (CredentialGroup credentialGroup : collection.getCredentialGroups()) {
          if (credentialGroup.getConnectorInstances().contains(connectorInstance)) {
            for (UserCredentials userCredentials : userCredentialsList) {
              if (userCredentials.getCredentialGroup().equals(credentialGroup)) {
                connectorCredentials = userCredentials;
                credentialsMap.put(connectorInstance, userCredentials);
                break;
              }
            }
            break;
          }
        }
      }
      if (connectorCredentials == null) {
        connectorsWithoutCredentials.add(connectorInstance);
        LOGGER.warning("Missing credentials for connector " + connectorInstance.getName());
      } else {
        String username = connectorCredentials.getUsername();
        if (StringUtils.isNotBlank(username)) {
          String password = EncryptionUtils.decrypt(connectorCredentials.getEncryptedPassword());
          String domain = connectorCredentials.getDomain();
          Element identityElement = connectorQueryElement.addElement(ServletUtil.XMLTAG_IDENTITY);
          identityElement.setText(username);
          if (StringUtils.isNotBlank(domain)) {
            identityElement.addAttribute(ServletUtil.XMLTAG_DOMAIN_ATTRIBUTE, domain);
          }
          identityElement.addAttribute(ServletUtil.XMLTAG_PASSWORD_ATTRIBUTE, password);

          Element resourceElement = identityElement.addElement(ServletUtil.XMLTAG_RESOURCE);
          resourceElement.setText(record.getUrl());
          resourceElement.addAttribute(
              ServletUtil.XMLTAG_CONNECTOR_NAME_ATTRIBUTE, connectorInstance.getName());
          recordToValidate = true;
        }
      }
    }

    if (recordToValidate) {
      Element response =
          ConnectorManagerRequestUtils.sendPost(connectorManager, "/authorization", document);
      Element authzResponseElement = response.element(ServletUtil.XMLTAG_AUTHZ_RESPONSE);
      List<Element> answerElements = authzResponseElement.elements(ServletUtil.XMLTAG_ANSWER);
      for (Element answerElement : answerElements) {
        Element decisionElement = answerElement.element(ServletUtil.XMLTAG_DECISION);
        boolean permit = decisionElement.getTextTrim().equals("Permit");
        if (permit) {
          Element resourceElement = answerElement.element(ServletUtil.XMLTAG_RESOURCE);
          String recordUrl = resourceElement.getTextTrim();
          Record record = recordsByURLMap.get(recordUrl);
          authorizedRecords.add(record);
        }
      }
    }
    return authorizedRecords;
  }