package io.abnd.rvep.user.service.impl;

import io.abnd.rvep.security.dao.intf.AuthProviderDAO;
import io.abnd.rvep.security.dao.intf.RvepUserAuthProviderDAO;
import io.abnd.rvep.security.model.AuthProvider;
import io.abnd.rvep.security.model.RvepUserAuthProvider;
import io.abnd.rvep.user.dao.intf.RvepUserDAO;
import io.abnd.rvep.user.dao.intf.RvepUserProfileDAO;
import io.abnd.rvep.user.model.RvepUser;
import io.abnd.rvep.user.model.RvepUserProfile;
import io.abnd.rvep.user.service.intf.RegisterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Calendar;

@Service
public class RvepRegisterUserService implements RegisterUserService {

    @Autowired
    private RvepUserProfileDAO rvepUserProfileDAO;
    @Autowired
    private AuthProviderDAO authProviderDAO;
    @Autowired
    private RvepUserDAO rvepUserDAO;
    @Autowired
    private RvepUserAuthProviderDAO rvepUserAuthProviderDAO;

    @Override
    public boolean isUserRegistered(String email) {
        RvepUserProfile rvepUserProfile = rvepUserProfileDAO.findByEmail(email);
        return rvepUserProfile != null;
    }

    @Override
    @Transactional
    public boolean registerUser(String email, String provider) {
        Calendar cal = Calendar.getInstance();

        // create user
        RvepUser user = new RvepUser();
        user.setCreatedOn(cal.getTime());
        user.setEnabled((byte)1);

        // create user profile
        RvepUserProfile userProfile = new RvepUserProfile();
        userProfile.setEmail(email);
        userProfile.setRvepUser(user);
        userProfile.setLastLogin(user.getCreatedOn());

        // create user auth provider
        AuthProvider authProvider = authProviderDAO.findByName(provider);
        RvepUserAuthProvider userAuthProvider = new RvepUserAuthProvider();
        userAuthProvider.setRvepUser(user);
        userAuthProvider.setAuthProvider(authProvider);

        try {
            rvepUserDAO.save(user);
            rvepUserProfileDAO.save(userProfile);
            rvepUserAuthProviderDAO.save(userAuthProvider);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

}

@@ -131,32 +131,138 @@
 ntf.Rvep
-UserAuthProvider
+RoleDAO;%0Aimport io.abnd.rvep.security.dao.intf.RvepUserAuthProviderDAO;%0Aimport io.abnd.rvep.security.dao.intf.RvepUserRole
 DAO;%0Aimp
@@ -299,32 +299,77 @@
 l.AuthProvider;%0A
+import io.abnd.rvep.security.model.RvepRole;%0A
 import io.abnd.r
@@ -401,32 +401,81 @@
 erAuthProvider;%0A
+import io.abnd.rvep.security.model.RvepUserRole;%0A
 import io.abnd.r
@@ -1243,16 +1243,128 @@
 iderDAO;
+%0A    @Autowired%0A    private RvepRoleDAO rvepRoleDAO;%0A    @Autowired%0A    private RvepUserRoleDAO rvepUserRoleDAO;
 %0A%0A    @O
@@ -2345,24 +2345,238 @@
 Provider);%0A%0A
+        // create user role%0A        RvepRole role = rvepRoleDAO.findByName(%22ROLE_USER%22);%0A        RvepUserRole userRole = new RvepUserRole();%0A        userRole.setRvepUser(user);%0A        userRole.setRvepRole(role);%0A%0A
         try 
@@ -2715,32 +2715,76 @@
 rAuthProvider);%0A
+            rvepUserRoleDAO.save(userRole);%0A
         %7D catch 

create rvep user role