Persoenliche Mediazentrale
ulrich
2021-04-08 e60cff473830164ac0f660523d02271d541f7d72
Abspielliste Titel hinzufuegen
6 files modified
105 ■■■■ changed files
src/de/uhilger/mediaz/Server.java 3 ●●●●● patch | view | raw | blame | history
src/de/uhilger/mediaz/api/ListHandler.java 61 ●●●●● patch | view | raw | blame | history
src/de/uhilger/mediaz/entity/Abspielliste.java 4 ●●●● patch | view | raw | blame | history
src/de/uhilger/mediaz/store/FileStorage.java 5 ●●●●● patch | view | raw | blame | history
src/mediaz_de_DE.properties 1 ●●●● patch | view | raw | blame | history
www/ui/js/app.js 31 ●●●●● patch | view | raw | blame | history
src/de/uhilger/mediaz/Server.java
@@ -20,6 +20,7 @@
import com.sun.net.httpserver.HttpServer;
import de.uhilger.mediaz.api.FileHandler;
import de.uhilger.mediaz.api.ListFileHandler;
import de.uhilger.mediaz.api.ListHandler;
import de.uhilger.mediaz.api.StopServerHandler;
import de.uhilger.mediaz.api.StorageHandler;
import de.uhilger.mediaz.store.FileStorage;
@@ -48,6 +49,7 @@
  public static final String RB_SERVER_START_MSG = "msgServerStart";
  public static final String RB_WEBROOT = "webroot";
  public static final String RB_STORE = "store";
  public static final String RB_ALIST= "alist";
  //public static final String RB_UI_ROOT = "uiroot";
  public static final String RB_STOP_SERVER = "stopServer";
  //public static final String RB_ABLAGE_TEST = "testAblage";
@@ -109,6 +111,7 @@
    server.createContext(ctx + App.getRs(RB_WEBROOT), new FileHandler(wwwDir.getAbsolutePath()));
    ablageorteEinklinken(server);
    server.createContext(ctx + App.getRs(RB_STORE), new StorageHandler());
    server.createContext(ctx + App.getRs(RB_ALIST), new ListHandler());
    server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler());
    server.setExecutor(Executors.newFixedThreadPool(20));
    server.start();
src/de/uhilger/mediaz/api/ListHandler.java
@@ -17,8 +17,16 @@
 */
package de.uhilger.mediaz.api;
import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
import de.uhilger.mediaz.App;
import de.uhilger.mediaz.Server;
import de.uhilger.mediaz.entity.Abspielliste;
import de.uhilger.mediaz.entity.Entity;
import de.uhilger.mediaz.entity.Titel;
import de.uhilger.mediaz.store.FileStorage;
import java.io.IOException;
import java.util.logging.Logger;
/**
 * Der ListHandler bearbeitet HTTP-Anfragen zu Abspiellisten
@@ -32,15 +40,64 @@
 * @version 1, 8.4.2021
 */
public class ListHandler extends AbstractHandler {
  private static final Logger logger = Logger.getLogger(ListHandler.class.getName());
  @Override
  protected String get(HttpExchange e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    String path = e.getRequestURI().toString();
    String[] elems = path.split(App.getRs(Server.RB_SLASH));
    String plname = elems[elems.length - 1];
    logger.finer("GET plname: " + plname);
    FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
    String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname);
    logger.finer("PL json: " + json);
    return json;
  }
  @Override
  protected String put(HttpExchange e) throws IOException {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    String path = e.getRequestURI().toString();
    String[] elems = path.split(App.getRs(Server.RB_SLASH));
    String response = "ListHandler.put: ungueltiger URL";
    logger.finer("elems.length: " + elems.length);
    for(String elem : elems) {
      logger.finer("elem: " + elem);
    }
    switch(elems.length) {
      case 5:
        response = addTitel(e, elems[4]);
        break;
      case 6:
        response = "Einfuegen noch nicht fertig.";
        break;
    }
    return response;
  }
  private String addTitel(HttpExchange e, String plname) throws IOException {
    //String plname = elems[elems.length - 1];
    logger.finer("plname: " + plname);
    FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
    Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
    String response = "Titel konnte nicht hinzugefuegt werden.";
    if(entity instanceof Abspielliste) {
      Abspielliste aliste = (Abspielliste) entity;
      logger.finer("aliste: " + aliste.getName());
      String titelJson = bodyLesen(e);
      logger.finer("titelJson: " + titelJson);
      Gson gson = new Gson();
      Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType());
      if(o instanceof Titel) {
        Titel titel = (Titel) o;
        aliste.addTitel(titel);
        fs.write(aliste, true);
        response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + " hinzugefuegt.";
      }
    }
    return response;
  }
  @Override
src/de/uhilger/mediaz/entity/Abspielliste.java
@@ -33,6 +33,10 @@
    titel = new ArrayList<>();
  }
  public void addTitel(Titel t) {
    this.titel.add(t);
  }
  public List<Titel> getTitel() {
    return titel;
  }
src/de/uhilger/mediaz/store/FileStorage.java
@@ -33,6 +33,7 @@
import java.io.IOException;
import java.util.logging.Logger;
import de.uhilger.mediaz.entity.Entity;
import de.uhilger.mediaz.entity.Titel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -58,6 +59,7 @@
  public static final String ST_ABLAGEORT = "Ablageort";
  public static final String ST_EINSTELLUNG = "Einstellung";
  public static final String ST_ABSPIELER = "Abspieler";
  public static final String ST_ABSPIELLISTE = "Abspielliste";
  
  private final String fileBase;
  
@@ -71,11 +73,13 @@
    TypeToken<Einstellung> ttEinstellung = new TypeToken<Einstellung>() {};
    TypeToken<Abspieler> ttAbspieler = new TypeToken<Abspieler>() {};
    TypeToken<Abspielliste> ttAbspielliste = new TypeToken<Abspielliste>() {};
    TypeToken<Titel> ttTitel = new TypeToken<Titel>() {};
    types = new HashMap();
    types.put(Ablageort.class.getSimpleName(), ttAblageort);
    types.put(Einstellung.class.getSimpleName(), ttEinstellung);
    types.put(Abspieler.class.getSimpleName(), ttAbspieler);
    types.put(Abspielliste.class.getSimpleName(), ttAbspielliste);
    types.put(Titel.class.getSimpleName(), ttTitel);
  }
  
  /**
@@ -127,6 +131,7 @@
  
  public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException {
    String json = readFromFile(file);
    logger.finer("json: " + json);
    Gson gson = new Gson();
    return gson.fromJson(json, typeFromName(typeNameFromPath(file)).getType());
  }
src/mediaz_de_DE.properties
@@ -10,6 +10,7 @@
webroot=/
# uiroot=/ui
store=/api/store
alist=/api/alist
epliste=liste
stopServer=/api/server/stop
testAblage=/api/test/ablage
www/ui/js/app.js
@@ -4,6 +4,8 @@
  var cache; // mustache templates
  var ortPfad;
  var mediaPfad;
  var katUrl;
  var selTitel;
  this.init = function () {
    self.mediaPfad = '/';
@@ -64,6 +66,7 @@
      dlg.innerHTML = html;
      self.abspieler_auswahl_fuellen();
      self.abspielliste_auswahl_fuellen();
      self.addEvtListener('#dazu-btn', 'click', self.addSelectedTitel);
      self.media_liste();
    });
  };
@@ -117,38 +120,27 @@
        });
      });
    } else {
      // Pfad listen
      // console.log("vorher ortPfad: " + self.ortPfad);
      //console.log("vorher mediaPfad: " + self.mediaPfad);
      //self.http_get('..' + self.ortPfad + '/' + self.mediaPfad + '/', function(responseText) {
      var url = '..' + self.ortPfad + self.mediaPfad;
      if(!url.endsWith('/')) {
        url = url + '/';
      }
      //console.log("url: " + url);
      self.http_get(url, function(responseText) {
        //console.log(responseText);
        self.vorlage_laden_und_fuellen("data/tpl/katalog_inhalt_liste.tpl", JSON.parse(responseText), function (html) {
          document.querySelector(".zentraler-inhalt").innerHTML = html;
          //console.log("mediaPfad bei Anzeige: " + self.mediaPfad);
          self.addEvtListener('.entity-eintrag', 'click', function (event) {
            var t = event.target;
            var tx = t.textContent;
            //console.log("tx: " + tx);
            //console.log("mediaPfad nach Auswahl: " + self.mediaPfad);
            if(t.classList.contains("entity-typ-folder")) {
              if(self.mediaPfad.endsWith('/')) {
                self.mediaPfad = self.mediaPfad + tx;                
              } else {
                self.mediaPfad = self.mediaPfad + '/' + tx;
              }
              //self.mediaPfad = self.mediaPfad + t.textContent;
              //console.log("mediaPfad neu: " + self.mediaPfad);
              self.media_liste();
            } else {
              //console.log("Media-Inhalt auswaehlen oder abspielen");
              self.removeClassMulti('selected');
              t.classList.add('selected');
              //self.selTitel = new Titel(t.textContent, self.ortPfad);
            }
          });
          self.addEvtListener('#top-up-btn', 'click', function(event) {
@@ -162,7 +154,6 @@
              } else {
                parent = '/';
              }
              //console.log("Parent: " + parent);
              self.mediaPfad = parent;
            }
            self.media_liste();
@@ -170,6 +161,15 @@
        });
      });
    }
  };
  this.addSelectedTitel = function() {
    var titelName = document.querySelector(".selected").textContent;
    var titel = new Titel(self.mediaPfad + titelName, self.ortPfad);
    var plname = document.querySelector('#playlist').value;
    self.http_put('../api/alist/' + plname, JSON.stringify(titel), function(responseText) {
      console.log(responseText);
    });
  };
  this.ablageort_liste = function() {
@@ -691,4 +691,9 @@
function Abspielliste(n) {
  this.name = n;
}
function Titel(n, u) {
  this.katalogUrl = u;
  this.name = n;
}