EMFStore - API Hello World Example


This code example shows how to use the basic API of the EMFStore. In this tutorial you will learn how to use the API to integrate the EMF Store into your own application. Follow the steps below to, first run an example client that can checkout, modify and commit projects using the EMF Store, and then learn where you can start customizing the source code of this example client to your own needs. If you just want to make yourself familiar with the features of EMFStore, you can also use the default UI of the EMFStore provided by the EMF client Platform, see here. All code example can be installed using our examples manager (starting from version 0.9.0) or be checked out from the repository.


In particular, this example demonstrates how to:

/**
 *  Copyright (c) 2008-2009 Jonas Helming, Maximilian Koegel. 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 
 */
package org.eclipse.emf.emfstore.exampleclient;

import java.util.List;

import org.eclipse.emf.emfstore.bowling.BowlingFactory;
import org.eclipse.emf.emfstore.bowling.League;
import org.eclipse.emf.emfstore.bowling.Player;
import org.eclipse.emf.emfstore.client.model.ProjectSpace;
import org.eclipse.emf.emfstore.client.model.Usersession;
import org.eclipse.emf.emfstore.client.model.Workspace;
import org.eclipse.emf.emfstore.client.model.WorkspaceManager;
import org.eclipse.emf.emfstore.client.model.util.EMFStoreClientUtil;
import org.eclipse.emf.emfstore.server.exceptions.EmfStoreException;
import org.eclipse.emf.emfstore.server.model.ProjectInfo;
import org.eclipse.emf.emfstore.server.model.versioning.LogMessage;
import org.eclipse.emf.emfstore.server.model.versioning.VersioningFactory;
import org.eclipse.emf.emfstore.server.startup.ConsoleProgressMonitor;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

public class Application implements IApplication {
	
	public Object start(IApplicationContext context) throws Exception {		

		// Run a client that shows the basic features of the EMFstore
		// If there is a problem with the connection to the server
		// e.g. a network, a specific EMFStoreException will be thrown
		try {
			runClient();

		} catch (EmfStoreException e) {
			System.out.println("No connection to server.");
			System.out.println("Did you start the server? :-)");
		}

		return IApplication.EXIT_OK;
	}

	private void runClient() throws EmfStoreException {
		System.out.println("Client starting...");

		// The workspace is the core controler to access 
		// local and remote projects
		Workspace workspace = WorkspaceManager.getInstance()
				.getCurrentWorkspace();
		
		// A user session stores credentials for login
		// Creates a user session with the default credentials
		Usersession usersession = EMFStoreClientUtil.createUsersession();
		usersession.logIn();
		
		// Retrieves a list of existing (and accessible) projects
		// on the sever and deletes them permanently (to have a
		// clean set-up)
		List projectList;
		projectList = workspace.getRemoteProjectList(usersession);
		for (ProjectInfo projectInfo : projectList) {
			workspace.deleteRemoteProject(usersession,
					projectInfo.getProjectId(), true);
		}

		// Create a project, share it with the server	
		ProjectSpace project1 = workspace.createLocalProject("projectNo1",
				"My project");
		project1.shareProject(usersession, new ConsoleProgressMonitor());

		// Create some EObjects and add them to the project
		// (To the projects containment tree)
		League league1 = BowlingFactory.eINSTANCE.createLeague();
		league1.setName("league");
		league1.getPlayers().add(createPlayer("no. 1"));
		league1.getPlayers().add(createPlayer("no. 2"));
		project1.getProject().addModelElement(league1);

		// commit the changes of the project to the EMFStore
		// including a commit message
		LogMessage logMessage = VersioningFactory.eINSTANCE.createLogMessage();
		logMessage.setMessage("My Message");
		project1.commit(logMessage, null, new ConsoleProgressMonitor());
		System.out.println("Project 1 committed!");

		// Check-out a second, independent copy of the project
		// (simulating a second client)
		ProjectSpace project2 = workspace.checkout(usersession,
				project1.getProjectInfo());
		League league2 = (League) project2.getProject().getModelElements()
				.get(0);
		System.out.println("Project 2: League name is "+league2.getName());
		
		// Apply changes in the second copy of the project
		// and commit
		league2.setName("league_changed");
		project2.commit(logMessage, null, new ConsoleProgressMonitor());
		System.out.println("Project 2 committed!");


		// Update the first copy of the project
		project1.update();
		System.out.println("Project 1 updated!");
		System.out.println("Project 1: League name is "+league1.getName());

		
		System.out.println("Client run completed.");

	}

	/**
	 * Creates a new instance of a player.
	 * 
	 * @param name
	 * @return
	 */
	private Player createPlayer(String name) {
		Player player = BowlingFactory.eINSTANCE.createPlayer();
		player.setName(String.format("Player %s", name));
		player.setEMail(String.format("%s@emfstore.org", name));
		return player;
	}

	public void stop() {
		// TODO Auto-generated method stub
	}
}