SharedServiceDefault.java 2.57 KB
package org.legrog.web.xyz;

import org.legrog.entities.*;
import org.legrog.web.account.AccountService;

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

/**
 * SharedService implementation
 */
@Stateless
public class SharedServiceDefault implements SharedService {

    CountryRepository countryRepository;
    AccountRoleRepository accountRoleRepository;
    AccountPropertyRepository accountPropertyRepository;
    AccountService accountService;

    private List<DisplayNameMask> allDisplayNameMasks;

    /**
     * Uses shared repositories for country and related to account and also AccountService
     *
     * @param countryRepository injected CountryRepository
     * @param accountRoleRepository injected accountRoleRepository
     * @param accountPropertyRepository injected AccountPropertyRepository
     * @param accountService injected AccountService
     */
    @Inject
    public SharedServiceDefault(CountryRepository countryRepository, AccountRoleRepository accountRoleRepository,
                                AccountPropertyRepository accountPropertyRepository, AccountService accountService) {
        this.countryRepository = countryRepository;
        this.accountRoleRepository = accountRoleRepository;
        this.accountPropertyRepository = accountPropertyRepository;
        this.accountService = accountService;
    }

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

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

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

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

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

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

    @Override
    public Account getCurrentUser() {
        // TODO Implement instead of randomly choosing one
        List<Account> accounts = accountService.getAllUsers();
        Random random = new Random();

        return accounts.get(random.nextInt(accounts.size()));
        // End TODO
    }
}