package com.jreaper.service.transactional;

import com.jreaper.common.security.SecurityService;
import com.jreaper.common.service.transactional.AbstractTransactionalEntityService;
import com.jreaper.model.dao.UserDao;
import com.jreaper.model.entity.user.JRUser;
import com.jreaper.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by roman on 06.01.15.
 */
@Service("transactionalUserService")
public class TransactionalUserService extends AbstractTransactionalEntityService<JRUser, UserDao>
        implements UserService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private SecurityService securityService;

    @Autowired
    public TransactionalUserService(UserDao dao, SecurityService securityService) {
        super(dao);
        this.securityService = securityService;
    }

    @Override
    public JRUser getCurrentUser() {
        String name = securityService.getCurrentUserUsername();

        if (name == null) {
            return null;
           // return new AnonymousUser();
        } else {
            return this.getDao().getByUsername(name);
        }
    }

}
