From a0ec7b1043129c09ad1ed6050c8fc8bbf05488fd Mon Sep 17 00:00:00 2001
From: ulrich <undisclosed>
Date: Sun, 25 Feb 2018 18:05:28 +0000
Subject: [PATCH] SenderApi begonnen

---
 web/WEB-INF/web.xml                                  |   12 +++
 src/java/de/uhilger/radiozentrale/api/SenderApi.java |   66 ++++++++++++++++
 web/WEB-INF/sql.properties                           |    4 +
 src/java/de/uhilger/radiozentrale/api/Api.java       |   82 ++++++++++++++++++++
 web/app.js                                           |   18 ++++
 5 files changed, 181 insertions(+), 1 deletions(-)

diff --git a/src/java/de/uhilger/radiozentrale/api/Api.java b/src/java/de/uhilger/radiozentrale/api/Api.java
new file mode 100644
index 0000000..d68b587
--- /dev/null
+++ b/src/java/de/uhilger/radiozentrale/api/Api.java
@@ -0,0 +1,82 @@
+/*
+ *  Radiozentrale - Webradio App
+ *  Copyright (C) 2018 Ulrich Hilger, http://uhilger.de
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/
+ */
+
+package de.uhilger.radiozentrale.api;
+
+import de.uhilger.baselink.PersistenceManager;
+import de.uhilger.baselink.Record;
+import de.uhilger.radiozentrale.web.Initialiser;
+import de.uhilger.transit.web.RequestKontext;
+import de.uhilger.transit.web.WebKontext;
+import java.util.Properties;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ *
+ */
+public abstract class Api implements WebKontext, RequestKontext {
+  
+  /** Zeiger zum Servlet-Kontext dieser Anwendung */
+  private ServletContext ctx;
+  
+  private HttpServletRequest request;  
+  
+  protected PersistenceManager getDb() {
+    return (PersistenceManager) ctx.getAttribute(Initialiser.RZ_DB);
+  }
+  
+  protected String getSql(String id) {
+    Properties sql = (Properties) ctx.getAttribute(Initialiser.RZ_SQL_PROPERTIES);
+    return sql.getProperty(id);
+  }
+  
+  protected Record getMapper(String mapperName) {
+    Record record = null;
+    Object o = getServletContext().getAttribute(mapperName);
+    if(o instanceof Record) {
+      record = (Record) o;
+    }
+    return record;
+  }
+  
+  /* ------------- Implementierung WebKontext ------------- */
+
+  @Override
+  public ServletContext getServletContext() {
+    return ctx;
+  }
+
+  @Override
+  public void setServletContext(ServletContext servletContext) {
+    this.ctx = servletContext;
+  }
+  
+  /* ------------- Implementierung RequestKontext ------------- */
+
+  @Override
+  public HttpServletRequest getRequest() {
+    return request;
+  }
+
+  @Override
+  public void setRequest(HttpServletRequest r) {
+    this.request = r;
+  }
+  
+}
diff --git a/src/java/de/uhilger/radiozentrale/api/SenderApi.java b/src/java/de/uhilger/radiozentrale/api/SenderApi.java
new file mode 100644
index 0000000..ac56940
--- /dev/null
+++ b/src/java/de/uhilger/radiozentrale/api/SenderApi.java
@@ -0,0 +1,66 @@
+/*
+ *  Radiozentrale - Webradio App
+ *  Copyright (C) 2018 Ulrich Hilger, http://uhilger.de
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/
+ */
+
+package de.uhilger.radiozentrale.api;
+
+import de.uhilger.baselink.GenericRecord;
+import de.uhilger.baselink.PersistenceManager;
+import de.uhilger.radiozentrale.daten.Sender;
+import de.uhilger.radiozentrale.web.Initialiser;
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ *
+ */
+public class SenderApi extends Api {
+  
+  public static final String SQL_GET_SENDER = "getSender";
+  public static final String SQL_GET_NEXT_KEY = "getNextKey";
+  public static final String SQL_INCREMENT_KEY = "incrementKey";
+  
+  public static final String KEY_SENDER_ID = "sender_id";
+  
+  
+  public Sender neuerSender(Sender sender) {
+    Sender neuerSender = null;
+    PersistenceManager db = getDb();
+    Connection c = db.getConnection();
+    db.startTransaction(c);
+    List<List<String>> list = db.select(getSql(SQL_GET_NEXT_KEY), GenericRecord.WITHOUT_BLOBS, KEY_SENDER_ID);
+    if(list != null && list.size() > 0) {
+      int nextKey = Integer.parseInt(list.get(1).get(0));
+      db.execute(getSql(SQL_INCREMENT_KEY), nextKey+1, KEY_SENDER_ID, nextKey);
+      sender.setId(nextKey);
+      Object o = getDb().insert(sender, getMapper(Initialiser.MP_SENDER));
+      if(o instanceof Sender) {
+        neuerSender = (Sender) o;
+      } 
+    }
+    db.commit(c);
+    return neuerSender;  
+  }
+  
+  public List senderliste() {
+    String sql = getSql(SQL_GET_SENDER);
+    return getDb().select(sql, getMapper(Initialiser.MP_SENDER));
+  }
+}
diff --git a/web/WEB-INF/sql.properties b/web/WEB-INF/sql.properties
index 5881357..f33598d 100644
--- a/web/WEB-INF/sql.properties
+++ b/web/WEB-INF/sql.properties
@@ -23,4 +23,8 @@
     app.keytable 
     where key_name = ?
   </entry>
+  <entry key="getSender">
+    select * from 
+    app.sender
+  </entry>
 </properties>
diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml
index 1f4d8f3..55b2926 100644
--- a/web/WEB-INF/web.xml
+++ b/web/WEB-INF/web.xml
@@ -9,6 +9,18 @@
     <listener>
         <listener-class>de.uhilger.radiozentrale.web.Initialiser</listener-class>
     </listener>
+    <servlet>
+        <servlet-name>TransitServlet</servlet-name>
+        <servlet-class>de.uhilger.transit.web.TransitServlet</servlet-class>
+        <init-param>
+            <param-name>klassen</param-name>
+            <param-value>de.uhilger.radiozentrale.api</param-value>
+        </init-param>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>TransitServlet</servlet-name>
+        <url-pattern>/api</url-pattern>
+    </servlet-mapping>
     <session-config>
         <session-timeout>
             30
diff --git a/web/app.js b/web/app.js
index 7647533..d3613cf 100644
--- a/web/app.js
+++ b/web/app.js
@@ -248,7 +248,9 @@
   app_dialog_laden_und_zeigen(templateCache[TPL_DLG_SENDER_NEU], '');
   $('#sender-speichern').on('click', function() {
     $('#sender-speichern').off('click');
-    app_kachel_neu(app_sender_dialog_lesen(--ID_UNDEFINED));
+    var sender = app_sender_dialog_lesen(--ID_UNDEFINED);
+    app_neuen_sender_speichern(sender);
+    app_kachel_neu(sender);
     app_dialog_schliessen();
     app_meldung_mit_timeout('Speichern gewaehlt', 1500);
   });
@@ -456,6 +458,20 @@
   });
 }
 
+function app_neuen_sender_speichern(sender) {
+  var obj = serialisieren(sender);
+  var url = 'api?c=de.uhilger.radiozentrale.api.SenderApi&m=neuerSender';
+  $.post( url, 'p=' + obj, function( result ) {
+    console.log( result );
+  }, "json");
+}
+
+/* Hilfsfunktionen */
+
+function serialisieren(obj) {
+  return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}';
+}
+
 /* Objekte */
 
 function Sender(i, n, u, l) {

--
Gitblit v1.9.3