Skip to main content
summaryrefslogtreecommitdiffstats
blob: 88e55bea1c0894ee462279bebf22d09f7d035ac4 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006 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
 * </copyright>
 *
 * $Id: Tutorial1.java,v 1.5 2006/11/13 21:55:11 mtaal Exp $
 */

package jpoxtutorial;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Properties;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
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.example.library.Book;
import org.eclipse.example.library.BookCategory;
import org.eclipse.example.library.Library;
import org.eclipse.example.library.LibraryFactory;
import org.eclipse.example.library.LibraryPackage;
import org.eclipse.example.library.Writer;
import org.eclipse.example.library.impl.BookImpl;
import org.eclipse.example.library.impl.LibraryImpl;
import org.eclipse.example.library.impl.WriterImpl;
import org.eclipse.emf.teneo.jpox.JpoxDataStore;
import org.eclipse.emf.teneo.jpox.JpoxHelper;
import org.eclipse.emf.teneo.jpox.resource.JPOXResourceDAO;
import org.jpox.PMFConfiguration;

/**
 * Tutorial
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.5 $
 */
public class Tutorial1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// At this position you can add your own specific types for jpox
		// This should be done before creating the persistence manager factory, e.g.
		// TypeManager.getTypeManager().addType(MyNiceType.class.getName(), MyNiceTypeMapping.class,
		// MyNiceTypeWrapper.class);

		// set the database connection info, PMFConfiguration is org.jpox.PMFConfiguration
		Properties properties = new Properties();
		properties.setProperty(PMFConfiguration.JDO_DATASTORE_DRIVERNAME_PROPERTY, "com.mysql.jdbc.Driver");
		properties.setProperty(PMFConfiguration.JDO_DATASTORE_URL_PROPERTY, "jdbc:mysql://127.0.0.1:3306/mylibrary");
		properties.setProperty(PMFConfiguration.JDO_DATASTORE_USERNAME_PROPERTY, "root");
		properties.setProperty(PMFConfiguration.JDO_DATASTORE_PASSWORD_PROPERTY, "root");

		// create/register the JpoxDataStore, set the db props and the epackages to persist, initialize creates
		// the database
		String pmfName = "MyPMF"; // the name of the JpoxDataStore
		JpoxDataStore jpoxDataStore = JpoxHelper.INSTANCE.createRegisterDataStore(pmfName);
		jpoxDataStore.setProperties(properties);
		jpoxDataStore.setEPackages(new EPackage[] { LibraryPackage.eINSTANCE });
		jpoxDataStore.initialize();

		// create a persistence manager and a transaction
		PersistenceManager pm = jpoxDataStore.getPMF().getPersistenceManager();
		Transaction tx = pm.currentTransaction();

		// start a transaction, create a library and make it persistent
		tx.begin();
		Library lib = LibraryFactory.eINSTANCE.createLibrary();
		lib.setName("My Library");
		pm.makePersistent(lib);

		// create a writer
		Writer writer = LibraryFactory.eINSTANCE.createWriter();
		writer.setName("JRR Tolkien");

		// and one of his books
		Book book = LibraryFactory.eINSTANCE.createBook();
		book.setAuthor(writer);
		book.setPages(305);
		book.setTitle("The Hobbit");
		book.setCategory(BookCategory.SCIENCE_FICTION_LITERAL);

		// add the writer/book to the library. The writer and book are automatically
		// made persistent because they are added to the library which is already
		// made persistent
		lib.getWriters().add(writer);
		lib.getBooks().add(book);

		// at commit the objects will be present in the database
		tx.commit();
		pm.close(); // empty the cache

		// at this point the database contains one library, one book and one writer
		// Reopen the transaction and query for the library objects
		pm = jpoxDataStore.getPMF().getPersistenceManager();
		tx = pm.currentTransaction();
		tx.begin();

		// NOTE: here you see one drawback of the current JPOX implementation:
		// for querying you require the concrete implementation instead of the
		// interface!
		// retrieve all LibraryImpl classes and subclasses.
		Extent e = pm.getExtent(LibraryImpl.class, true);
		Query q = pm.newQuery(e);

		// there is only one library
		Collection c = (Collection) q.execute();
		lib = (Library) c.iterator().next();

		// read the writer and book
		writer = (Writer) lib.getWriters().get(0);
		System.out.println(writer.getName());
		book = (Book) lib.getBooks().get(0);
		System.out.println(book.getTitle());

		// show that the container is set
		System.out.println(book.eContainer() == lib);
		System.out.println(writer.getBooks().get(0) == book);

		// Now add a new writer and book
		Writer george = LibraryFactory.eINSTANCE.createWriter();
		george.setName("G. Orwell");

		// create a new book and set the writer and library
		Book georgesBook = LibraryFactory.eINSTANCE.createBook();
		georgesBook.setPages(250);
		georgesBook.setTitle("1984");
		georgesBook.setCategory(BookCategory.SCIENCE_FICTION_LITERAL);
		georgesBook.setAuthor(george);

		lib.getBooks().add(georgesBook);
		lib.getWriters().add(george);

		// and close of
		tx.commit();
		pm.close();

		// reopen the pm/transaction
		pm = jpoxDataStore.getPMF().getPersistenceManager();
		tx = pm.currentTransaction();
		tx.begin();

		// retrieve all books
		Query qry = pm.newQuery("SELECT FROM " + BookImpl.class.getName());
		Collection coll = (Collection) qry.execute();
		System.out.println(((Book) coll.iterator().next()).getTitle()); // show a title
		System.out.println(((Book) coll.iterator().next()).getTitle()); // show a title

		// retrieve a book which has a writer with the name of G. Orwell
		qry = pm.newQuery("SELECT FROM " + BookImpl.class.getName() + " WHERE "
				+ " title==\"1984\" && author == writ && writ.name == \"G. Orwell\" " + "VARIABLES "
				+ WriterImpl.class.getName() + " writ");
		coll = (Collection) qry.execute();
		System.out.println(coll.size()); // should be 1
		Book bk = (Book) coll.iterator().next();
		System.out.println(bk.getTitle()); // should be 1984
		System.out.println(bk.getAuthor().getName()); // should be G. Orwell

		// read the library with a name ending on Library
		qry = pm.newQuery("SELECT FROM " + LibraryImpl.class.getName() + " WHERE name.endsWith(\"Library\")");
		coll = (Collection) qry.execute();
		lib = (Library) coll.iterator().next();

		qry = pm.newQuery("SELECT FROM " + JpoxHelper.INSTANCE.getInstanceClass(Book.class).getName() + " WHERE "
				+ " title==\"1984\" && author == writ && writ.name == \"G. Orwell\" " + "VARIABLES "
				+ WriterImpl.class.getName() + " writ");
		System.err.println(((Collection) qry.execute()).size());

		// retrieving all books
		qry = pm.newQuery("SELECT FROM " + JpoxHelper.INSTANCE.getInstanceClass(Book.class).getName());
		System.err.println(((Collection) qry.execute()).size());

		// retrieving all libraries with a name ending on library
		qry = pm.newQuery("SELECT FROM " + JpoxHelper.INSTANCE.getInstanceClass(Library.class).getName()
				+ " WHERE name.endsWith(\"Library\")");
		System.err.println(((Collection) qry.execute()).size());

		// and really close of
		tx.commit();
		pm.close();

		try {
			String uriStr = "jpox://?" + JPOXResourceDAO.DS_NAME_PARAM + "=MyPMF";
			final URI uri = URI.createURI(uriStr);
			ResourceSet resourceSet = new ResourceSetImpl();
			final Resource res = resourceSet.createResource(uri);

			res.load(Collections.EMPTY_MAP);
			Iterator it = res.getContents().iterator();
			Library libTest;
			while (it.hasNext()) {
				libTest = (Library) it.next();
				System.out.println(libTest.getName());
			}

			Library libNew = LibraryFactory.eINSTANCE.createLibrary();
			libNew.setName("My Second Library");

			// create a writer
			Writer writerNew = LibraryFactory.eINSTANCE.createWriter();
			writerNew.setName("I. Asimov");

			// and one of his books
			Book bookNew = LibraryFactory.eINSTANCE.createBook();
			bookNew.setAuthor(writerNew);
			bookNew.setPages(305);
			bookNew.setTitle("Foundation and Empire");
			bookNew.setCategory(BookCategory.SCIENCE_FICTION_LITERAL);

			// add the writer/book to the library.
			libNew.getWriters().add(writerNew);
			libNew.getBooks().add(bookNew);

			// Add the top-level object to the resource
			res.getContents().add(libNew);

			// and save them all
			res.save(Collections.EMPTY_MAP);
		} catch (IOException i) {
			throw new RuntimeException("IOException " + i.getMessage(), i);
		}

		jpoxDataStore.close();
	}
}

Back to the top