commit | author | age
|
081606
|
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.store; |
|
7 |
|
|
8 |
import com.google.gson.Gson; |
b1bf96
|
9 |
import com.google.gson.reflect.TypeToken; |
081606
|
10 |
import de.uhilger.mediaz.App; |
U |
11 |
import de.uhilger.mediaz.Server; |
|
12 |
import de.uhilger.mediaz.entity.Ablageort; |
|
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 |
import de.uhilger.mediaz.entity.Entity; |
|
21 |
import java.util.ArrayList; |
b1bf96
|
22 |
import java.util.HashMap; |
081606
|
23 |
import java.util.List; |
b1bf96
|
24 |
import java.util.Map; |
081606
|
25 |
import java.util.logging.Level; |
U |
26 |
|
|
27 |
/** |
|
28 |
* Ablage fuer Dateien |
|
29 |
* |
|
30 |
* [Basispfad]/[Typ]/[Name] |
|
31 |
* |
|
32 |
* Typ ist eine der Klassen der Package entity |
|
33 |
* Name muss identisch mit dem Element laut Entity.getName() sein |
|
34 |
* |
|
35 |
* @author ulrich |
|
36 |
* @version 1, 5.4.2021 |
|
37 |
*/ |
|
38 |
public class FileStorage implements Storage { |
|
39 |
|
|
40 |
private static final Logger logger = Logger.getLogger(FileStorage.class.getName()); |
|
41 |
|
|
42 |
/** StorageType Ablageort */ |
|
43 |
public static final String ST_ABLAGEORT = "Ablageort"; |
|
44 |
|
|
45 |
private String fileBase; |
|
46 |
|
b1bf96
|
47 |
private Map<String, TypeToken> types; |
U |
48 |
|
081606
|
49 |
public FileStorage(String base) { |
U |
50 |
this.fileBase = base; |
b1bf96
|
51 |
|
5f70da
|
52 |
// Beispiel: TypeToken<List<String>> list = new TypeToken<List<String>>() {}; |
b1bf96
|
53 |
TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {}; |
U |
54 |
types = new HashMap(); |
|
55 |
types.put(Ablageort.class.getSimpleName(), ttAblageort); |
081606
|
56 |
} |
U |
57 |
|
|
58 |
/** |
|
59 |
* Ein Objekt als JSON in eine Datei schreiben |
|
60 |
* |
|
61 |
* Es wird in den Ordner geschrieben, der von conf angegeben ist |
|
62 |
* |
|
63 |
* Wenn es z.B. ein Ablage-Objekt ist, wird das Objekt in die Datei |
|
64 |
* [conf]/Ablage/[name der Ablage].json geschrieben |
|
65 |
* |
|
66 |
* Der Name der Ablage muss eindeutig sein |
|
67 |
* |
|
68 |
* |
|
69 |
* @param o |
|
70 |
*/ |
|
71 |
public File writeToFile(Entity o) throws IOException { |
|
72 |
String className = o.getClass().getSimpleName(); |
|
73 |
logger.finer(className); |
|
74 |
File dir = new File(fileBase, className); |
|
75 |
dir.mkdirs(); |
|
76 |
File file = new File(dir, o.getName()); |
|
77 |
if(file.exists()) { |
|
78 |
file.delete(); |
|
79 |
} |
|
80 |
FileWriter fw = new FileWriter(file); |
5f70da
|
81 |
Gson gson = new Gson(); |
081606
|
82 |
fw.write(gson.toJson(o)); |
U |
83 |
fw.flush(); |
|
84 |
fw.close(); |
|
85 |
return file; |
|
86 |
} |
|
87 |
|
5f70da
|
88 |
public String readFromFile(File file) throws IOException { |
081606
|
89 |
StringBuilder sb = new StringBuilder(); |
5f70da
|
90 |
BufferedReader r = new BufferedReader(new FileReader(file)); |
081606
|
91 |
String line = r.readLine(); |
U |
92 |
while(line != null) { |
|
93 |
sb.append(line); |
|
94 |
line = r.readLine(); |
|
95 |
} |
|
96 |
r.close(); |
5f70da
|
97 |
return sb.toString(); |
U |
98 |
} |
|
99 |
|
|
100 |
public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
|
101 |
String json = readFromFile(file); |
081606
|
102 |
Gson gson = new Gson(); |
5f70da
|
103 |
return gson.fromJson(json, typeFromName(typeNameFromPath(file)).getType()); |
081606
|
104 |
} |
U |
105 |
|
b1bf96
|
106 |
private String typeNameFromPath(File file) { |
081606
|
107 |
String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH)); |
U |
108 |
logger.info(parts[parts.length-2]); |
|
109 |
return parts[parts.length-2]; |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public Object write(Entity e) { |
|
114 |
try { |
|
115 |
return writeToFile(e); |
|
116 |
} catch (IOException ex) { |
|
117 |
logger.log(Level.SEVERE, null, ex); |
|
118 |
return null; |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public Entity read(String typ, String name) { |
|
124 |
try { |
5f70da
|
125 |
return entityFromFile(getFile(typ, name)); |
081606
|
126 |
} catch (ClassNotFoundException | IOException ex) { |
U |
127 |
logger.log(Level.SEVERE, null, ex); |
|
128 |
return null; |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
@Override |
|
133 |
public List<String> list(String typ) { |
|
134 |
File base = new File(fileBase); |
|
135 |
File dir = new File(base, typ); |
|
136 |
File[] files = dir.listFiles(); |
|
137 |
List<String> list = new ArrayList(); |
|
138 |
for(File file : files) { |
|
139 |
list.add(file.getName()); |
|
140 |
} |
|
141 |
return list; |
|
142 |
} |
|
143 |
|
b1bf96
|
144 |
@Override |
U |
145 |
public TypeToken typeFromName(String name) { |
|
146 |
return types.get(name); |
|
147 |
} |
|
148 |
|
5f70da
|
149 |
@Override |
U |
150 |
public String readJson(String typ, String name) { |
|
151 |
try { |
|
152 |
return readFromFile(getFile(typ, name)); |
|
153 |
} catch (IOException ex) { |
|
154 |
logger.log(Level.SEVERE, null, ex); |
|
155 |
return null; |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
private File getFile(String typ, String name) { |
|
160 |
File base = new File(fileBase); |
|
161 |
File dir = new File(base, typ); |
|
162 |
return new File(dir, name); |
|
163 |
} |
|
164 |
|
081606
|
165 |
|
U |
166 |
|
|
167 |
} |