SharedServiceSpring.java 2.19 KB
package org.legrog.web.xyz;

import org.legrog.entities.*;
import org.legrog.web.user.UserService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.List;
import java.util.Random;
import java.util.Vector;

@Stateless
public class SharedServiceSpring implements SharedService {

    CountryRepository countryRepository;
    AccountRoleRepository accountRoleRepository;
    AccountPropertyRepository accountPropertyRepository;
    UserService userService;

    @Inject
    public SharedServiceSpring(CountryRepository countryRepository, AccountRoleRepository accountRoleRepository,
                               AccountPropertyRepository accountPropertyRepository, UserService userService) {
        this.countryRepository = countryRepository;
        this.accountRoleRepository = accountRoleRepository;
        this.accountPropertyRepository = accountPropertyRepository;
        this.userService = userService;
    }

    private List<DisplayNameMask> allDisplayNameMasks;

    public Country addCountry(Country country) {
        countryRepository.save(country);
        return country;
    }

    public List<Country> getAllCountries() {
        return countryRepository.findAll();
    }

    public List<DisplayNameMask> getAllDisplayNameMasks() {
        if (allDisplayNameMasks == null)
        {
            allDisplayNameMasks = new Vector<DisplayNameMask>(DisplayNameMask.values().length);

            for (DisplayNameMask mask : DisplayNameMask.values())
            {
                allDisplayNameMasks.add(mask);
            }
        }
        return allDisplayNameMasks;
    }

    public List<AccountRole> getAvailableUserRoles() {
        return accountRoleRepository.findAll();
    }

    public List<AccountProperty> getAvailableUserProperties() { return accountPropertyRepository.findAll(); }

    public Account getCurrentUser() {
        // TODO Remplacer l'astuce par une vraie récupération de l'utilisateur
        List<Account> accounts = userService.getAllUsers();
        Random random = new Random();
        Account account = accounts.get(random.nextInt(accounts.size()));
        // End TODO
        return account;
    }
}