BrightSide Workbench Full Report + Source Code
PublicationSendable.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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 
19 package org.turro.entities;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.apache.commons.mail.EmailException;
27 import org.turro.action.MailAttachment;
28 import org.turro.assistant.Assistant;
29 import org.turro.assistant.AssistantSet;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.elephant.entities.db.Sendable;
33 import org.turro.elephant.impl.repository.Repository;
34 import org.turro.file.util.FileAttach;
35 import org.turro.jpa.entity.EntityWebUrls;
36 import org.turro.mail.message.MailMessageTemplate;
37 import org.turro.mail.pool.MailMessagePool;
38 import org.turro.mail.provider.MailProviders;
39 import org.turro.marker.ElephantMarker;
40 import org.turro.publication.db.PublicationPU;
41 import org.turro.publication.entity.Publication;
42 import org.turro.sendable.AbstractSendable;
43 import org.turro.sendable.Sendables;
44 
49 public class PublicationSendable extends AbstractSendable<Publication> {
50 
51  private IConstructor constructor;
52 
54  super(entity);
55  }
56 
57  public PublicationSendable(String entityPath) {
58  super(entityPath);
59  }
60 
61  @Override
62  protected boolean doSend(Sendable sendable, Publication entity, IConstructor constructor, AssistantSet assistants, boolean checkSent) {
63  this.constructor = constructor;
64  if(ElephantMarker.existsTemplate(constructor, true, "envelope", "header")) {
65  MailMessagePool pool = MailProviders.instance().getPool(constructor, "Publication");
66  if(pool != null) {
67  List<MailAttachment> attached = getAttached(entity);
68  for(Assistant assistant : assistants) {
69  try {
71  mmt.setRoot("/publications");
72  mmt.setSubject(entity.getTitle()+ " - " + ElephantContext.getSiteName());
73  mmt.addTo(assistant.email, assistant.name);
74  ElephantMarker em = new ElephantMarker(constructor, true);
75  ArrayList<Publication> pubs = new ArrayList<>();
76  pubs.add(entity);
77  em.put("pubs", pubs);
78  em.put("contact", assistant.contact);
79  em.put("email", assistant.email);
80  em.put("notifier", this);
81  mmt.setMessage(em, "sendable", assistant.contact);
82  for(MailAttachment ma : attached) {
83  mmt.attach(ma.getPath(), ma.getName(), ma.getName());
84  }
85  pool.addToPool(mmt);
86  pool.sendPool();
87  if(checkSent) Sendables.delivered(sendable.getEntityPath(), assistant.contact.getId());
88  } catch (EmailException | NullPointerException ex) {
89  Logger.getLogger(PublicationSendable.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
90  }
91  }
92  }
93  }
94  return checkSent;
95  }
96 
97  @Override
98  protected void addAssistants(AssistantSet as, Publication entity) {
99  // do nothing
100  }
101 
102  public String createRef(Publication publication) throws Exception {
103  String redir;
104  String entityUrl = ElephantContext.getEntityWebContext("/publication");
105  String ewu = EntityWebUrls.getUrlFromEntity(publication);
106  if(ewu != null && entityUrl != null) {
107  redir = entityUrl + ewu;
108  } else {
109  redir = "/";
110  }
111  return createRef(redir);
112  }
113 
114  public String createRef(String redir) throws Exception {
115  return "{liveref:" + redir + "}";
116  }
117 
119  return new FileAttach(PublicationPU.getObjectPath(pub)).getPublishableRepository(constructor);
120  }
121 
122  private List<MailAttachment> getAttached(Publication pub) {
123  List<MailAttachment> list = new ArrayList<>();
124  File[] attached = new FileAttach(PublicationPU.getObjectPath(pub))
125  .getPublishableRepository(constructor).getRoot("/attached").getFiles("*.*");
126  if(attached != null) {
127  for(File file : attached) {
128  list.add(new MailAttachment(file.getAbsolutePath(), file.getName(), file));
129  }
130  }
131  return list;
132  }
133 
134 }
static String getEntityWebContext(String path)
boolean doSend(Sendable sendable, Publication entity, IConstructor constructor, AssistantSet assistants, boolean checkSent)
void addAssistants(AssistantSet as, Publication entity)
String createRef(Publication publication)
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
static String getUrlFromEntity(Object entity)
MailMessage addToPool(String from, String to, String cc, String subject, String message)
MailMessagePool getPool(IConstructor constructor, String name)
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name)
Object put(Object key, Object value)
static String getObjectPath(Object object)
static void delivered(String entityPath, String contactId)
Definition: Sendables.java:113