<commit_before>/* 
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.objecthunter.larch.service.impl;

import net.objecthunter.larch.model.Archive;
import net.objecthunter.larch.service.ArchiveService;
import net.objecthunter.larch.service.EntityService;
import net.objecthunter.larch.service.backend.BackendArchiveBlobService;
import net.objecthunter.larch.service.backend.BackendArchiveIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.IOException;
import java.io.InputStream;
import java.time.ZonedDateTime;
import java.util.List;

public class DefaultArchiveService implements ArchiveService {

    @Autowired
    private BackendArchiveBlobService archiveBlobStore;

    @Autowired
    private BackendArchiveIndexService archiveIndex;

    @Autowired
    private EntityService entityService;

    @Override
    public void archive(final String entityId, final int version) throws IOException {
        final String path = archiveBlobStore.saveOrUpdate(entityService.retrieve(entityId, version));
        final String userName = ((UserDetails) SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal()).getUsername();
        Archive a = new Archive();
        a.setEntityId(entityId);
        a.setEntityVersion(version);
        a.setCreatedDate(ZonedDateTime.now());
        a.setPath(path);
        a.setCreator(userName);
        archiveIndex.saveOrUpdate(a);
    }

    @Override
    public boolean isArchived(final String entityId, final int version) throws IOException {
        return archiveIndex.exists(entityId, version);
    }

    @Override
    public InputStream retrieveData(final String entityId, final int version) throws IOException {
        final Archive a = archiveIndex.retrieve(entityId, version);
        return archiveBlobStore.retrieve(a.getPath());
    }

    @Override
    public void delete(final String entityId, final int version) throws IOException {
        final Archive a = archiveIndex.retrieve(entityId, version);
        archiveIndex.delete(entityId, version);
        archiveBlobStore.delete(a.getPath());
    }

    @Override<commit_msg>fixed user fetching in archive service<commit_after>/* 
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.objecthunter.larch.service.impl;

import net.objecthunter.larch.model.Archive;
import net.objecthunter.larch.model.security.User;
import net.objecthunter.larch.service.ArchiveService;
import net.objecthunter.larch.service.EntityService;
import net.objecthunter.larch.service.backend.BackendArchiveBlobService;
import net.objecthunter.larch.service.backend.BackendArchiveIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.IOException;
import java.io.InputStream;
import java.time.ZonedDateTime;
import java.util.List;

public class DefaultArchiveService implements ArchiveService {

    @Autowired
    private BackendArchiveBlobService archiveBlobStore;

    @Autowired
    private BackendArchiveIndexService archiveIndex;

    @Autowired
    private EntityService entityService;

    @Override
    public void archive(final String entityId, final int version) throws IOException {
        final String path = archiveBlobStore.saveOrUpdate(entityService.retrieve(entityId, version));
        final String userName = ((User) SecurityContextHolder.getContext().getAuthentication()
                .getPrincipal()).getName();
        Archive a = new Archive();
        a.setEntityId(entityId);
        a.setEntityVersion(version);
        a.setCreatedDate(ZonedDateTime.now());
        a.setPath(path);
        a.setCreator(userName);
        archiveIndex.saveOrUpdate(a);
    }

    @Override
    public boolean isArchived(final String entityId, final int version) throws IOException {
        return archiveIndex.exists(entityId, version);
    }

    @Override
    public InputStream retrieveData(final String entityId, final int version) throws IOException {
        final Archive a = archiveIndex.retrieve(entityId, version);
        return archiveBlobStore.retrieve(a.getPath());
    }

    @Override
    public void delete(final String entityId, final int version) throws IOException {
        final Archive a = archiveIndex.retrieve(entityId, version);
        archiveIndex.delete(entityId, version);
        archiveBlobStore.delete(a.getPath());
    }

    @Override
{'fraction_non_alphanumeric': 0.08269525267993874, 'fraction_numerical': 0.00272247745448358, 'mean_word_length': 4.106863596872285, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1224182', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1493, 'n_words': 428}