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@0
|
24 |
private enum SQL {
|
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@0
|
32 |
* @param klic Enum konstnanta z dané třídy
|
franta-hg@0
|
33 |
* @return SQL dotaz nebo null v případě chyby
|
franta-hg@0
|
34 |
*/
|
franta-hg@0
|
35 |
protected String getSQL(Enum klic) {
|
franta-hg@0
|
36 |
|
franta-hg@0
|
37 |
|
franta-hg@0
|
38 |
|
franta-hg@0
|
39 |
return getHodnota(sql, "sql.xml", klic);
|
franta-hg@0
|
40 |
}
|
franta-hg@0
|
41 |
|
franta-hg@0
|
42 |
/**
|
franta-hg@0
|
43 |
* Načítá textové hodnoty (vlastnosti) ze souboru, který se jmenuje stejně jako třída a má příponu xml
|
franta-hg@0
|
44 |
* Sem patří všechny parametry tříd kromě SQL dotazů.
|
franta-hg@0
|
45 |
* @param klic Enum konstnanta z dané třídy
|
franta-hg@0
|
46 |
* @return textová hodnota nebo null v případě chyby
|
franta-hg@0
|
47 |
*/
|
franta-hg@0
|
48 |
protected String getVlastnost(Enum klic) {
|
franta-hg@0
|
49 |
return getHodnota(vlastnosti, "xml", klic);
|
franta-hg@0
|
50 |
}
|
franta-hg@0
|
51 |
|
franta-hg@0
|
52 |
/**
|
franta-hg@0
|
53 |
* Nečte hodnoty přímo ze souboru, ale snaží se je nejprve najít ve statické mezipaměti.
|
franta-hg@0
|
54 |
* @param klic Enum konstnanta z dané třídy
|
franta-hg@0
|
55 |
* @return textová hodnota nebo null v případě chyby
|
franta-hg@0
|
56 |
*/
|
franta-hg@0
|
57 |
private String getHodnota(Map<Class, Properties> mezipamet, String pripona, Enum klic) {
|
franta-hg@0
|
58 |
|
franta-hg@0
|
59 |
if (mezipamet.get(getClass()) == null) {
|
franta-hg@0
|
60 |
/** Ještě nemáme načteno ze souboru */
|
franta-hg@0
|
61 |
mezipamet.put(getClass(), getHodnoty(pripona));
|
franta-hg@0
|
62 |
|
franta-hg@0
|
63 |
if (mezipamet.get(getClass()) == null) {
|
franta-hg@0
|
64 |
/** Došlo k chybě --> už to příště nebudeme zkoušet načítat */
|
franta-hg@0
|
65 |
mezipamet.put(getClass(), new Properties());
|
franta-hg@0
|
66 |
}
|
franta-hg@0
|
67 |
}
|
franta-hg@0
|
68 |
|
franta-hg@0
|
69 |
return mezipamet.get(getClass()).getProperty(klic.toString());
|
franta-hg@0
|
70 |
}
|
franta-hg@0
|
71 |
|
franta-hg@0
|
72 |
public static void main(String[] args) {
|
franta-hg@0
|
73 |
System.out.println(new SuperDAO().getSQL(SQL.AHOJ));
|
franta-hg@0
|
74 |
|
franta-hg@0
|
75 |
}
|
franta-hg@0
|
76 |
|
franta-hg@0
|
77 |
/**
|
franta-hg@0
|
78 |
* Načte vlastnosti dané třídy z XML souboru.
|
franta-hg@0
|
79 |
* Soubory se dávají do stejného balíčku jako třída, akorát mají jinou příponu.
|
franta-hg@0
|
80 |
* @param pripona přípona souboru - obvykle "sql.xml" nebo "xml" (bez tečky)
|
franta-hg@0
|
81 |
* @return načtené vlastnosti nebo null v případě chyby
|
franta-hg@0
|
82 |
*/
|
franta-hg@0
|
83 |
private Properties getHodnoty(String pripona) {
|
franta-hg@0
|
84 |
Class trida = getClass();
|
franta-hg@0
|
85 |
String soubor = trida.getSimpleName() + "." + pripona;
|
franta-hg@0
|
86 |
|
franta-hg@0
|
87 |
Properties hodnoty = null;
|
franta-hg@0
|
88 |
try {
|
franta-hg@0
|
89 |
InputStream fis = trida.getResourceAsStream(soubor);
|
franta-hg@0
|
90 |
hodnoty = new Properties();
|
franta-hg@0
|
91 |
hodnoty.loadFromXML(fis);
|
franta-hg@0
|
92 |
} catch (Exception e) {
|
franta-hg@0
|
93 |
log.log(Level.SEVERE, "Chyba při načítání vlastností: " + soubor, e);
|
franta-hg@0
|
94 |
}
|
franta-hg@0
|
95 |
|
franta-hg@0
|
96 |
return hodnoty;
|
franta-hg@0
|
97 |
}
|
franta-hg@0
|
98 |
|
franta-hg@0
|
99 |
/**
|
franta-hg@0
|
100 |
* Zavře všechno
|
franta-hg@0
|
101 |
* @param spojeni DB spojení
|
franta-hg@0
|
102 |
* @param prikaz DB dotaz
|
franta-hg@0
|
103 |
* @param vysledek DB výsledek
|
franta-hg@0
|
104 |
*/
|
franta-hg@0
|
105 |
protected void zavri(Connection spojeni, Statement prikaz, ResultSet vysledek) {
|
franta-hg@0
|
106 |
if (vysledek != null) {
|
franta-hg@0
|
107 |
try {
|
franta-hg@0
|
108 |
vysledek.close();
|
franta-hg@0
|
109 |
} catch (Exception e) {
|
franta-hg@0
|
110 |
}
|
franta-hg@0
|
111 |
}
|
franta-hg@0
|
112 |
if (prikaz != null) {
|
franta-hg@0
|
113 |
try {
|
franta-hg@0
|
114 |
prikaz.close();
|
franta-hg@0
|
115 |
} catch (Exception e) {
|
franta-hg@0
|
116 |
}
|
franta-hg@0
|
117 |
}
|
franta-hg@0
|
118 |
if (spojeni != null) {
|
franta-hg@0
|
119 |
try {
|
franta-hg@0
|
120 |
spojeni.close();
|
franta-hg@0
|
121 |
} catch (Exception e) {
|
franta-hg@0
|
122 |
}
|
franta-hg@0
|
123 |
}
|
franta-hg@0
|
124 |
}
|
franta-hg@0
|
125 |
} |