BrightSide Workbench Full Report + Source Code
IdUtils.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.elephant.db;
19 
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22 import org.turro.string.Strings;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.elephant.impl.util.StringParser;
25 import org.turro.jpa.Dao;
26 
31 public class IdUtils {
32 
33  public static long getNewLongIdFromLong(Dao dao, String table, String field) {
34  return getNewLongIdFromLong(dao, table, field, new String[] {});
35  }
36 
37  public static long getNewLongIdFromLong(Dao dao, String table, String field, String fieldConstraints[]) {
38  Long result = getMaxLongIdFromLong(dao, table, field, fieldConstraints, true);
39  if(result == 0) {
40  WhereClause wc = new WhereClause();
41  wc.addClause("select max(a." + field + ") + 1 from " + table + " a");
42  wc.addClause("where a." + field + " = (");
43  wc.addClause(" select count(*) from " + table + " b");
44  wc.addClause(" where " + putPrefix(field, "b.") + " <= " + putPrefix(field, "a."));
45  wc.addClause(" and " + putPrefix(field, "b.") + " > 0");
46  for(String fc : fieldConstraints) {
47  if(!Strings.isBlank(fc)) wc.addClause(" and " + putPrefix(fc, "b."));
48  }
49  wc.addClause(")");
50  for(String fc : fieldConstraints) {
51  if(!Strings.isBlank(fc)) wc.addClause(" and " + putPrefix(fc, "a."));
52  }
53  result = (Long) dao.getSingleResult(wc.getClause());
54  if(result == null || result == 0) {
55  result = getMaxLongIdFromLong(dao, table, field, fieldConstraints);
56  }
57  }
58  return result;
59  }
60 
61  public static long getNewLongIdFromString(Dao dao, String table, String field) {
62  return getNewLongIdFromString(dao, table, field, new String[] {});
63  }
64 
65  public static long getNewLongIdFromString(Dao dao, String table, String field, String fieldConstraints[]) {
66  try {
67  WhereClause wc = new WhereClause();
68  wc.addClause("select max(cast(a." + field + " as signed)) + 1 from " + table + " a");
69  wc.addClause("where cast(" + putPrefix(field, "a.") + " as signed) = (");
70  wc.addClause(" select count(*) from " + table + " b");
71  wc.addClause(" where cast(" + putPrefix(field, "b.") + " as signed) <= cast(" + putPrefix(field, "a.") + " as signed)");
72  wc.addClause(" and cast(" + putPrefix(field, "b.") + " as signed) > 0");
73  for(String fc : fieldConstraints) {
74  if(!Strings.isBlank(fc)) wc.addClause(" and " + putPrefix(fc, "b."));
75  }
76  wc.addClause(")");
77  for(String fc : fieldConstraints) {
78  if(!Strings.isBlank(fc)) wc.addClause(" and " + putPrefix(fc, "a."));
79  }
80  Long result = (Long) StringParser.convertToClass(Long.class, dao.getSingleNativeResult(wc.getClause()));
81  if(result == null || result == 0) {
82  result = getMaxLongIdFromString(dao, table, field, fieldConstraints);
83  }
84  return result;
85  } catch(Exception ex) {
86  Logger.getLogger(IdUtils.class.getName()).log(Level.INFO, ElephantContext.logMsg(null), ex);
87  return 1;
88  }
89  }
90 
91  public static long getMaxLongIdFromLong(Dao dao, String table, String field) {
92  return getMaxLongIdFromLong(dao, table, field, new String[] {});
93  }
94 
95  public static long getMaxLongIdFromLong(Dao dao, String table, String field, String fieldConstraints[]) {
96  return getMaxLongIdFromLong(dao, table, field, fieldConstraints, false);
97  }
98 
99  public static long getMaxLongIdFromLong(Dao dao, String table, String field, String fieldConstraints[], boolean count) {
100  WhereClause wc = new WhereClause();
101  wc.addClause("select count(a." + field + "), max(a." + field + ") from " + table + " a");
102  wc.addClause("where 1=1");
103  for(String fc : fieldConstraints) {
104  if(!Strings.isBlank(fc)) wc.addClause(" and " + putPrefix(fc, "a."));
105  }
106  Object result[] = (Object[]) dao.getSingleResult(wc.getClause());
107  Long regs = (Long) result[0], id = (Long) result[1];
108  if(id == null || id == 0) {
109  regs = 0L;
110  id = 0L;
111  }
112  return count ? (regs.equals(id) ? id + 1L : 0L) : id + 1L;
113  }
114 
115  public static long getMaxLongIdFromString(Dao dao, String table, String field) {
116  return getMaxLongIdFromString(dao, table, field, new String[] {});
117  }
118 
119  public static long getMaxLongIdFromString(Dao dao, String table, String field, String fieldConstraints[]) {
120  try {
121  WhereClause wc = new WhereClause();
122  wc.addClause("select max(cast(a." + field + " as signed)) from " + table + " a");
123  wc.addClause("where 1=1");
124  for(String fc : fieldConstraints) {
125  if(!Strings.isBlank(fc)) wc.addClause(" and " + putPrefix(fc, "a."));
126  }
127  Long result = (Long) StringParser.convertToClass(Long.class, dao.getSingleNativeResult(wc.getClause()));
128  if(result == null || result == 0) {
129  result = Long.valueOf(0);
130  }
131  return result + 1;
132  } catch(Exception ex) {
133  Logger.getLogger(IdUtils.class.getName()).log(Level.INFO, ElephantContext.logMsg(null), ex);
134  return 1;
135  }
136  }
137 
138  public static String putPrefix(String field, String prefix) {
139  int p = Math.max(field.lastIndexOf("("), 0);
140  if(p > 0) {
141  field = field.substring(0, p + 1) + prefix + field.substring(p + 1);
142  } else {
143  field = prefix + field;
144  }
145  return field;
146  }
147 
148  private IdUtils() {
149  }
150 
151 }
static String putPrefix(String field, String prefix)
Definition: IdUtils.java:138
static long getNewLongIdFromLong(Dao dao, String table, String field)
Definition: IdUtils.java:33
static long getNewLongIdFromLong(Dao dao, String table, String field, String fieldConstraints[])
Definition: IdUtils.java:37
static long getMaxLongIdFromLong(Dao dao, String table, String field, String fieldConstraints[])
Definition: IdUtils.java:95
static long getMaxLongIdFromLong(Dao dao, String table, String field)
Definition: IdUtils.java:91
static long getMaxLongIdFromString(Dao dao, String table, String field, String fieldConstraints[])
Definition: IdUtils.java:119
static long getNewLongIdFromString(Dao dao, String table, String field, String fieldConstraints[])
Definition: IdUtils.java:65
static long getMaxLongIdFromLong(Dao dao, String table, String field, String fieldConstraints[], boolean count)
Definition: IdUtils.java:99
static long getMaxLongIdFromString(Dao dao, String table, String field)
Definition: IdUtils.java:115
static long getNewLongIdFromString(Dao dao, String table, String field)
Definition: IdUtils.java:61
static Object convertToClass(Class javaClass, Object value)
Object getSingleNativeResult(String query)
Definition: Dao.java:447
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380