-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replacing configration file with database
- Loading branch information
Showing
19 changed files
with
276 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 0 additions & 63 deletions
63
src/main/java/uk/yermak/audiobookconverter/AppProperties.java
This file was deleted.
Oops, something went wrong.
163 changes: 163 additions & 0 deletions
163
src/main/java/uk/yermak/audiobookconverter/AppSetting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package uk.yermak.audiobookconverter; | ||
|
||
import javafx.collections.FXCollections; | ||
import javafx.collections.ObservableList; | ||
import jetbrains.exodus.ByteIterable; | ||
import jetbrains.exodus.bindings.StringBinding; | ||
import jetbrains.exodus.entitystore.Entity; | ||
import jetbrains.exodus.entitystore.EntityIterable; | ||
import jetbrains.exodus.entitystore.PersistentEntityStore; | ||
import jetbrains.exodus.entitystore.PersistentEntityStores; | ||
import jetbrains.exodus.env.*; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.lang.invoke.MethodHandles; | ||
import java.util.*; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class AppSetting { | ||
final static Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
public static final File APP_DIR = new File(System.getProperty("APP_HOME")); | ||
public static final String PRESET_ENTITY = "Preset"; | ||
public static final String PRESET_NAME = "name"; | ||
public static final String PRESET_FORMAT = "format"; | ||
public static final String PRESET_BITRATE = "bitrate"; | ||
public static final String PRESET_FREQUENCY = "frequency"; | ||
public static final String PRESET_CHANNELS = "channels"; | ||
public static final String PRESET_CUTOFF = "cutoff"; | ||
public static final String PRESET_CBR = "cbr"; | ||
public static final String PRESET_QUALITY = "quality"; | ||
public static final String GENRE_ENTITY = "Genre"; | ||
public static final String GENRE_TITLE = "title"; | ||
public static final String GENRE_CREATED = "created"; | ||
public static final String SETTINGS = "Settings"; | ||
|
||
|
||
public static String getProperty(String key) { | ||
String result; | ||
try (jetbrains.exodus.env.Environment env = Environments.newInstance(APP_DIR.getPath())) { | ||
result = env.computeInReadonlyTransaction(txn -> { | ||
final Store store = env.openStore(SETTINGS, StoreConfig.WITHOUT_DUPLICATES, txn); | ||
ByteIterable entry = store.get(txn, StringBinding.stringToEntry(key)); | ||
if (entry!=null) { | ||
return StringBinding.entryToString(entry); | ||
} else { | ||
return null; | ||
} | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
public static synchronized void setProperty(String key, String value) { | ||
try (jetbrains.exodus.env.Environment env = Environments.newInstance(APP_DIR.getPath())) { | ||
env.executeInTransaction(txn -> { | ||
final Store store = env.openStore(SETTINGS, StoreConfig.WITHOUT_DUPLICATES, txn); | ||
store.put(txn, StringBinding.stringToEntry(key), StringBinding.stringToEntry(value)); | ||
}); | ||
} | ||
} | ||
|
||
public static void saveGenres(String genre) { | ||
if (StringUtils.isNotEmpty(genre) & loadGenres().stream().noneMatch(s -> s.equals(genre))) { | ||
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) { | ||
entityStore.executeInTransaction(txn -> { | ||
final Entity genreEntity = txn.newEntity(GENRE_ENTITY); | ||
genreEntity.setProperty(GENRE_TITLE, genre); | ||
genreEntity.setProperty(GENRE_CREATED, System.currentTimeMillis()); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
public static ObservableList<String> loadGenres() { | ||
ObservableList<String> genres; | ||
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) { | ||
genres = entityStore.computeInReadonlyTransaction(txn -> { | ||
ObservableList<String> list = FXCollections.observableArrayList(); | ||
StreamSupport.stream(txn.sort(GENRE_ENTITY, GENRE_TITLE, true).spliterator(), false).forEach(g -> list.add((String) g.getProperty(GENRE_TITLE))); | ||
return list; | ||
}); | ||
} | ||
return genres; | ||
} | ||
|
||
public static void removeGenre(String genre) { | ||
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) { | ||
entityStore.executeInTransaction(txn -> { | ||
EntityIterable entities = txn.find(GENRE_ENTITY, GENRE_TITLE, genre); | ||
for (Entity entity : entities) { | ||
entity.delete(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public static List<Preset> loadPresets() { | ||
List<Preset> presets; | ||
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) { | ||
presets = entityStore.computeInReadonlyTransaction(txn -> { | ||
List<Preset> list = new ArrayList<>(); | ||
EntityIterable all = txn.getAll(PRESET_ENTITY); | ||
for (Entity entity : all) { | ||
Preset preset = bindPreset(entity); | ||
list.add(preset); | ||
} | ||
return list; | ||
}); | ||
} | ||
return presets; | ||
} | ||
|
||
@NotNull | ||
private static Preset bindPreset(Entity entity) { | ||
String name = (String) entity.getProperty(PRESET_NAME); | ||
String format = (String) entity.getProperty(PRESET_FORMAT); | ||
Integer bitrate = (Integer) entity.getProperty(PRESET_BITRATE); | ||
Integer frequency = (Integer) entity.getProperty(PRESET_FREQUENCY); | ||
Integer channels = (Integer) entity.getProperty(PRESET_CHANNELS); | ||
Integer cutoff = (Integer) entity.getProperty(PRESET_CUTOFF); | ||
Boolean cbr = (Boolean) entity.getProperty(PRESET_CBR); | ||
Integer quality = (Integer) entity.getProperty(PRESET_QUALITY); | ||
Preset preset = new Preset(name, new OutputParameters(Format.instance(format), bitrate, frequency, channels, cutoff, cbr, quality)); | ||
return preset; | ||
} | ||
|
||
public static void savePreset(Preset preset) { | ||
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) { | ||
entityStore.executeInTransaction(txn -> { | ||
Entity entity; | ||
EntityIterable entities = txn.find(PRESET_ENTITY, PRESET_NAME, preset.getName()); | ||
if (entities.isEmpty()) { | ||
entity = txn.newEntity(PRESET_ENTITY); | ||
} else { | ||
entity = entities.getFirst(); | ||
} | ||
entity.setProperty(PRESET_NAME, preset.getName()); | ||
entity.setProperty(PRESET_FORMAT, preset.getFormat().format); | ||
entity.setProperty(PRESET_BITRATE, preset.getBitRate()); | ||
entity.setProperty(PRESET_FREQUENCY, preset.getFrequency()); | ||
entity.setProperty(PRESET_CHANNELS, preset.getChannels()); | ||
entity.setProperty(PRESET_CUTOFF, preset.getCutoff()); | ||
entity.setProperty(PRESET_CBR, preset.isCbr()); | ||
entity.setProperty(PRESET_QUALITY, preset.getVbrQuality()); | ||
}); | ||
} | ||
} | ||
|
||
public static Preset loadPreset(String presetName) { | ||
Preset preset; | ||
try (PersistentEntityStore entityStore = PersistentEntityStores.newInstance(APP_DIR.getPath())) { | ||
preset = entityStore.computeInReadonlyTransaction(txn -> { | ||
EntityIterable entities = txn.find(PRESET_ENTITY, PRESET_NAME, presetName); | ||
if (entities.isEmpty()) return null; | ||
return bindPreset(entities.getFirst()); | ||
}); | ||
} | ||
return preset; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.