BrightSide Workbench Full Report + Source Code
Sendables.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 java.util.concurrent.locks.ReentrantLock;
24 import org.turro.action.IElephantSendable;
25 import org.turro.elephant.context.IConstructor;
26 import org.turro.elephant.db.ElephantPU;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.elephant.entities.db.Sendable;
29 import org.turro.elephant.entities.db.SendableAssistant;
30 import org.turro.entities.Entities;
31 import org.turro.entities.IElephantEntity;
32 import org.turro.jpa.Dao;
33 
38 public class Sendables {
39 
40  public static void send(IConstructor constructor) {
41  boolean lockAcquired = SENDABLE_LOCK.tryLock();
42  if(lockAcquired) {
43  try {
44  for(Sendable sendable : getSendables()) {
45  IElephantEntity iee = Entities.getController(sendable.getEntityPath());
46  IElephantSendable is = iee.getSendable();
47  if(is != null) {
48  is.send(constructor);
49  }
50  }
51  } finally {
52  SENDABLE_LOCK.unlock();
53  }
54  }
55  }
56 
57  public static Collection<Sendable> getSendables() {
58  Dao dao = new ElephantPU();
59  WhereClause wc = new WhereClause();
60  wc.addClause("select s from Sendable s");
61  wc.addClause("where s.sent = FALSE");
62  wc.addClause("and s.schedule < :now");
63  wc.addNamedValue("now", new Date());
64  return dao.getResultList(wc);
65  }
66 
67  public static Sendable getSendable(String entityPath) {
68  Dao dao = new ElephantPU();
69  WhereClause wc = new WhereClause();
70  wc.addClause("select s from Sendable s");
71  wc.addClause("where s.entityPath = :path");
72  wc.addNamedValue("path", entityPath);
73  return (Sendable) dao.getSingleResultOrNull(wc);
74  }
75 
76  public static Collection<SendableAssistant> getAssistants(String entityPath, boolean all) {
77  Dao dao = new ElephantPU();
78  WhereClause wc = new WhereClause();
79  wc.addClause("select sa from SendableAssistant sa");
80  wc.addClause("where sa.entityPath = :path");
81  wc.addNamedValue("path", entityPath);
82  if(!all) {
83  wc.addClause("and sa.delivered = FALSE");
84  }
85  return dao.getResultList(wc);
86  }
87 
88  public static void cleanOrphans() {
89  Dao dao = new ElephantPU();
90  WhereClause wc = new WhereClause();
91  wc.addClause("delete from SendableAssistant");
92  wc.addClause("where not exists (");
93  wc.addClause("select s from Sendable s");
94  wc.addClause("where s.entityPath = entityPath");
95  wc.addClause(")");
96  dao.executeUpdate(wc);
97  }
98 
99  public static void delete(String entityPath) {
100  Dao dao = new ElephantPU();
101  WhereClause wc = new WhereClause();
102  wc.addClause("delete from SendableAssistant");
103  wc.addClause("where entityPath = :path");
104  wc.addNamedValue("path", entityPath);
105  dao.executeUpdate(wc);
106  wc = new WhereClause();
107  wc.addClause("delete from Sendable");
108  wc.addClause("where entityPath = :path");
109  wc.addNamedValue("path", entityPath);
110  dao.executeUpdate(wc);
111  }
112 
113  public static void delivered(String entityPath, String contactId) {
114  Dao dao = new ElephantPU();
115  WhereClause wc = new WhereClause();
116  wc.addClause("update SendableAssistant");
117  wc.addClause("set delivered = TRUE");
118  wc.addClause("where entityPath = :path");
119  wc.addNamedValue("path", entityPath);
120  wc.addClause("and contactId = :contact");
121  wc.addNamedValue("contact", contactId);
122  dao.executeUpdate(wc);
123  }
124 
125  public static void sent(String entityPath) {
126  Dao dao = new ElephantPU();
127  WhereClause wc = new WhereClause();
128  wc.addClause("update Sendable");
129  wc.addClause("set sent = TRUE");
130  wc.addClause("where entityPath = :path");
131  wc.addNamedValue("path", entityPath);
132  dao.executeUpdate(wc);
133  }
134 
135  private Sendables() {
136  }
137 
138  /* Sendables lock */
139 
140  private static final ReentrantLock SENDABLE_LOCK = new ReentrantLock();
141 
142 }
void addNamedValue(String name, Object value)
static IElephantEntity getController(String path)
Definition: Entities.java:78
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
static void send(IConstructor constructor)
Definition: Sendables.java:40
static void sent(String entityPath)
Definition: Sendables.java:125
static Collection< SendableAssistant > getAssistants(String entityPath, boolean all)
Definition: Sendables.java:76
static Sendable getSendable(String entityPath)
Definition: Sendables.java:67
static Collection< Sendable > getSendables()
Definition: Sendables.java:57
static void delivered(String entityPath, String contactId)
Definition: Sendables.java:113
void send(IConstructor constructor)
IElephantSendable getSendable()