BrightSide Workbench Full Report + Source Code
ElementDB.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.io.PrintWriter;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.Collection;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.context.IElement;
31 import org.turro.elephant.layout.IManageable;
32 
37 public class ElementDB {
38  protected IElement element;
39  protected Connection conn = null;
40  protected int lastFirst, lastCurrent, lastCount, totalCount;
41 
47  this.element = element;
48  }
49 
50  public Connection getConnection() throws SQLException {
51  if(conn == null || conn.isClosed()) {
52  String driverClass = ((IManageable)element).getAttributes().getAttributeValue("attrib:jdbc-driver", null),
53  url = ((IManageable)element).getAttributes().getAttributeValue("attrib:jdbc-url", null),
54  user = ((IManageable)element).getAttributes().getAttributeValue("attrib:jdbc-user", null),
55  password = ((IManageable)element).getAttributes().getAttributeValue("attrib:jdbc-password", null);
56  if(driverClass != null && url != null) {
57  // TODO: optimize
58  url = url.replaceAll("\\#idel", element.getId());
59  url = url.replaceAll("\\#ctx",
62  ));
63  url = url.replaceAll("\\#root", ElephantContext.getRealPath("/"));
64  url = url.replaceAll("\\\\","/");
65  try {
66  Class.forName(driverClass);
67  if(user != null && user.length() > 0 && password != null) {
68  conn = DriverManager.getConnection(url, user, password);
69  }
70  else {
71  conn = DriverManager.getConnection(url);
72  }
73  return conn;
74  } catch (ClassNotFoundException | SQLException ex) {
75  Logger.getLogger(ElementDB.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
76  return null;
77  }
78  }
79  return null;
80  }
81  return conn;
82  }
83 
84  public boolean positionFirst(ResultSet rs, int first) throws SQLException {
85  lastFirst = first;
86  boolean hasNext = true;
87  int current = 0;
88  while((current < first) && hasNext) {
89  current++;
90  hasNext = rs.next();
91  }
92  return hasNext;
93  }
94 
95  public boolean iterate(ResultSet rs, int count, int current) throws SQLException {
96  lastCount = count;
97  lastCurrent = current;
98  if(current - lastFirst < count) {
99  return rs.next();
100  }
101  return false;
102  }
103 
104  public int findLast(ResultSet rs) throws SQLException {
105  totalCount = lastCurrent;
106  while(rs.next()) {
107  totalCount++;
108  }
109  return totalCount;
110  }
111 
112  public ResultSet query(String sql) throws SQLException {
113  Connection con = getConnection();
114  return con.createStatement().executeQuery(sql);
115  }
116 
117  public ResultSet query(String sql, Collection params) throws SQLException {
118  return query(sql, params.toArray());
119  }
120 
121  public ResultSet query(String sql, Object[] params) throws SQLException {
122  if(params == null) {
123  return query(sql);
124  }
125  Connection con = getConnection();
126  PreparedStatement ps = con.prepareStatement(sql);
127  for(int i = 0; i < params.length; i++) {
128  if(params[i] != null) {
129  ps.setObject(i + 1, params[i]);
130  }
131  else {
132  ps.setNull(i + 1, java.sql.Types.VARCHAR);
133  }
134  }
135  return ps.executeQuery();
136  }
137 
138  public int execute(String sql) throws SQLException {
139  Connection con = getConnection();
140  return con.createStatement().executeUpdate(sql);
141  }
142 
143  public int execute(String sql, Collection params) throws SQLException {
144  return execute(sql, params.toArray());
145  }
146 
147  public int execute(String sql, Object[] params) throws SQLException {
148  if(params == null) {
149  return execute(sql);
150  }
151  Connection con = getConnection();
152  PreparedStatement ps = con.prepareStatement(sql);
153  for(int i = 0; i < params.length; i++) {
154  if(params[i] != null) {
155  ps.setObject(i + 1, params[i]);
156  }
157  else {
158  ps.setNull(i + 1, java.sql.Types.VARCHAR);
159  }
160  }
161  return ps.executeUpdate();
162  }
163 
164  public void execute(String commands[]) throws SQLException {
165  for (String command : commands) {
166  execute(command);
167  }
168  }
169 
170  public void close(ResultSet rs) throws SQLException {
171  rs.close();
172  }
173 
174  public void close() throws SQLException {
175  if(conn != null && !conn.isClosed())
176  conn.close();
177  }
178 
179  public void renderNavigator(PrintWriter out, String param) {
180  if(totalCount <= lastCount) return;
181  out.print("<div class='dbnav'>");
182  out.print("<ul>");
183  if(lastFirst > 0) {
184  out.print("<li>");
185  out.print("<a href='?" + param + "=" + (lastFirst - lastCount) + "'>");
186  out.print("&lt;&lt;");
187  out.print("</a>");
188  out.print("</li>");
189  }
190  int iter = (int)Math.ceil(((double)totalCount) / ((double)lastCount));
191  for(int i = 0; i < iter; i++) {
192  if(lastFirst >= i * lastCount && lastFirst < (i+1) * lastCount) {
193  out.print("<li class='active'>");
194  }
195  else {
196  out.print("<li>");
197  }
198  out.print("<a href='?" + param + "=" + (i * lastCount) + "'>");
199  out.print(i + 1);
200  out.print("</a>");
201  out.print("</li>");
202  }
203  if(lastFirst + lastCount < totalCount) {
204  out.print("<li>");
205  out.print("<a href='?" + param + "=" + (lastFirst + lastCount) + "'>");
206  out.print("&gt;&gt;");
207  out.print("</a>");
208  out.print("</li>");
209  }
210  out.print("</ul>");
211  out.print("</div>");
212  }
213 
214  public java.util.Date toDate(java.sql.Timestamp date) {
215  if(date == null) return null;
216  return new java.util.Date(date.getTime());
217  }
218 
219  public java.sql.Timestamp toTimestamp(java.util.Date date) {
220  if(date == null) return null;
221  return new java.sql.Timestamp(date.getTime());
222  }
223 
224  public java.util.Date toDate(java.sql.Date date) {
225  if(date == null) return null;
226  return new java.util.Date(date.getTime());
227  }
228 
229  public java.sql.Date toSqlDate(java.util.Date date) {
230  if(date == null) return null;
231  return new java.sql.Date(date.getTime());
232  }
233 
234 }
void execute(String commands[])
Definition: ElementDB.java:164
ElementDB(IElement element)
Definition: ElementDB.java:46
void renderNavigator(PrintWriter out, String param)
Definition: ElementDB.java:179
ResultSet query(String sql, Collection params)
Definition: ElementDB.java:117
java.sql.Timestamp toTimestamp(java.util.Date date)
Definition: ElementDB.java:219
ResultSet query(String sql)
Definition: ElementDB.java:112
boolean positionFirst(ResultSet rs, int first)
Definition: ElementDB.java:84
boolean iterate(ResultSet rs, int count, int current)
Definition: ElementDB.java:95
int execute(String sql, Object[] params)
Definition: ElementDB.java:147
java.util.Date toDate(java.sql.Date date)
Definition: ElementDB.java:224
ResultSet query(String sql, Object[] params)
Definition: ElementDB.java:121
java.sql.Date toSqlDate(java.util.Date date)
Definition: ElementDB.java:229
java.util.Date toDate(java.sql.Timestamp date)
Definition: ElementDB.java:214
int execute(String sql, Collection params)
Definition: ElementDB.java:143