public EmailTemplate getEmailTemplate(String key, Locale locale) {
    if (key == null || "".equals(key)) {
      throw new IllegalArgumentException("key cannot be null or empty");
    }

    if (log.isDebugEnabled()) {
      log.debug("getEmailTemplate(key=" + key + ", locale=" + locale + ")");
    }
    EmailTemplate et = null;
    // TODO make this more efficient
    if (locale != null) {
      Search search = new Search("key", key);
      search.addRestriction(new Restriction("locale", locale.toString()));
      et = dao.findOneBySearch(EmailTemplate.class, search);
      if (et == null) {
        search.addRestriction(new Restriction("locale", locale.getLanguage()));
        et = dao.findOneBySearch(EmailTemplate.class, search);
      }
    }
    if (et == null) {
      Search search = new Search("key", key);
      search.addRestriction(new Restriction("locale", EmailTemplate.DEFAULT_LOCALE));
      et = dao.findOneBySearch(EmailTemplate.class, search);
    }
    if (et == null) {
      log.warn("no template found for: " + key + " in locale " + locale);
    }
    return et;
  }
 /**
  * Delete all templates in the Database Only used in unit tests so not in API TODO rewrite for
  * efficiency
  */
 public void deleteAllTemplates() {
   log.debug("deleteAllTemplates");
   List<EmailTemplate> templates = dao.findAll(EmailTemplate.class);
   for (int i = 0; i < templates.size(); i++) {
     EmailTemplate template = templates.get(i);
     log.debug("deleting template: " + template.getId());
     dao.delete(template);
   }
 }
 public EmailTemplate getEmailTemplateById(Long id) {
   if (id == null) {
     throw new IllegalArgumentException("id cannot be null or empty");
   }
   EmailTemplate et = dao.findById(EmailTemplate.class, id);
   return et;
 }
 private EmailTemplate getEmailTemplateNoDefault(String key, Locale locale) {
   log.debug("getEmailTemplateNoDefault( " + key + "," + locale);
   if (key == null || "".equals(key)) {
     throw new IllegalArgumentException("key cannot be null or empty");
   }
   EmailTemplate et = null;
   if (locale != null) {
     Search search = new Search("key", key);
     search.addRestriction(new Restriction("locale", locale.toString()));
     et = dao.findOneBySearch(EmailTemplate.class, search);
   } else {
     Search search = new Search("key", key);
     search.addRestriction(new Restriction("locale", EmailTemplate.DEFAULT_LOCALE));
     et = dao.findOneBySearch(EmailTemplate.class, search);
   }
   return et;
 }
 public void updateTemplate(EmailTemplate template) {
   template.setLastModified(new Date());
   String locale = template.getLocale();
   if (locale == null || "".equals(locale)) {
     template.setLocale(EmailTemplate.DEFAULT_LOCALE);
   }
   dao.update(template);
   log.info("updated template: " + template.getId());
 }
  public boolean templateExists(String key, Locale locale) {
    List<EmailTemplate> et = null;
    Search search = new Search("key", key);
    if (locale == null) {
      search.addRestriction(new Restriction("locale", EmailTemplate.DEFAULT_LOCALE));
    } else {
      search.addRestriction(new Restriction("locale", locale.toString()));
    }
    et = dao.findBySearch(EmailTemplate.class, search);
    if (et != null && et.size() > 0) {
      return true;
    }

    return false;
  }
  public void saveTemplate(EmailTemplate template) {
    // check that fields are set
    if (template == null) {
      throw new IllegalArgumentException("Template can't be null");
    }

    if (template.getKey() == null) {
      throw new IllegalArgumentException("Template key can't be null");
    }

    if (template.getOwner() == null) {
      throw new IllegalArgumentException("Template owner can't be null");
    }

    if (template.getSubject() == null) {
      throw new IllegalArgumentException("Template subject can't be null");
    }

    if (template.getMessage() == null) {
      throw new IllegalArgumentException("Template message can't be null");
    }

    String locale = template.getLocale();
    if (locale == null || locale.trim().length() == 0) {
      // For backward compatibility set it to default
      template.setLocale(EmailTemplate.DEFAULT_LOCALE);
    }

    // update the modified date
    template.setLastModified(new Date());
    try {
      dao.save(template);
    } catch (DataIntegrityViolationException die) {
      throw new IllegalArgumentException(
          "Key: " + template.getKey() + " and locale: " + template.getLocale() + " in use already",
          die);
    }
    log.info("saved template: " + template.getId());
  }
 public List<EmailTemplate> getEmailTemplates(int max, int start) {
   return dao.findAll(EmailTemplate.class, start, max);
 }