BrightSide Workbench Full Report + Source Code
PublicationCategories.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.publication.util;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.List;
26 import org.turro.string.Strings;
27 import org.turro.action.Contacts;
28 import org.turro.action.queue.ConstraintKeys;
29 import org.turro.elephant.db.WhereClause;
30 import org.turro.mail.queue.QueueManager;
31 import org.turro.plugin.contacts.IContact;
32 import org.turro.publication.db.PublicationPU;
33 import org.turro.publication.entity.Publication;
34 import org.turro.publication.entity.PublicationCategory;
35 import org.turro.publication.entity.PublicationGroup;
36 import org.turro.sql.SqlClause;
37 
42 public class PublicationCategories {
43 
44  public static List<Publication> getPublicationsBy(int max, long group, long category,
45  String idContact, boolean accepted) {
46  return getPublicationsBy(max, group, category, idContact, accepted, null);
47  }
48 
49  public static List<Publication> getPublicationsBy(int max, long group, long category,
50  IContact contact, boolean accepted) {
51  return getPublicationsBy(max, group, category, contact, accepted, null);
52  }
53 
54  public static List<Publication> getPublicationsBy(int max, long group, long category,
55  String idContact, boolean accepted, String extraSql) {
56  return getPublicationsBy(max, group, category, Contacts.getContactById(idContact), accepted, extraSql);
57  }
58 
59  public static List<Publication> getPublicationsBy(int max, long group, long category,
60  IContact contact, boolean accepted, String extraSql) {
61  return SqlClause.select("publication").from("Publication publication")
62  .where().equal("accepted", accepted)
63  .startIf(group != 0)
64  .and().equal("publication.publicationGroup.id", group)
65  .endIf()
66  .startIf(category != 0)
67  .and().equal("publication.publicationCategory.id", category)
68  .endIf()
69  .startIf(category == 0 || group == 0)
70  .and().isTrue("publication.publicationGroup.publishable")
71  .endIf()
72  .startIf(contact != null && contact.isWebUser())
73  .and().equal("publication.idContact", contact.getId())
74  .endIf()
75  .startIf(!Strings.isBlank(extraSql))
76  .append(extraSql)
77  .endIf()
78  .orderBy("publication.date DESC")
79  .dao(new PublicationPU())
80  .max(max)
81  .resultList(Publication.class)
82  .stream()
83  .filter(p -> {
84  if(category == 0) {
85  return p.getPublicationCategory().strongBond(ConstraintKeys.from(contact));
86  } else {
87  return true;
88  }
89  })
90  .toList();
91 // WhereClause wc = new WhereClause();
92 // wc.addClause("select publication from Publication publication");
93 // wc.addClause("where publication.accepted = :accepted");
94 // wc.addNamedValue("accepted", accepted);
95 // if(group != 0) {
96 // wc.addClause("and publication.publicationGroup.id = :group");
97 // wc.addNamedValue("group", group);
98 // } else {
99 // wc.addClause("and publication.publicationGroup.publishable = TRUE");
100 // }
101 // if(category != 0) {
102 // wc.addClause("and publication.publicationCategory.id = :category");
103 // wc.addNamedValue("category", category);
104 // } else {
105 // wc.addClause("and publication.publicationCategory.publishable = TRUE");
106 // if(contact != null && contact.isWebUser()) {
107 // wc.addClause("and (");
108 // wc.addClause("publication.publicationCategory.conditionedBy in :syndications");
109 // wc.addNamedValue("syndications", contact.getSyndications());
110 // wc.addClause("or (publication.publicationCategory.conditionedBy is null or trim(publication.publicationCategory.conditionedBy) = '')");
111 // wc.addClause(")");
112 // } else {
113 // wc.addClause("and (publication.publicationCategory.conditionedBy is null or trim(publication.publicationCategory.conditionedBy) = '')");
114 // }
115 // }
116 // if(contact != null && contact.isWebUser()) {
117 // wc.addClause("and publication.idContact = :idContact");
118 // wc.addNamedValue("idContact", contact.getId());
119 // }
120 // if(!Strings.isBlank(extraSql)) {
121 // wc.addClause(extraSql);
122 // }
123 // wc.addClause("order by publication.date DESC");
124 //
125 // return new PublicationPU().getResultList(wc, max);
126  }
127 
128  public static Collection<PublicationCategory> getPublicCategories() {
129  return getPublicCategories((IContact) null);
130  }
131 
132  public static Collection<PublicationCategory> getPublicCategories(String idContact) {
133  return getPublicCategories(Contacts.getContactById(idContact));
134  }
135 
136  public static Collection<PublicationCategory> getPublicCategories(IContact contact) {
137  return SqlClause.select("c").from("PublicationCategory c")
138  .where().isTrue("c.publishable")
139  .dao(new PublicationPU())
140  .resultList(PublicationCategory.class)
141  .stream()
142  .filter(pc -> {
143  if(contact != null && contact.isWebUser()) {
144  return pc.strongBond(ConstraintKeys.from(contact));
145  }
146  return !pc.isRestricted();
147  })
148  .toList();
149  }
150 
151  public static Collection<PublicationCategory> getSubscribedPrivateCategories(String idContact) {
153  }
154 
155  public static Collection<PublicationCategory> getSubscribedPrivateCategories(IContact contact) {
156  ArrayList<PublicationCategory> list = new ArrayList<>();
157  for(PublicationCategory category : getPrivateCategories()) {
158  QueueManager qm = new QueueManager();
159  if(qm.contactWants(contact, PublicationElephantNotification.convertId(category))) {
160  list.add(category);
161  }
162  }
163  return list;
164  }
165 
166  public static Collection<PublicationCategory> getPrivateCategories() {
167  return SqlClause.select("c").from("PublicationCategory c")
168  .where().isFalse("c.publishable")
169  .dao(new PublicationPU())
170  .resultList(PublicationCategory.class);
171  }
172 
173  public static Collection<? extends PublicationCategory> getPrivateBloggerCategories(IContact contact) {
174  if(contact != null && contact.isWebUser()) {
175  WhereClause wc = new WhereClause();
176  wc.addClause("select b.publicationCategory from PublicationBlogger b");
177  wc.addClause("where b.idContact = :idContact");
178  wc.addNamedValue("idContact", contact.getId());
179  wc.addClause("and b.publicationCategory.publishable = FALSE");
180  return new PublicationPU().getResultList(wc);
181  } else {
182  return Collections.EMPTY_LIST;
183  }
184  }
185 
186  public static List<Publication> getNotSentPublications(long categoryID) {
187  WhereClause wc = new WhereClause();
188  wc.addClause("select publication from Publication publication");
189  wc.addClause("where publication.sent = FALSE");
190  wc.addClause("and publication.accepted = TRUE");
191  wc.addClause("and (retainTill is null or retainTill <= :now)");
192  wc.addNamedValue("now", new Date());
193  if(categoryID != 0) {
194  wc.addClause("and publication.publicationCategory.id = :category");
195  wc.addNamedValue("category", categoryID);
196  }
197  wc.addClause("order by publication.date DESC");
198 
199  return new PublicationPU().getResultList(wc);
200  }
201 
202  public static void markAsSentPublications(long categoryID) {
203  WhereClause wc = new WhereClause();
204  wc.addClause("update Publication publication");
205  wc.addClause("set publication.sent = TRUE");
206  wc.addClause("where publication.accepted = TRUE");
207  wc.addClause("and (publication.retainTill is null or publication.retainTill <= :now)");
208  wc.addNamedValue("now", new Date());
209  if(categoryID != 0) {
210  wc.addClause("and publication.publicationCategory.id = :category");
211  wc.addNamedValue("category", categoryID);
212  }
213 
214  new PublicationPU().executeUpdate(wc);
215  }
216 
217  public static List<PublicationCategory> getPublicationCategories() {
218  return new PublicationPU().getResultList(
219  "select pc from PublicationCategory pc order by name");
220  }
221 
222  public static List<PublicationGroup> getPublicationGroups() {
223  return new PublicationPU().getResultList(
224  "select pg from PublicationGroup pg order by name");
225  }
226 
227 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static ConstraintKeys from(IContact contact)
void addNamedValue(String name, Object value)
int executeUpdate(String query)
Definition: Dao.java:463
boolean contactWants(IContact contact, String idCategory)
static List< Publication > getPublicationsBy(int max, long group, long category, IContact contact, boolean accepted, String extraSql)
static Collection< PublicationCategory > getPublicCategories()
static List< Publication > getPublicationsBy(int max, long group, long category, IContact contact, boolean accepted)
static List< PublicationCategory > getPublicationCategories()
static List< Publication > getNotSentPublications(long categoryID)
static List< Publication > getPublicationsBy(int max, long group, long category, String idContact, boolean accepted, String extraSql)
static List< PublicationGroup > getPublicationGroups()
static Collection< PublicationCategory > getPublicCategories(String idContact)
static Collection< PublicationCategory > getPublicCategories(IContact contact)
static List< Publication > getPublicationsBy(int max, long group, long category, String idContact, boolean accepted)
static Collection< PublicationCategory > getSubscribedPrivateCategories(IContact contact)
static Collection< PublicationCategory > getPrivateCategories()
static Collection< PublicationCategory > getSubscribedPrivateCategories(String idContact)
static Collection<? extends PublicationCategory > getPrivateBloggerCategories(IContact contact)