| | |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import de.uhilger.mediaz.App; |
| | | import de.uhilger.mediaz.Server; |
| | | import de.uhilger.mediaz.entity.Ablageort; |
| | | import de.uhilger.mediaz.entity.Abspieler; |
| | | import de.uhilger.mediaz.entity.Abspielliste; |
| | | import de.uhilger.mediaz.entity.Einstellung; |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | |
| | | import java.io.IOException; |
| | | import java.util.logging.Logger; |
| | | import de.uhilger.mediaz.entity.Entity; |
| | | import de.uhilger.mediaz.entity.Geraet; |
| | | import de.uhilger.mediaz.entity.Livestream; |
| | | import de.uhilger.mediaz.entity.Titel; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | |
| | | /** StorageType Ablageort */ |
| | | public static final String ST_ABLAGEORT = "Ablageort"; |
| | | public static final String ST_EINSTELLUNG = "Einstellung"; |
| | | public static final String ST_ABSPIELER = "Abspieler"; |
| | | public static final String ST_ABSPIELLISTE = "Abspielliste"; |
| | | public static final String ST_LIVESTREAM = "Livestream"; |
| | | public static final String ST_GERAET = "Geraet"; |
| | | |
| | | private final String fileBase; |
| | | |
| | |
| | | // Beispiel: TypeToken<List<String>> list = new TypeToken<List<String>>() {}; |
| | | TypeToken<Ablageort> ttAblageort = new TypeToken<Ablageort>() {}; |
| | | TypeToken<Einstellung> ttEinstellung = new TypeToken<Einstellung>() {}; |
| | | TypeToken<Abspieler> ttAbspieler = new TypeToken<Abspieler>() {}; |
| | | TypeToken<Abspielliste> ttAbspielliste = new TypeToken<Abspielliste>() {}; |
| | | TypeToken<Livestream> ttLivestream = new TypeToken<Livestream>() {}; |
| | | TypeToken<Geraet> ttGeraet = new TypeToken<Geraet>() {}; |
| | | TypeToken<Titel> ttTitel = new TypeToken<Titel>() {}; |
| | | types = new HashMap(); |
| | | types.put(Ablageort.class.getSimpleName(), ttAblageort); |
| | | types.put(Einstellung.class.getSimpleName(), ttEinstellung); |
| | | types.put(Abspieler.class.getSimpleName(), ttAbspieler); |
| | | types.put(Abspielliste.class.getSimpleName(), ttAbspielliste); |
| | | types.put(Titel.class.getSimpleName(), ttTitel); |
| | | types.put(Livestream.class.getSimpleName(), ttLivestream); |
| | | types.put(Geraet.class.getSimpleName(), ttGeraet); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | public Entity entityFromFile(File file) throws ClassNotFoundException, FileNotFoundException, IOException { |
| | | String json = readFromFile(file); |
| | | logger.finer("json: " + json); |
| | | Gson gson = new Gson(); |
| | | return gson.fromJson(json, typeFromName(typeNameFromPath(file)).getType()); |
| | | } |
| | | |
| | | private String typeNameFromPath(File file) { |
| | | String[] parts = file.getPath().split(App.getRs(Server.RB_SLASH)); |
| | | String[] parts = file.getPath().split(Server.SLASH); |
| | | return parts[parts.length-2]; |
| | | } |
| | | |
| | |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public TypeToken typeFromName(String name) { |
| | | return types.get(name); |
| | |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public List<Entity> listObjects(String typ) { |
| | | File base = new File(fileBase); |
| | | File dir = new File(base, typ); |
| | | List<Entity> list = new ArrayList(); |
| | | File[] files = dir.listFiles(); |
| | | if(files != null) { |
| | | for(File file : files) { |
| | | try { |
| | | list.add(entityFromFile(file)); |
| | | } catch (ClassNotFoundException | IOException ex) { |
| | | logger.log(Level.SEVERE, null, ex); |
| | | } |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | public boolean exists(String typ, String name) { |
| | | return getFile(typ, name).exists(); |
| | | } |
| | | |
| | | } |