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

Java tutorial

Introduction

Here is the source code for org.homiefund.api.service.impl.TransactionServiceImpl.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.TransactionDAO;
import org.homiefund.api.dao.TransactionPreviewDAO;
import org.homiefund.api.dao.TransactionTypeDAO;
import org.homiefund.api.dao.domain.Home;
import org.homiefund.api.dao.domain.Transaction;
import org.homiefund.api.dao.domain.TransactionPreview;
import org.homiefund.api.dao.domain.User;
import org.homiefund.api.dto.TransactionDTO;
import org.homiefund.api.dto.TransactionPreviewDTO;
import org.homiefund.api.dto.TransactionTypeDTO;
import org.homiefund.api.service.SecurityService;
import org.homiefund.api.service.TransactionService;
import org.homiefund.api.support.Page;
import org.homiefund.api.support.PaginatedRequest;
import org.homiefund.api.support.PaginatedResponse;
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.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 25.9.2016.
 */
@Log4j2
@Service
public class TransactionServiceImpl implements TransactionService {
    @Autowired
    private TransactionDAO transactionDAO;
    @Autowired
    private TransactionTypeDAO transactionTypeDAO;
    @Autowired
    private TransactionPreviewDAO transactionPreviewDAO;
    @Autowired
    private Mapper mapper;
    @Autowired
    private UserPreferencesBean userPreferencesBean;
    @Autowired
    private SecurityService securityService;

    @Override
    @Transactional
    public void create(TransactionDTO transactionDTO) throws IllegalArgumentException {
        transactionDTO.setOwner(securityService.getPrincipal());
        transactionDTO.setHome(userPreferencesBean.getPreferredHome());

        Transaction tx = mapper.map(transactionDTO, Transaction.class);
        transactionDTO.setId(transactionDAO.create(tx));
    }

    @Override
    public TransactionDTO getById(Long id) throws IllegalArgumentException {
        return null;
    }

    @Override
    @Transactional(readOnly = true)
    public PaginatedResponse<TransactionPreviewDTO> getTransactions(
            PaginatedRequest<TransactionPreviewDTO> paginatedRequest) throws IllegalArgumentException {
        if (userPreferencesBean.getPreferredHome() != null) {
            Home preferredHome = mapper.map(userPreferencesBean.getPreferredHome(), Home.class);
            User currentUser = mapper.map(securityService.getPrincipal(), User.class);
            Page current = paginatedRequest.getPage();
            Collection<TransactionPreview> result = transactionPreviewDAO.getTransactions(
                    (current.getPage() - 1) * current.getPageSize(), current.getPageSize(), preferredHome,
                    mapper.map(securityService.getPrincipal(), User.class));

            Long total = transactionPreviewDAO.totalTransactions(preferredHome, currentUser);

            return new PaginatedResponse<>(current.next(), total, (int) ((total / current.getPageSize()) + 1),
                    result.stream().map(tx -> mapper.map(tx, TransactionPreviewDTO.class))
                            .collect(Collectors.toList()));
        } else {
            return new PaginatedResponse<>(paginatedRequest.getPage(), 0L, 0, new ArrayList<>());
        }

    }

    @Override
    @Transactional
    public void invalidate(TransactionDTO transactionDTO) throws IllegalArgumentException {

    }

    @Override
    @Transactional(readOnly = true)
    public Collection<TransactionTypeDTO> getAll() {
        return transactionTypeDAO.getAll().stream().map(t -> mapper.map(t, TransactionTypeDTO.class))
                .collect(Collectors.toList());
    }
}