aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/SocialNetwork_plugin/src/ca/mcgill/ecse/socialnetwork/runner/Persisitence.java
blob: 3ebede25dcfec637e6cebfbfd05fdf934535a883 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package ca.mcgill.ecse.socialnetwork.runner;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
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 Persisitence<G extends EObject> {
	private String uri;
	
	public Persisitence (String suffix, String uri){		
		//prepare to save 
		Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
		Map<String, Object> m = reg.getExtensionToFactoryMap();
		m.put(suffix, new XMIResourceFactoryImpl());
		this.uri = uri;
	}
	
	
	/**
	 * Save the model 
	 * @return whether the model has saved successfully
	 */
	public boolean save(G model) {
		//create resource
		ResourceSet resSet = new ResourceSetImpl();
		Resource resource = resSet.createResource(URI.createURI(uri));
		resource.getContents().add(model);
		try {
			resource.save(Collections.EMPTY_MAP);
			return true;
		}catch(IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * load the model from persistent
	 * @return: the model loaded
	 */
	@SuppressWarnings("unchecked")
	public G load() {
		G model = null;
//		try {
			ResourceSet resSet = new ResourceSetImpl();
			Resource resource = resSet.getResource(URI.createURI(uri), true);
			model = (G) resource.getContents().get(0);
//		}catch (org.eclipse.emf.common.util.WrappedException e) {
//			// if the file cannot be found then return null
//			if(e.getCause().getClass() == java.io.FileNotFoundException.class) {
//				return null;
//			}
//		}
		
		return model;
	}
}