Advertisement
NLinker

Morphia DAO

Jul 1st, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.52 KB | None | 0 0
  1. import com.google.code.morphia.annotations.Entity;
  2. import com.google.code.morphia.annotations.Id;
  3.  
  4. //////////// ENTITY //////////////////
  5.  
  6. @Entity("locales")
  7. public class Locale implements Identity<Long>{
  8.  
  9.     @Id
  10.     Long id;
  11.     String code;
  12.     String description;
  13.  
  14.     @Override
  15.     public String toString() {
  16.         return this.code;
  17.     }
  18.  
  19.     @Override
  20.     public boolean equals(Object other) {
  21.         if (other != null && other instanceof Locale) {
  22.             return id.equals(((Locale) other).getId());
  23.         } else {
  24.             return false;
  25.         }
  26.     }
  27.  
  28.     @Override
  29.     public int hashCode() {
  30.         int hash = 7;
  31.         hash = 23 * hash + (this.id != null ? this.id.hashCode() : 0);
  32.         return hash;
  33.     }
  34.  
  35.     @Override
  36.     public Long getId() {
  37.         return this.id;
  38.     }
  39.  
  40.     public String getCode() {
  41.         return this.code;
  42.     }
  43.  
  44.     public String getDescription() {
  45.         return this.description;
  46.     }
  47.  
  48.     public void setId(Long id) {
  49.         this.id = id;
  50.     }
  51.  
  52.     public void setCode(String code) {
  53.         this.code = code;
  54.     }
  55.  
  56.     public void setDescription(String description) {
  57.         this.description = description;
  58.     }
  59.    
  60.     /**
  61.      * Will convert a proper locale code (ab_xy), to the form (ab_XY).
  62.      *
  63.      * @param localeCode
  64.      * @return
  65.      */
  66.     public static String convertLocaleCodeToCorrectCase(String localeCode) {
  67.         if (localeCode == null) {
  68.             return null;
  69.         }
  70.        
  71.         int indexOfUnderscore = localeCode.indexOf('_');
  72.         if (indexOfUnderscore != -1) {
  73.             String language = localeCode.substring(0, indexOfUnderscore).toLowerCase();
  74.             String region = localeCode.substring(indexOfUnderscore + 1).toUpperCase();
  75.            
  76.             return new StringBuilder(language).append("_").append(region).toString();
  77.         }
  78.        
  79.         return localeCode;
  80.     }
  81. }
  82.  
  83. //////////// DAO //////////////////
  84.  
  85. public class MongoLocaleDao extends BasicDAO<Locale, Long> implements LocaleDao {
  86.  
  87.     public MongoLocaleDao(Mongo mongo, Morphia morphia, String dbName) {
  88.         super(Locale.class, mongo, morphia, dbName);
  89.     }
  90.  
  91.     @Override
  92.     public Locale getLocale(Long id) {
  93.         return get(id);
  94.     }
  95.  
  96.     @Override
  97.     public Locale getLocaleByCode(String code) {
  98.         return find(createQuery().field("code").equal(code)).get();
  99.     }
  100.  
  101.     @Override
  102.     public Collection<Locale> getLocales() {
  103.         return find().asList();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement