BrightSide Workbench Full Report + Source Code
DescribeItUtil.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.describeit;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import org.turro.string.Strings;
24 import org.turro.callback.ChangedValue;
25 import org.turro.contacts.Contact;
26 import org.turro.contacts.DescribeIt;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.entities.Entities;
29 import org.turro.jpa.Dao;
30 import org.turro.parser.wiki.WikiCompiler;
31 
36 public class DescribeItUtil {
37 
38  public static final String DEFAULT_ID = "Default", DEFAULT_KEY = "Default";
39 
40  public static void addDescription(Object entity, String id, String description, Contact contact, ChangedValue changed) {
41  String path = Entities.getController(entity).getPath();
42  if(path != null) {
43  addDescription(path, id, description, contact, changed);
44  }
45  }
46 
47  public static void addDescription(String path, String id, String description, Contact contact, ChangedValue changed) {
48  if(Strings.isBlank(id)) {
49  id = DEFAULT_ID;
50  }
51  if(!Strings.isBlank(path) && !Strings.isBlank(description) && contact != null) {
52  DescribeIt describeIt = description(id, path);
53  Dao dao = new ContactsPU();
54  if(describeIt != null) {
55  if(describeIt.isContainsWiki()) {
56  if(!description.equals(describeIt.getWiki())) {
57  if(changed != null) {
58  changed.onChange(describeIt.getWiki(), description);
59  }
60  describeIt.setWiki(description);
61  describeIt.setBody(WikiCompiler.source(describeIt.getWiki()).html());
62  }
63  } else {
64  if(!description.equals(describeIt.getBody())) {
65  if(changed != null) {
66  changed.onChange(describeIt.getBody(), description);
67  }
68  describeIt.setBody(description);
69  }
70  }
71  } else {
72  describeIt = new DescribeIt();
73  describeIt.setDescribeId(id);
74  describeIt.setCreator(contact);
75  describeIt.setDateCreation(new Date());
76  describeIt.setPath(path);
77  if(describeIt.isContainsWiki()) {
78  describeIt.setWiki(description);
79  describeIt.setBody(WikiCompiler.source(describeIt.getWiki()).html());
80  } else {
81  describeIt.setBody(description);
82  }
83  }
84  dao.saveObject(describeIt);
85  }
86  }
87 
88  public static void remove(String id) {
89  Dao dao = new ContactsPU();
90  dao.executeUpdate(
91  " delete from DescribeIt c " +
92  " where c.id = '" + id + "'"
93  );
94  }
95 
96  public static void remove(String path, String id) {
97  Dao dao = new ContactsPU();
98  dao.executeUpdate(
99  " delete from DescribeIt c" +
100  " where c.describeId = '" + id + "'" +
101  " and c.path = '" + path + "'"
102  );
103  }
104 
105  public static Collection<DescribeIt> descriptions(Object entity) {
106  String path = Entities.getController(entity).getPath();
107  if(path != null) {
108  return descriptions(path);
109  }
110  return null;
111  }
112 
113  public static Collection<DescribeIt> descriptions(String path) {
114  Dao dao = new ContactsPU();
115  return dao.getResultList(
116  " select c from DescribeIt c " +
117  " where path = ? " +
118  " order by dateCreation ",
119  new Object[] { path }
120  );
121  }
122 
123  public static Collection<DescribeIt> descriptions(String id, Object entity) {
124  String path = Entities.getController(entity).getPath();
125  if(path != null) {
126  return descriptions(id, path);
127  }
128  return null;
129  }
130 
131  public static Collection<DescribeIt> descriptions(String id, String path) {
132  Dao dao = new ContactsPU();
133  return dao.getResultList(
134  " select c from DescribeIt c " +
135  " where path = ? " +
136  " and describeId = ? " +
137  " order by dateCreation ",
138  new Object[] { path, id }
139  );
140  }
141 
142  public static DescribeIt description(String id, Object entity) {
143  Collection<DescribeIt> list = descriptions(id, entity);
144  if(list != null && !list.isEmpty()) {
145  return list.iterator().next();
146  }
147  return null;
148  }
149 
150  public static DescribeIt description(String id, String path) {
151  Collection<DescribeIt> list = descriptions(id, path);
152  if(list != null && !list.isEmpty()) {
153  return list.iterator().next();
154  }
155  return null;
156  }
157 
158  public static String descriptionString(String id, Object entity) {
159  DescribeIt di = description(id, entity);
160  return di != null ? di.getBody() : null;
161  }
162 
163  public static String descriptionString(String id, String path) {
164  DescribeIt di = description(id, path);
165  return di != null ? di.getBody() : null;
166  }
167 
168  public static Collection<DescribeIt> pending() {
169  Dao dao = new ContactsPU();
170  return dao.getResultList(
171  " select c from DescribeIt c " +
172  " where accepted = FALSE " +
173  " order by dateCreation "
174  );
175  }
176 
177  public static long countPending() {
178  Dao dao = new ContactsPU();
179  return (Long) dao.getSingleResultOrNull(
180  " select count(c) from DescribeIt c " +
181  " where accepted = FALSE "
182  );
183  }
184 
185  public static long count() {
186  Dao dao = new ContactsPU();
187  return (Long) dao.getSingleResultOrNull(
188  " select count(c) from DescribeIt c "
189  );
190  }
191 
192  public static long count(Object entity) {
193  String path = Entities.getController(entity).getPath();
194  if(path != null) {
195  return count(path);
196  }
197  return 0;
198  }
199 
200  public static long count(String path) {
201  Dao dao = new ContactsPU();
202  Long count = (Long) dao.getSingleResultOrNull(
203  " select count(c) from DescribeIt c " +
204  " where path = ? " +
205  " order by dateCreation desc ",
206  new Object[] { path }
207  );
208  if(count != null) {
209  return count;
210  }
211  return 0;
212  }
213 
214  private DescribeItUtil() {
215  }
216 
217 }
void setPath(String path)
Definition: DescribeIt.java:86
void setDescribeId(String describeId)
Definition: DescribeIt.java:78
void setDateCreation(Date dateCreation)
void setCreator(Contact creator)
Definition: DescribeIt.java:94
static Collection< DescribeIt > descriptions(String id, Object entity)
static Collection< DescribeIt > descriptions(String id, String path)
static Collection< DescribeIt > descriptions(Object entity)
static String descriptionString(String id, Object entity)
static Collection< DescribeIt > descriptions(String path)
static DescribeIt description(String id, String path)
static void addDescription(String path, String id, String description, Contact contact, ChangedValue changed)
static DescribeIt description(String id, Object entity)
static Collection< DescribeIt > pending()
static String descriptionString(String id, String path)
static long count(Object entity)
static void addDescription(Object entity, String id, String description, Contact contact, ChangedValue changed)
static IElephantEntity getController(String path)
Definition: Entities.java:78
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419