commit | author | age
|
a0ec7b
|
1 |
/* |
U |
2 |
* Radiozentrale - Webradio App |
|
3 |
* Copyright (C) 2018 Ulrich Hilger, http://uhilger.de |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU 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 General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see http://www.gnu.org/licenses/ |
|
17 |
*/ |
|
18 |
|
|
19 |
package de.uhilger.radiozentrale.api; |
|
20 |
|
|
21 |
import de.uhilger.baselink.GenericRecord; |
|
22 |
import de.uhilger.baselink.PersistenceManager; |
|
23 |
import de.uhilger.radiozentrale.daten.Sender; |
|
24 |
import de.uhilger.radiozentrale.web.Initialiser; |
|
25 |
import java.sql.Connection; |
|
26 |
import java.util.List; |
|
27 |
|
|
28 |
/** |
|
29 |
* |
|
30 |
*/ |
|
31 |
public class SenderApi extends Api { |
|
32 |
|
|
33 |
public static final String SQL_GET_SENDER = "getSender"; |
|
34 |
public static final String SQL_GET_NEXT_KEY = "getNextKey"; |
|
35 |
public static final String SQL_INCREMENT_KEY = "incrementKey"; |
|
36 |
|
|
37 |
public static final String KEY_SENDER_ID = "sender_id"; |
|
38 |
|
|
39 |
|
|
40 |
public Sender neuerSender(Sender sender) { |
|
41 |
Sender neuerSender = null; |
|
42 |
PersistenceManager db = getDb(); |
|
43 |
Connection c = db.getConnection(); |
b1aeea
|
44 |
db.startTransaction(c); |
U |
45 |
int nextKey = getNextId(db, KEY_SENDER_ID); |
|
46 |
if(nextKey > -1) { |
a0ec7b
|
47 |
sender.setId(nextKey); |
U |
48 |
Object o = getDb().insert(sender, getMapper(Initialiser.MP_SENDER)); |
|
49 |
if(o instanceof Sender) { |
|
50 |
neuerSender = (Sender) o; |
8c352d
|
51 |
db.commit(c); |
U |
52 |
} else { |
|
53 |
db.rollback(c); |
a0ec7b
|
54 |
} |
U |
55 |
} |
|
56 |
return neuerSender; |
|
57 |
} |
|
58 |
|
|
59 |
public List senderliste() { |
8c352d
|
60 |
return getDb().select(getSql(SQL_GET_SENDER), getMapper(Initialiser.MP_SENDER)); |
a0ec7b
|
61 |
} |
b1aeea
|
62 |
|
U |
63 |
private int getNextId(PersistenceManager db, String key) { |
|
64 |
int nextKey = -1; |
|
65 |
List<List<String>> list = db.select(getSql(SQL_GET_NEXT_KEY), GenericRecord.WITHOUT_BLOBS, key); |
|
66 |
if(list != null && list.size() > 1) { |
|
67 |
nextKey = Integer.parseInt(list.get(1).get(0)); // erster Datensatz ist Ueberschrift |
|
68 |
if(nextKey > -1) { |
|
69 |
db.execute(getSql(SQL_INCREMENT_KEY), nextKey+1, key, nextKey); |
|
70 |
} |
|
71 |
} |
|
72 |
return nextKey; |
|
73 |
} |
a0ec7b
|
74 |
} |