Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3732ab9ed44307f443ae0b68ef1b679b9a521a9c (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) 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:
 *   Martin Taal - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: StoreController.java,v 1.14 2008/08/12 07:45:40 mtaal Exp $
 */
package org.eclipse.gmf.examples.mindmap.diagram.db;

import java.io.IOException;
import java.util.Properties;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.hibernate.HbHelper;
import org.eclipse.emf.teneo.hibernate.HbSessionDataStore;
import org.eclipse.emf.teneo.hibernate.mapping.property.EListPropertyHandler;
import org.eclipse.emf.teneo.hibernate.mapping.property.EReferencePropertyHandler;
import org.eclipse.gmf.examples.mindmap.MindmapPackage;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.hibernate.Session;

/**
 * This class manages one Hibernate Datastore, it offers static access to a
 * singleton DataStore. It initializes the datastore using the GMF and Ecore
 * epackages and the properties in the teneo.properties.
 * 
 * @author mtaal@elver.org
 */
public class StoreController {

	// This database uri ensures that the Hibernate XML Resource is used and
	// that the
	// Map and the Diagram are loaded in the top of the resource. The dsname
	// parameter
	// is the name of the datastore initialized below.
	public static final URI DATABASE_URI = URI
			.createURI("hbxml://?dsname=mindmap&query1=from Map&query2=from Diagram");

	// provide easy access to the datastore
	private static StoreController instance = new StoreController();

	public static StoreController getInstance() {
		return instance;
	}

	private HbSessionDataStore dataStore = null;
	private boolean initialized = false;

	public Session getSession() {
		return getDataStore().getSessionFactory().openSession();
	}

	public HbSessionDataStore getDataStore() {
		if (dataStore == null) {
			dataStore = initializeDataStore();
		}

		return dataStore;
	}

	public void closeDataStore() {
		getDataStore().close();
	}

	public HbSessionDataStore initializeDataStore() {
		if (initialized) {
			return dataStore;
		}

		// create and register the datastore using the mindmap name
		final HbSessionDataStore localDataStore = new HbSessionDataStore();
		localDataStore.setName("mindmap");
		HbHelper.INSTANCE.register(localDataStore);

		// now register the epackages. There are four epackages:
		// 1) the model itself
		// 2) the GMF model
		// 3) the ecore model because GMF depends on it
		// 4) and the ecore XML type package
		final EPackage[] ePackages = new EPackage[] { MindmapPackage.eINSTANCE,
				NotationPackage.eINSTANCE, EcorePackage.eINSTANCE,
				XMLTypePackage.eINSTANCE };
		localDataStore.setEPackages(ePackages);

		// load the properties from the teneo.properties file
		try {
			final Properties props = new Properties();
			props.load(this.getClass().getResourceAsStream("teneo.properties"));

			// handle multiple inheritance in the GMF model
			props.setProperty(PersistenceOptions.PERSISTENCE_XML,
					"annotations.xml");

			localDataStore.setProperties(props);
		} catch (IOException e) {
			throw new IllegalStateException(e);
		}

		// solve a specific issue with the GMF model
		localDataStore.getExtensionManager().registerExtension(
				EListPropertyHandler.class.getName(),
				GMFEListPropertyHandler.class.getName());

		localDataStore.initialize();

		// print the hibernate mapping
		System.err.println(localDataStore.getMappingXML());
		initialized = true;

		return localDataStore;
	}
}

Back to the top