org.homiefund.api.service.impl.DebtPreviewServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.homiefund.api.service.impl.DebtPreviewServiceImpl.java

Source

/**
 * Copyright  2016 REPLACE ME OWNER (REPLACE ME YEAR)
 *
 * 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 org.homiefund.api.service.impl;

import lombok.extern.log4j.Log4j2;
import org.dozer.Mapper;
import org.homiefund.api.dao.DebtPreviewDAO;
import org.homiefund.api.dao.UserDAO;
import org.homiefund.api.dao.domain.DebtPreview;
import org.homiefund.api.dao.domain.Home;
import org.homiefund.api.dto.DebtPreviewDTO;
import org.homiefund.api.dto.UserDTO;
import org.homiefund.api.service.DebtPreviewService;
import org.homiefund.api.service.SecurityService;
import org.homiefund.api.service.impl.debtgraph.Graph;
import org.homiefund.api.support.UserPreferencesBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.Map.Entry.comparingByValue;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 25.9.2016.
 */
@Log4j2
@Service
public class DebtPreviewServiceImpl implements DebtPreviewService {
    @Autowired
    private UserPreferencesBean userPreferencesBean;
    @Autowired
    private SecurityService securityService;
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private DebtPreviewDAO debtPreviewDAO;
    @Autowired
    private Mapper mapper;

    private static final String I_OWE = "I am owning by {0} tons of moneyz : {1,number,#.##} kc";
    private static final String THEY_OWE = "I am owed by {0} tons of moneyz : {1,number,#.##} kc";

    @Override
    @Transactional(readOnly = true)
    public DebtPreviewDTO getOverview() {
        Graph debtGraph = new Graph();

        Collection<DebtPreview> list = debtPreviewDAO
                .getTransactionsForPreview(mapper.map(userPreferencesBean.getPreferredHome(), Home.class));
        int i = 0;
        for (DebtPreview p : list) {
            i++;
            debtGraph.add(p);
        }

        Long principalID = securityService.getPrincipal().getId();
        Map<Long, BigDecimal> debtMap = debtGraph.debts(principalID);

        Map<UserDTO, BigDecimal> debtsMap = debtMap.entrySet().stream().filter(d -> !d.getKey().equals(principalID))
                .sorted(comparingByValue((o1, o2) -> o2.compareTo(o1)))
                .collect(Collectors.toMap(e -> mapper.map(userDAO.getById(e.getKey()), UserDTO.class),
                        e -> e.getValue(), (e1, e2) -> e1, LinkedHashMap::new));

        DebtPreviewDTO debts = new DebtPreviewDTO();
        debts.setOverview(debtsMap);
        debts.setTotal(debtsMap.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add));

        return debts;
    }
}