commit | author | age
|
ceb15c
|
1 |
/* |
U |
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package de.uhilger.radiozentrale.api; |
|
7 |
|
|
8 |
import de.uhilger.baselink.GenericRecord; |
|
9 |
import de.uhilger.baselink.PersistenceManager; |
74f15b
|
10 |
import de.uhilger.radiozentrale.daten.Abspieler; |
U |
11 |
import de.uhilger.radiozentrale.daten.Sender; |
|
12 |
import de.uhilger.radiozentrale.web.Initialiser; |
|
13 |
import java.net.HttpURLConnection; |
|
14 |
import java.net.URL; |
ceb15c
|
15 |
import java.util.List; |
74f15b
|
16 |
import java.util.logging.Level; |
U |
17 |
import java.util.logging.Logger; |
ceb15c
|
18 |
|
U |
19 |
/** |
|
20 |
* |
|
21 |
*/ |
|
22 |
public abstract class DbApi extends Api { |
|
23 |
|
74f15b
|
24 |
private static final Logger logger = Logger.getLogger(DbApi.class.getName()); |
U |
25 |
|
ceb15c
|
26 |
public static final String SQL_GET_NEXT_KEY = "getNextKey"; |
U |
27 |
public static final String SQL_INCREMENT_KEY = "incrementKey"; |
74f15b
|
28 |
public static final String SQL_GET_ABSPIELER = "getAbspieler"; |
U |
29 |
public static final String SQL_GET_SENDER = "getSender"; |
ceb15c
|
30 |
|
U |
31 |
protected int getNextId(PersistenceManager db, String key) { |
|
32 |
int nextKey = -1; |
|
33 |
List<List<String>> list = db.select(getSql(SQL_GET_NEXT_KEY), GenericRecord.WITHOUT_BLOBS, key); |
|
34 |
if(list != null && list.size() > 1) { |
|
35 |
nextKey = Integer.parseInt(list.get(1).get(0)); // erster Datensatz ist Ueberschrift |
|
36 |
if(nextKey > -1) { |
|
37 |
db.execute(getSql(SQL_INCREMENT_KEY), nextKey+1, key, nextKey); |
|
38 |
} |
|
39 |
} |
|
40 |
return nextKey; |
|
41 |
} |
74f15b
|
42 |
|
U |
43 |
protected Abspieler getAbspieler(int id) { |
|
44 |
Abspieler a = null; |
|
45 |
List list = getDb().select(getSql(SQL_GET_ABSPIELER), getMapper(Initialiser.MP_ABSPIELER), true, id); |
|
46 |
if(list != null && list.size() > 0) { |
|
47 |
Object o = list.get(0); |
|
48 |
if(o instanceof Abspieler) { |
|
49 |
a = (Abspieler) o; |
|
50 |
} |
|
51 |
} |
|
52 |
return a; |
|
53 |
} |
|
54 |
|
|
55 |
protected Sender getSender(int id) { |
|
56 |
Sender s = null; |
|
57 |
List list = getDb().select(getSql(SQL_GET_SENDER), getMapper(Initialiser.MP_SENDER), true, id); |
|
58 |
if(list != null && list.size() > 0) { |
|
59 |
Object o = list.get(0); |
|
60 |
if(o instanceof Sender) { |
|
61 |
s = (Sender) o; |
|
62 |
} |
|
63 |
} |
|
64 |
return s; |
|
65 |
} |
|
66 |
|
|
67 |
protected String abspielerKommandoSenden(String kommando) { |
|
68 |
String antwort = null; |
|
69 |
logger.info(kommando); |
|
70 |
try { |
|
71 |
HttpURLConnection conn = (HttpURLConnection) new URL(kommando).openConnection(); |
|
72 |
conn.setRequestMethod("GET"); |
|
73 |
conn.connect(); |
|
74 |
int status = conn.getResponseCode(); |
|
75 |
String msg = conn.getResponseMessage(); |
|
76 |
antwort = "Kommando " + kommando + " mit Status " + status + " " + msg + " gesendet."; |
|
77 |
logger.log(Level.INFO, antwort); |
|
78 |
} catch(Exception ex) { |
|
79 |
logger.log(Level.INFO, ex.getMessage(), ex); |
|
80 |
} |
|
81 |
return antwort; |
|
82 |
} |
ceb15c
|
83 |
} |