BrightSide Workbench Full Report + Source Code
PublicationCategory.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.publication.entity;
19 
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.TreeSet;
25 import java.util.stream.Collectors;
26 import javax.persistence.CascadeType;
27 import javax.persistence.Column;
28 import javax.persistence.Entity;
29 import javax.persistence.FetchType;
30 import javax.persistence.GeneratedValue;
31 import javax.persistence.GenerationType;
32 import javax.persistence.Id;
33 import javax.persistence.Lob;
34 import javax.persistence.OneToMany;
35 import org.turro.action.queue.ConstraintKeys;
36 import org.turro.action.queue.NotificationConstraint;
37 import org.turro.action.queue.NotificationConstraintSet;
38 import org.turro.string.Strings;
39 import org.turro.elephant.db.WhereClause;
40 import org.turro.jpa.Dao;
41 import org.turro.plugin.contacts.IContact;
42 import org.turro.publication.db.PublicationPU;
43 
48 @Entity
49 public class PublicationCategory implements java.io.Serializable {
50 
51  @Id
52  @GeneratedValue(strategy=GenerationType.IDENTITY)
53  @Column(name="IDENTIFIER")
54  private Long id;
55 
56  private boolean publishable;
57 
58  private String name;
59 
60  private String conditionedBy;
61 
62  private boolean avoidQueue;
63 
64  @Lob
65  @Column(length=4096)
66  private String description;
67 
68  @OneToMany(mappedBy = "publicationCategory", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
69  private Set<PublicationConstraint> constraints = new TreeSet<PublicationConstraint>();
70 
71  public String getDescription() {
72  return description;
73  }
74 
75  public void setDescription(String description) {
76  this.description = description;
77  }
78 
79  public Long getId() {
80  return id;
81  }
82 
83  public void setId(Long id) {
84  this.id = id;
85  }
86 
87  public String getName() {
88  return name;
89  }
90 
91  public void setName(String name) {
92  this.name = name;
93  }
94 
95  public boolean isPublishable() {
96  return publishable;
97  }
98 
99  public void setPublishable(boolean publishable) {
100  this.publishable = publishable;
101  }
102 
103  public String getConditionedBy() {
104  return conditionedBy;
105  }
106 
107  public void setConditionedBy(String conditionedBy) {
108  this.conditionedBy = conditionedBy;
109  }
110 
111  public boolean isAvoidQueue() {
112  return avoidQueue;
113  }
114 
115  public void setAvoidQueue(boolean avoidQueue) {
116  this.avoidQueue = avoidQueue;
117  }
118 
119  public Set<PublicationConstraint> getConstraints() {
120  return constraints;
121  }
122 
123  public void setConstraints(Set<PublicationConstraint> constraints) {
124  this.constraints = constraints;
125  }
126 
127  /* Constraints */
128 
129  public boolean strongBond(ConstraintKeys keys) {
130  return getConstraintSet().strongBond(keys);
131  }
132 
133  public boolean wideBond(ConstraintKeys keys) {
134  return getConstraintSet().wideBond(keys);
135  }
136 
137  public boolean isRestricted() {
138  return getConstraintSet().isRestricted();
139  }
140 
143  public boolean isEmpty() {
144  return Strings.isBlank(name);
145  }
146 
148  return new NotificationConstraintSet(getConstraints().stream()
149  .map(c -> new NotificationConstraint(c.getKeyId(), c.isOptional()))
150  .collect(Collectors.toCollection(HashSet::new)));
151  }
152 
153  public List<Publication> getPublications() {
154  WhereClause wc = new WhereClause();
155  wc.addClause("select d from Publication d");
156  wc.addClause("where d.publicationCategory = :category");
157  wc.addNamedValue("category", this);
158  Dao dao = new PublicationPU();
159  return dao.getResultList(Publication.class, wc);
160  }
161 
162  @Deprecated
163  public Collection<PublicationSubscription> getSubscriptors() {
164  return new PublicationPU().getResultList(
165  "select ps from PublicationSubscription ps " +
166  "where ps.publicationCategory = ?",
167  new Object[] { this }
168  );
169  }
170 
171  @Deprecated
172  public Collection<Long> getSubscriptorsId() {
173  return new PublicationPU().getResultList(
174  "select ps.id from PublicationSubscription ps " +
175  "where ps.publicationCategory = ?",
176  new Object[] { this }
177  );
178  }
179 
180  public Collection<PublicationBlogger> getBloggers() {
181  return new PublicationPU().getResultList(
182  "select ps from PublicationBlogger ps " +
183  "where ps.publicationCategory = ?",
184  new Object[] { this }
185  );
186  }
187 
188  public Collection<Long> getBloggersId() {
189  return new PublicationPU().getResultList(
190  "select ps.id from PublicationBlogger ps " +
191  "where ps.publicationCategory = ?",
192  new Object[] { this }
193  );
194  }
195 
196  public boolean isBlogger(IContact contact) {
198  "select ps from PublicationBlogger ps " +
199  "where ps.idContact = ? " +
200  "and ps.publicationCategory = ?",
201  new Object[] { contact.getId(), this }
202  ) != null;
203  }
204 
205  static public boolean isAnyBlogger(IContact contact) {
207  "select distinct ps.idContact from PublicationBlogger ps " +
208  "where ps.idContact = ?",
209  new Object[] { contact.getId() }
210  ) != null;
211  }
212 
213 }
void addNamedValue(String name, Object value)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
void setConstraints(Set< PublicationConstraint > constraints)
Collection< PublicationSubscription > getSubscriptors()