switch from jpa to orientdb

This commit is contained in:
2014-08-16 01:49:39 +02:00
parent 07b78c520c
commit cb9032ce30
17 changed files with 89 additions and 173 deletions

View File

@@ -53,10 +53,13 @@ dependencies {
compile files('lib/JFXtras/jfxtras-labs-8.0-r1-SNAPSHOT.jar', 'lib/ScenicView/ScenicView.jar') compile files('lib/JFXtras/jfxtras-labs-8.0-r1-SNAPSHOT.jar', 'lib/ScenicView/ScenicView.jar')
compile group: 'org.jdom', name: 'jdom2', version: '2.0.5' compile group: 'org.jdom', name: 'jdom2', version: '2.0.5'
compile group: 'org.controlsfx', name: 'controlsfx', version: '8.0.6' compile group: 'org.controlsfx', name: 'controlsfx', version: '8.0.6'
compile group: 'com.h2database', name: 'h2', version: '1.4.178'
compile group: 'org.eclipse.persistence', name: 'eclipselink', version: '2.5.0'
compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.2.1' compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.2.1'
compile group: 'org.jboss.weld.se', name: 'weld-se', version: '2.2.0.Final' compile group: 'org.jboss.weld.se', name: 'weld-se', version: '2.2.0.Final'
compile group: 'com.orientechnologies', name: 'orient-commons', version: '2.0-SNAPSHOT'
compile group: 'com.orientechnologies', name: 'orientdb-core', version: '2.0-SNAPSHOT'
compile group: 'com.orientechnologies', name: 'orient-server', version: '2.0-SNAPSHOT'
compile group: 'com.orientechnologies', name: 'orientdb-object', version: '2.0-SNAPSHOT'
} }
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////

View File

@@ -1,6 +1,6 @@
#Sun Sep 22 11:48:27 CEST 2013 #Fri Aug 15 19:56:32 CEST 2014
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.7-bin.zip distributionUrl=http\://services.gradle.org/distributions/gradle-1.7-all.zip

View File

@@ -1,27 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="geocaches" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>de.geofroggerfx.model.Attribute</class>
<class>de.geofroggerfx.model.Cache</class>
<class>de.geofroggerfx.model.Log</class>
<class>de.geofroggerfx.model.TravelBug</class>
<class>de.geofroggerfx.model.Type</class>
<class>de.geofroggerfx.model.User</class>
<class>de.geofroggerfx.model.Waypoint</class>
<class>de.geofroggerfx.model.CacheList</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:./geofroggerfxdb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value="sa"/>
<!--<property name="eclipselink.logging.level" value="FINEST"/>-->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
</persistence>

View File

@@ -84,7 +84,7 @@ public class GeoFroggerFXMain extends Application {
@Override @Override
public void stop() throws Exception { public void stop() throws Exception {
weldContainer.instance().select(DatabaseService.class).get().getEntityManager().close(); weldContainer.instance().select(DatabaseService.class).get().close();
weld.shutdown(); weld.shutdown();
super.stop(); super.stop();
} }

View File

@@ -53,6 +53,7 @@ import jfxtras.labs.map.render.MapMarkable;
import javax.inject.Inject; import javax.inject.Inject;
import java.net.URL; import java.net.URL;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@@ -227,7 +228,7 @@ public class CacheDetailsController implements Initializable, SessionContextList
icon.setImage(GeocachingIcons.getIcon(currentCache)); icon.setImage(GeocachingIcons.getIcon(currentCache));
placedByTextfield.setText(currentCache.getPlacedBy()); placedByTextfield.setText(currentCache.getPlacedBy());
ownerTextfield.setText(currentCache.getOwner().getName()); ownerTextfield.setText(currentCache.getOwner().getName());
date.setValue(currentCache.getMainWayPoint().getTime().toLocalDate()); date.setValue(currentCache.getMainWayPoint().getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
typeTextfield.setText(currentCache.getType().toGroundspeakString()); typeTextfield.setText(currentCache.getType().toGroundspeakString());
containerTextfield.setText(currentCache.getContainer()); containerTextfield.setText(currentCache.getContainer());
fillShortDescription(currentCache); fillShortDescription(currentCache);

View File

@@ -36,16 +36,11 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.time.LocalDateTime; import java.text.ParseException;
import java.time.format.DateTimeFormatter; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* TODO: describe the class * TODO: describe the class
@@ -95,6 +90,9 @@ public class GroundspeakGPXReader implements GPXReader {
public static final String DEFAULT_NAMESPACE_URL = "http://www.topografix.com/GPX/1/0"; public static final String DEFAULT_NAMESPACE_URL = "http://www.topografix.com/GPX/1/0";
public static final String GROUNDSPEAK_NAMESPACE_URL = "http://www.groundspeak.com/cache/1/0/1"; public static final String GROUNDSPEAK_NAMESPACE_URL = "http://www.groundspeak.com/cache/1/0/1";
// 2011-12-03T10:15:30+01:00
private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
private final List<ProgressListener> listeners = new ArrayList<>(); private final List<ProgressListener> listeners = new ArrayList<>();
private final Map<Long, User> userCache = new HashMap<>(); private final Map<Long, User> userCache = new HashMap<>();
@@ -204,14 +202,14 @@ public class GroundspeakGPXReader implements GPXReader {
final Element cacheElement = waypointElement.getChild(CACHE, groundspeakNamespace); final Element cacheElement = waypointElement.getChild(CACHE, groundspeakNamespace);
setId(cacheElement, mainWaypoint); setId(cacheElement, mainWaypoint);
parseCacheElement(cacheElement, cache); parseCacheElement(cacheElement, cache);
} catch (DataConversionException | MalformedURLException e) { } catch (DataConversionException | ParseException e) {
// TODO: do some batch error handling // TODO: do some batch error handling
e.printStackTrace(); e.printStackTrace();
} }
return cache; return cache;
} }
private void parseCacheElement(Element cacheElement, Cache cache) throws DataConversionException { private void parseCacheElement(Element cacheElement, Cache cache) throws DataConversionException, ParseException {
setId(cacheElement, cache); setId(cacheElement, cache);
setAvailable(cacheElement, cache); setAvailable(cacheElement, cache);
setArchived(cacheElement, cache); setArchived(cacheElement, cache);
@@ -259,7 +257,7 @@ public class GroundspeakGPXReader implements GPXReader {
travelBug.setId(travelBugElement.getAttribute(ID).getLongValue()); travelBug.setId(travelBugElement.getAttribute(ID).getLongValue());
} }
private void setLogs(Element cacheElement, Cache cache) throws DataConversionException { private void setLogs(Element cacheElement, Cache cache) throws DataConversionException, ParseException {
final Element logsElement = cacheElement.getChild(LOGS, groundspeakNamespace); final Element logsElement = cacheElement.getChild(LOGS, groundspeakNamespace);
if (logsElement != null) { if (logsElement != null) {
final List<Log> logs = new ArrayList<>(); final List<Log> logs = new ArrayList<>();
@@ -305,9 +303,9 @@ public class GroundspeakGPXReader implements GPXReader {
log.setId(logElement.getAttribute(ID).getLongValue()); log.setId(logElement.getAttribute(ID).getLongValue());
} }
private void setDate(Element logElement, Log log) { private void setDate(Element logElement, Log log) throws ParseException {
final String dateText = logElement.getChild(DATE, groundspeakNamespace).getTextTrim(); final String dateText = logElement.getChild(DATE, groundspeakNamespace).getTextTrim();
final LocalDateTime date = LocalDateTime.parse(dateText, DateTimeFormatter.ISO_DATE_TIME); final Date date = DATE_FORMAT.parse(dateText);
log.setDate(date); log.setDate(date);
} }
@@ -424,12 +422,12 @@ public class GroundspeakGPXReader implements GPXReader {
waypoint.setSymbol(cacheElement.getChild(SYM, defaultNamespace).getTextTrim()); waypoint.setSymbol(cacheElement.getChild(SYM, defaultNamespace).getTextTrim());
} }
private void setUrlName(Element cacheElement, Waypoint waypoint) throws MalformedURLException { private void setUrlName(Element cacheElement, Waypoint waypoint) {
waypoint.setUrlName(cacheElement.getChild(URLNAME, defaultNamespace).getTextTrim()); waypoint.setUrlName(cacheElement.getChild(URLNAME, defaultNamespace).getTextTrim());
} }
private void setUrl(Element cacheElement, Waypoint waypoint) throws MalformedURLException { private void setUrl(Element cacheElement, Waypoint waypoint) {
waypoint.setUrl(new URL(cacheElement.getChild(URL, defaultNamespace).getTextTrim())); waypoint.setUrl(cacheElement.getChild(URL, defaultNamespace).getTextTrim());
} }
private void setDescription(Element cacheElement, Waypoint waypoint) { private void setDescription(Element cacheElement, Waypoint waypoint) {
@@ -445,9 +443,9 @@ public class GroundspeakGPXReader implements GPXReader {
waypoint.setLongitude(cacheElement.getAttribute(LON).getDoubleValue()); waypoint.setLongitude(cacheElement.getAttribute(LON).getDoubleValue());
} }
private void setTime(Element cacheElement, Waypoint waypoint) { private void setTime(Element cacheElement, Waypoint waypoint) throws ParseException {
final String timeText = cacheElement.getChild(TIME, defaultNamespace).getTextTrim(); final String timeText = cacheElement.getChild(TIME, defaultNamespace).getTextTrim();
final LocalDateTime date = LocalDateTime.parse(timeText, DateTimeFormatter.ISO_DATE_TIME); final Date date = DATE_FORMAT.parse(timeText);
waypoint.setTime(date); waypoint.setTime(date);
} }

View File

@@ -25,16 +25,11 @@
*/ */
package de.geofroggerfx.model; package de.geofroggerfx.model;
import javax.persistence.Entity;
import javax.persistence.Id;
/** /**
* @author Andreas Billmann * @author Andreas Billmann
*/ */
@Entity
public class Attribute { public class Attribute {
@Id
private Long id; private Long id;
private boolean inc; private boolean inc;
private String text; private String text;

View File

@@ -25,16 +25,13 @@
*/ */
package de.geofroggerfx.model; package de.geofroggerfx.model;
import javax.persistence.*;
import java.util.List; import java.util.List;
/** /**
* @author Andreas Billmann * @author Andreas Billmann
*/ */
@Entity
public class Cache { public class Cache {
@Id
private Long id; private Long id;
private boolean available; private boolean available;
private boolean archived; private boolean archived;
@@ -58,7 +55,6 @@ public class Cache {
private List<TravelBug> travelBugs; private List<TravelBug> travelBugs;
private Waypoint mainWayPoint; private Waypoint mainWayPoint;
@OneToOne(fetch=FetchType.LAZY)
public Waypoint getMainWayPoint() { public Waypoint getMainWayPoint() {
return mainWayPoint; return mainWayPoint;
} }
@@ -107,7 +103,6 @@ public class Cache {
this.placedBy = placedBy; this.placedBy = placedBy;
} }
@OneToOne(fetch=FetchType.LAZY)
public User getOwner() { public User getOwner() {
return owner; return owner;
} }
@@ -116,7 +111,6 @@ public class Cache {
this.owner = owner; this.owner = owner;
} }
@Enumerated(EnumType.STRING)
public Type getType() { public Type getType() {
return type; return type;
} }
@@ -133,7 +127,6 @@ public class Cache {
this.container = container; this.container = container;
} }
@ManyToMany(fetch=FetchType.LAZY)
public List<Attribute> getAttributes() { public List<Attribute> getAttributes() {
return attributes; return attributes;
} }
@@ -214,7 +207,6 @@ public class Cache {
this.encodedHints = encodedHints; this.encodedHints = encodedHints;
} }
@OneToMany(fetch=FetchType.LAZY)
public List<Log> getLogs() { public List<Log> getLogs() {
return logs; return logs;
} }
@@ -223,7 +215,6 @@ public class Cache {
this.logs = logs; this.logs = logs;
} }
@OneToMany(fetch=FetchType.LAZY)
public List<TravelBug> getTravelBugs() { public List<TravelBug> getTravelBugs() {
return travelBugs; return travelBugs;
} }

View File

@@ -25,19 +25,13 @@
*/ */
package de.geofroggerfx.model; package de.geofroggerfx.model;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.List; import java.util.List;
/** /**
* @author Andreas Billmann * @author Andreas Billmann
*/ */
@Entity
public class CacheList { public class CacheList {
@Id
private String name; private String name;
private List<Cache> caches; private List<Cache> caches;
@@ -50,7 +44,6 @@ public class CacheList {
this.name = name; this.name = name;
} }
@OneToMany(fetch= FetchType.LAZY)
public List<Cache> getCaches() { public List<Cache> getCaches() {
return caches; return caches;
} }

View File

@@ -25,18 +25,16 @@
*/ */
package de.geofroggerfx.model; package de.geofroggerfx.model;
import javax.persistence.*;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Date;
/** /**
* @author Andreas Billmann * @author Andreas Billmann
*/ */
@Entity
public class Log { public class Log {
@Id
private Long id; private Long id;
private LocalDateTime date; private Date date;
private String type; private String type;
private User finder; private User finder;
private boolean textEncoded; private boolean textEncoded;
@@ -50,11 +48,11 @@ public class Log {
this.id = id; this.id = id;
} }
public LocalDateTime getDate() { public Date getDate() {
return date; return date;
} }
public void setDate(LocalDateTime date) { public void setDate(Date date) {
this.date = date; this.date = date;
} }
@@ -66,7 +64,6 @@ public class Log {
this.type = type; this.type = type;
} }
@OneToOne(cascade = CascadeType.PERSIST)
public User getFinder() { public User getFinder() {
return finder; return finder;
} }

View File

@@ -1,17 +1,12 @@
package de.geofroggerfx.model; package de.geofroggerfx.model;
import javax.persistence.Entity;
import javax.persistence.Id;
/** /**
* This class represents the application settings, like current user, etc. * This class represents the application settings, like current user, etc.
* *
* @author abi * @author abi
*/ */
@Entity
public class Settings { public class Settings {
@Id
private Long id; private Long id;
private Long currentUserID = new Long(3906456); private Long currentUserID = new Long(3906456);

View File

@@ -25,16 +25,11 @@
*/ */
package de.geofroggerfx.model; package de.geofroggerfx.model;
import javax.persistence.Entity;
import javax.persistence.Id;
/** /**
* @author Andreas Billmann * @author Andreas Billmann
*/ */
@Entity
public class TravelBug { public class TravelBug {
@Id
private Long id; private Long id;
private String ref; private String ref;
private String name; private String name;

View File

@@ -25,16 +25,11 @@
*/ */
package de.geofroggerfx.model; package de.geofroggerfx.model;
import javax.persistence.Entity;
import javax.persistence.Id;
/** /**
* @author Andreas Billmann * @author Andreas Billmann
*/ */
@Entity
public class User { public class User {
@Id
private Long id; private Long id;
private String name; private String name;

View File

@@ -25,25 +25,22 @@
*/ */
package de.geofroggerfx.model; package de.geofroggerfx.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.net.URL; import java.net.URL;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Date;
/** /**
* @author Andreas Billmann * @author Andreas Billmann
*/ */
@Entity
public class Waypoint { public class Waypoint {
@Id
private Long id; private Long id;
private double latitude; private double latitude;
private double longitude; private double longitude;
private String name; private String name;
private LocalDateTime time; private Date time;
private String description; private String description;
private URL url; private String url;
private String urlName; private String urlName;
private String symbol; private String symbol;
private String type; private String type;
@@ -72,11 +69,11 @@ public class Waypoint {
this.longitude = longitude; this.longitude = longitude;
} }
public void setTime(LocalDateTime time) { public void setTime(Date time) {
this.time = time; this.time = time;
} }
public LocalDateTime getTime() { public Date getTime() {
return time; return time;
} }
@@ -96,11 +93,11 @@ public class Waypoint {
this.description = description; this.description = description;
} }
public URL getUrl() { public String getUrl() {
return url; return url;
} }
public void setUrl(URL url) { public void setUrl(String url) {
this.url = url; this.url = url;
} }

View File

@@ -5,13 +5,15 @@
*/ */
package de.geofroggerfx.service; package de.geofroggerfx.service;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import de.geofroggerfx.application.ProgressEvent; import de.geofroggerfx.application.ProgressEvent;
import de.geofroggerfx.application.ProgressListener; import de.geofroggerfx.application.ProgressListener;
import de.geofroggerfx.model.*; import de.geofroggerfx.model.Cache;
import de.geofroggerfx.model.CacheList;
import de.geofroggerfx.sql.DatabaseService; import de.geofroggerfx.sql.DatabaseService;
import javax.inject.Inject; import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -35,10 +37,9 @@ public class CacheServiceImpl implements CacheService {
@Override @Override
public void storeCaches(List<Cache> caches) { public void storeCaches(List<Cache> caches) {
EntityManager em = dbService.getEntityManager(); OObjectDatabaseTx database = dbService.getDatabase();
try { try {
int transactionNumber = 0;
int currentCacheNumber = 0; int currentCacheNumber = 0;
int numberOfCaches = caches.size(); int numberOfCaches = caches.size();
for (Cache cache : caches) { for (Cache cache : caches) {
@@ -48,42 +49,10 @@ public class CacheServiceImpl implements CacheService {
"Save caches to Database " + currentCacheNumber + " / " + numberOfCaches, "Save caches to Database " + currentCacheNumber + " / " + numberOfCaches,
(double) currentCacheNumber / (double) numberOfCaches)); (double) currentCacheNumber / (double) numberOfCaches));
// begin transaction if the transaction counter is set to zero database.save(cache);
if (transactionNumber == 0) { em.getTransaction().begin(); }
transactionNumber++;
em.merge(cache.getOwner());
em.merge(cache.getMainWayPoint());
for (Log log: cache.getLogs()) {
em.merge(log);
em.merge(log.getFinder());
}
for (Attribute attribute: cache.getAttributes()) {
em.merge(attribute);
}
for (TravelBug bug: cache.getTravelBugs()) {
em.merge(bug);
}
em.merge(cache);
// comit every X caches
if (transactionNumber == TRANSACTION_SIZE) {
em.getTransaction().commit();
transactionNumber = 0;
}
}
// if there wasn?t a commit right before, commit the rest
if (transactionNumber != 0) {
em.getTransaction().commit();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
em.getTransaction().rollback();
} }
fireEvent(new ProgressEvent("Database", fireEvent(new ProgressEvent("Database",
@@ -98,9 +67,9 @@ public class CacheServiceImpl implements CacheService {
List<Cache> caches = new ArrayList<>(); List<Cache> caches = new ArrayList<>();
try { try {
EntityManager em = dbService.getEntityManager(); OObjectDatabaseTx database = dbService.getDatabase();
String query = "select c from Cache c order by c."+sortField.getFieldName()+" "+direction.toString(); String query = "select * from Cache order by "+sortField.getFieldName()+" "+direction.toString();
List<Cache> result = em.createQuery(query).getResultList(); List<Cache> result = database.query(new OSQLSynchQuery<Cache>(query));
if (result != null) { if (result != null) {
caches = result; caches = result;
} }
@@ -122,9 +91,9 @@ public class CacheServiceImpl implements CacheService {
public List<CacheList> getAllCacheLists() { public List<CacheList> getAllCacheLists() {
List<CacheList> lists = new ArrayList<>(); List<CacheList> lists = new ArrayList<>();
try { try {
EntityManager em = dbService.getEntityManager(); OObjectDatabaseTx database = dbService.getDatabase();
String query = "select l from CacheList l order by l.name"; String query = "select * from CacheList order by name";
List<CacheList> result = em.createQuery(query).getResultList(); List<CacheList> result = database.query(new OSQLSynchQuery<CacheList>(query));
if (result != null) { if (result != null) {
lists = result; lists = result;
} }
@@ -139,10 +108,12 @@ public class CacheServiceImpl implements CacheService {
public boolean doesCacheListNameExist(String name) { public boolean doesCacheListNameExist(String name) {
boolean doesExist = false; boolean doesExist = false;
try { try {
EntityManager em = dbService.getEntityManager(); OObjectDatabaseTx database = dbService.getDatabase();
String query = "select count(l) from CacheList l where l.name = :name"; String query = "select * from CacheList l where l.name = :name";
Long result = (Long)em.createQuery(query).setParameter("name", name).getSingleResult(); List<CacheList> result = database.query(new OSQLSynchQuery<CacheList>(query));
doesExist = result > 0; if (result != null) {
doesExist = result.size() > 0;
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -151,14 +122,11 @@ public class CacheServiceImpl implements CacheService {
@Override @Override
public void deleteCacheList(CacheList cacheList) { public void deleteCacheList(CacheList cacheList) {
EntityManager em = dbService.getEntityManager(); OObjectDatabaseTx database = dbService.getDatabase();
try { try {
em.getTransaction().begin(); database.delete(cacheList);
em.remove(cacheList);
em.getTransaction().commit();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
em.getTransaction().rollback();
} }
} }
@@ -167,14 +135,11 @@ public class CacheServiceImpl implements CacheService {
*/ */
@Override @Override
public void storeCacheList(CacheList list) { public void storeCacheList(CacheList list) {
EntityManager em = dbService.getEntityManager(); OObjectDatabaseTx database = dbService.getDatabase();
try { try {
em.getTransaction().begin(); database.save(list);
em.merge(list);
em.getTransaction().commit();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
em.getTransaction().rollback();
} }
} }
} }

View File

@@ -25,11 +25,13 @@
*/ */
package de.geofroggerfx.sql; package de.geofroggerfx.sql;
import javax.persistence.EntityManager;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
/** /**
* @author Andreas * @author Andreas
*/ */
public interface DatabaseService { public interface DatabaseService {
EntityManager getEntityManager(); OObjectDatabaseTx getDatabase();
void close();
} }

View File

@@ -25,9 +25,9 @@
*/ */
package de.geofroggerfx.sql; package de.geofroggerfx.sql;
import javax.persistence.EntityManager; import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import javax.persistence.EntityManagerFactory; import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import javax.persistence.Persistence; import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
/** /**
* @author Andreas * @author Andreas
@@ -35,18 +35,34 @@ import javax.persistence.Persistence;
public class DatabaseServiceImpl implements DatabaseService { public class DatabaseServiceImpl implements DatabaseService {
private static final String PERSISTENCE_UNIT_NAME = "geocaches"; private static final String PERSISTENCE_UNIT_NAME = "geocaches";
private EntityManagerFactory factory; private OObjectDatabaseTx db ;
private EntityManager em;
public DatabaseServiceImpl() { public DatabaseServiceImpl() {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); db = new OObjectDatabaseTx("plocal:./"+PERSISTENCE_UNIT_NAME);
em = factory.createEntityManager(); if (!db.exists()) {
db.create();
} else {
db.open("admin", "admin");
}
db.getEntityManager().registerEntityClasses("de.geofroggerfx.model");
}
@Override
public OObjectDatabaseTx getDatabase() {
assert (db != null) : "no database available";
ODatabaseRecordThreadLocal.INSTANCE.set(db.getUnderlying().getUnderlying());
return db;
} }
@Override @Override
public EntityManager getEntityManager() { public void close() {
assert (em != null) : "no entity manager available"; assert (db != null) : "no database available";
return em; if (!db.isClosed()) {
db.close();
}
} }
} }