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