| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2008 IBM Corporation and others. |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v1.0 |
| // which accompanies this distribution, and is available at |
| // http://www.eclipse.org/legal/epl-v10.html |
| // |
| // Contributors: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| package org.eclipse.epf.library.configuration; |
| |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.HashMap; |
| import java.util.HashSet; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Set; |
| |
| import org.eclipse.epf.library.LibraryPlugin; |
| import org.eclipse.epf.library.LibraryService; |
| import org.eclipse.epf.library.LibraryServiceUtil; |
| import org.eclipse.epf.services.ILibraryPersister; |
| import org.eclipse.epf.uma.MethodConfiguration; |
| import org.eclipse.epf.uma.MethodElement; |
| import org.eclipse.epf.uma.MethodLibrary; |
| import org.eclipse.epf.uma.MethodPackage; |
| import org.eclipse.epf.uma.MethodPlugin; |
| import org.eclipse.epf.uma.ProcessComponent; |
| import org.eclipse.epf.uma.UmaFactory; |
| |
| |
| /** |
| * Class managing implicit method configurations |
| * |
| * @author Weiping Lu - Mar 12, 2008 |
| * @since 1.5 |
| */ |
| public class ImplicitConfigMgr { |
| |
| private static ImplicitConfigMgr instance = new ImplicitConfigMgr(); |
| private List<MethodPlugin> newAddedPlugins; |
| private MethodConfiguration tempConfig; |
| private Map<MethodConfiguration, MapEntry> configMap = new HashMap<MethodConfiguration, MapEntry>(); |
| |
| protected ImplicitConfigMgr() { |
| } |
| |
| public static ImplicitConfigMgr getInstance() { |
| return instance; |
| } |
| |
| public static void setInstance(ImplicitConfigMgr instance) { |
| ImplicitConfigMgr.instance = instance; |
| } |
| |
| /** |
| * Update config with the selected plug-ins. |
| * @param config |
| * @param selectedPlugins |
| */ |
| public void update(MethodConfiguration config, List<MethodPlugin> selectedPlugins) { |
| |
| List<MethodPlugin> plugins = config.getMethodPluginSelection(); |
| List<MethodPackage> pkgs = config.getMethodPackageSelection(); |
| |
| plugins.clear(); |
| pkgs.clear(); |
| |
| Set<MethodPlugin> addedPlugins = new HashSet<MethodPlugin>(); |
| Set<MethodPackage> addedPkgs = new HashSet<MethodPackage>(); |
| |
| List<MethodPlugin> matchedSelected = getNamespaceMatched(selectedPlugins); |
| |
| addPluginsAndPackages(config, matchedSelected, plugins, pkgs, addedPlugins, addedPkgs); |
| |
| handleIncludeExcludeSet(config, plugins, pkgs, addedPlugins, addedPkgs); |
| } |
| |
| private void handleIncludeExcludeSet(MethodConfiguration config, |
| List<MethodPlugin> plugins, List<MethodPackage> pkgs, |
| Set<MethodPlugin> addedPlugins, Set<MethodPackage> addedPkgs) { |
| List<MethodPlugin> includePlugins = new ArrayList<MethodPlugin>(); |
| MapEntry entry = getMapEntry(config); |
| for (MethodElement elem: entry.includeSet) { |
| if (elem instanceof MethodPlugin) { |
| includePlugins.add((MethodPlugin) elem); |
| } |
| } |
| addPluginsAndPackages(config, includePlugins, plugins, pkgs, addedPlugins, addedPkgs); |
| for (MethodElement elem: entry.excludeSet) { |
| if (elem instanceof MethodPackage && !addedPkgs.contains(elem)) { |
| pkgs.add((MethodPackage) elem); |
| addedPkgs.add((MethodPackage) elem); |
| } |
| } |
| } |
| |
| //May be overridden by sub-class |
| protected List<MethodPlugin> getNamespaceMatched(List<MethodPlugin> selectedPlugins) { |
| return selectedPlugins; |
| } |
| |
| private void addPluginsAndPackages(MethodConfiguration config, List<MethodPlugin> selectedPlugins, |
| List<MethodPlugin> plugins, List<MethodPackage> pkgs, |
| Set<MethodPlugin> addedPlugins, Set<MethodPackage> addedPkgs) { |
| |
| newAddedPlugins = new ArrayList<MethodPlugin>(); |
| |
| for (MethodPlugin plugin: selectedPlugins) { |
| addPlugin(config, addedPlugins, plugin, plugins); |
| } |
| |
| for (MethodPlugin plugin: newAddedPlugins) { |
| List<MethodPackage> pkgList = plugin.getMethodPackages(); |
| for (MethodPackage pkg: pkgList) { |
| addPackages(config, addedPkgs, pkg, pkgs); |
| } |
| } |
| |
| newAddedPlugins = null; |
| } |
| |
| /** |
| * Remove selected plug-ins from config |
| * @param config |
| * @param selectedPlugins |
| */ |
| public void remove(MethodConfiguration config, List<MethodPlugin> selectedPlugins) { |
| if (selectedPlugins == null || selectedPlugins.isEmpty()) { |
| return; |
| } |
| List<MethodPlugin> plugins = config.getMethodPluginSelection(); |
| |
| boolean needToUpdate = false; |
| List<MethodPlugin> finalSelectedPlugins = new ArrayList<MethodPlugin>(); |
| for (MethodPlugin plugin: plugins) { |
| if (selectedPlugins.contains(plugin)) { |
| needToUpdate = true; |
| } else { |
| finalSelectedPlugins.add(plugin); |
| } |
| } |
| |
| if (needToUpdate) { |
| update(config, finalSelectedPlugins); |
| } |
| } |
| |
| /** |
| * Add to config with the selected plug-ins. |
| * @param config |
| * @param selectedPlugins |
| */ |
| public void add(MethodConfiguration config, List<MethodPlugin> selectedPlugins) { |
| List<MethodPlugin> plugins = config.getMethodPluginSelection(); |
| List<MethodPackage> pkgs = config.getMethodPackageSelection(); |
| |
| Set<MethodPlugin> addedPlugins = new HashSet<MethodPlugin>(plugins); |
| Set<MethodPackage> addedPkgs = new HashSet<MethodPackage>(pkgs); |
| |
| List<MethodPlugin> matchedSelected = getNamespaceMatched(selectedPlugins); |
| |
| addPluginsAndPackages(config, matchedSelected, plugins, pkgs, addedPlugins, addedPkgs); |
| |
| handleIncludeExcludeSet(config, plugins, pkgs, addedPlugins, addedPkgs); |
| } |
| |
| private void addPlugin(MethodConfiguration config, Set<MethodPlugin> added, MethodPlugin plugin, List<MethodPlugin> plugins) { |
| if (added.contains(plugin)) { |
| return; |
| } |
| |
| MapEntry entry = getMapEntry(config); |
| if (entry.excludeSet.contains(plugin)) { |
| return; |
| } |
| |
| added.add(plugin); |
| newAddedPlugins.add(plugin); |
| |
| plugins.add(plugin); |
| List<MethodPlugin> basePlugins = plugin.getBases(); |
| for (MethodPlugin basePlugin: basePlugins) { |
| addPlugin(config, added, basePlugin, plugins); |
| } |
| } |
| |
| private void addPackages(MethodConfiguration config, Set<MethodPackage> added, MethodPackage pkg, List<MethodPackage> pkgs) { |
| if (added.contains(pkg)) { |
| return; |
| } |
| |
| MapEntry entry = getMapEntry(config); |
| if (entry.includeSet.contains(pkg)) { |
| return; |
| } |
| |
| added.add(pkg); |
| |
| pkgs.add(pkg); |
| if (pkg instanceof ProcessComponent) { |
| return; |
| } |
| |
| List<MethodPackage> childPkgs = pkg.getChildPackages(); |
| for (MethodPackage childPkg: childPkgs) { |
| addPackages(config, added, childPkg, pkgs); |
| } |
| } |
| |
| public void remove(MethodConfiguration config) { |
| MethodLibrary library = LibraryService.getInstance() |
| .getCurrentMethodLibrary(); |
| if (config != null) { |
| library.getPredefinedConfigurations().remove(config); |
| LibraryService.getInstance().removeConfigurationManager(config); |
| } |
| } |
| |
| public void save(MethodConfiguration config) { |
| MethodLibrary library = LibraryService.getInstance() |
| .getCurrentMethodLibrary(); |
| ILibraryPersister.FailSafeMethodLibraryPersister persister = LibraryServiceUtil |
| .getCurrentPersister().getFailSafePersister(); |
| |
| try { |
| persister.save(library.eResource()); |
| persister.commit(); |
| } catch (Exception e) { |
| try { |
| persister.rollback(); |
| } catch (Exception ex) { |
| LibraryPlugin.getDefault().getLogger().logError(ex); |
| } |
| } |
| } |
| |
| public MethodConfiguration createTemporaryConfiguration(String name, List<MethodPlugin> selectedPlugins) { |
| if (tempConfig == null) { |
| tempConfig = UmaFactory.eINSTANCE.createMethodConfiguration(); |
| tempConfig.setName(name); |
| } |
| MethodLibrary library = LibraryService.getInstance() |
| .getCurrentMethodLibrary(); |
| library.getPredefinedConfigurations().add(tempConfig); |
| update(tempConfig, selectedPlugins); |
| return tempConfig; |
| } |
| |
| public MethodConfiguration getTemporaryConfiguration() { |
| if (tempConfig == null) { |
| return createTemporaryConfiguration("TEMP_PRACTICE", new ArrayList<MethodPlugin>()); //$NON-NLS-1$ |
| } |
| return tempConfig; |
| } |
| |
| public void clearTemporaryConfiguration() { |
| if (tempConfig == null) { |
| return; |
| } |
| clearCacheData(tempConfig); |
| MethodLibrary library = LibraryService.getInstance() |
| .getCurrentMethodLibrary(); |
| library.getPredefinedConfigurations().remove(tempConfig); |
| tempConfig = null; |
| } |
| |
| public void clearCacheData(MethodConfiguration config) { |
| configMap.remove(config); |
| } |
| |
| public void updateTemporaryConfiguration(List<MethodPlugin> selectedPlugins) { |
| update(getTemporaryConfiguration(), selectedPlugins); |
| } |
| |
| public void addToTemporaryConfiguration(List<MethodPlugin> selectedPlugins) { |
| add(getTemporaryConfiguration(), selectedPlugins); |
| } |
| |
| public void removeFromTemporaryConfiguration(List<MethodPlugin> selectedPlugins) { |
| remove(getTemporaryConfiguration(), selectedPlugins); |
| } |
| |
| public void setIncluded(MethodConfiguration config, Collection<MethodElement> includedElements) { |
| MapEntry entry = getMapEntry(config); |
| for (MethodElement elem: includedElements) { |
| entry.includeSet.add(elem); |
| } |
| } |
| |
| public void setExcluded(MethodConfiguration config, Collection<MethodElement> excludedElements) { |
| MapEntry entry = getMapEntry(config); |
| for (MethodElement elem: excludedElements) { |
| entry.excludeSet.add(elem); |
| } |
| } |
| |
| public void setViews(MethodConfiguration config, Collection<MethodElement> views) { |
| MapEntry entry = getMapEntry(config); |
| for (MethodElement elem: views) { |
| entry.viewSet.add(elem); |
| } |
| } |
| |
| private MapEntry getMapEntry(MethodConfiguration config) { |
| MapEntry entry = configMap.get(config); |
| if (entry == null) { |
| entry = new MapEntry(); |
| configMap.put(config, entry); |
| } |
| return entry; |
| } |
| |
| static class MapEntry { |
| public Set<MethodElement> includeSet = new HashSet(); |
| public Set<MethodElement> excludeSet = new HashSet(); |
| public Set<MethodElement> viewSet = new HashSet(); |
| } |
| |
| |
| } |