BrightSide Workbench Full Report + Source Code
StarItUtil.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.starit;
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.StarIt;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.entities.Entities;
30 import org.turro.jpa.Dao;
31 import org.turro.plugin.contacts.SoftContact;
32 
37 public class StarItUtil {
38 
39  public static void addStars(Object entity, int stars, SoftContact contact) {
40  String path = Entities.getController(entity).getPath();
41  if(path != null) {
42  addStars(path, stars, contact);
43  }
44  }
45 
46  public static void addStars(String path, int stars, SoftContact contact) {
47  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
48  Dao dao = new ContactsPU();
49  if(((Long) dao.getSingleResult(
50  " select count(*) from StarIt " +
51  " where path = ? " +
52  " and (creator = ? or author_ip = ?)",
53  new Object[] { path, contact.getContact(), contact.author_ip }
54  )) == 0) {
55  StarIt starIt = new StarIt();
56  starIt.setCreator((Contact) contact.getContact());
57  starIt.setAuthor_ip(contact.author_ip);
58  starIt.setDateCreation(new Date());
59  starIt.setStars(stars);
60  starIt.setPath(path);
61  dao.saveObject(starIt);
62  } else {
63  changeStars(path, stars, contact);
64  }
65  }
66  }
67 
68  public static void removeStars(Object entity, SoftContact contact) {
69  String path = Entities.getController(entity).getPath();
70  if(path != null) {
71  removeStars(path, contact);
72  }
73  }
74 
75  public static void removeStars(String path, SoftContact contact) {
76  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
77  Dao dao = new ContactsPU();
78  if(contact.getContact() != null) {
79  dao.executeUpdate(
80  " delete from StarIt " +
81  " where path = ? " +
82  " and creator = ?",
83  new Object[] { path, contact.getContact() }
84  );
85  } else {
86  dao.executeUpdate(
87  " delete from StarIt " +
88  " where path = ? " +
89  " and (creator is null and author_ip = ?)",
90  new Object[] { path, contact.author_ip }
91  );
92  }
93  }
94  }
95 
96  public static void changeStars(Object entity, int newStars, SoftContact contact) {
97  String path = Entities.getController(entity).getPath();
98  if(path != null) {
99  changeStars(path, newStars, contact);
100  }
101  }
102 
103  public static void changeStars(String path, int newStars, SoftContact contact) {
104  if(!Strings.isBlank(path) && contact != null && contact.isValid()) {
105  Dao dao = new ContactsPU();
106  if(contact.getContact() != null) {
107  dao.executeUpdate(
108  " update StarIt " +
109  " set stars = ? " +
110  " where path = ? " +
111  " and creator = ?",
112  new Object[] { newStars, path, contact.getContact() }
113  );
114  } else {
115  dao.executeUpdate(
116  " update StarIt " +
117  " set stars = ? " +
118  " where path = ? " +
119  " and creator is null and author_ip = ?",
120  new Object[] { newStars, path, contact.author_ip }
121  );
122  }
123  }
124  }
125 
126  public static StarsInfo stars(Object entity) {
127  String path = Entities.getController(entity).getPath();
128  if(path != null) {
129  return stars(path);
130  }
131  return null;
132  }
133 
134  public static StarsInfo stars(String path) {
135  Dao dao = new ContactsPU();
136  Object o[] = (Object[]) dao.getSingleResult(
137  " select sum(stars), count(stars) from StarIt " +
138  " where path = ?",
139  new Object[] { path }
140  );
141  if(o != null) {
142  StarsInfo si = new StarsInfo();
143  si.stars = o[0] == null ? 0 : (Long) o[0];
144  si.count = o[1] == null ? 0 : (Long) o[1];
145  return si;
146  }
147  return null;
148  }
149 
150  public static List<StarIt> allStars(Object entity) {
151  String path = Entities.getController(entity).getPath();
152  if(path != null) {
153  return allStars(path);
154  }
155  return null;
156  }
157 
158  public static List<StarIt> allStars(String path) {
159  Dao dao = new ContactsPU();
160  return dao.getResultList(
161  " select s from StarIt as s " +
162  " where s.path = ?",
163  new Object[] { path }
164  );
165  }
166 
167  public static Collection<String> paths(String path, int fromDays) {
168  Dao dao = new ContactsPU();
169  return dao.getResultList(
170  " select g.path from StarIt g " +
171  " where g.path like ? " +
172  " and g.dateCreation >= ?",
173  new Object[] { path + "/%", new CheckDate().addDays(-fromDays).getDate() }
174  );
175  }
176 
177  private StarItUtil() {
178  }
179 
180 }
void setDateCreation(Date dateCreation)
Definition: StarIt.java:89
void setPath(String path)
Definition: StarIt.java:73
void setStars(int stars)
Definition: StarIt.java:97
void setCreator(Contact creator)
Definition: StarIt.java:81
void setAuthor_ip(String author_ip)
Definition: StarIt.java:105
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
static void addStars(Object entity, int stars, SoftContact contact)
Definition: StarItUtil.java:39
static StarsInfo stars(String path)
static void removeStars(String path, SoftContact contact)
Definition: StarItUtil.java:75
static List< StarIt > allStars(String path)
static void addStars(String path, int stars, SoftContact contact)
Definition: StarItUtil.java:46
static Collection< String > paths(String path, int fromDays)
static void changeStars(Object entity, int newStars, SoftContact contact)
Definition: StarItUtil.java:96
static List< StarIt > allStars(Object entity)
static void changeStars(String path, int newStars, SoftContact contact)
static StarsInfo stars(Object entity)
static void removeStars(Object entity, SoftContact contact)
Definition: StarItUtil.java:68