package org.cinnamon.core.service;

import org.cinnamon.core.domain.UserAuthority;
import org.cinnamon.core.domain.UserBase;
import org.cinnamon.core.domain.UserGroup;
import org.cinnamon.core.repository.UserAuthorityRepository;
import org.cinnamon.core.repository.UserBaseRepository;
import org.cinnamon.core.repository.UserGroupRepository;
import org.cinnamon.core.vo.search.UserGroupSearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * create date: 2015. 3. 30.
 * @author 동성
 *
 */
@Service
public class UserGroupService<T extends UserBase> {
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	UserGroupRepository userGroupRepository;
	
	@Autowired
	UserAuthorityRepository userAuthorityRepository;
	
	@Autowired
	UserBaseRepository<T> userRepository;
	
	@Transactional
	public UserGroup save(UserGroup userGroup) {
		logger.info("start");
		
		return userGroupRepository.save(userGroup);
	}
	
	
	@Transactional(readOnly=true)
	public UserGroup get(Long userGroupId) {
		logger.info("start");
		
		return userGroupRepository.findOne(userGroupId);
	}
	
	
	@Transactional(readOnly=true)
	public Page<UserGroup> getList(UserGroupSearch userGroupSearch, Pageable pageable) {
		logger.info("start");
		
		return userGroupRepository.find(userGroupSearch, pageable);
	}
	
	
	@Transactional
	public void addMember(Long userGroupId, String userId) {
		logger.info("start");
		
		UserBase user = userRepository.findOne(userId);
		if (user == null) {
			throw new RuntimeException("등록되지 않은 사용자 입니다. userId: " + userId);
		}
		
		UserGroup userGroup = userGroupRepository.findOne(userGroupId);
		if (userGroup == null) {
			throw new RuntimeException("등록되지 않은 사용자 그룹입니다. userGroupId: " + userGroupId);
		}
		
		user.getUserGroups().add(userGroup);
	}
	
	
//	@Transactional
//	public void addMember(Object authority, String userId) {
//		logger.info("start");
//		
//		T user = userRepository.findOne(userId);
//		if (user == null) {
//			throw new RuntimeException("등록되지 않은 사용자 입니다. userId: " + userId);
//		}
//		
//		UserAuthority userAuthority = userAuthorityRepository.findByAuthority(authority.toString());
//		if (userAuthority == null) {
//			throw new RuntimeException("등록되지 않은 authority 입니다. authority: " + authority);
//		}
//		
//		UserGroup userGroup = userAuthority.getDefaultUserGroup();
//		if (userGroup == null) {
//			throw new RuntimeException("기본 사용자 그룹이 등록되지 않았습니다. authority: " + authority);
//		}
//		
//		user.getUserGroups().add(userGroup);
//	}
	
	
	@Transactional
	public void removeMember(Object authority, String userId) {
		logger.info("start");
		
		T user = userRepository.findOne(userId);
		if (user == null) {
			throw new RuntimeException("등록되지 않은 사용자 입니다. userId: " + userId);
		}
		
		UserAuthority userAuthority = userAuthorityRepository.findByAuthority(authority.toString());
		if (userAuthority == null) {
			throw new RuntimeException("등록되지 않은 authority 입니다. authority: " + authority);
		}
		
		UserGroup userGroup = userAuthority.getDefaultUserGroup();
		if (userGroup == null) {
			throw new RuntimeException("기본 사용자 그룹이 등록되지 않았습니다. authority: " + authority);
		}
		
		user.getUserGroups().remove(userGroup);
	}
}

@@ -2051,18 +2051,16 @@
 %0A%09%7D%0A%09%0A%09%0A
-//
 %09@Transa
@@ -2067,18 +2067,16 @@
 ctional%0A
-//
 %09public 
@@ -2125,18 +2125,16 @@
 erId) %7B%0A
-//
 %09%09logger
@@ -2149,23 +2149,19 @@
 tart%22);%0A
-//
 %09%09%0A
-//
 %09%09T user
@@ -2195,18 +2195,16 @@
 serId);%0A
-//
 %09%09if (us
@@ -2209,34 +2209,32 @@
 user == null) %7B%0A
-//
 %09%09%09throw new Run
@@ -2286,29 +2286,23 @@
 serId);%0A
-//
 %09%09%7D%0A
-//
 %09%09%0A
-//
 %09%09UserAu
@@ -2388,18 +2388,16 @@
 ing());%0A
-//
 %09%09if (us
@@ -2411,34 +2411,32 @@
 rity == null) %7B%0A
-//
 %09%09%09throw new Run
@@ -2500,29 +2500,23 @@
 ority);%0A
-//
 %09%09%7D%0A
-//
 %09%09%0A
-//
 %09%09UserGr
@@ -2568,18 +2568,16 @@
 roup();%0A
-//
 %09%09if (us
@@ -2595,18 +2595,16 @@
 null) %7B%0A
-//
 %09%09%09throw
@@ -2680,21 +2680,15 @@
 y);%0A
-//
 %09%09%7D%0A
-//
 %09%09%0A
-//
 %09%09us
@@ -2726,10 +2726,8 @@
 p);%0A
-//
 %09%7D%0A%09

주석처리한 메서드 사용하는 곳이 있어서 주석 품