<commit_before>package ro.msg.learning.shop.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ro.msg.learning.shop.entities.*;
import ro.msg.learning.shop.exceptions.ProductNotFoundException;
import ro.msg.learning.shop.exceptions.QuantityExceedsStockException;
import ro.msg.learning.shop.model.OrderInput;
import ro.msg.learning.shop.repositories.*;
import ro.msg.learning.shop.services.strategies.QuantityStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <NAME> <<EMAIL>>
 */

@Service
public class OrderCreator {

    private final OrderRepository orderRepository;
    private final ProductRepository productRepository;
    private final CustomerRepository customerRepository;
    private final EmployeeRepository employeeRepository;
    private final AddressRepository addressRepository;
    private final QuantityStrategy quantityStrategy;


    @Autowired
    public OrderCreator(OrderRepository orderRepository, ProductRepository productRepository, CustomerRepository customerRepository, EmployeeRepository employeeRepository, AddressRepository addressRepository, QuantityStrategy quantityStrategy) {
        this.orderRepository = orderRepository;
        this.productRepository = productRepository;
        this.customerRepository = customerRepository;
        this.employeeRepository = employeeRepository;
        this.addressRepository = addressRepository;
        this.quantityStrategy = quantityStrategy;
    }

    private ArrayList<OrderDetails> getOrderDetails(OrderInput orderInput) {
        ArrayList<OrderDetails> orderDetailsList = new ArrayList<>();
        Product tempProduct;
        for (Long key : orderInput.getProductMap().keySet()) {
            tempProduct = productRepository.findOne(key);
            long tempLong = productRepository.getQuantityForProduct(key);
            if (tempProduct == null) {
                throw new ProductNotFoundException();
            } else if (tempLong < orderInput.getProductMap().get(key)) {
                throw new QuantityExceedsStockException(key, orderInput.getProductMap().get(key), tempLong);
            } else {
                orderDetailsList.add(new OrderDetails(orderInput.getProductMap().get(key), 0.0, tempProduct));
            }
        }
        return orderDetailsList;
    }

    private Address getAddress(OrderInput orderInput) {
        Address address = addressRepository.findByCountryAndCityAndStreet(orderInput.getAddress().getCountry(), orderInput.getAddress().getCity(), orderInput.getAddress().getStreet());
        if (address == null) {
            address = addressRepository.save(orderInput.getAddress());
        }
        return address;
    }

    @Transactional
    public Order createOrder(OrderInput orderInput) {

        Order order = new Order();

        order.setOrdersDetails(getOrderDetails(orderInput));
        order.setOrderDate(orderInput.getDate());
        order.setAddress(getAddress(orderInput));
        order.setCustomer(customerRepository.findAll().get(0));//TODO add customer by getting the logged customer
        order.setEmployee(employeeRepository.getOne(orderRepository.employeeIdWithLeastOrders()));

        List<ProductsLocations> locations = quantityStrategy.
            getLocations(orderInput.getProductMap(), productRepository.
                findAllProductsLocationsInSet(orderInput.getProductMap().keySet()));

        locations.forEach(x -> productRepository.changeTheQuantity(x.getLocationId(), x.getProductId(), x.getQuantity()));

        return orderRepository.save(order);
    }
}

<commit_msg>small service fix<commit_after>package ro.msg.learning.shop.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ro.msg.learning.shop.entities.*;
import ro.msg.learning.shop.exceptions.ProductNotFoundException;
import ro.msg.learning.shop.exceptions.QuantityExceedsStockException;
import ro.msg.learning.shop.model.OrderInput;
import ro.msg.learning.shop.repositories.*;
import ro.msg.learning.shop.services.strategies.QuantityStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <NAME> <<EMAIL>>
 */

@Service
public class OrderCreator {

    private final OrderRepository orderRepository;
    private final ProductRepository productRepository;
    private final CustomerRepository customerRepository;
    private final EmployeeRepository employeeRepository;
    private final AddressRepository addressRepository;
    private final QuantityStrategy quantityStrategy;


    @Autowired
    public OrderCreator(OrderRepository orderRepository, ProductRepository productRepository, CustomerRepository customerRepository, EmployeeRepository employeeRepository, AddressRepository addressRepository, QuantityStrategy quantityStrategy) {
        this.orderRepository = orderRepository;
        this.productRepository = productRepository;
        this.customerRepository = customerRepository;
        this.employeeRepository = employeeRepository;
        this.addressRepository = addressRepository;
        this.quantityStrategy = quantityStrategy;
    }

    private ArrayList<OrderDetails> getOrderDetails(OrderInput orderInput) {
        ArrayList<OrderDetails> orderDetailsList = new ArrayList<>();
        Product tempProduct;
        for (Long key : orderInput.getProductMap().keySet()) {
            tempProduct = productRepository.findOne(key);
            long tempLong = productRepository.getQuantityForProduct(key);
            if (tempProduct == null) {
                throw new ProductNotFoundException();
            } else if (tempLong < orderInput.getProductMap().get(key)) {
                throw new QuantityExceedsStockException(key, orderInput.getProductMap().get(key), tempLong);
            } else {
                orderDetailsList.add(new OrderDetails(orderInput.getProductMap().get(key), 0.0, tempProduct));
            }
        }
        return orderDetailsList;
    }

    private Address getAddress(OrderInput orderInput) {
        Address address = addressRepository.findByCountryAndCityAndStreet(orderInput.getAddress().getCountry(), orderInput.getAddress().getCity(), orderInput.getAddress().getStreet());
        if (address == null) {
            address = addressRepository.save(orderInput.getAddress());
        }
        return address;
    }

    @Transactional
    public Order createOrder(OrderInput orderInput) {

        Order order = new Order();
        for (OrderDetails orderDetail : getOrderDetails(orderInput)) {
            order.addOrderDetail(orderDetail);
        }
        order.setOrderDate(orderInput.getDate());
        order.setAddress(getAddress(orderInput));
        order.setCustomer(customerRepository.findAll().get(0));//TODO add customer by getting the logged customer
        order.setEmployee(employeeRepository.getOne(orderRepository.employeeIdWithLeastOrders()));

        List<ProductsLocations> locations = quantityStrategy.
            getLocations(orderInput.getProductMap(), productRepository.
                findAllProductsLocationsInSet(orderInput.getProductMap().keySet()));

        locations.forEach(x -> productRepository.changeTheQuantity(x.getLocationId(), x.getProductId(), x.getQuantity()));

        return orderRepository.save(order);
    }
}


{'fraction_non_alphanumeric': 0.09155117270788912, 'fraction_numerical': 0.0007995735607675906, 'mean_word_length': 3.9084368868541532, 'pattern_counts': {'":': 0, '<': 19, '<?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': '6959567', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1770, 'n_words': 348}