From 90f5d4ea4ddcded966c09809f3e65a54941a81c8 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Tue, 06 Apr 2021 09:00:43 +0000
Subject: [PATCH] Ablageort bearbeiten und loeschen in Arbeit

---
 src/de/uhilger/mediaz/store/FileStorage.java |   88 +++++++++++++++++++++++++++++---------------
 1 files changed, 58 insertions(+), 30 deletions(-)

diff --git a/src/de/uhilger/mediaz/store/FileStorage.java b/src/de/uhilger/mediaz/store/FileStorage.java
index 0282ed7..afea9b4 100644
--- a/src/de/uhilger/mediaz/store/FileStorage.java
+++ b/src/de/uhilger/mediaz/store/FileStorage.java
@@ -1,7 +1,19 @@
 /*
- * 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.
+  Mediazentrale - Personal Media Center
+  Copyright (C) 2021  Ulrich Hilger
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU Affero 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 Affero General Public License for more details.
+
+  You should have received a copy of the GNU Affero General Public License
+  along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 package de.uhilger.mediaz.store;
 
@@ -25,7 +37,7 @@
 import java.util.logging.Level;
 
 /**
- * Ablage fuer Dateien
+ * Ablage fuer Dateien der Mediazentrale
  * 
  * [Basispfad]/[Typ]/[Name]
  * 
@@ -49,7 +61,7 @@
   public FileStorage(String base) {
     this.fileBase = base;
     
-    //TypeToken<List<String>> list = new TypeToken<List<String>>() {};
+    // Beispiel: TypeToken<List<String>> list = new TypeToken<List<String>>() {};
     TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {};
     types = new HashMap();
     types.put(Ablageort.class.getSimpleName(), ttAblageort);
@@ -66,52 +78,45 @@
    * Der Name der Ablage muss eindeutig sein
    * 
    * 
-   * @param o 
+   * @param entity 
    */
-  public File writeToFile(Entity o) throws IOException {
-    Gson gson = new Gson();
-    String className = o.getClass().getSimpleName();
+  public File writeToFile(Entity entity) throws IOException {
+    String className = entity.getClass().getSimpleName();
     logger.finer(className); 
     File dir = new File(fileBase, className);
     dir.mkdirs();
-    File file = new File(dir, o.getName());
+    File file = new File(dir, entity.getName());
     if(file.exists()) {
       file.delete();
     }
     FileWriter fw = new FileWriter(file);
-    fw.write(gson.toJson(o));
+    Gson gson = new Gson();
+    fw.write(gson.toJson(entity));
     fw.flush();
     fw.close();
     return file;
   }
   
-  public Entity readFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException {
-    String type = typeNameFromPath(file);
+  public String readFromFile(File file) throws IOException {
     StringBuilder sb = new StringBuilder();
-    FileReader in = new FileReader(file);
-    BufferedReader r = new BufferedReader(in);
+    BufferedReader r = new BufferedReader(new FileReader(file));
     String line = r.readLine();
     while(line != null) {
       sb.append(line);
       line = r.readLine();
     }
     r.close();
-    in.close();
-    String json = sb.toString();
+    return sb.toString();
+  }
+  
+  public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException {
+    String json = readFromFile(file);
     Gson gson = new Gson();
-    switch(type) {
-      case ST_ABLAGEORT:
-        return gson.fromJson(json, Ablageort.class);
-      default:
-        Ablageort ablage = new Ablageort();
-        ablage.setName("Test");
-        return ablage;
-    }
+    return gson.fromJson(json, typeFromName(typeNameFromPath(file)).getType());
   }
   
   private String typeNameFromPath(File file) {
     String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH));
-    logger.info(parts[parts.length-2]);
     return parts[parts.length-2];
   }
 
@@ -127,11 +132,8 @@
 
   @Override
   public Entity read(String typ, String name) {
-    File base = new File(fileBase);
-    File dir = new File(base, typ);
-    File file = new File(dir, name);
     try {
-      return readFromFile(file);
+      return entityFromFile(getFile(typ, name));
     } catch (ClassNotFoundException | IOException ex) {
       logger.log(Level.SEVERE, null, ex);
       return null;
@@ -145,6 +147,8 @@
     File[] files = dir.listFiles();
     List<String> list = new ArrayList();
     for(File file : files) {
+      //NamedItem n = new NamedItem();
+      //n.setLabel(file.getName());
       list.add(file.getName());
     }
     return list;
@@ -155,6 +159,30 @@
     return types.get(name);
   }
 
+  @Override
+  public String readJson(String typ, String name) {
+    try {
+      return readFromFile(getFile(typ, name));
+    } catch (IOException ex) {
+      logger.log(Level.SEVERE, null, ex);
+      return null;
+    }
+  }
   
+  private File getFile(String typ, String name) {
+    File base = new File(fileBase);
+    File dir = new File(base, typ);
+    return new File(dir, name);    
+  }
+
+  @Override
+  public boolean delete(String typ, String name) {
+    File file = getFile(typ, name);
+    if(file.exists()) {
+      return file.delete();
+    } else {
+      return false;
+    }
+  }
   
 }

--
Gitblit v1.9.3