Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move custom filters into client file + Add identifier field to custom filter item and use it as new link (fix #2546) #2931

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,181 @@
package name.abuchen.portfolio.ui.editor;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.eclipse.jface.preference.PreferenceStore;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.ConfigurationSet;
import name.abuchen.portfolio.model.ConfigurationSet.Configuration;
import name.abuchen.portfolio.ui.util.ClientFilterMenu;
import name.abuchen.portfolio.ui.util.ClientFilterMenu.ClientFilterStorageHelper;
import name.abuchen.portfolio.ui.util.ClientFilterMenu.Item;

class ClientFilterMigration
{

private PreferenceStore preferenceStore;
private Client client;

ClientFilterMigration(PreferenceStore preferenceStore, Client client)
{
this.preferenceStore = preferenceStore;
this.client = client;
}

public static void setOldSettingsToDefault(PreferenceStore pref)
{
pref.setToDefault("ClientFilterDropDown"); //$NON-NLS-1$
pref.setToDefault("ClientFilterDropDown@json"); //$NON-NLS-1$
Arrays.stream(pref.preferenceNames()).filter(name -> name.endsWith("-client-filter")) //$NON-NLS-1$
.forEach(name -> pref.setToDefault(name));
}

void migrateClientFilter()
{
// load custom filters
List<ClientFilterMenu.Item> clientFilters = loadFilters();
if (clientFilters == null || clientFilters.isEmpty())
return;

moveFiltersIntoClientFile(clientFilters);
migrateFilterLinksInSettingsFileAndMoveToClient(clientFilters);
migrateWidgets(clientFilters);
migrateCharts(clientFilters);

// signal user to save file
client.touch();
}

private void moveFiltersIntoClientFile(List<ClientFilterMenu.Item> clientFilters)
{
ConfigurationSet set = ClientFilterStorageHelper.getFilterConfigurationSet(client);
clientFilters.forEach(cf -> set.add(new Configuration(cf.getIdent(), cf.getLabel(), cf.getUUIDs())));
}

private List<Item> deserializeFromJSON(String json)
{
Type listType = new TypeToken<ArrayList<Item>>()
{
}.getType();
return new Gson().fromJson(json, listType);
}

private List<ClientFilterMenu.Item> loadFilters()
{
String json = preferenceStore.getString("ClientFilterDropDown@json"); //$NON-NLS-1$
if (json != null && !json.isEmpty())
return deserializeFromJSON(json);

// legacy loading
List<ClientFilterMenu.Item> clientFilters = null;
String legacy = preferenceStore.getString("ClientFilterDropDown"); //$NON-NLS-1$
if (legacy != null && !legacy.isEmpty())
return loadCustomItemsLegacy(legacy);

return clientFilters;
}

private List<Item> loadCustomItemsLegacy(String code)
{
List<Item> result = new LinkedList<>();
String[] items = code.split(";"); //$NON-NLS-1$
for (String item : items)
ClientFilterMenu.buildItem(UUID.randomUUID().toString(), "", item, client).ifPresent(result::add); //$NON-NLS-1$

return result;
}

private void migrateFilterLinksInSettingsFileAndMoveToClient(List<ClientFilterMenu.Item> clientFilters)
{
// prepare filter
Map<String, String> oldToNewIdentMap = new HashMap<>();
clientFilters.forEach(f -> oldToNewIdentMap.putIfAbsent(f.getUUIDs(), f.getIdent()));

// migrate each filter link in settings file and move into client file
Arrays.stream(preferenceStore.preferenceNames()).filter(name -> name.endsWith("-client-filter")) //$NON-NLS-1$
.forEach(prefKey -> migrateSingleClientFilterAndMoveIntoClientFile(prefKey, oldToNewIdentMap));

}

private void migrateSingleClientFilterAndMoveIntoClientFile(String prefKey, Map<String, String> oldToNewIdentMap)
{
String link = preferenceStore.getString(prefKey);
if (oldToNewIdentMap.containsKey(link))
{
ConfigurationSet set = ClientFilterStorageHelper.getFilterUsagesConfigurationSet(client);
set.add(new Configuration(prefKey, "", oldToNewIdentMap.get(link))); //$NON-NLS-1$
}
}

private void migrateWidgets(List<ClientFilterMenu.Item> clientFilters)
{
// create map with all possible values as dashboard settings (normal
// filter and pretax filter). Then iterate all widgets and check if
// any of the possible value is included in widget config, and
// migrate link if so.
Map<String, String> oldToNewIdentMap = new HashMap<>();
clientFilters.forEach(f -> {
String oldIdentWithComma = f.getUUIDs();
String oldIdentWithoutComma = oldIdentWithComma.replace(",", ""); //$NON-NLS-1$ //$NON-NLS-2$
String newIdent = f.getIdent();
// For DATA_SERIES and SECONDARY_DATA_SERIES
oldToNewIdentMap.putIfAbsent("ClientFilter" + oldIdentWithoutComma, "ClientFilter" + newIdent); //$NON-NLS-1$ //$NON-NLS-2$
// For DATA_SERIES and SECONDARY_DATA_SERIES
oldToNewIdentMap.putIfAbsent("ClientFilter-PreTax" + oldIdentWithoutComma, //$NON-NLS-1$
"ClientFilter-PreTax" + newIdent); //$NON-NLS-1$
// For CLIENT_FILTER.
oldToNewIdentMap.putIfAbsent(oldIdentWithComma, newIdent);
});
client.getDashboards().forEach(d -> d.getColumns().forEach(c -> c.getWidgets().forEach(w -> {
migrateSingleWidgetConfig("DATA_SERIES", w.getConfiguration(), oldToNewIdentMap); //$NON-NLS-1$
migrateSingleWidgetConfig("SECONDARY_DATA_SERIES", w.getConfiguration(), oldToNewIdentMap); //$NON-NLS-1$
migrateSingleWidgetConfig("CLIENT_FILTER", w.getConfiguration(), oldToNewIdentMap); //$NON-NLS-1$
})));
}

private void migrateSingleWidgetConfig(String configName, Map<String, String> configuration,
Map<String, String> oldToNewIdentMap)
{
String config = configuration.get(configName);
if (config != null && oldToNewIdentMap.containsKey(config))
{
configuration.put(configName, oldToNewIdentMap.get(config));
}
}

private void migrateCharts(List<ClientFilterMenu.Item> clientFilters)
{
Map<String, String> oldToNewIdentMap = new HashMap<>();
clientFilters.forEach(f -> oldToNewIdentMap.putIfAbsent(f.getUUIDs().replace(",", ""), f.getIdent())); //$NON-NLS-1$ //$NON-NLS-2$
String[] charts = new String[] { "StatementOfAssetsHistoryView-PICKER", "ReturnsVolatilityChartView-PICKER", //$NON-NLS-1$ //$NON-NLS-2$
"PerformanceChartView-PICKER" }; //$NON-NLS-1$
for (String chart : charts)
{
ConfigurationSet conf = client.getSettings().getConfigurationSet(chart);
if (conf == null)
continue;

conf.getConfigurations().forEach(c -> {
// check all filters if they are present in data and replace
// if so
oldToNewIdentMap.forEach((key, val) -> {
if (c.getData().contains(key))
{
c.setData(c.getData().replace(key, val));
}
});
});
}
}
}
Expand Up @@ -197,6 +197,12 @@ public void savePreferences()
storePreferences(false);
}

private void clientFilterMigrationSetOldSettingsToDefault()
{
if (client.shouldDoFilterMigration())
ClientFilterMigration.setOldSettingsToDefault(preferenceStore);
}

public void save(Shell shell)
{
if (clientFile == null)
Expand All @@ -212,6 +218,9 @@ public void save(Shell shell)
createBackup(clientFile, "backup"); //$NON-NLS-1$

ClientFactory.save(client, clientFile);

clientFilterMigrationSetOldSettingsToDefault();

storePreferences(false);

broker.post(UIConstants.Event.File.SAVED, clientFile.getAbsolutePath());
Expand Down Expand Up @@ -251,6 +260,9 @@ public void doSaveAs(Shell shell, String extension, Set<SaveFlag> flags) // NOSO
try
{
ClientFactory.saveAs(client, clientFile, pwd, flags);

clientFilterMigrationSetOldSettingsToDefault();

storePreferences(true);

broker.post(UIConstants.Event.File.SAVED, clientFile.getAbsolutePath());
Expand Down Expand Up @@ -497,9 +509,7 @@ private void loadPreferences()

private File getPreferenceStoreFile(File file) throws IOException
{
boolean storeNextToFile = preferences.getBoolean(UIConstants.Preferences.STORE_SETTINGS_NEXT_TO_FILE, false);

if (storeNextToFile)
if (preferences.getBoolean(UIConstants.Preferences.STORE_SETTINGS_NEXT_TO_FILE, false))
{
String filename = file.getName();
int last = filename.lastIndexOf('.');
Expand Down Expand Up @@ -691,6 +701,10 @@ private void scheduleAutoSaveJob()
});

loadPreferences();
if (client.shouldDoFilterMigration())
{
new ClientFilterMigration(preferenceStore, client).migrateClientFilter();
}

scheduleOnlineUpdateJobs();

Expand All @@ -711,4 +725,5 @@ private void scheduleAutoSaveJob()
{
this.listeners.forEach(consumer::accept);
}

}
Expand Up @@ -25,17 +25,12 @@ public ClientFilterDropDown(Client client, IPreferenceStore preferences, String
this.menu = new ClientFilterMenu(client, preferences, listener);
this.menu.addListener(filter -> setImage(menu.hasActiveFilter() ? Images.FILTER_ON : Images.FILTER_OFF));

String selection = preferences.getString(prefKey + ClientFilterMenu.PREF_KEY_POSTFIX);
if (selection != null)
{
this.menu.getAllItems().filter(item -> item.getUUIDs().equals(selection)).findAny().ifPresent(item -> {
this.menu.select(item);
setImage(menu.hasActiveFilter() ? Images.FILTER_ON : Images.FILTER_OFF);
});
}

addDisposeListener(e -> preferences.putValue(prefKey + ClientFilterMenu.PREF_KEY_POSTFIX,
this.menu.getSelectedItem().getUUIDs()));
String key = prefKey + ClientFilterMenu.PREF_KEY_POSTFIX;
menu.loadPreselectedClientFilter(key);
setImage(menu.hasActiveFilter() ? Images.FILTER_ON : Images.FILTER_OFF);

// store/save selected filter
addDisposeListener(e -> menu.saveSelectedFilter(key));
}

@Override
Expand Down