| | |
| | | /* |
| | | * 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; |
| | | |
| | |
| | | import de.uhilger.mediaz.App; |
| | | import de.uhilger.mediaz.Server; |
| | | import de.uhilger.mediaz.entity.Ablageort; |
| | | import de.uhilger.mediaz.entity.Abspieler; |
| | | import de.uhilger.mediaz.entity.Abspielliste; |
| | | import de.uhilger.mediaz.entity.Einstellung; |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.FileNotFoundException; |
| | |
| | | import java.io.IOException; |
| | | import java.util.logging.Logger; |
| | | import de.uhilger.mediaz.entity.Entity; |
| | | import de.uhilger.mediaz.entity.Titel; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | import java.util.logging.Level; |
| | | |
| | | /** |
| | | * Ablage fuer Dateien |
| | | * Ablage fuer Dateien der Mediazentrale |
| | | * |
| | | * [Basispfad]/[Typ]/[Name] |
| | | * |
| | |
| | | |
| | | /** StorageType Ablageort */ |
| | | public static final String ST_ABLAGEORT = "Ablageort"; |
| | | public static final String ST_EINSTELLUNG = "Einstellung"; |
| | | public static final String ST_ABSPIELER = "Abspieler"; |
| | | public static final String ST_ABSPIELLISTE = "Abspielliste"; |
| | | |
| | | private String fileBase; |
| | | private final String fileBase; |
| | | |
| | | private Map<String, TypeToken> types; |
| | | private final 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>() {}; |
| | | TypeToken<Einstellung> ttEinstellung = new TypeToken<Einstellung>() {}; |
| | | TypeToken<Abspieler> ttAbspieler = new TypeToken<Abspieler>() {}; |
| | | TypeToken<Abspielliste> ttAbspielliste = new TypeToken<Abspielliste>() {}; |
| | | TypeToken<Titel> ttTitel = new TypeToken<Titel>() {}; |
| | | types = new HashMap(); |
| | | types.put(Ablageort.class.getSimpleName(), ttAblageort); |
| | | types.put(Einstellung.class.getSimpleName(), ttEinstellung); |
| | | types.put(Abspieler.class.getSimpleName(), ttAbspieler); |
| | | types.put(Abspielliste.class.getSimpleName(), ttAbspielliste); |
| | | types.put(Titel.class.getSimpleName(), ttTitel); |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * Es wird in den Ordner geschrieben, der von conf angegeben ist |
| | | * |
| | | * Wenn es z.B. ein Ablage-Objekt ist, wird das Objekt in die Datei |
| | | * [conf]/Ablage/[name der Ablage].json geschrieben |
| | | * |
| | | * Der Name der Ablage muss eindeutig sein |
| | | * Wenn es z.B.ein Ablage-Objekt ist, wird das Objekt in die Datei |
| | | [conf]/Ablage/[name der Ablage].json geschrieben |
| | | |
| | | Der Name der Ablage muss eindeutig sein |
| | | * |
| | | * |
| | | * @param o |
| | | * @param entity das Objekt, das geschrieben werden soll |
| | | * @param overwrite true, wenn Aenderung, false fuer neue Elemente |
| | | * @return die Datei oder null, wenn die Datei existiert und ein |
| | | * neues Element (overwrite=false) uebergeben wurde |
| | | * @throws java.io.IOException |
| | | */ |
| | | 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() && !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 { |
| | |
| | | |
| | | public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
| | | String json = readFromFile(file); |
| | | logger.finer("json: " + json); |
| | | 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)); |
| | | logger.info(parts[parts.length-2]); |
| | | String[] parts = file.getPath().split(Server.SLASH); |
| | | 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; |
| | |
| | | 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; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public TypeToken typeFromName(String name) { |
| | | return types.get(name); |
| | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public List<Entity> listObjects(String typ) { |
| | | File base = new File(fileBase); |
| | | File dir = new File(base, typ); |
| | | List<Entity> list = new ArrayList(); |
| | | File[] files = dir.listFiles(); |
| | | if(files != null) { |
| | | for(File file : files) { |
| | | try { |
| | | list.add(entityFromFile(file)); |
| | | } catch (ClassNotFoundException | IOException ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | } |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | public boolean exists(String typ, String name) { |
| | | return getFile(typ, name).exists(); |
| | | } |
| | | |
| | | } |