Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.code.morphia.annotations.Entity;
- import com.google.code.morphia.annotations.Id;
- //////////// ENTITY //////////////////
- @Entity("locales")
- public class Locale implements Identity<Long>{
- @Id
- Long id;
- String code;
- String description;
- @Override
- public String toString() {
- return this.code;
- }
- @Override
- public boolean equals(Object other) {
- if (other != null && other instanceof Locale) {
- return id.equals(((Locale) other).getId());
- } else {
- return false;
- }
- }
- @Override
- public int hashCode() {
- int hash = 7;
- hash = 23 * hash + (this.id != null ? this.id.hashCode() : 0);
- return hash;
- }
- @Override
- public Long getId() {
- return this.id;
- }
- public String getCode() {
- return this.code;
- }
- public String getDescription() {
- return this.description;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public void setCode(String code) {
- this.code = code;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- /**
- * Will convert a proper locale code (ab_xy), to the form (ab_XY).
- *
- * @param localeCode
- * @return
- */
- public static String convertLocaleCodeToCorrectCase(String localeCode) {
- if (localeCode == null) {
- return null;
- }
- int indexOfUnderscore = localeCode.indexOf('_');
- if (indexOfUnderscore != -1) {
- String language = localeCode.substring(0, indexOfUnderscore).toLowerCase();
- String region = localeCode.substring(indexOfUnderscore + 1).toUpperCase();
- return new StringBuilder(language).append("_").append(region).toString();
- }
- return localeCode;
- }
- }
- //////////// DAO //////////////////
- public class MongoLocaleDao extends BasicDAO<Locale, Long> implements LocaleDao {
- public MongoLocaleDao(Mongo mongo, Morphia morphia, String dbName) {
- super(Locale.class, mongo, morphia, dbName);
- }
- @Override
- public Locale getLocale(Long id) {
- return get(id);
- }
- @Override
- public Locale getLocaleByCode(String code) {
- return find(createQuery().field("code").equal(code)).get();
- }
- @Override
- public Collection<Locale> getLocales() {
- return find().asList();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement