protected NewsletterReport prepareNewsletterReport(List<Content> contents) {
   NewsletterConfig config = this.getConfig();
   NewsletterReport newsletterReport = new NewsletterReport();
   newsletterReport.setSubject(config.getSubject());
   newsletterReport.setSendDate(new Date());
   String defaultLang = this.getLangManager().getDefaultLang().getCode();
   boolean alsoHtml = config.isAlsoHtml();
   for (Content content : contents) {
     boolean isConfiguredWithModels = false;
     ContentReport contentReport = new ContentReport();
     contentReport.setContentId(content.getId());
     String textBodyPart = this.prepareMailBodyContentPart(content, defaultLang, false);
     if (null != textBodyPart) {
       isConfiguredWithModels = true;
       contentReport.setTextBody(textBodyPart);
     }
     if (alsoHtml) {
       String htmlBodyPart = this.prepareMailBodyContentPart(content, defaultLang, true);
       contentReport.setHtmlBody(htmlBodyPart);
     }
     if (isConfiguredWithModels) {
       newsletterReport.addContentReport(contentReport);
     } else {
       ApsSystemUtils.getLogger()
           .info(
               " Newsletter content "
                   + content.getId()
                   + " not added, because has not model in config.");
     }
   }
   return newsletterReport;
 }
 /**
  * Salva il report della newsletter appena inviata.
  *
  * @param newsletterReport Il report della newsletter.
  * @throws ApsSystemException In caso di errore.
  */
 protected void addNewsletterReport(NewsletterReport newsletterReport) throws ApsSystemException {
   if (null == newsletterReport) return;
   try {
     IKeyGeneratorManager keyGeneratorManager = this.getKeyGeneratorManager();
     newsletterReport.setId(keyGeneratorManager.getUniqueKeyCurrentValue());
     for (ContentReport contentReport : newsletterReport.getContentReports().values()) {
       contentReport.setId(keyGeneratorManager.getUniqueKeyCurrentValue());
     }
     this.getNewsletterDAO().addNewsletterReport(newsletterReport);
   } catch (Throwable t) {
     ApsSystemUtils.logThrowable(
         t,
         this,
         "buildMailBody",
         "Error adding newsletter report : id " + newsletterReport.getId());
   }
 }
 private List<Content> extractContentsForUser(
     UserDetails user,
     String eMail,
     List<Content> contents,
     Map<String, List<String>> profileAttributes,
     NewsletterReport newsletterReport)
     throws ApsSystemException {
   NewsletterConfig config = this.getConfig();
   List<Content> userContents = new ArrayList<Content>();
   String username = user.getUsername();
   IUserProfile profile = (IUserProfile) user.getProfile();
   if (profile != null) {
     String allContentsAttribute = config.getAllContentsAttributeName();
     boolean allContents = false;
     if (null != allContentsAttribute) {
       Boolean value = (Boolean) profile.getValue(allContentsAttribute);
       allContents = value != null && value.booleanValue();
     }
     List<String> groupNames = this.extractUserGroupNames(user);
     boolean isGroupAdmin = groupNames.contains(Group.ADMINS_GROUP_NAME);
     for (int i = 0; i < contents.size(); i++) {
       Content content = contents.get(i);
       String contentId = content.getId();
       List<String> contentProfileAttributes = profileAttributes.get(contentId);
       ContentReport contentReport = newsletterReport.getContentReport(contentId);
       if (contentReport != null
           && (isGroupAdmin || this.checkUserAllowedOnContent(groupNames, content))) {
         if (allContents) {
           userContents.add(content);
           contentReport.addRecipient(username, eMail);
         } else if (contentProfileAttributes != null && contentProfileAttributes.size() > 0) {
           for (String profileAttrName : contentProfileAttributes) {
             Boolean value = (Boolean) profile.getValue(profileAttrName);
             if (value != null && value.booleanValue()) {
               userContents.add(content);
               contentReport.addRecipient(username, eMail);
               break;
             }
           }
         }
       }
     }
   }
   return userContents;
 }
 protected StringBuffer prepareMailCommonBody(
     List<Content> userContents, NewsletterReport newsletterReport, boolean isHtml) {
   NewsletterConfig config = this.getConfig();
   StringBuffer body = new StringBuffer(isHtml ? config.getHtmlHeader() : config.getTextHeader());
   String separator = isHtml ? config.getHtmlSeparator() : config.getTextSeparator();
   boolean first = true;
   for (Content content : userContents) {
     ContentReport contentReport = newsletterReport.getContentReport(content.getId());
     if (contentReport != null) {
       if (first) {
         first = false;
       } else {
         body.append(separator);
       }
       String text = isHtml ? contentReport.getHtmlBody() : contentReport.getTextBody();
       body.append(text);
     }
   }
   return body;
 }