/*
|
* To change this license header, choose License Headers in Project Properties.
|
* To change this template file, choose Tools | Templates
|
* and open the template in the editor.
|
*/
|
package de.uhilger.radiozentrale.api;
|
|
import de.uhilger.baselink.GenericRecord;
|
import de.uhilger.baselink.PersistenceManager;
|
import de.uhilger.radiozentrale.daten.Abspieler;
|
import de.uhilger.radiozentrale.daten.Sender;
|
import de.uhilger.radiozentrale.web.Initialiser;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.List;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
|
/**
|
*
|
*/
|
public abstract class DbApi extends Api {
|
|
private static final Logger logger = Logger.getLogger(DbApi.class.getName());
|
|
public static final String SQL_GET_NEXT_KEY = "getNextKey";
|
public static final String SQL_INCREMENT_KEY = "incrementKey";
|
public static final String SQL_GET_ABSPIELER = "getAbspieler";
|
public static final String SQL_GET_SENDER = "getSender";
|
|
protected int getNextId(PersistenceManager db, String key) {
|
int nextKey = -1;
|
List<List<String>> list = db.select(getSql(SQL_GET_NEXT_KEY), GenericRecord.WITHOUT_BLOBS, key);
|
if(list != null && list.size() > 1) {
|
nextKey = Integer.parseInt(list.get(1).get(0)); // erster Datensatz ist Ueberschrift
|
if(nextKey > -1) {
|
db.execute(getSql(SQL_INCREMENT_KEY), nextKey+1, key, nextKey);
|
}
|
}
|
return nextKey;
|
}
|
|
protected Abspieler getAbspieler(int id) {
|
Abspieler a = null;
|
List list = getDb().select(getSql(SQL_GET_ABSPIELER), getMapper(Initialiser.MP_ABSPIELER), true, id);
|
if(list != null && list.size() > 0) {
|
Object o = list.get(0);
|
if(o instanceof Abspieler) {
|
a = (Abspieler) o;
|
}
|
}
|
return a;
|
}
|
|
protected Sender getSender(int id) {
|
Sender s = null;
|
List list = getDb().select(getSql(SQL_GET_SENDER), getMapper(Initialiser.MP_SENDER), true, id);
|
if(list != null && list.size() > 0) {
|
Object o = list.get(0);
|
if(o instanceof Sender) {
|
s = (Sender) o;
|
}
|
}
|
return s;
|
}
|
|
protected String abspielerKommandoSenden(String kommando) {
|
String antwort = null;
|
logger.info(kommando);
|
try {
|
HttpURLConnection conn = (HttpURLConnection) new URL(kommando).openConnection();
|
conn.setRequestMethod("GET");
|
conn.connect();
|
int status = conn.getResponseCode();
|
String msg = conn.getResponseMessage();
|
antwort = "Kommando " + kommando + " mit Status " + status + " " + msg + " gesendet.";
|
logger.log(Level.INFO, antwort);
|
} catch(Exception ex) {
|
logger.log(Level.INFO, ex.getMessage(), ex);
|
}
|
return antwort;
|
}
|
}
|