Skip to content

AKJac/emfjson-mongo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB Adapter for Eclipse Modeling Framework (EMF)

Build Status

An easy to use adapter that works on top of the EMF Resource API to store and retrieve EMF Models on MongoDB.

Download

Add the following dependency to your pom file.

<dependency>
    <groupId>org.emfjson</groupId>
    <artifactId>emfjson-mongo</artifactId>
    <version>0.3.0</version>
</dependency>

You can also find the jars in maven central

Usage

Setup the ResourceSet:

ResourceSet resourceSet = new ResourceSetImpl();

resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new JsonResourceFactory());
resourceSet.getURIConverter().getURIHandlers().add(0, new MongoHandler());

Create a Resource with a URI pointing to MongoDB. The URI must contains 3 segments identifying the database, the collection and the document id.

The URI should have the form mongodb://{host}[:{port}]/{db}/{collection}/{id}

URI uri = URI.createURI("mongodb://localhost:27017/emfjson-test/models/model1");
Resource resource = resourceSet.createResource(uri);

Alternatively, you can use a URI mapping:

resourceSet.getURIConverter().getURIMap().put(
	URI.createURI("http://resources/"), 
	URI.createURI("mongodb://localhost:27017/emfjson-test/models/"));
	
Resource resource = resourceSet.createResource(URI.createURI("http://resources/model1"));

Override the name of the field to avoid problem with the reserved name of Bson. And set the uri handler, to avoid problem with external resources.

	HashMap<String, Object> DEFAULT_OPTIONS = new HashMap<String, Object>();
	DEFAULT_OPTIONS.put(EMFJs.OPTION_URI_HANDLER, new IdentityURIHandler());
	DEFAULT_OPTIONS.put(EMFJs.OPTION_REF_FIELD, "_ref");
	resourceSet.getLoadOptions().putAll(DEFAULT_OPTIONS);

Saving documents

EPackage p = EcoreFactory.eINSTANCE.createEPackage();
p.setName("p");

EClass c = EcoreFactory.eINSTANCE.createEClass();
c.setName("A");

p.getEClassifiers().add(c);

resource.getContents().add(p);
resource.save(null);

Loading document

resource.load(null);

About

MongoDB adapter for EMF

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%