/** | |
* Copyright (c) 2020 RBEI and others. | |
* | |
* This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v. 2.0 | |
* which accompanies this distribution, and is available at | |
* https://www.eclipse.org/legal/epl-2.0/ | |
* | |
* SPDX-License-Identifier: EPL-2.0 | |
* | |
* Contributors: | |
* Adhith Gopal - Initial API and Implementation | |
* | |
*/ | |
package org.eclipse.blockchain.model.core; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.eclipse.blockchain.model.ethproject.DeploymentModel; | |
import org.eclipse.blockchain.model.ethproject.EthereumProject; | |
import org.eclipse.emf.common.util.URI; | |
import org.eclipse.emf.ecore.resource.Resource; | |
import org.eclipse.emf.ecore.resource.ResourceSet; | |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; | |
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; | |
public class EthereumProjectModelHandler { | |
private static EthereumProjectModelHandler instance = null; | |
private EthereumProjectModelHandler() { | |
} | |
/** | |
* @return | |
*/ | |
public static EthereumProjectModelHandler getInstance() { | |
if (null == instance) { | |
instance = new EthereumProjectModelHandler(); | |
} | |
return instance; | |
} | |
/** | |
* @param projectsToSave | |
* @throws IOException | |
*/ | |
public void saveProjects(final List<EthereumProject> projectsToSave) throws IOException { | |
for (EthereumProject ethProj : projectsToSave) { | |
saveProject(ethProj); | |
} | |
} | |
/** | |
* @param projectsToSave | |
* @throws IOException | |
*/ | |
public void saveProject(final EthereumProject projectToSave) throws IOException { | |
// Assign XMI resource factory IMPL for ethproject | |
Resource.Factory.Registry registry = Resource.Factory.Registry.INSTANCE; | |
registry.getExtensionToFactoryMap().put("ethproject", new XMIResourceFactoryImpl()); | |
ResourceSet resourceSet = new ResourceSetImpl(); | |
Resource resource = resourceSet | |
.createResource(URI.createFileURI(projectToSave.getProjectLocation() + File.separator + ".ethproject")); | |
resource.getContents().add(projectToSave); | |
resource.save(Collections.emptyMap()); | |
} | |
/** | |
* @param ethProjectPath | |
* @return | |
*/ | |
public EthereumProject loadProject(final String ethProjectPath) { | |
Resource.Factory.Registry registry = Resource.Factory.Registry.INSTANCE; | |
registry.getExtensionToFactoryMap().put("ethproject", new XMIResourceFactoryImpl()); | |
ResourceSet resourceSet = new ResourceSetImpl(); | |
Resource resource = resourceSet.getResource(URI.createFileURI(ethProjectPath), true); | |
EthereumProject ethProject = (EthereumProject) resource.getContents().get(0); | |
Map<String, Map<String, DeploymentModel>> deploymentMap = new HashMap<>(); | |
reconstructEnvironmentBasedDeploymentMap(deploymentMap, ethProject.getDeploymentmodel()); | |
ethProject.setEnvironmentBasedDeployedModel(deploymentMap); | |
return ethProject; | |
} | |
private void reconstructEnvironmentBasedDeploymentMap(final Map<String, Map<String, DeploymentModel>> map, | |
final List<DeploymentModel> list) { | |
for (DeploymentModel dm : list) { | |
map.computeIfAbsent(dm.getEnvironment(), v -> new HashMap<>()).put(dm.getScFile(), dm); | |
} | |
} | |
} |