franta-hg@20
|
1 |
package cz.frantovo.superDAO;
|
franta-hg@20
|
2 |
|
franta-hg@20
|
3 |
import java.io.InputStream;
|
franta-hg@20
|
4 |
import java.sql.Connection;
|
franta-hg@20
|
5 |
import java.sql.ResultSet;
|
franta-hg@23
|
6 |
import java.sql.SQLException;
|
franta-hg@20
|
7 |
import java.sql.Statement;
|
franta-hg@20
|
8 |
import java.util.Collections;
|
franta-hg@20
|
9 |
import java.util.HashMap;
|
franta-hg@20
|
10 |
import java.util.Map;
|
franta-hg@20
|
11 |
import java.util.Properties;
|
franta-hg@20
|
12 |
import java.util.logging.Level;
|
franta-hg@20
|
13 |
import java.util.logging.Logger;
|
franta-hg@20
|
14 |
|
franta-hg@20
|
15 |
/**
|
franta-hg@20
|
16 |
* Předek všech DTOček
|
franta-hg@20
|
17 |
* @author fiki
|
franta-hg@20
|
18 |
*/
|
franta-hg@20
|
19 |
public class SuperDAO {
|
franta-hg@20
|
20 |
|
franta-hg@20
|
21 |
protected Logger log = Logger.getLogger(getClass().getName());
|
franta-hg@20
|
22 |
private static Map<Class, Properties> sql = Collections.synchronizedMap(new HashMap<Class, Properties>());
|
franta-hg@20
|
23 |
private static Map<Class, Properties> vlastnosti = Collections.synchronizedMap(new HashMap<Class, Properties>());
|
franta-hg@20
|
24 |
|
franta-hg@20
|
25 |
private enum VLASTNOSTI {
|
franta-hg@20
|
26 |
|
franta-hg@23
|
27 |
AHOJ
|
franta-hg@20
|
28 |
}
|
franta-hg@20
|
29 |
|
franta-hg@20
|
30 |
/**
|
franta-hg@20
|
31 |
* Načítá SQL dotazy ze souboru, který se jmenuje stejně jako třída a má příponu sql.xml
|
franta-hg@20
|
32 |
* Do těchto souborů patří pouze SQL dotazy - všechny ostatní parametry dáme do .xml souboru.
|
franta-hg@20
|
33 |
* <p><strong>Pozor</strong> na použití v <em>protected</em> metodách, které mohou být volány z potomků –
|
franta-hg@20
|
34 |
* pak se použije XML soubor daného potomka a ne XML soubor třídy, ve které se <em>protected</em> metoda nachází.
|
franta-hg@20
|
35 |
* V takovém případě je potřeba použít parametr <em>trida</em></p>
|
franta-hg@20
|
36 |
* @param klic Enum konstnanta z dané třídy
|
franta-hg@20
|
37 |
* @param trida třída, jejíž XML soubor budeme hledat.
|
franta-hg@20
|
38 |
* @return SQL dotaz nebo null v případě chyby
|
franta-hg@20
|
39 |
*/
|
franta-hg@20
|
40 |
protected String getSQL(Enum klic, Class trida) {
|
franta-hg@23
|
41 |
return getHodnota(sql, "sql.xml", klic, trida);
|
franta-hg@20
|
42 |
}
|
franta-hg@20
|
43 |
|
franta-hg@20
|
44 |
/**
|
franta-hg@20
|
45 |
* @see getSQL(Enum klic, Class trida)
|
franta-hg@20
|
46 |
*/
|
franta-hg@20
|
47 |
protected String getSQL(Enum klic) {
|
franta-hg@23
|
48 |
return getSQL(klic, null);
|
franta-hg@20
|
49 |
}
|
franta-hg@20
|
50 |
|
franta-hg@20
|
51 |
/**
|
franta-hg@20
|
52 |
* Načítá textové hodnoty (vlastnosti) ze souboru, který se jmenuje stejně jako třída a má příponu xml
|
franta-hg@20
|
53 |
* Sem patří všechny parametry tříd kromě SQL dotazů.
|
franta-hg@20
|
54 |
* <p><strong>Pozor</strong> na použití v <code>protected</code> metodách, které mohou být volány z potomků –
|
franta-hg@20
|
55 |
* pak se použije XML soubor daného potomka a ne XML soubor třídy, ve které se <code>protected</code> metoda nachází.
|
franta-hg@20
|
56 |
* V takovém případě je potřeba použít parametr <code>trida</code></p>
|
franta-hg@20
|
57 |
* @param klic Enum konstnanta z dané třídy
|
franta-hg@20
|
58 |
* @param trida třída, jejíž XML soubor budeme hledat.
|
franta-hg@20
|
59 |
* @return textová hodnota nebo null v případě chyby
|
franta-hg@20
|
60 |
*/
|
franta-hg@20
|
61 |
protected String getVlastnost(Enum klic, Class trida) {
|
franta-hg@23
|
62 |
return getHodnota(vlastnosti, "xml", klic, trida);
|
franta-hg@20
|
63 |
}
|
franta-hg@20
|
64 |
|
franta-hg@20
|
65 |
/**
|
franta-hg@20
|
66 |
* @see getVlastnost(Enum klic, Class trida)
|
franta-hg@20
|
67 |
*/
|
franta-hg@20
|
68 |
protected String getVlastnost(Enum klic) {
|
franta-hg@23
|
69 |
return getVlastnost(klic, null);
|
franta-hg@20
|
70 |
}
|
franta-hg@20
|
71 |
|
franta-hg@20
|
72 |
/**
|
franta-hg@20
|
73 |
* Nečte hodnoty přímo ze souboru, ale snaží se je nejprve najít ve statické mezipaměti.
|
franta-hg@20
|
74 |
* @param klic Enum konstnanta z dané třídy
|
franta-hg@20
|
75 |
* @return textová hodnota nebo null v případě chyby
|
franta-hg@20
|
76 |
*/
|
franta-hg@20
|
77 |
private String getHodnota(Map<Class, Properties> mezipamet, String pripona, Enum klic, Class trida) {
|
franta-hg@20
|
78 |
|
franta-hg@23
|
79 |
if (trida == null) {
|
franta-hg@23
|
80 |
trida = getClass();
|
franta-hg@23
|
81 |
}
|
franta-hg@20
|
82 |
|
franta-hg@23
|
83 |
if (mezipamet.get(trida) == null) {
|
franta-hg@23
|
84 |
/** Ještě nemáme načteno ze souboru */
|
franta-hg@23
|
85 |
mezipamet.put(trida, getHodnoty(pripona, trida));
|
franta-hg@20
|
86 |
|
franta-hg@23
|
87 |
if (mezipamet.get(trida) == null) {
|
franta-hg@23
|
88 |
/** Došlo k chybě --> už to příště nebudeme zkoušet načítat */
|
franta-hg@23
|
89 |
mezipamet.put(trida, new Properties());
|
franta-hg@23
|
90 |
}
|
franta-hg@23
|
91 |
}
|
franta-hg@20
|
92 |
|
franta-hg@23
|
93 |
return mezipamet.get(trida).getProperty(klic.toString());
|
franta-hg@20
|
94 |
}
|
franta-hg@20
|
95 |
|
franta-hg@20
|
96 |
/**
|
franta-hg@20
|
97 |
* Jen pro vyzkoušení, jak načítání XML funguje.
|
franta-hg@20
|
98 |
* @param args prázdné
|
franta-hg@20
|
99 |
*/
|
franta-hg@20
|
100 |
public static void main(String[] args) {
|
franta-hg@23
|
101 |
System.out.println(new SuperDAO().getSQL(VLASTNOSTI.AHOJ));
|
franta-hg@20
|
102 |
}
|
franta-hg@20
|
103 |
|
franta-hg@20
|
104 |
/**
|
franta-hg@20
|
105 |
* Načte vlastnosti dané třídy z XML souboru.
|
franta-hg@20
|
106 |
* Soubory se dávají do stejného balíčku jako třída, akorát mají jinou příponu.
|
franta-hg@20
|
107 |
* @param pripona přípona souboru - obvykle "sql.xml" nebo "xml" (bez tečky)
|
franta-hg@20
|
108 |
* @return načtené vlastnosti nebo null v případě chyby
|
franta-hg@20
|
109 |
*/
|
franta-hg@20
|
110 |
private Properties getHodnoty(String pripona, Class trida) {
|
franta-hg@23
|
111 |
String soubor = trida.getSimpleName() + "." + pripona;
|
franta-hg@20
|
112 |
|
franta-hg@23
|
113 |
try {
|
franta-hg@23
|
114 |
Properties hodnoty = new Properties();
|
franta-hg@23
|
115 |
InputStream fis = trida.getResourceAsStream(soubor);
|
franta-hg@23
|
116 |
hodnoty.loadFromXML(fis);
|
franta-hg@23
|
117 |
return hodnoty;
|
franta-hg@23
|
118 |
} catch (Exception e) {
|
franta-hg@23
|
119 |
log.log(Level.SEVERE, "Chyba při načítání vlastností: " + soubor, e);
|
franta-hg@23
|
120 |
return null;
|
franta-hg@23
|
121 |
}
|
franta-hg@23
|
122 |
}
|
franta-hg@23
|
123 |
|
franta-hg@23
|
124 |
/**
|
franta-hg@23
|
125 |
* Pokusí se zrušit transakci.
|
franta-hg@23
|
126 |
* @param spojeni databázové spojení.
|
franta-hg@23
|
127 |
*/
|
franta-hg@23
|
128 |
protected void rollback(Connection spojeni) {
|
franta-hg@23
|
129 |
try {
|
franta-hg@23
|
130 |
spojeni.rollback();
|
franta-hg@23
|
131 |
} catch (SQLException e) {
|
franta-hg@23
|
132 |
log.log(Level.FINEST, "rollback se nezdařil", e);
|
franta-hg@23
|
133 |
}
|
franta-hg@20
|
134 |
}
|
franta-hg@20
|
135 |
|
franta-hg@20
|
136 |
/**
|
franta-hg@20
|
137 |
* Zavře všechno
|
franta-hg@20
|
138 |
* @param spojeni DB spojení
|
franta-hg@20
|
139 |
* @param prikaz DB dotaz
|
franta-hg@20
|
140 |
* @param vysledek DB výsledek
|
franta-hg@20
|
141 |
*/
|
franta-hg@20
|
142 |
protected static void zavri(Connection spojeni, Statement prikaz, ResultSet vysledek) {
|
franta-hg@23
|
143 |
if (vysledek != null) {
|
franta-hg@23
|
144 |
try {
|
franta-hg@23
|
145 |
vysledek.close();
|
franta-hg@23
|
146 |
} catch (Exception e) {
|
franta-hg@23
|
147 |
}
|
franta-hg@23
|
148 |
}
|
franta-hg@23
|
149 |
if (prikaz != null) {
|
franta-hg@23
|
150 |
try {
|
franta-hg@23
|
151 |
prikaz.close();
|
franta-hg@23
|
152 |
} catch (Exception e) {
|
franta-hg@23
|
153 |
}
|
franta-hg@23
|
154 |
}
|
franta-hg@23
|
155 |
if (spojeni != null) {
|
franta-hg@23
|
156 |
try {
|
franta-hg@23
|
157 |
spojeni.close();
|
franta-hg@23
|
158 |
} catch (Exception e) {
|
franta-hg@23
|
159 |
}
|
franta-hg@23
|
160 |
}
|
franta-hg@20
|
161 |
}
|
franta-hg@20
|
162 |
|
franta-hg@20
|
163 |
/**
|
franta-hg@20
|
164 |
* Vhodné pokud v XML používáme <![CDATA[ … ]]>.
|
franta-hg@20
|
165 |
* (hodí se pro JNDI)
|
franta-hg@20
|
166 |
* @param text původní řetězec načtený z XML souboru.
|
franta-hg@20
|
167 |
* @return Text s oříznutými bílými znany nazačátku a nakonci.
|
franta-hg@20
|
168 |
*/
|
franta-hg@20
|
169 |
public static String orizni(String text) {
|
franta-hg@23
|
170 |
if (text == null) {
|
franta-hg@23
|
171 |
return null;
|
franta-hg@23
|
172 |
} else {
|
franta-hg@23
|
173 |
return text.trim();
|
franta-hg@23
|
174 |
}
|
franta-hg@20
|
175 |
}
|
franta-hg@23
|
176 |
}
|