BrightSide Workbench Full Report + Source Code
Sendable.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.elephant.entities.db;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import javax.persistence.Entity;
24 import javax.persistence.Id;
25 import javax.persistence.Temporal;
26 import org.turro.string.Strings;
27 import org.turro.assistant.Assistant;
28 import org.turro.assistant.AssistantConstants;
29 import org.turro.assistant.AssistantSet;
30 import org.turro.assistant.Assistants;
31 import org.turro.elephant.db.ElephantPU;
32 import org.turro.elephant.db.WhereClause;
33 import org.turro.entities.Entities;
34 import org.turro.jpa.Dao;
35 import org.turro.plugin.contacts.IContact;
36 
41 @Entity
42 public class Sendable implements java.io.Serializable {
43 
44  @Id
45  private String entityPath;
46 
47  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
48  private java.util.Date schedule;
49 
50  private boolean sent;
51 
52  public String getEntityPath() {
53  return entityPath;
54  }
55 
56  public void setEntityPath(String entityPath) {
57  this.entityPath = entityPath;
58  }
59 
60  public Date getSchedule() {
61  return schedule;
62  }
63 
64  public void setSchedule(Date schedule) {
65  this.schedule = schedule;
66  }
67 
68  public boolean isSent() {
69  return sent;
70  }
71 
72  public void setSent(boolean sent) {
73  this.sent = sent;
74  }
75 
76  /* Utils */
77 
78  public boolean isEmpty() {
79  return Strings.isBlank(entityPath) || schedule == null;
80  }
81 
82  public Object getEntity() {
83  return Entities.getController(entityPath).getEntity();
84  }
85 
86  /* Assistants */
87 
88  public void addAssistantsByEntity(String entityPath, boolean deep) {
89  AssistantSet as = new AssistantSet();
90  Assistants.addAssistants(entityPath, deep, as, deep ? AssistantConstants.all() : null);
91  for(Assistant a : as) {
92  addContact((IContact) a.contact);
93  }
94  }
95 
96  public void addContact(IContact contact) {
97  if(contact != null && contact.isWebUser()) {
99  sa.setContactId(contact.getId());
100  sa.setEntityPath(entityPath);
101  sa.setDelivered(false);
102  new ElephantPU().saveObject(sa);
103  }
104  }
105 
106  public Collection<SendableAssistant> getAssistants(boolean all) {
107  Dao dao = new ElephantPU();
108  WhereClause wc = new WhereClause();
109  wc.addClause("select sa from SendableAssistant sa");
110  wc.addClause("where sa.entityPath = :path");
111  wc.addNamedValue("path", entityPath);
112  if(!all) {
113  wc.addClause("and sa.delivered = FALSE");
114  }
115  return dao.getResultList(wc);
116  }
117 
118  public void removeAssistants() {
119  Dao dao = new ElephantPU();
120  WhereClause wc = new WhereClause();
121  wc.addClause("delete from SendableAssistant sa");
122  wc.addClause("where sa.entityPath = :path");
123  wc.addNamedValue("path", entityPath);
124  dao.executeUpdate(wc);
125  }
126 
127 }
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
void addNamedValue(String name, Object value)
void addAssistantsByEntity(String entityPath, boolean deep)
Definition: Sendable.java:88
void addContact(IContact contact)
Definition: Sendable.java:96
Collection< SendableAssistant > getAssistants(boolean all)
Definition: Sendable.java:106
void setEntityPath(String entityPath)
Definition: Sendable.java:56
static IElephantEntity getController(String path)
Definition: Entities.java:78
int executeUpdate(String query)
Definition: Dao.java:463