<commit_before>package com.imcode.imcms.domain.service.api;

import com.imcode.imcms.domain.service.ImageHistoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("imageHistoryService")
@Transactional
public class DefaultImageHistoryService implements ImageHistoryService {
}<commit_msg>IMCMS-407 - Text history, image history changes: - Add image history service implementation<commit_after>package com.imcode.imcms.domain.service.api;

import com.imcode.imcms.domain.dto.ImageDTO;
import com.imcode.imcms.domain.dto.ImageHistoryDTO;
import com.imcode.imcms.domain.service.ImageHistoryService;
import com.imcode.imcms.domain.service.LanguageService;
import com.imcode.imcms.domain.service.UserService;
import com.imcode.imcms.persistence.entity.Image;
import com.imcode.imcms.persistence.entity.ImageHistoryJPA;
import com.imcode.imcms.persistence.entity.LanguageJPA;
import com.imcode.imcms.persistence.entity.LoopEntryRefJPA;
import com.imcode.imcms.persistence.repository.ImageHistoryRepository;
import imcode.server.Imcms;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service("imageHistoryService")
@Transactional
public class DefaultImageHistoryService implements ImageHistoryService {

    private final ImageHistoryRepository imageHistoryRepository;
    private final LanguageService languageService;
    private final UserService userService;
    private final ModelMapper modelMapper;

    public DefaultImageHistoryService(ImageHistoryRepository imageHistoryRepository, LanguageService languageService, UserService userService, ModelMapper modelMapper) {
        this.imageHistoryRepository = imageHistoryRepository;
        this.languageService = languageService;
        this.userService = userService;
        this.modelMapper = modelMapper;
    }

    @Override
    public void save(Image image) {
        final ImageHistoryJPA mappedHistory = new ImageHistoryJPA(
                image,
                userService.getUser(Imcms.getUser().getId()),
                LocalDateTime.now()
        );

        imageHistoryRepository.save(mappedHistory);
    }

    @Override
    public List<ImageHistoryDTO> getAll(ImageDTO image) {
        final Integer docId = image.getDocId();
        final Integer index = image.getIndex();
        final LanguageJPA language = new LanguageJPA(languageService.findByCode(image.getLangCode()));

        final Optional<LoopEntryRefJPA> loopEntry = Optional.ofNullable(image.getLoopEntryRef())
                .map(LoopEntryRefJPA::new);


        final List<ImageHistoryJPA> imageHistory = (loopEntry.isPresent())
                ? imageHistoryRepository.findImageHistoryInLoop(docId, language, loopEntry.get(), index)
                : imageHistoryRepository.findImageHistoryNotInLoop(docId, language, index);

        return imageHistory.stream()
                .sorted(Comparator.comparing(ImageHistoryJPA::getModifiedAt).reversed())
                .map(history -> modelMapper.map(history, ImageHistoryDTO.class))
                .collect(Collectors.toList());
    }
}
{'fraction_non_alphanumeric': 0.08735837805605247, 'fraction_numerical': 0.0008944543828264759, 'mean_word_length': 4.393890675241158, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, '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': '6349566', 'n_tokens_mistral': 901, 'n_tokens_neox': 875, 'n_words': 160}