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

Java tutorial

Introduction

Here is the source code for org.homiefund.api.service.impl.UserServiceImpl.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.HomeDAO;
import org.homiefund.api.dao.InvitationDAO;
import org.homiefund.api.dao.UserDAO;
import org.homiefund.api.dao.domain.User;
import org.homiefund.api.dto.HomeDTO;
import org.homiefund.api.dto.UserDTO;
import org.homiefund.api.service.UserService;
import org.homiefund.exceptions.FieldException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 21.9.2016.
 */
@Service("userService")
@Log4j2
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private HomeDAO homeDAO;
    @Autowired
    private InvitationDAO invitationDAO;

    @Autowired
    private Mapper mapper;

    @Override
    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Optional<User> user = userDAO.getByUsername(s);
        if (!user.isPresent()) {
            throw new UsernameNotFoundException(s);
        } else {
            UserDTO result = mapper.map(user.get(), UserDTO.class);
            result.setHomes(homeDAO.getHomes(user.get()).stream().map(h -> {
                HomeDTO hdto = new HomeDTO();
                hdto.setId(h.getId());
                return hdto;
            }).collect(Collectors.toList()));

            return result;
        }
    }

    @Override
    public UserDTO getProfile() {
        return null;
    }

    @Override
    public void updateProfile(UserDTO userDTO) throws IllegalArgumentException, FieldException {

    }
}