| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2006 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.export.services; |
| |
| import java.io.File; |
| |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.epf.export.ExportPlugin; |
| import org.eclipse.epf.export.ExportResources; |
| import org.eclipse.epf.library.configuration.ConfigurationClosure; |
| import org.eclipse.epf.library.configuration.ConfigurationFactory; |
| import org.eclipse.epf.library.layout.LayoutResources; |
| import org.eclipse.epf.library.services.LibraryProcessor; |
| import org.eclipse.epf.library.util.LibraryUtil; |
| |
| import com.ibm.uma.MethodConfiguration; |
| import com.ibm.uma.MethodLibrary; |
| |
| /** |
| * Exports a library configuration. |
| * |
| * @author Jinhua Xi |
| * @since 1.0 |
| */ |
| public class ConfigurationExportService { |
| |
| private ConfigurationExportData data; |
| |
| /** |
| * Creates a new instance. |
| */ |
| public ConfigurationExportService(ConfigurationExportData data) { |
| this.data = data; |
| } |
| |
| public void run(IProgressMonitor monitor) { |
| try { |
| if (data.selectedConfigs == null |
| || data.selectedConfigs.size() == 0) { |
| return; |
| } |
| monitor.setTaskName(ExportResources |
| .getString("Export.ConfigurationExportService.MSG0")); //$NON-NLS-1$ |
| String exportLibPath = data.llData.getParentFolder(); |
| File exportLibFolder = new File(exportLibPath); |
| if (!exportLibFolder.exists()) { |
| exportLibFolder.mkdir(); |
| } |
| MethodConfiguration config = (MethodConfiguration) data.selectedConfigs |
| .get(0); |
| exportConfig(config.getName(), exportLibFolder.getAbsolutePath(), |
| monitor); |
| } catch (Exception e) { |
| ExportPlugin.getDefault().getLogger().logError(e); |
| } |
| } |
| |
| public void exportConfig(String selectedConfigName, String filePath, |
| IProgressMonitor monitor) throws Exception { |
| MethodLibrary currentLib = LibraryProcessor.getInstance().getLibrary(); |
| |
| try { |
| // Load the whole Method Library. |
| LibraryUtil.loadAll(currentLib); |
| } catch (Throwable e) { |
| ExportPlugin.getDefault().getLogger().logError( |
| "Error loading library", e); //$NON-NLS-1$ |
| data.errorMsg = ExportResources |
| .getString("Export.ConfigurationExportService.MSG2"); //$NON-NLS-1$ |
| return; |
| } |
| |
| // Copy the current library to a new library, |
| // since we need to make changes when packaging the new library. |
| MethodLibrary newLibrary = null; |
| try { |
| newLibrary = (MethodLibrary) EcoreUtil.copy(currentLib); |
| } catch (Throwable e) { |
| ExportPlugin.getDefault().getLogger().logError(e); |
| data.errorMsg = ExportResources |
| .getString("Export.ConfigurationExportService.MSG4"); //$NON-NLS-1$ |
| return; |
| } |
| |
| LibraryProcessor proc = new LibraryProcessor((Resource) null); |
| ConfigurationFactory newFactory = null; |
| ConfigurationClosure closure = null; |
| try { |
| // Detach the new library from the current resource so it can be |
| // added to a new Library Processor instance. |
| LibraryUtil.detachFromResource(newLibrary); |
| |
| // Create a new Library Processor and create a new resource for the |
| // new library. |
| proc.newLibrary("library.xmi", filePath, true); //$NON-NLS-1$ |
| proc.setLibrary(newLibrary); |
| |
| // Begin packaging the new library for export. |
| MethodConfiguration config = proc |
| .getConfiguration(selectedConfigName); |
| |
| // Validate the configuration and make sure the global packages are |
| // selected. If global packages are missing, the exported library |
| // can't be loaded. |
| LibraryUtil.validateMethodConfiguration(config); |
| |
| newFactory = new ConfigurationFactory(proc); |
| closure = new ConfigurationClosure(newFactory, config); |
| closure.packageLibrary(data.removeBrokenReferences); |
| } catch (Throwable e) { |
| ExportPlugin.getDefault().getLogger().logError( |
| "Error making library configuration closure", e); //$NON-NLS-1$ |
| data.errorMsg = ExportResources |
| .getString("Export.ConfigurationExportService.MSG1"); //$NON-NLS-1$ |
| |
| if (closure != null) |
| closure.dispose(); |
| if (newFactory != null) |
| newFactory.clear(); |
| proc.closeOpenedLibrary(); |
| |
| return; |
| } |
| |
| try { |
| LibraryUtil.saveAll(newLibrary); |
| } catch (Throwable e) { |
| ExportPlugin.getDefault().getLogger().logError( |
| "Error saving library", e); //$NON-NLS-1$ |
| data.errorMsg = ExportResources |
| .getString("Export.ConfigurationExportService.MSG9"); //$NON-NLS-1$ |
| |
| if (closure != null) |
| closure.dispose(); |
| if (newFactory != null) |
| newFactory.clear(); |
| proc.closeOpenedLibrary(); |
| return; |
| } |
| |
| // Copy the resource files in the current library to the new library. |
| // For simplicity sake, copy all resource files if the files do not |
| // exist in the target library or if the files are newer |
| String includes = "*resources/*.*, **/resources/*.*"; //$NON-NLS-1$ |
| File srcDir = LibraryUtil.getLibraryRootPath(currentLib); |
| File destDir = LibraryUtil.getLibraryRootPath(newLibrary); |
| |
| LayoutResources.copyDir(srcDir, destDir, includes, null); |
| |
| // Close the newly created library. |
| if (closure != null) |
| closure.dispose(); |
| if (newFactory != null) |
| newFactory.clear(); |
| proc.closeOpenedLibrary(); |
| } |
| |
| } |