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