<commit_before>package fi.otavanopisto.kuntaapi.server.integrations.vcard;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.AccessTimeout;
import javax.ejb.Singleton;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.io.chain.ChainingTextParser;
import fi.metatavu.kuntaapi.server.rest.model.Contact;
import fi.otavanopisto.kuntaapi.server.cache.ModificationHashCache;
import fi.otavanopisto.kuntaapi.server.controllers.IdentifierController;
import fi.otavanopisto.kuntaapi.server.controllers.IdentifierRelationController;
import fi.otavanopisto.kuntaapi.server.discover.EntityUpdater;
import fi.otavanopisto.kuntaapi.server.id.ContactId;
import fi.otavanopisto.kuntaapi.server.id.OrganizationId;
import fi.otavanopisto.kuntaapi.server.integrations.BinaryHttpClient;
import fi.otavanopisto.kuntaapi.server.integrations.BinaryHttpClient.BinaryResponse;
import fi.otavanopisto.kuntaapi.server.integrations.GenericHttpClient.Response;
import fi.otavanopisto.kuntaapi.server.integrations.KuntaApiConsts;
import fi.otavanopisto.kuntaapi.server.integrations.vcard.tasks.OrganizationVCardsTaskQueue;
import fi.otavanopisto.kuntaapi.server.persistence.model.Identifier;
import fi.otavanopisto.kuntaapi.server.resources.ContactResourceContainer;
import fi.otavanopisto.kuntaapi.server.settings.OrganizationSettingController;
import fi.otavanopisto.kuntaapi.server.settings.SystemSettingController;
import fi.otavanopisto.kuntaapi.server.tasks.OrganizationEntityUpdateTask;

@ApplicationScoped
@Singleton
@AccessTimeout (unit = TimeUnit.HOURS, value = 1l)
@SuppressWarnings ("squid:S3306")
public class VCardEntityUpdater extends EntityUpdater {

  @Inject
  private Logger logger;

  @Inject  
  private SystemSettingController systemSettingController;

  @Inject
  private BinaryHttpClient binaryHttpClient;
  
  @Inject
  private VCardTranslator vCardTranslator;
  
  @Inject
  private ContactResourceContainer contactCache;
  
  @Inject
  private OrganizationSettingController organizationSettingController; 
  
  @Inject
  private IdentifierController identifierController;

  @Inject
  private IdentifierRelationController identifierRelationController;

  @Inject
  private ModificationHashCache modificationHashCache;
  
  @Inject
  private OrganizationVCardsTaskQueue organizationVCardsTaskQueue;

  @Override
  public String getName() {
    return "vcard-contacts";
  }

  @Override
  public void timeout() {
    if (systemSettingController.isNotTestingOrTestRunning()) {
      OrganizationEntityUpdateTask task = organizationVCardsTaskQueue.next();
      if (task != null) {
        updateContacts(task.getOrganizationId());
      } else {
        organizationVCardsTaskQueue.enqueueTasks(organizationSettingController.listOrganizationIdsWithSetting(VCardConsts.ORGANIZATION_SETTING_URL));
      }
    }
  }
  
  private void updateContacts(OrganizationId organizationId) {
    String url = getUrl(organizationId);
    if (StringUtils.isBlank(url)) {
      return;
    }
    
    String username = getUsername(organizationId);
    String password = getPassword(organizationId);
    
    Response<BinaryResponse> response = binaryHttpClient.downloadBinary(url, username, password);
    if (response.isOk()) {
      BinaryResponse responseEntity = response.getResponseEntity();
      try {
        updateContacts(organizationId, parseVCards(responseEntity));
      } catch (IOException e) {
        logger.log(Level.SEVERE, String.format("Failed to read VCard stream for organization %s", organizationId.toString()), e);
      }
    } else {
      logger.severe(String.format("Organization %s vcard contact list download failed on [%d] %s", organizationId.toString(), response.getStatus(), response.getMessage()));
    }
  }

  private void updateContacts(OrganizationId organizationId, List<VCard> vCards) {
    List<ContactId> removedIds = identifierController.listOrganizationContactIdsBySource(organizationId, VCardConsts.IDENTIFIER_NAME);
    <commit_msg>Fixed queue filling issue in organization vcard updater<commit_after>package fi.otavanopisto.kuntaapi.server.integrations.vcard;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.AccessTimeout;
import javax.ejb.Singleton;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.io.chain.ChainingTextParser;
import fi.metatavu.kuntaapi.server.rest.model.Contact;
import fi.otavanopisto.kuntaapi.server.cache.ModificationHashCache;
import fi.otavanopisto.kuntaapi.server.controllers.IdentifierController;
import fi.otavanopisto.kuntaapi.server.controllers.IdentifierRelationController;
import fi.otavanopisto.kuntaapi.server.discover.EntityUpdater;
import fi.otavanopisto.kuntaapi.server.id.ContactId;
import fi.otavanopisto.kuntaapi.server.id.OrganizationId;
import fi.otavanopisto.kuntaapi.server.integrations.BinaryHttpClient;
import fi.otavanopisto.kuntaapi.server.integrations.BinaryHttpClient.BinaryResponse;
import fi.otavanopisto.kuntaapi.server.integrations.GenericHttpClient.Response;
import fi.otavanopisto.kuntaapi.server.integrations.KuntaApiConsts;
import fi.otavanopisto.kuntaapi.server.integrations.vcard.tasks.OrganizationVCardsTaskQueue;
import fi.otavanopisto.kuntaapi.server.persistence.model.Identifier;
import fi.otavanopisto.kuntaapi.server.resources.ContactResourceContainer;
import fi.otavanopisto.kuntaapi.server.settings.OrganizationSettingController;
import fi.otavanopisto.kuntaapi.server.settings.SystemSettingController;
import fi.otavanopisto.kuntaapi.server.tasks.OrganizationEntityUpdateTask;

@ApplicationScoped
@Singleton
@AccessTimeout (unit = TimeUnit.HOURS, value = 1l)
@SuppressWarnings ("squid:S3306")
public class VCardEntityUpdater extends EntityUpdater {

  @Inject
  private Logger logger;

  @Inject  
  private SystemSettingController systemSettingController;

  @Inject
  private BinaryHttpClient binaryHttpClient;
  
  @Inject
  private VCardTranslator vCardTranslator;
  
  @Inject
  private ContactResourceContainer contactCache;
  
  @Inject
  private OrganizationSettingController organizationSettingController; 
  
  @Inject
  private IdentifierController identifierController;

  @Inject
  private IdentifierRelationController identifierRelationController;

  @Inject
  private ModificationHashCache modificationHashCache;
  
  @Inject
  private OrganizationVCardsTaskQueue organizationVCardsTaskQueue;

  @Override
  public String getName() {
    return "vcard-contacts";
  }

  @Override
  public void timeout() {
    if (systemSettingController.isNotTestingOrTestRunning()) {
      OrganizationEntityUpdateTask task = organizationVCardsTaskQueue.next();
      if (task != null) {
        updateContacts(task.getOrganizationId());
      } else {
        if (organizationVCardsTaskQueue.isEmpty()) {
          organizationVCardsTaskQueue.enqueueTasks(organizationSettingController.listOrganizationIdsWithSetting(VCardConsts.ORGANIZATION_SETTING_URL));
        }
      }
    }
  }
  
  private void updateContacts(OrganizationId organizationId) {
    String url = getUrl(organizationId);
    if (StringUtils.isBlank(url)) {
      return;
    }
    
    String username = getUsername(organizationId);
    String password = getPassword(organizationId);
    
    Response<BinaryResponse> response = binaryHttpClient.downloadBinary(url, username, password);
    if (response.isOk()) {
      BinaryResponse responseEntity = response.getResponseEntity();
      try {
        updateContacts(organizationId, parseVCards(responseEntity));
      } catch (IOException e) {
        logger.log(Level.SEVERE, String.format("Failed to read VCard stream for organization %s", organizationId.toString()), e);
      }
    } else {
      logger.severe(String.format("Organization %s vcard contact list download failed on [%d] %s", organizationId.toString(), response.getStatus(), response.getMessage()));
    }
  }

  private void updateContacts(OrganizationId organizationId, List<VCard> vCards) {
    List<ContactId> removedIds = identifierController.listOrganizationContactIdsBySource(organizationId, VCardConsts.IDENTIFIER_NAME);
    
{'fraction_non_alphanumeric': 0.08574699484831139, 'fraction_numerical': 0.0013737836290784202, 'mean_word_length': 5.563486100676183, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6027666', 'n_tokens_mistral': 2577, 'n_tokens_neox': 2567, 'n_words': 429}