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