<commit_before>package ca.corefacility.bioinformatics.irida.service.remote;

import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.SampleGenomeAssemblyJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField;
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin;
import ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry;
import ca.corefacility.bioinformatics.irida.service.GenomeAssemblyService;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.SequencingObjectService;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class LocalProjectHashingService {
    ProjectService projectService;
    SampleService sampleService;
    SequencingObjectService sequencingObjectService;
    GenomeAssemblyService assemblyService;

    @Autowired
    public LocalProjectHashingService(ProjectService projectService, SampleService sampleService, SequencingObjectService sequencingObjectService, GenomeAssemblyService assemblyService) {
        this.projectService = projectService;
        this.sampleService = sampleService;
        this.sequencingObjectService = sequencingObjectService;
        this.assemblyService = assemblyService;
    }

    public void hashAllProjects() {
        Iterable<Project> all = projectService.findAll();

        for (Project p : all) {
            System.out.println("==========/nID: " + p.getId() + ": " + p.getName());
            Integer projectHash = getProjectHash(p);
            System.out.println(projectHash);

        }
    }

    public Integer getProjectHash(Project project) {

        HashCodeBuilder builder = new HashCodeBuilder();

        builder.append(project);

        List<Join<Project, Sample>> samplesForProject = sampleService.getSamplesForProject(project);

        for (Join<Project, Sample> j : samplesForProject) {
            Sample sample = j.getObject();

            getSampleHash(sample, builder);
        }

        return builder.toHashCode();

    }

    public void getSampleHash(Sample sample, HashCodeBuilder builder) {
        //add the sample itself
        builder.append(sample);

        //add all the metadata entries
        Set<MetadataEntry> metadataForSample = sampleService.getMetadataForSample(sample);
        for (MetadataEntry e : metadataForSample) {
            builder.append(e);
        }

        //add all the sequence files
        Collection<SampleSequencingObjectJoin> sequencingObjectsForSample = sequencingObjectService.getSequencingObjectsForSample(sample);
        for (SampleSequencingObjectJoin join : sequencingObjectsForSample) {
            builder.append(join.getObject());
        }

        //add all assemblies<commit_msg>added an auth line to the project hash so only users on project can read the hash<commit_after>package ca.corefacility.bioinformatics.irida.service.remote;

import ca.corefacility.bioinformatics.irida.model.joins.Join;
import ca.corefacility.bioinformatics.irida.model.joins.impl.SampleGenomeAssemblyJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField;
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin;
import ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry;
import ca.corefacility.bioinformatics.irida.service.GenomeAssemblyService;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.SequencingObjectService;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class LocalProjectHashingService {
    ProjectService projectService;
    SampleService sampleService;
    SequencingObjectService sequencingObjectService;
    GenomeAssemblyService assemblyService;

    @Autowired
    public LocalProjectHashingService(ProjectService projectService, SampleService sampleService, SequencingObjectService sequencingObjectService, GenomeAssemblyService assemblyService) {
        this.projectService = projectService;
        this.sampleService = sampleService;
        this.sequencingObjectService = sequencingObjectService;
        this.assemblyService = assemblyService;
    }

    @PreAuthorize("hasPermission(#project, 'canReadProject')")
    public Integer getProjectHash(Project project) {

        HashCodeBuilder builder = new HashCodeBuilder();

        builder.append(project);

        List<Join<Project, Sample>> samplesForProject = sampleService.getSamplesForProject(project);

        for (Join<Project, Sample> j : samplesForProject) {
            Sample sample = j.getObject();

            getSampleHash(sample, builder);
        }

        return builder.toHashCode();

    }

    private void getSampleHash(Sample sample, HashCodeBuilder builder) {
        //add the sample itself
        builder.append(sample);

        //add all the metadata entries
        Set<MetadataEntry> metadataForSample = sampleService.getMetadataForSample(sample);
        for (MetadataEntry e : metadataForSample) {
            builder.append(e);
        }

        //add all the sequence files
        Collection<SampleSequencingObjectJoin> sequencingObjectsForSample = sequencingObjectService.getSequencingObjectsForSample(sample);
        for (SampleSequencingObjectJoin join : sequencingObjectsForSample) {
            builder.append(join.getObject());
        }

        //add all assemblies
{'fraction_non_alphanumeric': 0.08316107489266975, 'fraction_numerical': 0.0003180155827635554, 'mean_word_length': 4.254803675856308, 'pattern_counts': {'":': 1, '<': 14, '<?xml version=': 0, '>': 14, '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': '145606', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1573, 'n_words': 290}