BrightSide Workbench Full Report + Source Code
VoteItUtil.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.voteit;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
24 import org.amic.util.date.CheckDate;
25 import org.turro.string.Strings;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.VoteIt;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.entities.Entities;
30 import org.turro.jpa.Dao;
31 import org.turro.log.SystemLogType;
32 import org.turro.log.SystemLogger;
33 import org.turro.plugin.contacts.IContact;
34 
39 public class VoteItUtil {
40 
41  public static void addVote(Object entity, int vote, IContact contact) {
42  String path = Entities.getController(entity).getPath();
43  if(path != null) {
44  addVote(path, vote, contact);
45  }
46  }
47 
48  public static void addVote(String path, int vote, IContact contact) {
49  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
50  Dao dao = new ContactsPU();
51  if(((Long) dao.getSingleResult(
52  " select count(*) from VoteIt " +
53  " where path = ? " +
54  " and creator = ?",
55  new Object[] { path, contact.getContact() }
56  )) == 0) {
57  VoteIt voteIt = new VoteIt();
58  voteIt.setCreator((Contact) contact.getContact());
59  voteIt.setDateCreation(new Date());
60  voteIt.setVote(vote);
61  voteIt.setPath(path);
62  dao.saveObject(voteIt);
63  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, contact, path, "vote-it", vote + "");
64  } else {
65  changeVote(path, vote, contact);
66  }
67  }
68  }
69 
70  public static void removeVote(Object entity, IContact contact) {
71  String path = Entities.getController(entity).getPath();
72  if(path != null) {
73  removeVote(path, contact);
74  }
75  }
76 
77  public static void removeVote(String path, IContact contact) {
78  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
79  Dao dao = new ContactsPU();
80  dao.executeUpdate(
81  " delete from VoteIt " +
82  " where path = ? " +
83  " and creator = ?",
84  new Object[] { path, contact.getContact() }
85  );
86  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, contact, path, "vote-it", "deleted");
87  }
88  }
89 
90  public static void changeVote(Object entity, int newVote, IContact contact) {
91  String path = Entities.getController(entity).getPath();
92  if(path != null) {
93  changeVote(path, newVote, contact);
94  }
95  }
96 
97  public static void changeVote(String path, int newVote, IContact contact) {
98  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
99  Dao dao = new ContactsPU();
100  dao.executeUpdate(
101  " update VoteIt " +
102  " set vote = ? " +
103  " where path = ? " +
104  " and creator = ?",
105  new Object[] { newVote, path, contact.getContact() }
106  );
107  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, contact, path, "vote-it", newVote + "");
108  }
109  }
110 
111  public static VoteItInfo info(Object entity, IContact contact) {
112  return info(entity, null, contact);
113  }
114 
115  public static VoteItInfo info(Object entity, String allowed, IContact contact) {
116  String path = Entities.getController(entity).getPath();
117  if(path != null) {
118  return info(path, allowed, contact);
119  }
120  return null;
121  }
122 
123  public static VoteItInfo info(String path, IContact contact) {
124  return info(path, null, contact);
125  }
126 
127  public static VoteItInfo info(String path, String allowed, IContact contact) {
128  VoteItInfo vi = new VoteItInfo();
129  Dao dao = new ContactsPU();
130  vi.setPositive((Long) dao.getSingleResult(
131  " select count(vote) from VoteIt " +
132  " where path = ? and vote > 0 " +
133  getAllowedString(allowed),
134  new Object[] { path }
135  ));
136  vi.setNocare((Long) dao.getSingleResult(
137  " select count(vote) from VoteIt " +
138  " where path = ? and vote = 0 " +
139  getAllowedString(allowed),
140  new Object[] { path }
141  ));
142  vi.setNegative((Long) dao.getSingleResult(
143  " select count(vote) from VoteIt " +
144  " where path = ? and vote < 0 " +
145  getAllowedString(allowed),
146  new Object[] { path }
147  ));
148  if(contact != null) {
149  vi.setMyVote(myVote(dao, path, contact));
150  }
151  return vi;
152  }
153 
154  public static MyVote myVote(String path, IContact contact) {
155  Dao dao = new ContactsPU();
156  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
158  " select vi from VoteIt as vi " +
159  " where path = ? and creator = ?",
160  new Object[] { path, contact.getContact() }
161  );
162  if(vi != null) {
163  return new MyVote(contact, vi.getVote());
164  }
165  }
166  return null;
167  }
168 
169  public static MyVote myVote(Dao dao, String path, IContact contact) {
170  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
172  " select vi from VoteIt as vi " +
173  " where path = ? and creator = ?",
174  new Object[] { path, contact.getContact() }
175  );
176  if(vi != null) {
177  return new MyVote(contact, vi.getVote());
178  }
179  }
180  return null;
181  }
182 
183  public static List<VoteIt> allVotes(String path) {
184  Dao dao = new ContactsPU();
185  return dao.getResultList(
186  " select vi from VoteIt as vi " +
187  " where vi.path = ?",
188  new Object[] { path }
189  );
190  }
191 
192  public static String getAllowedString(String allowed) {
193  if(allowed == null) {
194  return "";
195  } else {
196  if(Strings.isBlank(allowed)) {
197  return " and 1 = 2 ";
198  } else {
199  return " and creator.id in (" + allowed + ")";
200  }
201  }
202  }
203 
204  public static Collection<String> paths(String path, int fromDays) {
205  Dao dao = new ContactsPU();
206  return dao.getResultList(
207  " select g.path from VoteIt g " +
208  " where g.path like ? " +
209  " and g.dateCreation >= ?",
210  new Object[] { path + "/%", new CheckDate().addDays(-fromDays).getDate() }
211  );
212  }
213 
214  private VoteItUtil() {
215  }
216 
217 }
void setVote(int vote)
Definition: VoteIt.java:72
void setCreator(Contact creator)
Definition: VoteIt.java:88
void setPath(String path)
Definition: VoteIt.java:80
void setDateCreation(Date dateCreation)
Definition: VoteIt.java:96
static IElephantEntity getController(String path)
Definition: Entities.java:78
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
static ISystemLogger getInstance()
void setNocare(long nocare)
Definition: VoteItInfo.java:59
void setMyVote(MyVote myVote)
Definition: VoteItInfo.java:67
void setNegative(long negative)
Definition: VoteItInfo.java:51
void setPositive(long positive)
Definition: VoteItInfo.java:43
static List< VoteIt > allVotes(String path)
static String getAllowedString(String allowed)
static void removeVote(String path, IContact contact)
Definition: VoteItUtil.java:77
static void addVote(String path, int vote, IContact contact)
Definition: VoteItUtil.java:48
static VoteItInfo info(String path, String allowed, IContact contact)
static MyVote myVote(Dao dao, String path, IContact contact)
static VoteItInfo info(String path, IContact contact)
static Collection< String > paths(String path, int fromDays)
static void changeVote(String path, int newVote, IContact contact)
Definition: VoteItUtil.java:97
static MyVote myVote(String path, IContact contact)
static void changeVote(Object entity, int newVote, IContact contact)
Definition: VoteItUtil.java:90
static VoteItInfo info(Object entity, String allowed, IContact contact)
static VoteItInfo info(Object entity, IContact contact)
static void removeVote(Object entity, IContact contact)
Definition: VoteItUtil.java:70
static void addVote(Object entity, int vote, IContact contact)
Definition: VoteItUtil.java:41
void doLog(SystemLogType type, Object entity, String comment, Serializable data)