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