/*
|
* 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.store;
|
|
import com.google.gson.Gson;
|
import de.uhilger.mediaz.App;
|
import de.uhilger.mediaz.Server;
|
import de.uhilger.mediaz.entity.Ablageort;
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileNotFoundException;
|
import java.io.FileReader;
|
import java.io.FileWriter;
|
import java.io.IOException;
|
import java.util.logging.Logger;
|
import de.uhilger.mediaz.entity.Entity;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.logging.Level;
|
|
/**
|
* Ablage fuer Dateien
|
*
|
* [Basispfad]/[Typ]/[Name]
|
*
|
* Typ ist eine der Klassen der Package entity
|
* Name muss identisch mit dem Element laut Entity.getName() sein
|
*
|
* @author ulrich
|
* @version 1, 5.4.2021
|
*/
|
public class FileStorage implements Storage {
|
|
private static final Logger logger = Logger.getLogger(FileStorage.class.getName());
|
|
/** StorageType Ablageort */
|
public static final String ST_ABLAGEORT = "Ablageort";
|
|
private String fileBase;
|
|
public FileStorage(String base) {
|
this.fileBase = base;
|
}
|
|
/**
|
* Ein Objekt als JSON in eine Datei schreiben
|
*
|
* 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
|
*
|
*
|
* @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);
|
dir.mkdirs();
|
File file = new File(dir, o.getName());
|
if(file.exists()) {
|
file.delete();
|
}
|
FileWriter fw = new FileWriter(file);
|
fw.write(gson.toJson(o));
|
fw.flush();
|
fw.close();
|
return file;
|
}
|
|
public Entity readFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException {
|
String type = typeFromName(file);
|
StringBuilder sb = new StringBuilder();
|
FileReader in = new FileReader(file);
|
BufferedReader r = new BufferedReader(in);
|
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;
|
}
|
}
|
|
private String typeFromName(File file) {
|
//String path = file.getPath();
|
//logger.info(path);
|
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];
|
}
|
|
@Override
|
public Object write(Entity e) {
|
try {
|
return writeToFile(e);
|
} catch (IOException ex) {
|
logger.log(Level.SEVERE, null, ex);
|
return null;
|
}
|
}
|
|
@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);
|
} catch (ClassNotFoundException | IOException ex) {
|
logger.log(Level.SEVERE, null, ex);
|
return null;
|
}
|
}
|
|
@Override
|
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());
|
}
|
return list;
|
}
|
|
|
|
}
|