commit | author | age
|
d86ba2
|
1 |
/* |
U |
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package de.uhilger.mediaz.conf; |
|
7 |
|
|
8 |
import com.google.gson.Gson; |
|
9 |
import de.uhilger.mediaz.App; |
|
10 |
import de.uhilger.mediaz.Server; |
|
11 |
import de.uhilger.mediaz.entity.Ablage; |
|
12 |
import de.uhilger.mediaz.entity.ConfigurationElement; |
|
13 |
import java.io.BufferedReader; |
|
14 |
import java.io.File; |
|
15 |
import java.io.FileInputStream; |
|
16 |
import java.io.FileNotFoundException; |
|
17 |
import java.io.FileReader; |
|
18 |
import java.io.FileWriter; |
|
19 |
import java.io.IOException; |
|
20 |
import java.io.InputStreamReader; |
|
21 |
import java.util.logging.Logger; |
|
22 |
|
|
23 |
/** |
|
24 |
* |
|
25 |
* @author ulrich |
|
26 |
*/ |
|
27 |
public class Store { |
|
28 |
|
|
29 |
private static final Logger logger = Logger.getLogger(Store.class.getName()); |
|
30 |
|
|
31 |
|
|
32 |
private static final String typeAblage = "Ablage"; |
|
33 |
|
|
34 |
/** |
|
35 |
* Ein Objekt als JSON in eine Datei schreiben |
|
36 |
* |
|
37 |
* Es wird in den Ordner geschrieben, der von conf angegeben ist |
|
38 |
* |
|
39 |
* Wenn es z.B. ein Ablage-Objekt ist, wird das Objekt in die Datei |
|
40 |
* [conf]/Ablage/[name der Ablage].json geschrieben |
|
41 |
* |
|
42 |
* Der Name der Ablage muss eindeutig sein |
|
43 |
* |
|
44 |
* |
|
45 |
* @param o |
|
46 |
*/ |
|
47 |
public File writeToFile(ConfigurationElement o) throws IOException { |
|
48 |
Gson gson = new Gson(); |
|
49 |
String className = o.getClass().getSimpleName(); |
|
50 |
logger.finer(className); |
|
51 |
File dir = new File(App.getInitParameter(App.getRs(App.RB_AP_CONF)), className); |
|
52 |
dir.mkdirs(); |
|
53 |
File file = new File(dir, o.getName()); |
|
54 |
if(file.exists()) { |
|
55 |
file.delete(); |
|
56 |
} |
|
57 |
FileWriter fw = new FileWriter(file); |
|
58 |
fw.write(gson.toJson(o)); |
|
59 |
fw.flush(); |
|
60 |
fw.close(); |
|
61 |
return file; |
|
62 |
} |
|
63 |
|
|
64 |
public ConfigurationElement readFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
|
65 |
String type = typeFromName(file); |
|
66 |
StringBuilder sb = new StringBuilder(); |
|
67 |
FileReader in = new FileReader(file); |
|
68 |
BufferedReader r = new BufferedReader(in); |
|
69 |
String line = r.readLine(); |
|
70 |
while(line != null) { |
|
71 |
sb.append(line); |
|
72 |
line = r.readLine(); |
|
73 |
} |
|
74 |
r.close(); |
|
75 |
in.close(); |
|
76 |
String json = sb.toString(); |
|
77 |
Gson gson = new Gson(); |
|
78 |
switch(type) { |
|
79 |
case typeAblage: |
|
80 |
return gson.fromJson(json, Ablage.class); |
|
81 |
default: |
|
82 |
Ablage ablage = new Ablage(); |
|
83 |
ablage.setName("Test"); |
|
84 |
return ablage; |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
private String typeFromName(File file) { |
|
89 |
//String path = file.getPath(); |
|
90 |
//logger.info(path); |
|
91 |
String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH)); |
|
92 |
//for(String part : parts) { |
|
93 |
//logger.info(part); |
|
94 |
//} |
|
95 |
//logger.info("" + parts.length); |
|
96 |
logger.info(parts[parts.length-2]); |
|
97 |
return parts[parts.length-2]; |
|
98 |
} |
|
99 |
} |