BrightSide Workbench Full Report + Source Code
DaoUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.jpa;
20 
21 import java.util.Collection;
22 import org.turro.string.Strings;
23 import org.turro.collections.ItemOf;
24 import org.turro.collections.SetOf;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.path.Path;
27 import org.turro.reflection.JSONSerializer;
28 
33 public class DaoUtil {
34 
35  public static boolean isEmpty(Dao dao, String table) {
36  Long count = count(dao, table);
37  return count == null || count == 0L;
38  }
39 
40  public static boolean isEmpty(Dao dao, String table, String condition) {
41  Long count = count(dao, table, condition);
42  return count == null || count == 0L;
43  }
44 
45  public static boolean isEmpty(Dao dao, String table, String condition, Object... pars) {
46  Long count = count(dao, table, condition, pars);
47  return count == null || count == 0L;
48  }
49 
50  public static Long count(Dao dao, String table) {
51  Long count = (Long) dao.getSingleResultOrNull("select count(x) from " + table + " x");
52  return count;
53  }
54 
55  public static Long count(Dao dao, String table, String condition) {
56  Long count = (Long) dao.getSingleResultOrNull("select count(x) from " + table + " x " + condition);
57  return count;
58  }
59 
60  public static Long count(Dao dao, String table, String condition, Object... pars) {
61  Long count = (Long) dao.getSingleResultOrNull("select count(x) from " + table + " x " + condition, pars);
62  return count;
63  }
64 
65  public static Long count(Dao dao, WhereClause wc) {
66  Long count = (Long) dao.getSingleResultOrNull(wc);
67  return count;
68  }
69 
70  public static String getInClauseFromPaths(Collection<String> paths, boolean quoted) {
71  return getCommaSeparatedValues(new SetOf<String, String>(paths, new ItemOf<String, String>() {
72  @Override
73  public String getFrom(String e) {
74  return getIdentifierFromPath(e);
75  }
76  }), quoted);
77  }
78 
79  public static String getCommaSeparatedValues(Collection<String> values, boolean quoted) {
80  String result = "";
81  for(String s : values) {
82  if(quoted) s = "\"" + s + "\"";
83  result += (String) (Strings.isBlank(result) ? s : "," + s);
84  }
85  return result;
86  }
87 
88  public static String getIdentifierFromPath(String e) {
89  return new Path(e).getNode(1);
90  }
91 
92  public static String getJson(Dao dao, Class javaClass, Object identifier, final JSONSerializer json) {
93  final StringBuilder sb = new StringBuilder();
94  dao.find(javaClass, identifier, new DaoCallback() {
95  @Override
96  public void before(Object value) {
97  }
98  @Override
99  public void after(Object oldValue, Object newValue) {
100  json.setObject(newValue);
101  sb.append(json.toJson());
102  }
103  });
104  return sb.length() > 0 ? sb.toString() : null;
105  }
106 
107  private DaoUtil() {
108  }
109 
110 }
static String getInClauseFromPaths(Collection< String > paths, boolean quoted)
Definition: DaoUtil.java:70
static String getCommaSeparatedValues(Collection< String > values, boolean quoted)
Definition: DaoUtil.java:79
static Long count(Dao dao, String table, String condition)
Definition: DaoUtil.java:55
static Long count(Dao dao, WhereClause wc)
Definition: DaoUtil.java:65
static boolean isEmpty(Dao dao, String table)
Definition: DaoUtil.java:35
static Long count(Dao dao, String table)
Definition: DaoUtil.java:50
static boolean isEmpty(Dao dao, String table, String condition)
Definition: DaoUtil.java:40
static Long count(Dao dao, String table, String condition, Object... pars)
Definition: DaoUtil.java:60
static String getJson(Dao dao, Class javaClass, Object identifier, final JSONSerializer json)
Definition: DaoUtil.java:92
static boolean isEmpty(Dao dao, String table, String condition, Object... pars)
Definition: DaoUtil.java:45
static String getIdentifierFromPath(String e)
Definition: DaoUtil.java:88
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419