| | |
| | | public FileStorage(String base) { |
| | | this.fileBase = base; |
| | | |
| | | //TypeToken<List<String>> list = new TypeToken<List<String>>() {}; |
| | | // Beispiel: TypeToken<List<String>> list = new TypeToken<List<String>>() {}; |
| | | TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {}; |
| | | types = new HashMap(); |
| | | types.put(Ablageort.class.getSimpleName(), ttAblageort); |
| | |
| | | * @param o |
| | | */ |
| | | public File writeToFile(Entity o) throws IOException { |
| | | Gson gson = new Gson(); |
| | | String className = o.getClass().getSimpleName(); |
| | | logger.finer(className); |
| | | File dir = new File(fileBase, className); |
| | |
| | | file.delete(); |
| | | } |
| | | FileWriter fw = new FileWriter(file); |
| | | Gson gson = new Gson(); |
| | | fw.write(gson.toJson(o)); |
| | | fw.flush(); |
| | | fw.close(); |
| | | return file; |
| | | } |
| | | |
| | | public Entity readFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
| | | String type = typeNameFromPath(file); |
| | | public String readFromFile(File file) throws IOException { |
| | | StringBuilder sb = new StringBuilder(); |
| | | FileReader in = new FileReader(file); |
| | | BufferedReader r = new BufferedReader(in); |
| | | BufferedReader r = new BufferedReader(new FileReader(file)); |
| | | String line = r.readLine(); |
| | | while(line != null) { |
| | | sb.append(line); |
| | | line = r.readLine(); |
| | | } |
| | | r.close(); |
| | | in.close(); |
| | | String json = sb.toString(); |
| | | return sb.toString(); |
| | | } |
| | | |
| | | public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
| | | String json = readFromFile(file); |
| | | Gson gson = new Gson(); |
| | | switch(type) { |
| | | case ST_ABLAGEORT: |
| | | return gson.fromJson(json, Ablageort.class); |
| | | default: |
| | | Ablageort ablage = new Ablageort(); |
| | | ablage.setName("Test"); |
| | | return ablage; |
| | | } |
| | | return gson.fromJson(json, typeFromName(typeNameFromPath(file)).getType()); |
| | | } |
| | | |
| | | private String typeNameFromPath(File file) { |
| | |
| | | |
| | | @Override |
| | | public Entity read(String typ, String name) { |
| | | File base = new File(fileBase); |
| | | File dir = new File(base, typ); |
| | | File file = new File(dir, name); |
| | | try { |
| | | return readFromFile(file); |
| | | return entityFromFile(getFile(typ, name)); |
| | | } catch (ClassNotFoundException | IOException ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | return null; |
| | |
| | | return types.get(name); |
| | | } |
| | | |
| | | @Override |
| | | public String readJson(String typ, String name) { |
| | | try { |
| | | return readFromFile(getFile(typ, name)); |
| | | } catch (IOException ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private File getFile(String typ, String name) { |
| | | File base = new File(fileBase); |
| | | File dir = new File(base, typ); |
| | | return new File(dir, name); |
| | | } |
| | | |
| | | |
| | | |
| | | } |