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