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

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

View File

@@ -53,6 +53,7 @@ import jfxtras.labs.map.render.MapMarkable;
import javax.inject.Inject;
import java.net.URL;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
@@ -227,7 +228,7 @@ public class CacheDetailsController implements Initializable, SessionContextList
icon.setImage(GeocachingIcons.getIcon(currentCache));
placedByTextfield.setText(currentCache.getPlacedBy());
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());
containerTextfield.setText(currentCache.getContainer());
fillShortDescription(currentCache);

View File

@@ -36,16 +36,11 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 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 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 Map<Long, User> userCache = new HashMap<>();
@@ -204,14 +202,14 @@ public class GroundspeakGPXReader implements GPXReader {
final Element cacheElement = waypointElement.getChild(CACHE, groundspeakNamespace);
setId(cacheElement, mainWaypoint);
parseCacheElement(cacheElement, cache);
} catch (DataConversionException | MalformedURLException e) {
} catch (DataConversionException | ParseException e) {
// TODO: do some batch error handling
e.printStackTrace();
}
return cache;
}
private void parseCacheElement(Element cacheElement, Cache cache) throws DataConversionException {
private void parseCacheElement(Element cacheElement, Cache cache) throws DataConversionException, ParseException {
setId(cacheElement, cache);
setAvailable(cacheElement, cache);
setArchived(cacheElement, cache);
@@ -259,7 +257,7 @@ public class GroundspeakGPXReader implements GPXReader {
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);
if (logsElement != null) {
final List<Log> logs = new ArrayList<>();
@@ -305,9 +303,9 @@ public class GroundspeakGPXReader implements GPXReader {
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 LocalDateTime date = LocalDateTime.parse(dateText, DateTimeFormatter.ISO_DATE_TIME);
final Date date = DATE_FORMAT.parse(dateText);
log.setDate(date);
}
@@ -424,12 +422,12 @@ public class GroundspeakGPXReader implements GPXReader {
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());
}
private void setUrl(Element cacheElement, Waypoint waypoint) throws MalformedURLException {
waypoint.setUrl(new URL(cacheElement.getChild(URL, defaultNamespace).getTextTrim()));
private void setUrl(Element cacheElement, Waypoint waypoint) {
waypoint.setUrl(cacheElement.getChild(URL, defaultNamespace).getTextTrim());
}
private void setDescription(Element cacheElement, Waypoint waypoint) {
@@ -445,9 +443,9 @@ public class GroundspeakGPXReader implements GPXReader {
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 LocalDateTime date = LocalDateTime.parse(timeText, DateTimeFormatter.ISO_DATE_TIME);
final Date date = DATE_FORMAT.parse(timeText);
waypoint.setTime(date);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,13 +5,15 @@
*/
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.ProgressListener;
import de.geofroggerfx.model.*;
import de.geofroggerfx.model.Cache;
import de.geofroggerfx.model.CacheList;
import de.geofroggerfx.sql.DatabaseService;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
@@ -35,10 +37,9 @@ public class CacheServiceImpl implements CacheService {
@Override
public void storeCaches(List<Cache> caches) {
EntityManager em = dbService.getEntityManager();
OObjectDatabaseTx database = dbService.getDatabase();
try {
int transactionNumber = 0;
int currentCacheNumber = 0;
int numberOfCaches = caches.size();
for (Cache cache : caches) {
@@ -48,42 +49,10 @@ public class CacheServiceImpl implements CacheService {
"Save caches to Database " + currentCacheNumber + " / " + numberOfCaches,
(double) currentCacheNumber / (double) numberOfCaches));
// begin transaction if the transaction counter is set to zero
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();
database.save(cache);
}
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
}
fireEvent(new ProgressEvent("Database",
@@ -98,9 +67,9 @@ public class CacheServiceImpl implements CacheService {
List<Cache> caches = new ArrayList<>();
try {
EntityManager em = dbService.getEntityManager();
String query = "select c from Cache c order by c."+sortField.getFieldName()+" "+direction.toString();
List<Cache> result = em.createQuery(query).getResultList();
OObjectDatabaseTx database = dbService.getDatabase();
String query = "select * from Cache order by "+sortField.getFieldName()+" "+direction.toString();
List<Cache> result = database.query(new OSQLSynchQuery<Cache>(query));
if (result != null) {
caches = result;
}
@@ -122,9 +91,9 @@ public class CacheServiceImpl implements CacheService {
public List<CacheList> getAllCacheLists() {
List<CacheList> lists = new ArrayList<>();
try {
EntityManager em = dbService.getEntityManager();
String query = "select l from CacheList l order by l.name";
List<CacheList> result = em.createQuery(query).getResultList();
OObjectDatabaseTx database = dbService.getDatabase();
String query = "select * from CacheList order by name";
List<CacheList> result = database.query(new OSQLSynchQuery<CacheList>(query));
if (result != null) {
lists = result;
}
@@ -139,10 +108,12 @@ public class CacheServiceImpl implements CacheService {
public boolean doesCacheListNameExist(String name) {
boolean doesExist = false;
try {
EntityManager em = dbService.getEntityManager();
String query = "select count(l) from CacheList l where l.name = :name";
Long result = (Long)em.createQuery(query).setParameter("name", name).getSingleResult();
doesExist = result > 0;
OObjectDatabaseTx database = dbService.getDatabase();
String query = "select * from CacheList l where l.name = :name";
List<CacheList> result = database.query(new OSQLSynchQuery<CacheList>(query));
if (result != null) {
doesExist = result.size() > 0;
}
} catch (Exception e) {
e.printStackTrace();
}
@@ -151,14 +122,11 @@ public class CacheServiceImpl implements CacheService {
@Override
public void deleteCacheList(CacheList cacheList) {
EntityManager em = dbService.getEntityManager();
OObjectDatabaseTx database = dbService.getDatabase();
try {
em.getTransaction().begin();
em.remove(cacheList);
em.getTransaction().commit();
database.delete(cacheList);
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
}
}
@@ -167,14 +135,11 @@ public class CacheServiceImpl implements CacheService {
*/
@Override
public void storeCacheList(CacheList list) {
EntityManager em = dbService.getEntityManager();
OObjectDatabaseTx database = dbService.getDatabase();
try {
em.getTransaction().begin();
em.merge(list);
em.getTransaction().commit();
database.save(list);
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
}
}
}

View File

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

View File

@@ -25,9 +25,9 @@
*/
package de.geofroggerfx.sql;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
/**
* @author Andreas
@@ -35,18 +35,34 @@ import javax.persistence.Persistence;
public class DatabaseServiceImpl implements DatabaseService {
private static final String PERSISTENCE_UNIT_NAME = "geocaches";
private EntityManagerFactory factory;
private EntityManager em;
private OObjectDatabaseTx db ;
public DatabaseServiceImpl() {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
em = factory.createEntityManager();
db = new OObjectDatabaseTx("plocal:./"+PERSISTENCE_UNIT_NAME);
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
public EntityManager getEntityManager() {
assert (em != null) : "no entity manager available";
return em;
public void close() {
assert (db != null) : "no database available";
if (!db.isClosed()) {
db.close();
}
}
}