BrightSide Workbench Full Report + Source Code
PublicationMail.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.publication.util;
19 
20 import java.io.PrintWriter;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.string.Strings;
27 import org.turro.action.Contacts;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.HeadlessApplication;
30 import org.turro.elephant.context.IConstructor;
31 import org.turro.elephant.entities.db.QueueOptions;
32 import org.turro.elephant.impl.util.Parser;
33 import org.turro.i18n.I_;
34 import org.turro.mail.impl.MailPool;
35 import org.turro.mail.queue.QueueManager;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.plugin.contacts.MailContact;
38 import org.turro.publication.entity.Publication;
39 import org.turro.publication.entity.PublicationCategory;
40 import org.turro.publication.zul.portal.PublicationContent;
41 
46 public class PublicationMail {
47 
48  private String mailStr, templatePublication;
49 
50  public void prepareMail(IConstructor constructor, PublicationCategory pubcat,
51  List<Publication> pubs, String path) {
52  constructor.setOutputToString();
53  try {
54  MailPool mp = (MailPool) HeadlessApplication.getInstance().getImplementation("IMailPool_Publication");
55  templatePublication = mp.getTemplateString("template-publication");
56  String start = extractStart(), end = extractEnd();
57  PrintWriter out = constructor.getOut();
58  if(Strings.isBlank(start)) {
59  out.print("<h1>" + MailContact.createLink(pubcat.getName() + " - " + ElephantContext.getSiteName(), path, false) + "</h1>");
60  } else {
61  out.print(start.replaceAll("#pathall", Parser.escapeGrouping(MailContact.createLink(pubcat.getName() + " - " + ElephantContext.getSiteName(), path, false))));
62  }
63  if(Strings.isBlank(templatePublication)) {
64  out.print(PublicationContent.getPublicationsStr(pubs, null, true, false, true, false, path, true, null, constructor));
65  } else {
66  out.print(PublicationContent.getPublicationsTemplateStr(templatePublication, pubs, null, true, true, true, true, path, false, null, constructor));
67  }
68  if(Strings.isBlank(end)) {
69  out.print("<p>" + MailContact.createLink(I_.get("My subscriptions"), "/user", true) + "</p>");
70  } else {
71  out.print(end.replaceAll("#mysubscriptions", Parser.escapeGrouping(MailContact.createLink(I_.get("My subscriptions"), "/user", true))));
72  }
73  } catch(Exception ex) {
74  Logger.getLogger(PublicationMail.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
75  }
76 
77  mailStr = constructor.getOutputString();
78  }
79 
80  public void sendMail(IConstructor constructor, PublicationCategory pubcat) {
81 // Iterator<PublicationSubscription> itps = subs.iterator();
82 // ArrayList<PublicationSubscription> pool = new ArrayList<PublicationSubscription>();
83 // int count = 0;
84 // while(itps.hasNext()) {
85 // pool.add(itps.next());
86 // itps.remove();
87 // count++;
88 // if(count > 50) {
89 // doSend(pool, pubcat);
90 // pool = new ArrayList<>();
91 // count = 0;
92 // }
93 // }
94  doSend(pubcat);
95  }
96 
97  private List<IContact> getPlayers(PublicationCategory pubcat) {
98  QueueManager qm = new QueueManager();
99  Collection<QueueOptions> subs = qm.getSubcribers(PublicationElephantNotification.convertId(pubcat));
100  ArrayList<IContact> list = new ArrayList<>();
101  for(QueueOptions qo : subs) {
102  if(qo.getIdContact() != null) {
103  IContact c = Contacts.getContact(qo.getIdContact());
104  if(c.isWebUser()) {
105  list.add(c);
106  }
107  }
108  }
109  return list;
110 // List<PublicationSubscription> subs = PublicationSearch.getSubscriptions(pubcat.getId());
111 // ArrayList<IContact> list = new ArrayList<>();
112 // for(PublicationSubscription ps : subs) {
113 // if(ps.getIdContact() != null) {
114 // list.add(Contacts.getContact(ps.getIdContact()));
115 // }
116 // }
117 // return list;
118  }
119 
120  private String extractStart() {
121  String start = null;
122  if(templatePublication != null) {
123  int p = templatePublication.indexOf("#start#");
124  if(p > -1) {
125  start = templatePublication.substring(0, p);
126  templatePublication = templatePublication.substring(p + 7);
127  }
128  }
129  return start;
130  }
131 
132  private String extractEnd() {
133  String end = null;
134  if(templatePublication != null) {
135  int p = templatePublication.indexOf("#end#");
136  if(p > -1) {
137  end = templatePublication.substring(p + 5);
138  templatePublication = templatePublication.substring(0, p);
139  }
140  }
141  return end;
142  }
143 
144  private void doSend(PublicationCategory pubcat) {
145  MailContact.sendToIPlayers(
146  getPlayers(pubcat),
147  pubcat.getName() + " - " + ElephantContext.getSiteName(),
148  pubcat.getName() + " - " + ElephantContext.getSiteName(),
149  mailStr,
150  "",
151  null,
152  "IMailPool_Publication");
153  }
154 
155 }
static IContact getContact()
Definition: Contacts.java:54
IImplementation getImplementation(String name)
static String escapeGrouping(String sequence)
Definition: Parser.java:233
static String get(String msg)
Definition: I_.java:41
Collection< QueueOptions > getSubcribers(String idCategory)
void sendMail(IConstructor constructor, PublicationCategory pubcat)
void prepareMail(IConstructor constructor, PublicationCategory pubcat, List< Publication > pubs, String path)
static String getPublicationsStr(List< Publication > publications, String sclass, boolean title, boolean details, boolean summary, boolean body, String path, boolean asMail, String readAll, IConstructor constructor)
static String getPublicationsTemplateStr(String template, List< Publication > publications, String sclass, boolean title, boolean details, boolean summary, boolean body, String path, boolean asMail, String readAll, IConstructor constructor)