<commit_before>package org.sagebionetworks.bridge.services.backfill;

import java.util.Iterator;

import org.sagebionetworks.bridge.dao.AccountDao;
import org.sagebionetworks.bridge.dao.UserConsentDao;
import org.sagebionetworks.bridge.models.accounts.Account;
import org.sagebionetworks.bridge.models.accounts.HealthId;
import org.sagebionetworks.bridge.models.accounts.UserConsent;
import org.sagebionetworks.bridge.models.backfill.BackfillTask;
import org.sagebionetworks.bridge.models.studies.Study;
import org.sagebionetworks.bridge.services.HealthCodeService;
import org.sagebionetworks.bridge.services.StudyService;
import org.sagebionetworks.bridge.services.TaskEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("enrollmentEventBackfill")
public class EnrollmentEventBackfill extends AsyncBackfillTemplate {
    
    private BackfillRecordFactory backfillFactory;
    private TaskEventService taskEventService;
    private AccountDao accountDao;
    private StudyService studyService;
    private UserConsentDao userConsentDao;
    private HealthCodeService healthCodeService;
    
    @Autowired
    public void setBackfillFactory(BackfillRecordFactory backfillFactory) {
        this.backfillFactory = backfillFactory;
    }
    @Autowired
    public void setTaskEventService(TaskEventService taskEventService) {
        this.taskEventService = taskEventService;
    }
    @Autowired
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Autowired
    public void setStudyService(StudyService studyService) {
        this.studyService = studyService;
    }
    @Autowired
    public void setUserConsentDao(UserConsentDao userConsentDao) {
        this.userConsentDao = userConsentDao;
    }
    @Autowired
    public void setHealthCodeService(HealthCodeService healthCodeService) {
        this.healthCodeService = healthCodeService;
    }
    
    @Override
    int getLockExpireInSeconds() {
        return 30 * 60;
    }

    @Override
    void doBackfill(BackfillTask task, BackfillCallback callback) {
        for (Iterator<Account> i = accountDao.getAllAccounts(); i.hasNext();) {
            Account account = i.next();
            Study study = studyService.getStudy(account.getStudyIdentifier());
            HealthId mapping = healthCodeService.getMapping(account.getHealthId());
            UserConsent consent = null;
            if (mapping != null) {
                String healthCode = mapping.getCode();
                
                consent = userConsentDao.getUserConsent(healthCode, study.getStudyIdentifier());
                if (consent != null) {
                    taskEventService.publishEvent(healthCode, consent);
                    callback.newRecords(
                        backfillFactory.createAndSave(task, study, account, "enrollment event created"));
                }
            }
            if (mapping == null && consent == null) {
                backfillFactory.createOnly(task, "Health code and consent record not found");
            } else if (mapping == null) {
                backfillFactory.createOnly(task, "Health code not found");    
            } else if (consent == null) {
                backfillFactory.createOnly(task, "Consent record not found");
            }
        }
    }<commit_msg>Not logging the accounts that are not touched<commit_after>package org.sagebionetworks.bridge.services.backfill;

import java.util.Iterator;

import org.sagebionetworks.bridge.dao.AccountDao;
import org.sagebionetworks.bridge.dao.UserConsentDao;
import org.sagebionetworks.bridge.models.accounts.Account;
import org.sagebionetworks.bridge.models.accounts.HealthId;
import org.sagebionetworks.bridge.models.accounts.UserConsent;
import org.sagebionetworks.bridge.models.backfill.BackfillTask;
import org.sagebionetworks.bridge.models.studies.Study;
import org.sagebionetworks.bridge.services.HealthCodeService;
import org.sagebionetworks.bridge.services.StudyService;
import org.sagebionetworks.bridge.services.TaskEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("enrollmentEventBackfill")
public class EnrollmentEventBackfill extends AsyncBackfillTemplate {
    
    private BackfillRecordFactory backfillFactory;
    private TaskEventService taskEventService;
    private AccountDao accountDao;
    private StudyService studyService;
    private UserConsentDao userConsentDao;
    private HealthCodeService healthCodeService;
    
    @Autowired
    public void setBackfillFactory(BackfillRecordFactory backfillFactory) {
        this.backfillFactory = backfillFactory;
    }
    @Autowired
    public void setTaskEventService(TaskEventService taskEventService) {
        this.taskEventService = taskEventService;
    }
    @Autowired
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Autowired
    public void setStudyService(StudyService studyService) {
        this.studyService = studyService;
    }
    @Autowired
    public void setUserConsentDao(UserConsentDao userConsentDao) {
        this.userConsentDao = userConsentDao;
    }
    @Autowired
    public void setHealthCodeService(HealthCodeService healthCodeService) {
        this.healthCodeService = healthCodeService;
    }
    
    @Override
    int getLockExpireInSeconds() {
        return 30 * 60;
    }

    @Override
    void doBackfill(BackfillTask task, BackfillCallback callback) {
        for (Iterator<Account> i = accountDao.getAllAccounts(); i.hasNext();) {
            Account account = i.next();
            Study study = studyService.getStudy(account.getStudyIdentifier());
            HealthId mapping = healthCodeService.getMapping(account.getHealthId());
            UserConsent consent = null;
            if (mapping != null) {
                String healthCode = mapping.getCode();
                
                consent = userConsentDao.getUserConsent(healthCode, study.getStudyIdentifier());
                if (consent != null) {
                    taskEventService.publishEvent(healthCode, consent);
                    callback.newRecords(
                        backfillFactory.createAndSave(task, study, account, "enrollment event created"));
                }
            }
            if (mapping == null && consent == null) {
                callback.newRecords(backfillFactory.createOnly(task, "Health code and consent record not found"));
            } else if (mapping == null) {
                callback.newRecords(backfillFactory.createOnly(task, "Health code not found"));    
            } else if (consent == null) {
                callback.newRecords(backfillFactory.createOnly(task, "Consent record not found"));
            }
        }
    }
{'fraction_non_alphanumeric': 0.08044644151326279, 'fraction_numerical': 0.0011595883461371214, 'mean_word_length': 3.1144901610017888, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3636174', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1883, 'n_words': 357}