by.bsuir.finance.service.user.UserInfoServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for by.bsuir.finance.service.user.UserInfoServiceImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package by.bsuir.finance.service.user;

import by.bsuir.finance.domain.UserInfo;
import by.bsuir.finance.domain.UserRoles;
import by.bsuir.finance.domain.Users;
import by.bsuir.finance.exceptions.UserInfoNotFoundException;
import by.bsuir.finance.repositories.userinfo.DepartmentRepository;
import by.bsuir.finance.repositories.userinfo.PositionRepository;
import by.bsuir.finance.repositories.userinfo.UserInfoRepository;
import by.bsuir.finance.repositories.userinfo.UserRepository;
import by.bsuir.finance.repositories.userinfo.UserRoleRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author igor
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    private static final Logger log = Logger.getLogger(UserInfoServiceImpl.class.getName());

    @Resource
    private UserInfoRepository infoRepository;

    @Resource
    private UserRoleRepository roleRepository;

    @Resource
    private UserRepository userRepository;

    @Resource
    private DepartmentRepository departmentRepository;

    @Resource
    private PositionRepository positionRepository;

    @Override
    public List<UserInfo> findAll() {
        return infoRepository.findAll();
    }

    @Override
    @Transactional
    public void delete(Long id) {
        UserInfo info = infoRepository.findOne(id);
        if (info != null) {
            Users user = info.getUsers();
            UserRoles roles = roleRepository.findByUsers(user);
            infoRepository.delete(info);
            roleRepository.delete(roles);
            userRepository.delete(user);
        }
    }

    @Override
    public void delete(int idUserInfo) {
        UserInfo info = infoRepository.findByIdUserInfo(idUserInfo);
        log.log(Level.INFO, "info : {0}", info);
        Users user = info.getUsers();
        //UserRoles role = roleRepository.findByUsername(user.getUsername());
        userRepository.delete(user);
    }

    @Override
    public void save(UserInfo info) {
        info.setDepartment(departmentRepository.findByIdDepartment(info.getIdDepartment()));
        info.setPositions(positionRepository.findByIdpositions(info.getIdPosition()));
        info.setLogo("");
        Users user = new Users(info.getUsername(), info.getPassword(), true);
        userRepository.save(user);
        info.setUsers(user);
        infoRepository.save(info);
        switch (info.getRole()) {
        case 1:
            roleRepository.save(new UserRoles(user, "ROLE_USER"));
            break;
        case 2:
            roleRepository.save(new UserRoles(user, "ROLE_ADMIN"));
            break;
        case 3:
            roleRepository.save(new UserRoles(user, "ROLE_ACCOUNTANT"));
            break;
        }
    }

    @Override
    public UserInfo findById(int idUserInfo) throws UserInfoNotFoundException {
        UserInfo info = infoRepository.findByIdUserInfo(idUserInfo);
        if (info != null)
            return info;
        else
            throw new UserInfoNotFoundException("user not found");
    }

    public UserInfo getCurrentUserInfo(Strign username) {
        UserInfo userInfo = null;
        List<UserInfo> infos = infoRepository.findAll();
        for (UserInfo info : infos) {
            if (info.getUsers().getUsername().equals(username))
                userInfo = info;
        }
        return userInfo;
    }
}