From 2597cdc3863be2c566c6bba8fe985eaff4a87df6 Mon Sep 17 00:00:00 2001 From: ulrich Date: Tue, 06 Apr 2021 11:02:29 +0000 Subject: [PATCH] Ablageorte bearbeiten fertig --- src/de/uhilger/mediaz/api/StorageHandler.java | 19 +++- src/de/uhilger/mediaz/store/FileStorage.java | 26 +++-- /dev/null | 53 ------------- src/de/uhilger/mediaz/store/Storage.java | 2 src/de/uhilger/mediaz/Server.java | 29 +------ www/ui/data/tpl/form_ablageort.tpl | 6 www/ui/js/app.js | 83 +++++--------------- 7 files changed, 60 insertions(+), 158 deletions(-) diff --git a/src/de/uhilger/mediaz/Server.java b/src/de/uhilger/mediaz/Server.java index 4a3c97c..8fedc40 100644 --- a/src/de/uhilger/mediaz/Server.java +++ b/src/de/uhilger/mediaz/Server.java @@ -18,11 +18,9 @@ package de.uhilger.mediaz; import com.sun.net.httpserver.HttpServer; -import de.uhilger.mediaz.api.AblageTestHandler; import de.uhilger.mediaz.api.FileHandler; import de.uhilger.mediaz.api.StopServerHandler; import de.uhilger.mediaz.api.StorageHandler; -import de.uhilger.mediaz.api.StoreTestHandler; import de.uhilger.mediaz.store.FileStorage; import de.uhilger.mediaz.entity.Ablageort; import java.io.File; @@ -51,8 +49,8 @@ public static final String RB_STORE = "store"; //public static final String RB_UI_ROOT = "uiroot"; public static final String RB_STOP_SERVER = "stopServer"; - public static final String RB_ABLAGE_TEST = "testAblage"; - public static final String RB_STORE_TEST = "testStore"; + //public static final String RB_ABLAGE_TEST = "testAblage"; + //public static final String RB_STORE_TEST = "testStore"; public static final String RB_SLASH = "slash"; private int port; @@ -93,7 +91,7 @@ } /** - * Die Endpunkte ('Context'e) einrichten, unter denen die Dienste dieses + * Die Endpunkte einrichten, unter denen die Dienste dieses * Servers erreichbar sein sollen und den Server starten * * @throws IOException wenn etwas schief geht, finden sich Angaben in diesem @@ -115,8 +113,8 @@ server.createContext(ctx + App.getRs(RB_STORE), new StorageHandler()); //server.createContext(ctx + App.getRs(RB_UI_ROOT), new FileHandler(uiDir.getAbsolutePath())); server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler()); - server.createContext(ctx + App.getRs(RB_ABLAGE_TEST), new AblageTestHandler()); - server.createContext(ctx + App.getRs(RB_STORE_TEST), new StoreTestHandler()); + //server.createContext(ctx + App.getRs(RB_ABLAGE_TEST), new AblageTestHandler()); + //server.createContext(ctx + App.getRs(RB_STORE_TEST), new StoreTestHandler()); server.setExecutor(Executors.newFixedThreadPool(20)); server.start(); } @@ -136,22 +134,5 @@ new FileHandler(new File(ablageort.getOrt()).getAbsolutePath())); } } - - /* - String conf = App.getInitParameter(App.getRs(App.RB_AP_CONF)); - File ablageortDir = new File(conf, Ablageort.class.getSimpleName()); - File[] orte = ablageortDir.listFiles(); - if (orte != null) { - for (File ort : orte) { - Entity elem = store.readFromFile(ort); - if (elem instanceof Ablageort) { - Ablageort ablageort = (Ablageort) elem; - server.createContext(ctx + ablageort.getUrl(), - new FileHandler(new File(ablageort.getOrt()).getAbsolutePath())); - } - } - } - */ } - } diff --git a/src/de/uhilger/mediaz/api/AblageTestHandler.java b/src/de/uhilger/mediaz/api/AblageTestHandler.java deleted file mode 100644 index b22a6f5..0000000 --- a/src/de/uhilger/mediaz/api/AblageTestHandler.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.mediaz.api; - -import com.google.gson.Gson; -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import de.uhilger.mediaz.entity.Ablageort; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; - -/** - * - * @author ulrich - */ -public class AblageTestHandler implements HttpHandler { - - @Override - public void handle(HttpExchange e) throws IOException { - Ablageort ablageort = new Ablageort(); - ablageort.setName("Katalog"); - ablageort.setOrt("/home/ulrich/Videos"); - ablageort.setUrl("/media/test"); - - Gson gson = new Gson(); - File mediaOrdner = new File(ablageort.getOrt()); - File[] files = mediaOrdner.listFiles(); - String json = gson.toJson(files); - - StringBuilder sb = new StringBuilder(); - sb.append(json); - - json = gson.toJson(ablageort); - sb.append(json); - - byte[] b = sb.toString().getBytes(); - - //String response = getResponseString(map, cmd, antwort); - e.sendResponseHeaders(200, b.length); - OutputStream os = e.getResponseBody(); - os.write(b); - os.close(); - } - -} diff --git a/src/de/uhilger/mediaz/api/StorageHandler.java b/src/de/uhilger/mediaz/api/StorageHandler.java index 8de56fd..e4bf00d 100644 --- a/src/de/uhilger/mediaz/api/StorageHandler.java +++ b/src/de/uhilger/mediaz/api/StorageHandler.java @@ -18,7 +18,6 @@ package de.uhilger.mediaz.api; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import de.uhilger.mediaz.App; @@ -37,7 +36,7 @@ import java.util.logging.Logger; /** - * HttpHandler fuer die Ablage von Entitaeten der Mediazentrale + * HttpHandler fuer die Verwaltung von Entitaeten der Mediazentrale * * @author Ulrich Hilger * @version 1, 5.4.2021 @@ -129,6 +128,14 @@ } private String neu(HttpExchange e) throws IOException { + return schreiben(e, false); + } + + private String aendern(HttpExchange e) throws IOException { + return schreiben(e, true); + } + + private String schreiben(HttpExchange e, boolean overwrite) throws IOException { String path = e.getRequestURI().toString(); String[] elems = path.split(App.getRs(Server.RB_SLASH)); String type = elems[elems.length - 1]; @@ -139,19 +146,22 @@ logger.log(Level.INFO, "type: {0}", type); Object o = gson.fromJson(body, fs.typeFromName(type).getType()); if(o instanceof Entity) { - Object antwortObjekt = fs.write((Entity) o); + Object antwortObjekt = fs.write((Entity) o, overwrite); if(antwortObjekt instanceof File) { File file = (File) antwortObjekt; logger.log(Level.INFO, "Datei {0} geschrieben.", file.getAbsolutePath()); filename = file.getName(); + return type + FileHandler.STR_BLANK + filename; } } - return type + FileHandler.STR_BLANK + filename; + return type + FileHandler.STR_BLANK + " Operation 'neu' fuer bereits existierende Entitaet."; } + /* private String aendern(HttpExchange e) throws IOException { return neu(e); // einstweilen wird einfach ueberschrieben } + */ private boolean loeschen(HttpExchange e) { String path = e.getRequestURI().toString(); @@ -188,7 +198,6 @@ sb.append("}"); return sb.toString(); } - private String bodyLesen(HttpExchange e) throws IOException { InputStream is = e.getRequestBody(); diff --git a/src/de/uhilger/mediaz/api/StoreTestHandler.java b/src/de/uhilger/mediaz/api/StoreTestHandler.java deleted file mode 100644 index 156e06c..0000000 --- a/src/de/uhilger/mediaz/api/StoreTestHandler.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.mediaz.api; - -import com.sun.net.httpserver.HttpExchange; -import com.sun.net.httpserver.HttpHandler; -import de.uhilger.mediaz.App; -import de.uhilger.mediaz.store.FileStorage; -import de.uhilger.mediaz.entity.Ablageort; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.util.logging.Level; -import java.util.logging.Logger; -import de.uhilger.mediaz.entity.Entity; - -/** - * - * @author ulrich - */ -public class StoreTestHandler implements HttpHandler { - - private static final Logger logger = Logger.getLogger(StoreTestHandler.class.getName()); - - - @Override - public void handle(HttpExchange e) throws IOException { - Ablageort ort = new Ablageort(); - ort.setName("Katalog"); - ort.setOrt("/home/ulrich/Videos"); - ort.setUrl("/media/test"); - FileStorage store = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); - File file = store.writeToFile(ort); - try { - Entity elem = store.entityFromFile(file); - logger.log(Level.INFO, "Typ: {0}, Name: {1}", - new Object[]{elem.getClass().getSimpleName(), elem.getName()}); - } catch (ClassNotFoundException ex) { - logger.log(Level.SEVERE, null, ex); - } - - String response = "OK"; - e.sendResponseHeaders(200, response.length()); - OutputStream os = e.getResponseBody(); - os.write(response.getBytes()); - os.close(); - - } - -} diff --git a/src/de/uhilger/mediaz/store/FileStorage.java b/src/de/uhilger/mediaz/store/FileStorage.java index afea9b4..bf06140 100644 --- a/src/de/uhilger/mediaz/store/FileStorage.java +++ b/src/de/uhilger/mediaz/store/FileStorage.java @@ -80,21 +80,25 @@ * * @param entity */ - public File writeToFile(Entity entity) throws IOException { + public File writeToFile(Entity entity, boolean overwrite) throws IOException { String className = entity.getClass().getSimpleName(); logger.finer(className); File dir = new File(fileBase, className); dir.mkdirs(); File file = new File(dir, entity.getName()); - if(file.exists()) { - file.delete(); + //if(file.exists()) { + // file.delete(); + //} + if(file.exists() && !overwrite) { + return null; + } else { + FileWriter fw = new FileWriter(file); + Gson gson = new Gson(); + fw.write(gson.toJson(entity)); + fw.flush(); + fw.close(); + return file; } - FileWriter fw = new FileWriter(file); - Gson gson = new Gson(); - fw.write(gson.toJson(entity)); - fw.flush(); - fw.close(); - return file; } public String readFromFile(File file) throws IOException { @@ -121,9 +125,9 @@ } @Override - public Object write(Entity e) { + public Object write(Entity e, boolean overwrite) { try { - return writeToFile(e); + return writeToFile(e, overwrite); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); return null; diff --git a/src/de/uhilger/mediaz/store/Storage.java b/src/de/uhilger/mediaz/store/Storage.java index ddbc6e7..de960bb 100644 --- a/src/de/uhilger/mediaz/store/Storage.java +++ b/src/de/uhilger/mediaz/store/Storage.java @@ -56,7 +56,7 @@ * @param e das Objekt, das geschrieben werden soll * @return ein Antwortobjekt oder null */ - public Object write(Entity e); + public Object write(Entity e, boolean overwrite); /** * Ein Objekt aus der Ablage lesen diff --git a/www/ui/data/tpl/form_ablageort.tpl b/www/ui/data/tpl/form_ablageort.tpl index 8d4bf21..4ccc8a6 100644 --- a/www/ui/data/tpl/form_ablageort.tpl +++ b/www/ui/data/tpl/form_ablageort.tpl @@ -1,9 +1,9 @@ <div class="entity-formular"> Ablageort - <input class="entity-element" type="text" id="ablageort-name" placeholder="Name" /> - <input class="entity-element" type="text" id="ablageort-ort" placeholder="Pfad" /> - <input class="entity-element" type="text" id="ablageort-url" placeholder="URL" /> + <input class="entity-element" type="text" id="ablageort-name" placeholder="Name" value="{{name}}" /> + <input class="entity-element" type="text" id="ablageort-ort" placeholder="Pfad" value="{{ort}}" /> + <input class="entity-element" type="text" id="ablageort-url" placeholder="URL" value="{{url}}" /> <div class="entity-buttons"> <button class="button-primary" id="ok-btn">Speichern</button> <button class="button" id="cancel-btn">Abbrechen</button> diff --git a/www/ui/js/app.js b/www/ui/js/app.js index d242320..691185d 100644 --- a/www/ui/js/app.js +++ b/www/ui/js/app.js @@ -9,7 +9,6 @@ function Mediazentrale() { var self = this; var appMenu; - // var vorlagen; var cache; // mustache templates this.ablageort_liste = function() { @@ -18,7 +17,6 @@ document.querySelector(".zentraler-inhalt").innerHTML = html; self.addEvtListener('.entity-eintrag', 'click', function (event) { var t = event.target; - //self.meldung_mit_timeout(t.textContent, 1500); self.http_get('../api/store/Ablageort/' + t.textContent, function(responseText){ ablageort = JSON.parse(responseText); self.ablageort_form(ablageort); @@ -40,21 +38,26 @@ self.vorlage_laden_und_fuellen("data/tpl/form_ablageort.tpl", ort, function (html) { document.querySelector(".zentraler-inhalt").innerHTML = html; self.addEvtListener('#ok-btn', 'click', function () { + var aName = document.querySelector('#ablageort-name').value; + aName = aName.replace(' ', '').replace(/[\W]+/g, ''); var a = new Ablageort( - document.querySelector('#ablageort-name').value, + aName, document.querySelector('#ablageort-ort').value, document.querySelector('#ablageort-url').value ); var daten = JSON.stringify(a); - if(typeof ort === "undefined") { + if(typeof ort === "undefined" || ort.name !== aName) { + // neu self.http_post('../api/store/Ablageort', daten, function (responseText) { // hier die Antwort verarbeiten }); } else { + // aendern self.http_put('../api/store/Ablageort', daten, function (responseText) { // hier die Antwort verarbeiten }); } + document.querySelector(".zentraler-inhalt").innerHTML = ''; }); self.addEvtListener('#cancel-btn', 'click', function () { document.querySelector(".zentraler-inhalt").innerHTML = ''; @@ -67,6 +70,8 @@ self.addEvtListener('#ja-btn', 'click', function() { self.http_delete('../api/store/Ablageort/' + aoname, '', function (responseText) { // hier die Antwort verarbeiten + self.dialog_schliessen(); + document.querySelector(".zentraler-inhalt").innerHTML = ''; }); }); }); @@ -95,6 +100,7 @@ }; this.http_delete = function (u, data, cb) { + console.log("delete " + u); self.http_call('DELETE', u, data, cb); }; @@ -109,18 +115,11 @@ xhr.open(method, url); if (method === 'GET') { xhr.send(); - } else if (method === 'POST' || method === 'PUT') { + } else if (method === 'POST' || method === 'PUT' || method === 'DELETE') { xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr.send(data); } }; - - this.serialisieren = function (obj) { - return '{"' + obj.constructor.name + '":' + JSON.stringify(obj) + '}'; - }; - - - /* ab hier aus App-Vorlage */ @@ -233,55 +232,29 @@ self.http_get(vurl, function(antwort) { self.cache[vurl] = antwort; self.dialog_zeigen(vurl, msgTpl, cb); - //self.dialog_zeigen(vurl, antwort, cb); - //self.vorlage_fuellen(vurl, inhalt, cb); }); } else { self.dialog_zeigen(vurl, msgTpl, cb); - //self.dialog_zeigen(vurl, vorlage, cb); } }; this.dialog_zeigen = function (vurl, inhalt, cb) { var dlg = document.querySelector(".dialog"); - self.html_erzeugen( - vurl, - inhalt, - function (html) { - //dlg.html(html); - dlg.style.height = '7em'; - dlg.innerHTML = html; - document.querySelector('.close-btn').addEventListener('click', self.dialog_schliessen); - //dlg.slideDown(300); - if(typeof(cb) !== 'function') { - // .. - } else { - cb(); - } - }); + self.html_erzeugen(vurl, inhalt, function (html) { + dlg.style.height = '7em'; + dlg.innerHTML = html; + document.querySelector('.close-btn').addEventListener('click', self.dialog_schliessen); + if(typeof(cb) !== 'function') { + // .. + } else { + cb(); + } + }); }; - - -/* - this.dialog_zeigen = function (vurl, inhalt) { - var dlg = document.querySelector(".dialog"); - self.html_erzeugen( - vurl, - inhalt, - function (html) { - //dlg.html(html); - dlg.style.height = '5em'; - dlg.innerHTML = html; - document.querySelector('.close-btn').addEventListener('click', self.dialog_schliessen); - //dlg.slideDown(300); - }); - }; -*/ + self.dialog_schliessen = function () { document.querySelector('.close-btn').removeEventListener('click', self.dialog_schliessen); - //$('.dialog').slideUp(300); var dlg = document.querySelector('.dialog'); - //dlg.style.display = "none"; dlg.style.height = '0'; dlg.innerHTML = ''; }; @@ -314,8 +287,6 @@ }; this.vorlage_fuellen = function (vurl, inhalt, cb) { - //console.log("vorlage " + self.cache[vurl]); - //console.log("render " + inhalt); cb(Mustache.render(self.cache[vurl], inhalt)); }; @@ -328,16 +299,6 @@ Inhalt gefüllt ist */ this.vorlage_laden_und_fuellen = function (vurl, inhalt, cb) { - /* - $.ajax({ - url: vurl, - type: "GET", - dataType : "text" - }).done(function( vorlage ) { - self.cache[vurl] = vorlage; - self.vorlage_fuellen(vurl, inhalt, cb); - }); - */ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { -- Gitblit v1.9.3