upgrade variants to new format on start

fix for variant export
This commit is contained in:
Branislav Stojkovic
2019-02-09 18:55:34 +01:00
parent b02eaf928d
commit bb69d8dd04
4 changed files with 143 additions and 73 deletions
@@ -21,7 +21,6 @@
*/ */
package org.diylc.common; package org.diylc.common;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@@ -191,6 +191,7 @@ public class Presenter implements IPlugInPort {
// lockedLayers = EnumSet.noneOf(ComponentLayer.class); // lockedLayers = EnumSet.noneOf(ComponentLayer.class);
// visibleLayers = EnumSet.allOf(ComponentLayer.class); // visibleLayers = EnumSet.allOf(ComponentLayer.class);
upgradeVariants();
} }
public void installPlugin(IPlugIn plugIn) { public void installPlugin(IPlugIn plugIn) {
@@ -2633,4 +2634,47 @@ public class Presenter implements IPlugInPort {
return pkg.getBlocks().size(); return pkg.getBlocks().size();
} }
private static boolean upgradedVariants = false;
@SuppressWarnings("unchecked")
private synchronized void upgradeVariants() {
if (upgradedVariants)
return;
upgradedVariants = true;
LOG.info("Checking if variants need to be updated");
Map<String, List<Template>> lookupMap = new TreeMap<String, List<Template>>(String.CASE_INSENSITIVE_ORDER);
Map<String, List<Template>> variantMap =
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
if (variantMap == null)
return;
Map<String, ComponentType> typeMap = new TreeMap<String, ComponentType>(String.CASE_INSENSITIVE_ORDER);
Map<String, List<ComponentType>> componentTypes = getComponentTypes();
for (Map.Entry<String, List<ComponentType>> entry : componentTypes.entrySet())
for (ComponentType type : entry.getValue()) {
typeMap.put(type.getInstanceClass().getCanonicalName(), type);
typeMap.put(type.getCategory() + "." + type.getName(), type);
if (type.getCategory().contains("Electro-Mechanical"))
typeMap.put(type.getCategory().replace("Electro-Mechanical", "Electromechanical") + "." + type.getName(), type);
}
Map<String, List<Template>> newVariantMap = new HashMap<String, List<Template>>();
lookupMap.putAll(variantMap);
for (Map.Entry<String, List<Template>> entry : variantMap.entrySet()) {
if (typeMap.containsKey(entry.getKey())) {
newVariantMap.put(typeMap.get(entry.getKey()).getInstanceClass().getCanonicalName(), entry.getValue()); // great, nothing to upgrade
} else {
LOG.warn("Could not upgrade variants for: " + entry.getKey());
}
}
ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, newVariantMap);
}
} }
Binary file not shown.
@@ -36,6 +36,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@@ -697,7 +698,7 @@ public class ActionFactory {
private ISwingUI swingUI; private ISwingUI swingUI;
private IPlugInPort plugInPort; private IPlugInPort plugInPort;
private Map<String, ComponentType> typeMap = new HashMap<String, ComponentType>(); private Map<String, ComponentType> typeMap = new TreeMap<String, ComponentType>(String.CASE_INSENSITIVE_ORDER);
public ExportVariantsAction(ISwingUI swingUI, IPlugInPort plugInPort) { public ExportVariantsAction(ISwingUI swingUI, IPlugInPort plugInPort) {
super(); super();
@@ -707,14 +708,19 @@ public class ActionFactory {
Map<String, List<ComponentType>> componentTypes = plugInPort.getComponentTypes(); Map<String, List<ComponentType>> componentTypes = plugInPort.getComponentTypes();
for (Map.Entry<String, List<ComponentType>> entry : componentTypes.entrySet()) for (Map.Entry<String, List<ComponentType>> entry : componentTypes.entrySet())
for (ComponentType type : entry.getValue()) for (ComponentType type : entry.getValue()) {
typeMap.put(type.getInstanceClass().getCanonicalName(), type); typeMap.put(type.getInstanceClass().getCanonicalName(), type);
} }
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
LOG.info("ExportVariantsAction triggered"); LOG.info("ExportVariantsAction triggered");
Map<String, List<Template>> selectedVariants;
try {
Map<String, List<Template>> variantMap = Map<String, List<Template>> variantMap =
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(IPlugInPort.TEMPLATES_KEY, null); (Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(IPlugInPort.TEMPLATES_KEY, null);
if (variantMap == null || variantMap.isEmpty()) { if (variantMap == null || variantMap.isEmpty()) {
@@ -723,8 +729,13 @@ public class ActionFactory {
} }
List<ComponentType> types = new ArrayList<ComponentType>(); List<ComponentType> types = new ArrayList<ComponentType>();
for (String className : variantMap.keySet()) for (String className : variantMap.keySet()) {
types.add(typeMap.get(className)); ComponentType type = typeMap.get(className);
if (type != null)
types.add(type);
else
LOG.warn("Could not find type for: " + className);
}
Collections.sort(types, new Comparator<ComponentType>() { Collections.sort(types, new Comparator<ComponentType>() {
@@ -743,15 +754,22 @@ public class ActionFactory {
Object[] selected = dialog.getSelectedOptions(); Object[] selected = dialog.getSelectedOptions();
if (selected.length == 0) { if (selected.length == 0) {
swingUI.showMessage("No building blocks selected for export.", "Error", IView.ERROR_MESSAGE); swingUI.showMessage("No variants selected for export.", "Error", IView.ERROR_MESSAGE);
return; return;
} }
Map<String, List<Template>> selectedVariants = new HashMap<String, List<Template>>(); selectedVariants = new HashMap<String, List<Template>>();
for (Object key : selected) { for (Object key : selected) {
ComponentType type = (ComponentType) key; ComponentType type = (ComponentType) key;
String clazz = type.getInstanceClass().getCanonicalName(); String clazz = type.getInstanceClass().getCanonicalName();
selectedVariants.put(clazz, variantMap.get(clazz)); List<Template> variants = variantMap.get(clazz);
if (variants != null)
selectedVariants.put(clazz, variants);
}
} catch (Exception ex) {
LOG.error("Error preparing variants for export", ex);
swingUI.showMessage("Could not export variants. Please check the log for details", "Export Variants", ISwingUI.ERROR_MESSAGE);
return;
} }
final VariantPackage variantPkg = new VariantPackage(selectedVariants, System.getProperty("user.name")); final VariantPackage variantPkg = new VariantPackage(selectedVariants, System.getProperty("user.name"));
@@ -861,6 +879,10 @@ public class ActionFactory {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
LOG.info("ExportBuildingBlocksAction triggered"); LOG.info("ExportBuildingBlocksAction triggered");
Map<String, Collection<IDIYComponent<?>>> selectedBlocks;
try {
Map<String, Collection<IDIYComponent<?>>> blocks = Map<String, Collection<IDIYComponent<?>>> blocks =
(Map<String, Collection<IDIYComponent<?>>>) ConfigurationManager.getInstance().readObject(IPlugInPort.BLOCKS_KEY, null); (Map<String, Collection<IDIYComponent<?>>>) ConfigurationManager.getInstance().readObject(IPlugInPort.BLOCKS_KEY, null);
if (blocks == null || blocks.isEmpty()) { if (blocks == null || blocks.isEmpty()) {
@@ -891,10 +913,15 @@ public class ActionFactory {
return; return;
} }
Map<String, Collection<IDIYComponent<?>>> selectedBlocks = new HashMap<String, Collection<IDIYComponent<?>>>(); selectedBlocks = new HashMap<String, Collection<IDIYComponent<?>>>();
for (Object key : selected) { for (Object key : selected) {
selectedBlocks.put(key.toString(), blocks.get(key)); selectedBlocks.put(key.toString(), blocks.get(key));
} }
} catch (Exception ex) {
LOG.error("Error preparing building blocks for export", ex);
swingUI.showMessage("Could not export building blocks. Please check the log for details", "Export Building Blocks", ISwingUI.ERROR_MESSAGE);
return;
}
final BuildingBlockPackage variantPkg = new BuildingBlockPackage(selectedBlocks, System.getProperty("user.name")); final BuildingBlockPackage variantPkg = new BuildingBlockPackage(selectedBlocks, System.getProperty("user.name"));