From 5f70da71c8e71990715dcb4be5cc433284abdede Mon Sep 17 00:00:00 2001 From: ulrich Date: Mon, 05 Apr 2021 12:35:50 +0000 Subject: [PATCH] Lesen und neu fuer Ablageort fertig --- src/de/uhilger/mediaz/store/FileStorage.java | 70 +++++++++++++++++++++------------- 1 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/de/uhilger/mediaz/store/FileStorage.java b/src/de/uhilger/mediaz/store/FileStorage.java index 1d6373a..3819699 100644 --- a/src/de/uhilger/mediaz/store/FileStorage.java +++ b/src/de/uhilger/mediaz/store/FileStorage.java @@ -6,6 +6,7 @@ package de.uhilger.mediaz.store; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import de.uhilger.mediaz.App; import de.uhilger.mediaz.Server; import de.uhilger.mediaz.entity.Ablageort; @@ -18,7 +19,9 @@ import java.util.logging.Logger; import de.uhilger.mediaz.entity.Entity; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Level; /** @@ -41,8 +44,15 @@ private String fileBase; + private Map<String, TypeToken> types; + public FileStorage(String base) { this.fileBase = base; + + // Beispiel: TypeToken<List<String>> list = new TypeToken<List<String>>() {}; + TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {}; + types = new HashMap(); + types.put(Ablageort.class.getSimpleName(), ttAblageort); } /** @@ -59,7 +69,6 @@ * @param o */ public File writeToFile(Entity o) throws IOException { - Gson gson = new Gson(); String className = o.getClass().getSimpleName(); logger.finer(className); File dir = new File(fileBase, className); @@ -69,44 +78,33 @@ file.delete(); } FileWriter fw = new FileWriter(file); + Gson gson = new Gson(); fw.write(gson.toJson(o)); fw.flush(); fw.close(); return file; } - public Entity readFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { - String type = typeFromName(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(); - 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 sb.toString(); } - private String typeFromName(File file) { - //String path = file.getPath(); - //logger.info(path); + public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { + String json = readFromFile(file); + Gson gson = new Gson(); + return gson.fromJson(json, typeFromName(typeNameFromPath(file)).getType()); + } + + private String typeNameFromPath(File file) { String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH)); - //for(String part : parts) { - //logger.info(part); - //} - //logger.info("" + parts.length); logger.info(parts[parts.length-2]); return parts[parts.length-2]; } @@ -123,11 +121,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; @@ -146,6 +141,27 @@ return list; } + @Override + public TypeToken typeFromName(String name) { + 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); + } + } -- Gitblit v1.9.3