From b90d6705d78a7ad60a1e0419eb27ab20927d3fdd Mon Sep 17 00:00:00 2001
From: ulrich
Date: Wed, 07 Apr 2021 07:09:58 +0000
Subject: [PATCH] Piktogramme und Dropdown

---
 src/de/uhilger/mediaz/store/FileStorage.java |   73 +++++++++++++++++++++++++-----------
 1 files changed, 50 insertions(+), 23 deletions(-)

diff --git a/src/de/uhilger/mediaz/store/FileStorage.java b/src/de/uhilger/mediaz/store/FileStorage.java
index 3819699..524e4d2 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]
  * 
@@ -66,23 +78,27 @@
    * Der Name der Ablage muss eindeutig sein
    * 
    * 
-   * @param o 
+   * @param entity 
    */
-  public File writeToFile(Entity o) throws IOException {
-    String className = o.getClass().getSimpleName();
+  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, o.getName());
-    if(file.exists()) {
-      file.delete();
+    File file = new File(dir, entity.getName());
+    //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(o));
-    fw.flush();
-    fw.close();
-    return file;
   }
   
   public String readFromFile(File file) throws IOException {
@@ -105,14 +121,13 @@
   
   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];
   }
 
   @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;
@@ -133,10 +148,14 @@
   public List<String> list(String typ) {
     File base = new File(fileBase);
     File dir = new File(base, typ);
-    File[] files = dir.listFiles();
     List<String> list = new ArrayList();
-    for(File file : files) {
-      list.add(file.getName());
+    File[] files = dir.listFiles();
+    if(files != null) {
+      for(File file : files) {
+        //NamedItem n = new NamedItem();
+        //n.setLabel(file.getName());
+        list.add(file.getName());
+      }
     }
     return list;
   }
@@ -162,6 +181,14 @@
     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