/** * Generates a new {@link com.noiraak.enhrick.model.Entry Entry} from the given String and adds it * to the corresponding file on the disk. * * @param newText The content of the new {@link com.noiraak.enhrick.model.Entry Entry} * @return the list with including the newly generated {@link com.noiraak.enhrick.model.Entry * Entry} */ public List<Entry> generateNewEntries(String newText) { List<Entry> clonedEntries = new ArrayList<>(entries); Entry entry = new Entry( newText + "\n", ConfigurationProvider.getStringConfiguration(ConfigurationKey.USERNAME), System.currentTimeMillis()); fileManager.saveNewEntry(entry, FileConfigurationProvider.getFilePath()); // For some bizarre reason fileManager.saveNewEntry() completely empties entries sometimes - // hence the cloned list. if (entries.size() < clonedEntries.size()) { entries = clonedEntries; } entries.add(0, entry); if (entries.size() > ConfigurationProvider.getIntegerConfiguration(ConfigurationKey.MAX_RESULTS)) { entries = entries.subList( 0, ConfigurationProvider.getIntegerConfiguration(ConfigurationKey.MAX_RESULTS)); } return entries; }
/** * Edits an {@link com.noiraak.enhrick.model.Entry Entry} at the specified index with the given * String and adds it to the corresponding file on the disk. * * @param newText The content of the edited {@link com.noiraak.enhrick.model.Entry Entry} * @param selectedIndex The index of the {@link com.noiraak.enhrick.model.Entry Entry} to edit * @return the list with including the newly generated {@link com.noiraak.enhrick.model.Entry * Entry} */ public List<Entry> editEntry(String newText, int selectedIndex) { List<Entry> clonedEntries = new ArrayList<>(entries); Entry entry = entries.get(selectedIndex); entry.setText(newText + "\n"); entry.setModified(System.currentTimeMillis()); fileManager.saveEditedEntry(entry, selectedIndex, FileConfigurationProvider.getFilePath()); // For some bizarre reason fileManager.editEntry() completely empties entries sometimes - hence // the cloned list. if (entries.size() < clonedEntries.size()) { entries = clonedEntries; } entries.set(selectedIndex, entry); return entries; }
/** * Updates the username of entries. Also re-writes the entry-file. * * @param oldUsername the old username */ public void updateUsername(String oldUsername) { if (oldUsername.equals( ConfigurationProvider.getStringConfiguration(ConfigurationKey.USERNAME))) { return; } for (Entry entry : entries) { if (entry.getModifier() != null && entry.getModifier().equals(oldUsername)) { entry.setModifier(ConfigurationProvider.getStringConfiguration(ConfigurationKey.USERNAME)); } } fileManager.rewriteEntries( FileConfigurationProvider.getFilePath(), oldUsername, ConfigurationProvider.getStringConfiguration(ConfigurationKey.USERNAME)); }