Persoenliche Mediazentrale
ulrich
2021-04-04 cfa85894465dbf2d286e083d962babdf14641582
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * 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.conf;
 
import com.google.gson.Gson;
import de.uhilger.mediaz.App;
import de.uhilger.mediaz.Server;
import de.uhilger.mediaz.entity.Ablageort;
import de.uhilger.mediaz.entity.ConfigurationElement;
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;
 
/**
 *
 * @author ulrich
 */
public class Store {
  
  private static final Logger logger = Logger.getLogger(Store.class.getName());
  
  
  private static final String typeAblageort = "Ablageort";
  
  /**
   * 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(ConfigurationElement o) throws IOException {
    Gson gson = new Gson();
    String className = o.getClass().getSimpleName();
    logger.finer(className); 
    File dir = new File(App.getInitParameter(App.getRs(App.RB_AP_CONF)), 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 ConfigurationElement 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 typeAblageort:
        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];
  }
}