/**
  * Saves a mail template
  *
  * @param copy
  * @param objectID
  * @param templateID
  * @param plain
  * @param theLocale
  * @param mailSubject
  * @param mailBody
  * @param locale
  * @return
  */
 static String saveTemplateDef(
     boolean copy,
     Integer templateID,
     Integer objectID,
     boolean plain,
     String theLocale,
     String mailSubject,
     String mailBody,
     Locale locale) {
   TMailTemplateDefBean mailTemplateDefBean = null;
   if (objectID != null && !copy) {
     LOGGER.info("Load by primary key=" + objectID.toString());
     mailTemplateDefBean = mailTemplateDefDAO.loadByPrimaryKey(objectID);
   }
   List<TMailTemplateDefBean> mailTemplates =
       mailTemplateDefDAO.loadByTemplateTypeAndLocale(templateID, plain, theLocale);
   if (mailTemplates != null && !mailTemplates.isEmpty()) {
     TMailTemplateDefBean existingMailTemplateBean = mailTemplates.get(0);
     LOGGER.info("Existing TemplateBean ID=" + existingMailTemplateBean.getObjectID().toString());
     if (objectID == null || copy || !existingMailTemplateBean.getObjectID().equals(objectID)) {
       return JSONUtility.encodeJSONFailure(
           LocalizeUtil.getLocalizedTextFromApplicationResources(
               "admin.customize.mailTemplate.err.existingTemplate", locale));
     }
   }
   if (mailTemplateDefBean == null) {
     mailTemplateDefBean = new TMailTemplateDefBean();
   }
   LOGGER.debug("setting data");
   mailTemplateDefBean.setMailTemplate(templateID);
   mailTemplateDefBean.setTheLocale(theLocale);
   mailTemplateDefBean.setIsPlainEmailBool(plain);
   mailTemplateDefBean.setMailSubject(mailSubject);
   mailTemplateDefBean.setMailBody(mailBody);
   mailTemplateDefBean.setTemplateChanged(BooleanFields.TRUE_VALUE); // mark it private
   mailTemplateDefDAO.save(mailTemplateDefBean);
   LOGGER.debug("saving data");
   return JSONUtility.encodeJSONSuccess();
 }