commit | author | age
|
081606
|
1 |
/* |
U |
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package de.uhilger.mediaz.store; |
|
7 |
|
|
8 |
import com.google.gson.Gson; |
|
9 |
import de.uhilger.mediaz.App; |
|
10 |
import de.uhilger.mediaz.Server; |
|
11 |
import de.uhilger.mediaz.entity.Ablageort; |
|
12 |
import java.io.BufferedReader; |
|
13 |
import java.io.File; |
|
14 |
import java.io.FileNotFoundException; |
|
15 |
import java.io.FileReader; |
|
16 |
import java.io.FileWriter; |
|
17 |
import java.io.IOException; |
|
18 |
import java.util.logging.Logger; |
|
19 |
import de.uhilger.mediaz.entity.Entity; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.List; |
|
22 |
import java.util.logging.Level; |
|
23 |
|
|
24 |
/** |
|
25 |
* Ablage fuer Dateien |
|
26 |
* |
|
27 |
* [Basispfad]/[Typ]/[Name] |
|
28 |
* |
|
29 |
* Typ ist eine der Klassen der Package entity |
|
30 |
* Name muss identisch mit dem Element laut Entity.getName() sein |
|
31 |
* |
|
32 |
* @author ulrich |
|
33 |
* @version 1, 5.4.2021 |
|
34 |
*/ |
|
35 |
public class FileStorage implements Storage { |
|
36 |
|
|
37 |
private static final Logger logger = Logger.getLogger(FileStorage.class.getName()); |
|
38 |
|
|
39 |
/** StorageType Ablageort */ |
|
40 |
public static final String ST_ABLAGEORT = "Ablageort"; |
|
41 |
|
|
42 |
private String fileBase; |
|
43 |
|
|
44 |
public FileStorage(String base) { |
|
45 |
this.fileBase = base; |
|
46 |
} |
|
47 |
|
|
48 |
/** |
|
49 |
* Ein Objekt als JSON in eine Datei schreiben |
|
50 |
* |
|
51 |
* Es wird in den Ordner geschrieben, der von conf angegeben ist |
|
52 |
* |
|
53 |
* Wenn es z.B. ein Ablage-Objekt ist, wird das Objekt in die Datei |
|
54 |
* [conf]/Ablage/[name der Ablage].json geschrieben |
|
55 |
* |
|
56 |
* Der Name der Ablage muss eindeutig sein |
|
57 |
* |
|
58 |
* |
|
59 |
* @param o |
|
60 |
*/ |
|
61 |
public File writeToFile(Entity o) throws IOException { |
|
62 |
Gson gson = new Gson(); |
|
63 |
String className = o.getClass().getSimpleName(); |
|
64 |
logger.finer(className); |
|
65 |
File dir = new File(fileBase, className); |
|
66 |
dir.mkdirs(); |
|
67 |
File file = new File(dir, o.getName()); |
|
68 |
if(file.exists()) { |
|
69 |
file.delete(); |
|
70 |
} |
|
71 |
FileWriter fw = new FileWriter(file); |
|
72 |
fw.write(gson.toJson(o)); |
|
73 |
fw.flush(); |
|
74 |
fw.close(); |
|
75 |
return file; |
|
76 |
} |
|
77 |
|
|
78 |
public Entity readFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
|
79 |
String type = typeFromName(file); |
|
80 |
StringBuilder sb = new StringBuilder(); |
|
81 |
FileReader in = new FileReader(file); |
|
82 |
BufferedReader r = new BufferedReader(in); |
|
83 |
String line = r.readLine(); |
|
84 |
while(line != null) { |
|
85 |
sb.append(line); |
|
86 |
line = r.readLine(); |
|
87 |
} |
|
88 |
r.close(); |
|
89 |
in.close(); |
|
90 |
String json = sb.toString(); |
|
91 |
Gson gson = new Gson(); |
|
92 |
switch(type) { |
|
93 |
case ST_ABLAGEORT: |
|
94 |
return gson.fromJson(json, Ablageort.class); |
|
95 |
default: |
|
96 |
Ablageort ablage = new Ablageort(); |
|
97 |
ablage.setName("Test"); |
|
98 |
return ablage; |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
private String typeFromName(File file) { |
|
103 |
//String path = file.getPath(); |
|
104 |
//logger.info(path); |
|
105 |
String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH)); |
|
106 |
//for(String part : parts) { |
|
107 |
//logger.info(part); |
|
108 |
//} |
|
109 |
//logger.info("" + parts.length); |
|
110 |
logger.info(parts[parts.length-2]); |
|
111 |
return parts[parts.length-2]; |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public Object write(Entity e) { |
|
116 |
try { |
|
117 |
return writeToFile(e); |
|
118 |
} catch (IOException ex) { |
|
119 |
logger.log(Level.SEVERE, null, ex); |
|
120 |
return null; |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public Entity read(String typ, String name) { |
|
126 |
File base = new File(fileBase); |
|
127 |
File dir = new File(base, typ); |
|
128 |
File file = new File(dir, name); |
|
129 |
try { |
|
130 |
return readFromFile(file); |
|
131 |
} catch (ClassNotFoundException | IOException ex) { |
|
132 |
logger.log(Level.SEVERE, null, ex); |
|
133 |
return null; |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
@Override |
|
138 |
public List<String> list(String typ) { |
|
139 |
File base = new File(fileBase); |
|
140 |
File dir = new File(base, typ); |
|
141 |
File[] files = dir.listFiles(); |
|
142 |
List<String> list = new ArrayList(); |
|
143 |
for(File file : files) { |
|
144 |
list.add(file.getName()); |
|
145 |
} |
|
146 |
return list; |
|
147 |
} |
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
} |