BrightSide Workbench Full Report + Source Code
CommentItUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.commentit;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.amic.util.date.CheckDate;
26 import org.turro.string.Strings;
27 import org.apache.commons.mail.EmailException;
28 import org.turro.assistant.AssistantConstants;
29 import org.turro.contacts.CommentIt;
30 import org.turro.contacts.Contact;
31 import org.turro.contacts.db.ContactsPU;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.entities.Entities;
34 import org.turro.entities.IElephantEntity;
35 import org.turro.html.HTMLEntities;
36 import org.turro.i18n.I_;
37 import org.turro.jpa.Dao;
38 import org.turro.mail.queue.GenericElephantNotification;
39 import org.turro.mail.sender.MailQueue;
40 import org.turro.plugin.contacts.SoftContact;
41 
46 public class CommentItUtil {
47 
48  public static void addComment(Object entity, String comment, SoftContact contact) {
49  String path = Entities.getController(entity).getPath();
50  if(path != null) {
51  addComment(path, comment, contact);
52  }
53  }
54 
55  public static void addComment(String path, String comment, SoftContact contact) {
56  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
57  Dao dao = new ContactsPU();
58  CommentIt commentIt = new CommentIt();
59  commentIt.setCreator((Contact) contact.getContact());
60  commentIt.setAuthor(contact.author);
61  commentIt.setAuthor_email(contact.author_email);
62  commentIt.setAuthor_ip(contact.author_ip);
63  commentIt.setAuthor_web(contact.author_web);
64  commentIt.setDateCreation(new Date());
65  commentIt.setBody(HTMLEntities.escape(comment));
66  commentIt.setPath(path);
67  dao.saveObject(commentIt);
69  try {
70  new MailQueue()
71  .setRoot("/comments")
73  .addByRole("PublicationAdmin")
74  .setReason(I_.get("Pending revision"))
75  .put("iee", iee)
76  .put("commentIt", commentIt)
77  .sendTemplate("new-comment", I_.get("New comment"));
78  } catch (EmailException ex) {
79  Logger.getLogger(CommentItUtil.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
80  }
81  }
82  }
83 
84  public static void acceptComment(CommentIt comment) {
85  comment.setAccepted(true);
86  new ContactsPU().saveObject(comment);
88  try {
89  new MailQueue()
90  .setRoot("/comments")
93  .setReason(I_.get("Comments"))
94  .put("iee", iee)
95  .put("commentIt", comment)
96  .put("entityUrl", iee.getEntityUrl())
97  .sendTemplate("comment", I_.get("Comment"));
98  } catch (EmailException ex) {
99  Logger.getLogger(CommentItUtil.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
100  }
101  }
102 
103  public static void remove(String id) {
104  Dao dao = new ContactsPU();
105  dao.executeUpdate(
106  " delete from CommentIt c " +
107  " where c.id = '" + id + "'"
108  );
109  }
110 
111  public static Collection<CommentIt> comments(Object entity, SoftContact contact) {
112  String path = Entities.getController(entity).getPath();
113  if(path != null) {
114  return comments(path, contact);
115  }
116  return null;
117  }
118 
119  public static Collection<CommentIt> comments(String path, SoftContact contact) {
120  Dao dao = new ContactsPU();
121  if(contact.getContact() != null) {
122  return dao.getResultList(
123  " select c from CommentIt c " +
124  " where path = ? " +
125  " and (accepted = TRUE or creator = ?) " +
126  " order by dateCreation ",
127  new Object[] { path, contact.getContact() }
128  );
129  } else {
130  return dao.getResultList(
131  " select c from CommentIt c " +
132  " where path = ? " +
133  " and (accepted = TRUE or author_ip = ?) " +
134  " order by dateCreation ",
135  new Object[] { path, contact.author_ip }
136  );
137  }
138  }
139 
140  public static Collection<CommentIt> allComments(Object entity) {
141  String path = Entities.getController(entity).getPath();
142  if(path != null) {
143  return allComments(path);
144  }
145  return null;
146  }
147 
148  public static Collection<CommentIt> allComments(String path) {
149  Dao dao = new ContactsPU();
150  return dao.getResultList(
151  " select c from CommentIt c " +
152  " where path = ? " +
153  " order by dateCreation ",
154  new Object[] { path }
155  );
156  }
157 
158  public static Collection<CommentIt> pending() {
159  Dao dao = new ContactsPU();
160  return dao.getResultList(
161  " select c from CommentIt c " +
162  " where accepted = FALSE " +
163  " order by dateCreation "
164  );
165  }
166 
167  public static long countPending() {
168  Dao dao = new ContactsPU();
169  return (Long) dao.getSingleResultOrNull(
170  " select count(c) from CommentIt c " +
171  " where accepted = FALSE "
172  );
173  }
174 
175  public static long count() {
176  Dao dao = new ContactsPU();
177  return (Long) dao.getSingleResultOrNull(
178  " select count(c) from CommentIt c "
179  );
180  }
181 
182  public static long count(Object entity, SoftContact contact) {
183  String path = Entities.getController(entity).getPath();
184  if(path != null) {
185  return count(path, contact);
186  }
187  return 0;
188  }
189 
190  public static long count(String path, SoftContact contact) {
191  Dao dao = new ContactsPU();
192  Long count = 0L;
193  if(contact.getContact() != null) {
194  count = (Long) dao.getSingleResultOrNull(
195  " select count(c) from CommentIt c " +
196  " where path = ? " +
197  " and (accepted = TRUE or creator = ?) " +
198  " order by dateCreation desc ",
199  new Object[] { path, contact.getContact() }
200  );
201  } else {
202  count = (Long) dao.getSingleResultOrNull(
203  " select count(c) from CommentIt c " +
204  " where path = ? " +
205  " and (accepted = TRUE or author_ip = ?) " +
206  " order by dateCreation desc ",
207  new Object[] { path, contact.author_ip }
208  );
209  }
210  if(count != null) {
211  return count;
212  }
213  return 0;
214  }
215 
216  public static Collection<String> paths(String path, int fromDays) {
217  Dao dao = new ContactsPU();
218  return dao.getResultList(
219  " select g.path from CommentIt g " +
220  " where g.path like ? " +
221  " and g.dateCreation >= ?",
222  new Object[] { path + "/%", new CheckDate().addDays(-fromDays).getDate() }
223  );
224  }
225 
226  private CommentItUtil() {
227  }
228 
229 }
static Collection< CommentIt > comments(Object entity, SoftContact contact)
static Collection< String > paths(String path, int fromDays)
static void addComment(Object entity, String comment, SoftContact contact)
static void acceptComment(CommentIt comment)
static long count(Object entity, SoftContact contact)
static long count(String path, SoftContact contact)
static void addComment(String path, String comment, SoftContact contact)
static Collection< CommentIt > allComments(String path)
static Collection< CommentIt > pending()
static Collection< CommentIt > allComments(Object entity)
static Collection< CommentIt > comments(String path, SoftContact contact)
void setAuthor_ip(String author_ip)
Definition: CommentIt.java:149
void setBody(String body)
Definition: CommentIt.java:109
void setAuthor_web(String author_web)
Definition: CommentIt.java:141
void setPath(String path)
Definition: CommentIt.java:85
void setAuthor(String author)
Definition: CommentIt.java:125
void setAccepted(boolean accepted)
Definition: CommentIt.java:117
void setCreator(Contact creator)
Definition: CommentIt.java:93
void setDateCreation(Date dateCreation)
Definition: CommentIt.java:101
void setAuthor_email(String author_email)
Definition: CommentIt.java:133
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String escape(String html)
static String get(String msg)
Definition: I_.java:41
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
T addByEntity(Object entity, Object data)