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

Java tutorial

Introduction

Here is the source code for org.homiefund.api.service.impl.InviteServiceImpl.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.Home;
import org.homiefund.api.dao.domain.Invitation;
import org.homiefund.api.dao.domain.User;
import org.homiefund.api.dto.HomeDTO;
import org.homiefund.api.dto.InvitationDTO;
import org.homiefund.api.dto.UserDTO;
import org.homiefund.api.events.UserJoinsHome;
import org.homiefund.api.service.InviteService;
import org.homiefund.api.service.SecurityService;
import org.homiefund.api.support.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Optional;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 25.9.2016.
 */
@Service
@Log4j2
public class InviteServiceImpl implements InviteService, ApplicationEventPublisherAware {
    @Autowired
    private HomeDAO homeDAO;
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private InvitationDAO invitationDAO;
    @Autowired
    private MailService mailService;
    @Autowired
    private SecurityService securityService;
    @Autowired
    private Mapper mapper;
    private ApplicationEventPublisher applicationEventPublisher;

    private SecureRandom secureRandom = new SecureRandom();

    @Override
    @Transactional
    public void sendInvite(HomeDTO home, String email) throws IllegalArgumentException {
        Home dao = homeDAO.getById(home.getId());
        if (dao != null) {
            Invitation invitation = new Invitation();
            invitation.setValid(true);
            invitation.setHome(dao);
            invitation.setHash(new BigInteger(130, secureRandom).toString(64));
            invitation.setEmail(email);

            log.info(invitation);
            invitationDAO.create(invitation);

            mailService.sendInvite(mapper.map(invitation, InvitationDTO.class), email);
        } else {
            //todo or throw exception ?
            log.warn("Trying to invite into nonexistent home");
        }
    }

    @Override
    @Transactional
    public Long acceptInvite(String inviteToken) throws IllegalArgumentException {
        return inviteRegistered(securityService.getPrincipal(), inviteToken);
    }

    @Override
    @Transactional
    public Long inviteRegistered(UserDTO user, String inviteToken) throws IllegalArgumentException {
        Optional<Invitation> optionalInvite = invitationDAO.getByHash(inviteToken);

        if (!optionalInvite.isPresent()) {
            throw new IllegalArgumentException("Attempting to accept wrong invitation.");
        } else {
            Invitation invite = optionalInvite.get();
            if (!invite.getValid()) {
                throw new IllegalArgumentException("Attempting to accept invite which is already invalid.");
            } else {
                if (!user.getEmail().equals(invite.getEmail())) {
                    throw new AccessDeniedException("Your email is not matching email in invitation");
                } else {
                    User userDB = userDAO.getById(user.getId());
                    Home home = invite.getHome();
                    home.getMembers().add(userDB);

                    homeDAO.update(home);
                    invite.setValid(false);

                    invitationDAO.update(invite);
                    applicationEventPublisher.publishEvent(new UserJoinsHome(this,
                            mapper.map(userDB, UserDTO.class), mapper.map(home, HomeDTO.class)));

                    return home.getId();
                }
            }
        }
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }
}