package net.sourceforge.fenixedu.domain; import java.text.Collator; import java.util.Comparator; import java.util.HashSet; import java.util.Locale; import java.util.ResourceBundle; import java.util.Set; import org.apache.commons.lang.StringUtils; import com.linkare.commons.metainfo.Linkare; import pt.utl.ist.fenix.tools.util.i18n.Language; import pt.utl.ist.fenix.tools.util.i18n.MultiLanguageString; public class Country extends Country_Base { static final public String PORTUGAL = "PORTUGAL"; static final public String NATIONALITY_PORTUGUESE = "PORTUGUESA"; static final public String DEFAULT_COUNTRY_NATIONALITY = NATIONALITY_PORTUGUESE; public static Comparator COMPARATOR_BY_NAME = new Comparator() { public int compare(Country leftCountry, Country rightCountry) { int comparationResult = Collator.getInstance().compare(leftCountry.getName(), rightCountry.getName()); return (comparationResult == 0) ? leftCountry.getIdInternal().compareTo(rightCountry.getIdInternal()) : comparationResult; } }; public Country() { super(); setRootDomainObject(RootDomainObject.getInstance()); setDefaultCountry(false); } @Linkare(author = "Paulo Zenida", comments = "Delegated on the new constructor") @Deprecated public Country(final String name, final String nationality, final String code) { this(name, new MultiLanguageString(Language.getDefaultLanguage(), nationality), code); } @Linkare(author = "Paulo Zenida", comments = "Added the set to the localized name") public Country(final String name, final MultiLanguageString nationality, final String code) { this(); setCode(code); setCountryNationality(nationality); setName(name); setLocalizedName(new MultiLanguageString(Language.getDefaultLanguage(), name)); } // ------------------------------------------------------------- // read static methods // ------------------------------------------------------------- /** * If the person country is undefined it is set to default. In a not * distance future this will not be needed since the coutry can never be * null. */ public static Country readDefault() { for (final Country country : RootDomainObject.getInstance().getCountrysSet()) { if (country.isDefaultCountry()) { return country; } } return null; } public static Country readCountryByNationality(final String nationality) { for (final Country country : RootDomainObject.getInstance().getCountrysSet()) { if (country.getNationality().equals(nationality)) { return country; } } return null; } // FIXME: This method is wrong and should not exist // exists only because PORTUGAL is repeated // country object should be split in 2 objects: Country and District // where Country has many Districts public static Set readDistinctCountries() { final Set result = new HashSet(); for (final Country country : RootDomainObject.getInstance().getCountrysSet()) { if (!country.getName().equalsIgnoreCase(PORTUGAL)) { result.add(country); } else { if (country.getNationality().equalsIgnoreCase(NATIONALITY_PORTUGUESE)) { result.add(country); } } } return result; } /** * This method is (yet another) hack in Country due to strange values for * the Portuguese nationality. * */ @Deprecated public String getFilteredNationality(final Locale locale) { final String nationality = getCountryNationality().getContent(Language.valueOf(locale.getLanguage())); if (this != readDefault()) { return nationality; } final String specialCase = ResourceBundle.getBundle("resources/ApplicationResources", Language.getLocale()).getString( "label.person.portugueseNationality").toUpperCase(); return nationality.trim().contains(specialCase) ? specialCase : nationality; } public boolean isDefaultCountry() { return getDefaultCountry(); } static public Country readByTwoLetterCode(String code) { if (StringUtils.isEmpty(code)) { return null; } // TODO: Hack to remove, when we no longer have 4(!!) Portugal countries // with same code (pt) final Country defaultCountry = readDefault(); if (defaultCountry.getCode().equalsIgnoreCase(code)) { return defaultCountry; } for (final Country country : RootDomainObject.getInstance().getCountrysSet()) { if (country.getCode().equalsIgnoreCase(code)) { return country; } } return null; } static public Country readByThreeLetterCode(String code) { if (StringUtils.isEmpty(code)) { return null; } // TODO: Hack to remove, when we no longer have 4(!!) Portugal countries // with same code (pt) Country defaultCountry = readDefault(); if (defaultCountry.getThreeLetterCode().equalsIgnoreCase(code)) { return defaultCountry; } for (final Country country : RootDomainObject.getInstance().getCountrysSet()) { if (country.getThreeLetterCode() != null && country.getThreeLetterCode().equalsIgnoreCase(code)) { return country; } } return null; } @Deprecated public String getNationality() { return getCountryNationality().getPreferedContent(); } @Deprecated public void setNationality(final String nationality) { getCountryNationality().setContent(Language.getDefaultLanguage(), nationality); } }