<commit_before>/*
 * IoT - Industrial Internet Framework
 * Apache License Version 2.0, January 2004
 * Released as a part of Helsinki University
 * Software Engineering Lab in summer 2015
 */
package fi.iot.iiframework.source;

import java.util.List;
import javax.transaction.Transactional;
import fi.iot.iiframework.domain.InformationSource;
import fi.iot.iiframework.domain.Readout;
import fi.iot.iiframework.domain.Sensor;
import fi.iot.iiframework.mutator.MarkReadoutAsErronousIfValueIs;
import fi.iot.iiframework.mutator.ValueCondition;
import fi.iot.iiframework.services.domain.InformationSourceService;
import fi.iot.iiframework.services.domain.ReadoutService;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

/**
 * Responsible for writing read source-information to services and retrieving
 * information from services.
 */
@Service
public class InformationSourcePersistenceImpl implements InformationSourcePersistence {
    
    private final InformationSourceService sourceService;
    private final ReadoutService readoutService;

    @Autowired
    public InformationSourcePersistenceImpl(
            InformationSourceService sourceService,
            ReadoutService readoutService) {
        this.sourceService = sourceService;
        this.readoutService = readoutService;
    }

    @Override
    public List<InformationSource> loadSourcesFromDB() {
        return sourceService.getAll();
    }

    @Override
    @Transactional
    public InformationSource writeReadoutsToSource(InformationSource source, List<Sensor> sensors) {
        final InformationSource src = sourceService.getWithSensors(source.getId());
        sensors.forEach(s -> {
            s.setSource(src);
            src.getSensors().add(s);
        });
        associateReadoutsWithPersistentSensors(src, sensors);
        sourceService.save(src);
        return src;
    }

    /**
     * Adds new readouts to known sensors and saves those sensors with their new
     * readouts.
     *
     * @param source
     * @param sensors
     */
    private void associateReadoutsWithPersistentSensors(InformationSource source, List<Sensor> sensors) {
        sensors.forEach(s -> {
            source.getSensors().stream()
                    .filter(sensor -> sensor.isActive())
                    .forEach(sensor -> {
                if (s.equals(sensor)) {
                    s.getReadouts().forEach(r -> r.setSensor(sensor));
                    sensor.getReadouts().addAll(s.getReadouts());
                    mutateReadouts(sensor);
                }
            });
        });<commit_msg>Added TODO comment in ISPersistence-class<commit_after>/*
 * IoT - Industrial Internet Framework
 * Apache License Version 2.0, January 2004
 * Released as a part of Helsinki University
 * Software Engineering Lab in summer 2015
 */
package fi.iot.iiframework.source;

import java.util.List;
import javax.transaction.Transactional;
import fi.iot.iiframework.domain.InformationSource;
import fi.iot.iiframework.domain.Readout;
import fi.iot.iiframework.domain.Sensor;
import fi.iot.iiframework.mutator.MarkReadoutAsErronousIfValueIs;
import fi.iot.iiframework.mutator.ValueCondition;
import fi.iot.iiframework.services.domain.InformationSourceService;
import fi.iot.iiframework.services.domain.ReadoutService;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

/**
 * Responsible for writing read source-information to services and retrieving
 * information from services.
 */
@Service
public class InformationSourcePersistenceImpl implements InformationSourcePersistence {
    
    private final InformationSourceService sourceService;
    private final ReadoutService readoutService;

    @Autowired
    public InformationSourcePersistenceImpl(
            InformationSourceService sourceService,
            ReadoutService readoutService) {
        this.sourceService = sourceService;
        this.readoutService = readoutService;
    }

    @Override
    public List<InformationSource> loadSourcesFromDB() {
        return sourceService.getAll();
    }

    @Override
    @Transactional
    public InformationSource writeReadoutsToSource(InformationSource source, List<Sensor> sensors) {
        final InformationSource src = sourceService.getWithSensors(source.getId());
        sensors.forEach(s -> {
            s.setSource(src);
            src.getSensors().add(s);
        });
        associateReadoutsWithPersistentSensors(src, sensors);
        sourceService.save(src);
        return src;
    }

    /**
     * Adds new readouts to known sensors and saves those sensors with their new
     * readouts. Takes horrific amounts of memory, TODO: FIX.
     *
     * @param source
     * @param sensors
     */
    private void associateReadoutsWithPersistentSensors(InformationSource source, List<Sensor> sensors) {
        sensors.forEach(s -> {
            source.getSensors().stream()
                    .filter(sensor -> sensor.isActive())
                    .forEach(sensor -> {
                if (s.equals(sensor)) {
                    s.getReadouts().forEach(r -> r.setSensor(sensor));
                    sensor.getReadouts().addAll(s.getReadouts());
                    mutateReadouts(sensor);
                }
            });
        });
{'fraction_non_alphanumeric': 0.08697214734950584, 'fraction_numerical': 0.0035938903863432167, 'mean_word_length': 3.074670571010249, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '3408710', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1409, 'n_words': 311}