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; |
d027b5
|
35 |
import de.uhilger.mediaz.entity.Livestream; |
e60cff
|
36 |
import de.uhilger.mediaz.entity.Titel; |
081606
|
37 |
import java.util.ArrayList; |
b1bf96
|
38 |
import java.util.HashMap; |
081606
|
39 |
import java.util.List; |
b1bf96
|
40 |
import java.util.Map; |
081606
|
41 |
import java.util.logging.Level; |
U |
42 |
|
|
43 |
/** |
2b5c60
|
44 |
* Ablage fuer Dateien der Mediazentrale |
081606
|
45 |
* |
U |
46 |
* [Basispfad]/[Typ]/[Name] |
|
47 |
* |
|
48 |
* Typ ist eine der Klassen der Package entity |
|
49 |
* Name muss identisch mit dem Element laut Entity.getName() sein |
|
50 |
* |
|
51 |
* @author ulrich |
|
52 |
* @version 1, 5.4.2021 |
|
53 |
*/ |
|
54 |
public class FileStorage implements Storage { |
|
55 |
|
|
56 |
private static final Logger logger = Logger.getLogger(FileStorage.class.getName()); |
|
57 |
|
|
58 |
/** StorageType Ablageort */ |
|
59 |
public static final String ST_ABLAGEORT = "Ablageort"; |
dce2c7
|
60 |
public static final String ST_EINSTELLUNG = "Einstellung"; |
3d4bca
|
61 |
public static final String ST_ABSPIELER = "Abspieler"; |
e60cff
|
62 |
public static final String ST_ABSPIELLISTE = "Abspielliste"; |
d027b5
|
63 |
public static final String ST_LIVESTREAM = "Livestream"; |
081606
|
64 |
|
cf6509
|
65 |
private final String fileBase; |
081606
|
66 |
|
cf6509
|
67 |
private final Map<String, TypeToken> types; |
b1bf96
|
68 |
|
081606
|
69 |
public FileStorage(String base) { |
U |
70 |
this.fileBase = base; |
b1bf96
|
71 |
|
5f70da
|
72 |
// Beispiel: TypeToken<List<String>> list = new TypeToken<List<String>>() {}; |
b1bf96
|
73 |
TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {}; |
cf6509
|
74 |
TypeToken<Einstellung> ttEinstellung = new TypeToken<Einstellung>() {}; |
3d4bca
|
75 |
TypeToken<Abspieler> ttAbspieler = new TypeToken<Abspieler>() {}; |
8d7d35
|
76 |
TypeToken<Abspielliste> ttAbspielliste = new TypeToken<Abspielliste>() {}; |
d027b5
|
77 |
TypeToken<Livestream> ttLivestream = new TypeToken<Livestream>() {}; |
e60cff
|
78 |
TypeToken<Titel> ttTitel = new TypeToken<Titel>() {}; |
b1bf96
|
79 |
types = new HashMap(); |
U |
80 |
types.put(Ablageort.class.getSimpleName(), ttAblageort); |
cf6509
|
81 |
types.put(Einstellung.class.getSimpleName(), ttEinstellung); |
3d4bca
|
82 |
types.put(Abspieler.class.getSimpleName(), ttAbspieler); |
8d7d35
|
83 |
types.put(Abspielliste.class.getSimpleName(), ttAbspielliste); |
e60cff
|
84 |
types.put(Titel.class.getSimpleName(), ttTitel); |
d027b5
|
85 |
types.put(Livestream.class.getSimpleName(), ttLivestream); |
081606
|
86 |
} |
U |
87 |
|
|
88 |
/** |
|
89 |
* Ein Objekt als JSON in eine Datei schreiben |
|
90 |
* |
|
91 |
* Es wird in den Ordner geschrieben, der von conf angegeben ist |
|
92 |
* |
cf6509
|
93 |
* Wenn es z.B.ein Ablage-Objekt ist, wird das Objekt in die Datei |
U |
94 |
[conf]/Ablage/[name der Ablage].json geschrieben |
|
95 |
|
|
96 |
Der Name der Ablage muss eindeutig sein |
081606
|
97 |
* |
U |
98 |
* |
cf6509
|
99 |
* @param entity das Objekt, das geschrieben werden soll |
U |
100 |
* @param overwrite true, wenn Aenderung, false fuer neue Elemente |
|
101 |
* @return die Datei oder null, wenn die Datei existiert und ein |
|
102 |
* neues Element (overwrite=false) uebergeben wurde |
|
103 |
* @throws java.io.IOException |
081606
|
104 |
*/ |
2597cd
|
105 |
public File writeToFile(Entity entity, boolean overwrite) throws IOException { |
2b5c60
|
106 |
String className = entity.getClass().getSimpleName(); |
081606
|
107 |
logger.finer(className); |
U |
108 |
File dir = new File(fileBase, className); |
|
109 |
dir.mkdirs(); |
2b5c60
|
110 |
File file = new File(dir, entity.getName()); |
2597cd
|
111 |
if(file.exists() && !overwrite) { |
U |
112 |
return null; |
|
113 |
} else { |
|
114 |
FileWriter fw = new FileWriter(file); |
|
115 |
Gson gson = new Gson(); |
|
116 |
fw.write(gson.toJson(entity)); |
|
117 |
fw.flush(); |
|
118 |
fw.close(); |
|
119 |
return file; |
081606
|
120 |
} |
U |
121 |
} |
|
122 |
|
5f70da
|
123 |
public String readFromFile(File file) throws IOException { |
081606
|
124 |
StringBuilder sb = new StringBuilder(); |
5f70da
|
125 |
BufferedReader r = new BufferedReader(new FileReader(file)); |
081606
|
126 |
String line = r.readLine(); |
U |
127 |
while(line != null) { |
|
128 |
sb.append(line); |
|
129 |
line = r.readLine(); |
|
130 |
} |
|
131 |
r.close(); |
5f70da
|
132 |
return sb.toString(); |
U |
133 |
} |
|
134 |
|
|
135 |
public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
|
136 |
String json = readFromFile(file); |
e60cff
|
137 |
logger.finer("json: " + json); |
081606
|
138 |
Gson gson = new Gson(); |
5f70da
|
139 |
return gson.fromJson(json, typeFromName(typeNameFromPath(file)).getType()); |
081606
|
140 |
} |
U |
141 |
|
b1bf96
|
142 |
private String typeNameFromPath(File file) { |
0e9cd3
|
143 |
String[] parts = file.getPath().split(Server.SLASH); |
081606
|
144 |
return parts[parts.length-2]; |
U |
145 |
} |
|
146 |
|
|
147 |
@Override |
2597cd
|
148 |
public Object write(Entity e, boolean overwrite) { |
081606
|
149 |
try { |
2597cd
|
150 |
return writeToFile(e, overwrite); |
081606
|
151 |
} catch (IOException ex) { |
U |
152 |
logger.log(Level.SEVERE, null, ex); |
|
153 |
return null; |
|
154 |
} |
|
155 |
} |
|
156 |
|
|
157 |
@Override |
|
158 |
public Entity read(String typ, String name) { |
|
159 |
try { |
5f70da
|
160 |
return entityFromFile(getFile(typ, name)); |
081606
|
161 |
} catch (ClassNotFoundException | IOException ex) { |
U |
162 |
logger.log(Level.SEVERE, null, ex); |
|
163 |
return null; |
|
164 |
} |
|
165 |
} |
|
166 |
|
|
167 |
@Override |
|
168 |
public List<String> list(String typ) { |
|
169 |
File base = new File(fileBase); |
|
170 |
File dir = new File(base, typ); |
|
171 |
List<String> list = new ArrayList(); |
b29119
|
172 |
File[] files = dir.listFiles(); |
U |
173 |
if(files != null) { |
|
174 |
for(File file : files) { |
|
175 |
//NamedItem n = new NamedItem(); |
|
176 |
//n.setLabel(file.getName()); |
|
177 |
list.add(file.getName()); |
|
178 |
} |
081606
|
179 |
} |
U |
180 |
return list; |
|
181 |
} |
f45e20
|
182 |
|
b1bf96
|
183 |
@Override |
U |
184 |
public TypeToken typeFromName(String name) { |
|
185 |
return types.get(name); |
|
186 |
} |
|
187 |
|
5f70da
|
188 |
@Override |
U |
189 |
public String readJson(String typ, String name) { |
|
190 |
try { |
|
191 |
return readFromFile(getFile(typ, name)); |
|
192 |
} catch (IOException ex) { |
|
193 |
logger.log(Level.SEVERE, null, ex); |
|
194 |
return null; |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
private File getFile(String typ, String name) { |
|
199 |
File base = new File(fileBase); |
|
200 |
File dir = new File(base, typ); |
|
201 |
return new File(dir, name); |
|
202 |
} |
|
203 |
|
2b5c60
|
204 |
@Override |
U |
205 |
public boolean delete(String typ, String name) { |
|
206 |
File file = getFile(typ, name); |
|
207 |
if(file.exists()) { |
|
208 |
return file.delete(); |
|
209 |
} else { |
|
210 |
return false; |
|
211 |
} |
|
212 |
} |
f45e20
|
213 |
|
U |
214 |
@Override |
|
215 |
public List<Entity> listObjects(String typ) { |
|
216 |
File base = new File(fileBase); |
|
217 |
File dir = new File(base, typ); |
|
218 |
List<Entity> list = new ArrayList(); |
|
219 |
File[] files = dir.listFiles(); |
|
220 |
if(files != null) { |
|
221 |
for(File file : files) { |
|
222 |
try { |
|
223 |
list.add(entityFromFile(file)); |
|
224 |
} catch (ClassNotFoundException | IOException ex) { |
|
225 |
logger.log(Level.SEVERE, null, ex); |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
229 |
return list; |
|
230 |
} |
081606
|
231 |
|
dfb7d3
|
232 |
public boolean exists(String typ, String name) { |
U |
233 |
return getFile(typ, name).exists(); |
|
234 |
} |
|
235 |
|
081606
|
236 |
} |