@SuppressWarnings("unchecked")
 @Override
 public List<String> getConnectorInstanceNames(
     ConnectorManager connectorManager, String collectionName) {
   List<String> connectorInstanceNames = new ArrayList<String>();
   RecordCollectionServices collectionServices =
       ConstellioSpringUtils.getRecordCollectionServices();
   RecordCollection collection = collectionServices.get(collectionName);
   if (collection != null) {
     Element xml =
         ConnectorManagerRequestUtils.sendGet(connectorManager, "/getConnectorInstanceList", null);
     // ConnectorInstances / ConnectorInstance / ConnectorName
     if (xml.element(ServletUtil.XMLTAG_CONNECTOR_INSTANCES) != null) {
       List<Element> connectorIntanceElements =
           xml.element(ServletUtil.XMLTAG_CONNECTOR_INSTANCES)
               .elements(ServletUtil.XMLTAG_CONNECTOR_INSTANCE);
       for (Iterator<Element> it = connectorIntanceElements.iterator(); it.hasNext(); ) {
         Element connectorInstanceElement = (Element) it.next();
         Element connectorInstanceNameElement =
             connectorInstanceElement.element(ServletUtil.XMLTAG_CONNECTOR_NAME);
         String connectorInstanceName = connectorInstanceNameElement.getTextTrim();
         if (collection.getConnectorInstance(connectorInstanceName) != null) {
           connectorInstanceNames.add(connectorInstanceName);
         }
       }
     }
   }
   return connectorInstanceNames;
 }
 @Override
 public ConnectorManager createDefaultConnectorManager() {
   ConnectorManager connectorManager = new ConnectorManager();
   connectorManager.setName(DEFAUT_NAME);
   try {
     URL defaultConnectorManagerURL = ConstellioSpringUtils.getDefaultConnectorManagerURL();
     connectorManager.setUrl(defaultConnectorManagerURL.toString());
   } catch (MalformedURLException e) {
     LOGGER.log(Level.WARNING, "Invalid URL for default ConnectorManager", e);
   }
   this.getEntityManager().persist(connectorManager);
   return connectorManager;
 }
  @Override
  public void removeConnector(ConnectorManager connectorManager, String connectorName) {
    String connectorType = getConnectorType(connectorManager, connectorName);

    Map<String, String> paramsMap = new HashMap<String, String>();
    paramsMap.put(ServletUtil.XMLTAG_CONNECTOR_NAME, connectorName);
    ConnectorManagerRequestUtils.sendGet(connectorManager, "/removeConnector", paramsMap);

    BackupServices backupServices = ConstellioSpringUtils.getBackupServices();
    if (backupServices.hasConfigBackup(connectorName, connectorType)) {
      backupServices.deleteConfigBackup(connectorName, connectorType);
    }
  }
  @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;
    }
  }
  @Override
  public void synchronizeWithDatabase(ConnectorManager connectorManager) {
    ConnectorTypeServices connectorTypeServices = ConstellioSpringUtils.getConnectorTypeServices();

    // Add missing connector types
    List<String> connectorTypeNames = getConnectorTypes(connectorManager);
    for (String connectorTypeName : connectorTypeNames) {
      ConnectorType connectorType = connectorTypeServices.get(connectorTypeName);
      if (connectorType == null) {
        connectorType = new ConnectorType();
        connectorType.setName(connectorTypeName);

        File connectorsDir = ConstellioSpringUtils.getGoogleConnectorsDir();
        File connectorTypeDir = new File(connectorsDir, connectorType.getName());

        File iconFile = new File(connectorTypeDir, "icon.gif");
        if (iconFile.exists()) {
          try {
            byte[] iconBytes = FileUtils.readFileToByteArray(iconFile);
            connectorType.setIconFileContent(iconBytes);
          } catch (IOException e) {
            throw new RuntimeException(e);
          }
        }

        File connectorTypeMetaXmlFile = new File(connectorTypeDir, "connectorTypeMeta.xml");
        if (connectorTypeMetaXmlFile.exists()) {
          String path = connectorTypeMetaXmlFile.toURI().toString();
          BeanFactory connectorTypeMeta = new FileSystemXmlApplicationContext(path);
          if (connectorTypeMeta.containsBean("searchResultPanelClassName")) {
            String searchResultPanelClassName =
                (String) connectorTypeMeta.getBean("searchResultPanelClassName");
            connectorType.setSearchResultPanelClassName(searchResultPanelClassName);
          }
          if (connectorTypeMeta.containsBean("initInstanceHandlerClassName")) {
            String initInstancePluginClassName =
                (String) connectorTypeMeta.getBean("initInstanceHandlerClassName");
            connectorType.setInitInstanceHandlerClassName(initInstancePluginClassName);
          }
        }

        connectorType.setConnectorManager(connectorManager);
        connectorTypeServices.makePersistent(connectorType);
      }
    }

    // Remove deleted connector types
    List<ConnectorType> dbConnectorType = connectorTypeServices.list();
    for (ConnectorType connectorType : dbConnectorType) {
      if (!connectorTypeNames.contains(connectorType.getName())) {
        connectorTypeServices.makeTransient(connectorType);
      }
    }

    ConnectorInstanceServices connectorInstanceServices =
        ConstellioSpringUtils.getConnectorInstanceServices();
    BackupServices backupServices = ConstellioSpringUtils.getBackupServices();
    List<ConnectorInstance> connectorInstances = connectorInstanceServices.list();
    for (ConnectorInstance connectorInstance : connectorInstances) {
      String connectorName = connectorInstance.getName();
      String connectorTypeName = connectorInstance.getConnectorType().getName();
      boolean existingConnector = isExistingConnector(connectorManager, connectorName);
      boolean hasConfigBackup = backupServices.hasConfigBackup(connectorName, connectorTypeName);
      if (!existingConnector && hasConfigBackup) {
        backupServices.restoreConfigBackup(connectorName, connectorTypeName);
      } else if (existingConnector && !hasConfigBackup) {
        backupServices.backupConfig(connectorName, connectorTypeName);
      }
    }
  }