commit | author | age
|
081606
|
1 |
/* |
2b5c60
|
2 |
Mediazentrale - Personal Media Center |
U |
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
081606
|
17 |
*/ |
U |
18 |
package de.uhilger.mediaz.store; |
|
19 |
|
|
20 |
import com.google.gson.Gson; |
b1bf96
|
21 |
import com.google.gson.reflect.TypeToken; |
081606
|
22 |
import de.uhilger.mediaz.Server; |
U |
23 |
import de.uhilger.mediaz.entity.Ablageort; |
3d4bca
|
24 |
import de.uhilger.mediaz.entity.Abspieler; |
8d7d35
|
25 |
import de.uhilger.mediaz.entity.Abspielliste; |
cf6509
|
26 |
import de.uhilger.mediaz.entity.Einstellung; |
081606
|
27 |
import java.io.BufferedReader; |
U |
28 |
import java.io.File; |
|
29 |
import java.io.FileNotFoundException; |
|
30 |
import java.io.FileReader; |
|
31 |
import java.io.FileWriter; |
|
32 |
import java.io.IOException; |
|
33 |
import java.util.logging.Logger; |
|
34 |
import de.uhilger.mediaz.entity.Entity; |
3929b0
|
35 |
import de.uhilger.mediaz.entity.Geraet; |
d027b5
|
36 |
import de.uhilger.mediaz.entity.Livestream; |
e60cff
|
37 |
import de.uhilger.mediaz.entity.Titel; |
081606
|
38 |
import java.util.ArrayList; |
b1bf96
|
39 |
import java.util.HashMap; |
081606
|
40 |
import java.util.List; |
b1bf96
|
41 |
import java.util.Map; |
081606
|
42 |
import java.util.logging.Level; |
U |
43 |
|
|
44 |
/** |
2b5c60
|
45 |
* Ablage fuer Dateien der Mediazentrale |
081606
|
46 |
* |
U |
47 |
* [Basispfad]/[Typ]/[Name] |
|
48 |
* |
|
49 |
* Typ ist eine der Klassen der Package entity |
|
50 |
* Name muss identisch mit dem Element laut Entity.getName() sein |
|
51 |
* |
|
52 |
* @author ulrich |
|
53 |
* @version 1, 5.4.2021 |
|
54 |
*/ |
|
55 |
public class FileStorage implements Storage { |
|
56 |
|
|
57 |
private static final Logger logger = Logger.getLogger(FileStorage.class.getName()); |
|
58 |
|
|
59 |
/** StorageType Ablageort */ |
|
60 |
public static final String ST_ABLAGEORT = "Ablageort"; |
dce2c7
|
61 |
public static final String ST_EINSTELLUNG = "Einstellung"; |
3d4bca
|
62 |
public static final String ST_ABSPIELER = "Abspieler"; |
e60cff
|
63 |
public static final String ST_ABSPIELLISTE = "Abspielliste"; |
d027b5
|
64 |
public static final String ST_LIVESTREAM = "Livestream"; |
3929b0
|
65 |
public static final String ST_GERAET = "Geraet"; |
081606
|
66 |
|
cf6509
|
67 |
private final String fileBase; |
081606
|
68 |
|
cf6509
|
69 |
private final Map<String, TypeToken> types; |
b1bf96
|
70 |
|
081606
|
71 |
public FileStorage(String base) { |
U |
72 |
this.fileBase = base; |
b1bf96
|
73 |
|
5f70da
|
74 |
// Beispiel: TypeToken<List<String>> list = new TypeToken<List<String>>() {}; |
b1bf96
|
75 |
TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {}; |
cf6509
|
76 |
TypeToken<Einstellung> ttEinstellung = new TypeToken<Einstellung>() {}; |
3d4bca
|
77 |
TypeToken<Abspieler> ttAbspieler = new TypeToken<Abspieler>() {}; |
8d7d35
|
78 |
TypeToken<Abspielliste> ttAbspielliste = new TypeToken<Abspielliste>() {}; |
d027b5
|
79 |
TypeToken<Livestream> ttLivestream = new TypeToken<Livestream>() {}; |
3929b0
|
80 |
TypeToken<Geraet> ttGeraet = new TypeToken<Geraet>() {}; |
e60cff
|
81 |
TypeToken<Titel> ttTitel = new TypeToken<Titel>() {}; |
b1bf96
|
82 |
types = new HashMap(); |
U |
83 |
types.put(Ablageort.class.getSimpleName(), ttAblageort); |
cf6509
|
84 |
types.put(Einstellung.class.getSimpleName(), ttEinstellung); |
3d4bca
|
85 |
types.put(Abspieler.class.getSimpleName(), ttAbspieler); |
8d7d35
|
86 |
types.put(Abspielliste.class.getSimpleName(), ttAbspielliste); |
e60cff
|
87 |
types.put(Titel.class.getSimpleName(), ttTitel); |
d027b5
|
88 |
types.put(Livestream.class.getSimpleName(), ttLivestream); |
3929b0
|
89 |
types.put(Geraet.class.getSimpleName(), ttGeraet); |
081606
|
90 |
} |
U |
91 |
|
|
92 |
/** |
|
93 |
* Ein Objekt als JSON in eine Datei schreiben |
|
94 |
* |
|
95 |
* Es wird in den Ordner geschrieben, der von conf angegeben ist |
|
96 |
* |
cf6509
|
97 |
* Wenn es z.B.ein Ablage-Objekt ist, wird das Objekt in die Datei |
U |
98 |
[conf]/Ablage/[name der Ablage].json geschrieben |
|
99 |
|
|
100 |
Der Name der Ablage muss eindeutig sein |
081606
|
101 |
* |
U |
102 |
* |
cf6509
|
103 |
* @param entity das Objekt, das geschrieben werden soll |
U |
104 |
* @param overwrite true, wenn Aenderung, false fuer neue Elemente |
|
105 |
* @return die Datei oder null, wenn die Datei existiert und ein |
|
106 |
* neues Element (overwrite=false) uebergeben wurde |
|
107 |
* @throws java.io.IOException |
081606
|
108 |
*/ |
2597cd
|
109 |
public File writeToFile(Entity entity, boolean overwrite) throws IOException { |
2b5c60
|
110 |
String className = entity.getClass().getSimpleName(); |
081606
|
111 |
logger.finer(className); |
U |
112 |
File dir = new File(fileBase, className); |
|
113 |
dir.mkdirs(); |
2b5c60
|
114 |
File file = new File(dir, entity.getName()); |
2597cd
|
115 |
if(file.exists() && !overwrite) { |
U |
116 |
return null; |
|
117 |
} else { |
|
118 |
FileWriter fw = new FileWriter(file); |
|
119 |
Gson gson = new Gson(); |
|
120 |
fw.write(gson.toJson(entity)); |
|
121 |
fw.flush(); |
|
122 |
fw.close(); |
|
123 |
return file; |
081606
|
124 |
} |
U |
125 |
} |
|
126 |
|
5f70da
|
127 |
public String readFromFile(File file) throws IOException { |
081606
|
128 |
StringBuilder sb = new StringBuilder(); |
5f70da
|
129 |
BufferedReader r = new BufferedReader(new FileReader(file)); |
081606
|
130 |
String line = r.readLine(); |
U |
131 |
while(line != null) { |
|
132 |
sb.append(line); |
|
133 |
line = r.readLine(); |
|
134 |
} |
|
135 |
r.close(); |
5f70da
|
136 |
return sb.toString(); |
U |
137 |
} |
|
138 |
|
|
139 |
public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
|
140 |
String json = readFromFile(file); |
e60cff
|
141 |
logger.finer("json: " + json); |
081606
|
142 |
Gson gson = new Gson(); |
5f70da
|
143 |
return gson.fromJson(json, typeFromName(typeNameFromPath(file)).getType()); |
081606
|
144 |
} |
U |
145 |
|
b1bf96
|
146 |
private String typeNameFromPath(File file) { |
0e9cd3
|
147 |
String[] parts = file.getPath().split(Server.SLASH); |
081606
|
148 |
return parts[parts.length-2]; |
U |
149 |
} |
|
150 |
|
|
151 |
@Override |
2597cd
|
152 |
public Object write(Entity e, boolean overwrite) { |
081606
|
153 |
try { |
2597cd
|
154 |
return writeToFile(e, overwrite); |
081606
|
155 |
} catch (IOException ex) { |
U |
156 |
logger.log(Level.SEVERE, null, ex); |
|
157 |
return null; |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 |
public Entity read(String typ, String name) { |
|
163 |
try { |
5f70da
|
164 |
return entityFromFile(getFile(typ, name)); |
081606
|
165 |
} catch (ClassNotFoundException | IOException ex) { |
U |
166 |
logger.log(Level.SEVERE, null, ex); |
|
167 |
return null; |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
@Override |
|
172 |
public List<String> list(String typ) { |
|
173 |
File base = new File(fileBase); |
|
174 |
File dir = new File(base, typ); |
|
175 |
List<String> list = new ArrayList(); |
b29119
|
176 |
File[] files = dir.listFiles(); |
U |
177 |
if(files != null) { |
|
178 |
for(File file : files) { |
|
179 |
//NamedItem n = new NamedItem(); |
|
180 |
//n.setLabel(file.getName()); |
|
181 |
list.add(file.getName()); |
|
182 |
} |
081606
|
183 |
} |
U |
184 |
return list; |
|
185 |
} |
f45e20
|
186 |
|
b1bf96
|
187 |
@Override |
U |
188 |
public TypeToken typeFromName(String name) { |
|
189 |
return types.get(name); |
|
190 |
} |
|
191 |
|
5f70da
|
192 |
@Override |
U |
193 |
public String readJson(String typ, String name) { |
|
194 |
try { |
|
195 |
return readFromFile(getFile(typ, name)); |
|
196 |
} catch (IOException ex) { |
|
197 |
logger.log(Level.SEVERE, null, ex); |
|
198 |
return null; |
|
199 |
} |
|
200 |
} |
|
201 |
|
|
202 |
private File getFile(String typ, String name) { |
|
203 |
File base = new File(fileBase); |
|
204 |
File dir = new File(base, typ); |
|
205 |
return new File(dir, name); |
|
206 |
} |
|
207 |
|
2b5c60
|
208 |
@Override |
U |
209 |
public boolean delete(String typ, String name) { |
|
210 |
File file = getFile(typ, name); |
|
211 |
if(file.exists()) { |
|
212 |
return file.delete(); |
|
213 |
} else { |
|
214 |
return false; |
|
215 |
} |
|
216 |
} |
f45e20
|
217 |
|
U |
218 |
@Override |
|
219 |
public List<Entity> listObjects(String typ) { |
|
220 |
File base = new File(fileBase); |
|
221 |
File dir = new File(base, typ); |
|
222 |
List<Entity> list = new ArrayList(); |
|
223 |
File[] files = dir.listFiles(); |
|
224 |
if(files != null) { |
|
225 |
for(File file : files) { |
|
226 |
try { |
|
227 |
list.add(entityFromFile(file)); |
|
228 |
} catch (ClassNotFoundException | IOException ex) { |
|
229 |
logger.log(Level.SEVERE, null, ex); |
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
233 |
return list; |
|
234 |
} |
081606
|
235 |
|
dfb7d3
|
236 |
public boolean exists(String typ, String name) { |
U |
237 |
return getFile(typ, name).exists(); |
|
238 |
} |
|
239 |
|
081606
|
240 |
} |