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