BrightSide Workbench Full Report + Source Code
AbstractSendable.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.sendable;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import org.turro.string.Strings;
24 import org.turro.action.IAgreements;
25 import org.turro.action.IElephantSendable;
26 import org.turro.action.Plugins;
27 import org.turro.assistant.AssistantSet;
28 import org.turro.elephant.context.IConstructor;
29 import org.turro.elephant.db.ElephantPU;
30 import org.turro.elephant.entities.db.Sendable;
31 import org.turro.elephant.entities.db.SendableAssistant;
32 import org.turro.entities.Entities;
33 import org.turro.jpa.Dao;
34 import org.turro.plugin.contacts.IContact;
35 
40 public abstract class AbstractSendable<T> implements IElephantSendable {
41 
42  private Sendable sendable;
43  private T entity;
44 
45  public AbstractSendable(T entity) {
46  this.entity = entity;
47  assignByEntity(entity);
48  }
49 
50  public AbstractSendable(String entityPath) {
51  assignByPath(entityPath);
52  }
53 
54  @Override
55  public Date getSchedule() {
56  return sendable.getSchedule();
57  }
58 
59  @Override
60  public boolean isSent() {
61  return sendable.isSent();
62  }
63 
64  @Override
66  AssistantSet as = new AssistantSet();
67  addAssistants(as, entity);
68  IAgreements agreements = Plugins.loadImplementation(IAgreements.class, "agreements");
69  for(SendableAssistant sa : Sendables.getAssistants(sendable.getEntityPath(), false)) {
70  agreements.setContact(sa.getIContact());
71  if(!agreements.declinedNotifications()) {
72  as.addContact(sa.getIContact(), sendable);
73  }
74  }
75  return as;
76  }
77 
78  @Override
79  public void send(IConstructor constructor) {
80  try {
81  doSend(sendable, getEntity(), constructor, getAssistants(), true);
82  } finally {
83  sendable.setSent(true);
84  getDao().saveObject(sendable);
85  }
86  }
87 
88  @Override
89  public void send(IConstructor constructor, Collection<IContact> contacts) {
90  AssistantSet assistants = new AssistantSet();
91  assistants.addContacts(contacts);
92  if(!assistants.isEmpty()) doSend(sendable, getEntity(), constructor, assistants, false);
93  }
94 
95  protected abstract boolean doSend(Sendable sendable, T entity, IConstructor constructor, AssistantSet assistants, boolean checkSent);
96  protected abstract void addAssistants(AssistantSet as, T entity);
97 
98  private Sendable assignByEntity(Object entity) {
99  return assignByPath(Entities.getController(entity).getPath());
100  }
101 
102  private Sendable assignByPath(String entityPath) {
103  if(!Strings.isBlank(entityPath)) {
104  sendable = (Sendable) getDao()
105  .getSingleResultOrNull("select s from Sendable s where s.entityPath = ?",
106  new Object[] { entityPath });
107  } else {
108  sendable = null;
109  }
110  if(sendable == null) {
111  sendable = new Sendable();
112  sendable.setEntityPath(entityPath);
113  }
114  return sendable;
115  }
116 
117  private T getEntity() {
118  if(entity == null) {
119  entity = (T) Entities.getController(sendable.getEntityPath()).getEntity();
120  }
121  return (T) entity;
122  }
123 
124  /* Dao */
125 
126  private Dao _dao;
127 
128  private Dao getDao() {
129  if(_dao == null) {
130  _dao = new ElephantPU();
131  }
132  return _dao;
133  }
134 
135 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
void addContacts(Collection< IContact > contacts)
void addContact(IContact contact, Object relationEntity)
static IElephantEntity getController(String path)
Definition: Entities.java:78
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
void send(IConstructor constructor)
void send(IConstructor constructor, Collection< IContact > contacts)
abstract boolean doSend(Sendable sendable, T entity, IConstructor constructor, AssistantSet assistants, boolean checkSent)
abstract void addAssistants(AssistantSet as, T entity)
static Collection< SendableAssistant > getAssistants(String entityPath, boolean all)
Definition: Sendables.java:76
void setContact(IContact contact)