From 5bf530d39a7e06bdd982a5c2495ff9a04fd83da4 Mon Sep 17 00:00:00 2001
From: undisclosed
Date: Wed, 11 Jan 2023 17:38:22 +0000
Subject: [PATCH] Erste Versuche mit StreamHandler
---
src/de/uhilger/tango/api/ListHandler.java | 95 ++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 84 insertions(+), 11 deletions(-)
diff --git a/src/de/uhilger/tango/api/ListHandler.java b/src/de/uhilger/tango/api/ListHandler.java
index 719f911..efb5d4e 100644
--- a/src/de/uhilger/tango/api/ListHandler.java
+++ b/src/de/uhilger/tango/api/ListHandler.java
@@ -19,13 +19,13 @@
import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
-import de.uhilger.tango.App;
import de.uhilger.tango.Server;
import de.uhilger.tango.entity.Abspielliste;
import de.uhilger.tango.entity.Entity;
import de.uhilger.tango.entity.Titel;
import de.uhilger.tango.store.FileStorage;
import java.io.IOException;
+import java.util.List;
import java.util.logging.Logger;
/**
@@ -34,8 +34,14 @@
* GET /mz/api/alist/[pl-name] die Titel-Objekte der Liste [pl-name] liefern
* PUT /mz/api/alist/[pl-name] den Titel im Body anfuegen an die Liste [pl-name]
* PUT /mz/api/alist/[pl-name]/[nr] an der Position nr der Liste [pl-name] den Titel im Body einfuegen
+ * PUT /mz/api/alist/[pl-name]/[nrVon]/[nrNach] den Titel von seiner aktuellen Position an eine
+ * andere Position der Liste [pl-name] verschieben
* DELETE /mz/api/alist/[pl-name]/[nr] den Titel an der Position [nr] aus der Liste [pl-name] entfernen
* DELETE /mz/api/alist/[pl-name]/alle alle Titel aus der Liste [pl-name] entfernen
+ *
+ * TODO (2.1.2023):
+ * - Liste ab Titel spielen
+ * - Ganzes Album der Liste hinzufuegen
*
* @author Ulrich Hilger
* @version 1, 8.4.2021
@@ -45,14 +51,19 @@
private static final Logger logger = Logger.getLogger(ListHandler.class.getName());
public static final String ALLE_TITEL = "alle";
-
+
+ private String conf;
+
+ public ListHandler(String conf) {
+ this.conf = conf;
+ }
@Override
protected String get(HttpExchange e) {
String path = e.getRequestURI().toString();
String[] elems = path.split(Server.SLASH);
String plname = elems[elems.length - 1];
- FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
+ FileStorage fs = new FileStorage(conf);
String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname);
return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE);
}
@@ -68,14 +79,81 @@
break;
case 6:
- response = "Einfuegen noch nicht fertig.";
+ response = insertTitel(e, elems[4], Integer.parseInt(elems[5]));
+ break;
+
+ case 7:
+ response = moveTitel(e, elems[4], Integer.parseInt(elems[5]), Integer.parseInt(elems[6]));
break;
}
return response;
}
+ /**
+ * Den Titel im Body von seiner aktuellen Position an die angegebene
+ * Position setzen. Der Titel an der angegebenen Position rueckt nach
+ * unten.
+ *
+ * Annahme: Die Abspielliste enthaelt keine Titel mehrfach.
+ *
+ * @param e
+ * @param plname
+ * @param zielPos
+ * @return
+ * @throws IOException
+ */
+ private String moveTitel(HttpExchange e, String plname, int pos, int zielPos) throws IOException {
+ FileStorage fs = new FileStorage(conf);
+ Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
+ String response = "Titel konnte nicht verschoben werden.";
+ if(entity instanceof Abspielliste) {
+ if(pos < zielPos) {
+ --zielPos;
+ }
+ Abspielliste aliste = (Abspielliste) entity;
+ List<Titel> liste = aliste.getTitel();
+ Titel t = liste.get(pos);
+ liste.remove(pos);
+ liste.add(zielPos, t);
+ fs.write(aliste, true);
+ response = "Titel " + t.getName() + " der Liste " + aliste.getName() +
+ " an Position " + zielPos + " verschoben.";
+ }
+ return response;
+ }
+
+ /**
+ * Den Titel im Body an Position anPos einfuegen. Der Titel an anPos
+ * rueckt nach unten.
+ *
+ * @param e
+ * @param plname
+ * @param anPos
+ * @return
+ * @throws IOException
+ */
+ private String insertTitel(HttpExchange e, String plname, int anPos) throws IOException {
+ FileStorage fs = new FileStorage(conf);
+ Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
+ String response = "Titel konnte nicht hinzugefuegt werden.";
+ if(entity instanceof Abspielliste) {
+ Abspielliste aliste = (Abspielliste) entity;
+ String titelJson = bodyLesen(e);
+ Gson gson = new Gson();
+ Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType());
+ if(o instanceof Titel) {
+ Titel titel = (Titel) o;
+ aliste.putTitel(titel, anPos);
+ fs.write(aliste, true);
+ response = "Titel " + titel.getName() + " der Liste " + aliste.getName() +
+ " an Position " + anPos + " hinzugefuegt.";
+ }
+ }
+ return response;
+ }
+
private String addTitel(HttpExchange e, String plname) throws IOException {
- FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
+ FileStorage fs = new FileStorage(conf);
Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
String response = "Titel konnte nicht hinzugefuegt werden.";
if(entity instanceof Abspielliste) {
@@ -93,18 +171,13 @@
return response;
}
- @Override
- protected String post(HttpExchange e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
-
// DELETE /mz/api/alist/[pl-name]/[nr] den Titel an der Position [nr] aus der Liste [pl-name] entfernen
@Override
protected boolean delete(HttpExchange e) {
String path = e.getRequestURI().toString();
String[] elems = path.split(Server.SLASH);
String listName = elems[elems.length - 2];
- FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF)));
+ FileStorage fs = new FileStorage(conf);
Entity entity = fs.read(Abspielliste.class.getSimpleName(), listName);
if(entity instanceof Abspielliste) {
Abspielliste liste = (Abspielliste) entity;
--
Gitblit v1.9.3