2013-09-18 20:44:20 +02:00
|
|
|
/*
|
|
|
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
|
|
|
* To change this template file, choose Tools | Templates
|
|
|
|
|
* and open the template in the editor.
|
|
|
|
|
*/
|
2014-05-17 07:18:37 +02:00
|
|
|
package de.geofroggerfx.service;
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2014-08-16 01:49:39 +02:00
|
|
|
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
|
|
|
|
|
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
|
2014-05-17 07:18:37 +02:00
|
|
|
import de.geofroggerfx.application.ProgressEvent;
|
|
|
|
|
import de.geofroggerfx.application.ProgressListener;
|
2014-08-16 08:18:37 +02:00
|
|
|
import de.geofroggerfx.model.*;
|
2014-05-17 07:18:37 +02:00
|
|
|
import de.geofroggerfx.sql.DatabaseService;
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2014-05-23 10:24:33 +02:00
|
|
|
import javax.inject.Inject;
|
2013-09-18 20:44:20 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Andreas
|
|
|
|
|
*/
|
|
|
|
|
public class CacheServiceImpl implements CacheService {
|
|
|
|
|
|
2014-05-23 10:24:33 +02:00
|
|
|
@Inject
|
|
|
|
|
private DatabaseService dbService;
|
2013-09-18 20:44:20 +02:00
|
|
|
private final List<ProgressListener> listeners = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addListener(ProgressListener listener) {
|
|
|
|
|
if (!listeners.contains(listener)) {
|
|
|
|
|
listeners.add(listener);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void storeCaches(List<Cache> caches) {
|
2014-08-16 01:49:39 +02:00
|
|
|
OObjectDatabaseTx database = dbService.getDatabase();
|
2013-09-27 20:30:35 +02:00
|
|
|
|
2013-09-29 17:50:09 +02:00
|
|
|
try {
|
2013-09-27 20:30:35 +02:00
|
|
|
int currentCacheNumber = 0;
|
|
|
|
|
int numberOfCaches = caches.size();
|
|
|
|
|
for (Cache cache : caches) {
|
2013-09-18 20:44:20 +02:00
|
|
|
currentCacheNumber++;
|
2013-09-27 20:30:35 +02:00
|
|
|
fireEvent(new ProgressEvent("Database",
|
|
|
|
|
ProgressEvent.State.RUNNING,
|
|
|
|
|
"Save caches to Database " + currentCacheNumber + " / " + numberOfCaches,
|
|
|
|
|
(double) currentCacheNumber / (double) numberOfCaches));
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2014-08-16 08:18:37 +02:00
|
|
|
Cache existingCache = findCacheById(cache.getId());
|
|
|
|
|
if (existingCache != null) {
|
|
|
|
|
database.delete(existingCache);
|
|
|
|
|
}
|
2014-08-16 01:49:39 +02:00
|
|
|
database.save(cache);
|
2014-08-16 08:18:37 +02:00
|
|
|
|
|
|
|
|
|
2013-09-27 20:30:35 +02:00
|
|
|
}
|
2013-09-29 17:50:09 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2014-08-16 08:18:37 +02:00
|
|
|
System.out.println("cache count: " + database.countClass(Cache.class));
|
|
|
|
|
System.out.println("Log count: "+ database.countClass(Log.class));
|
|
|
|
|
System.out.println("Waypoint count: "+ database.countClass(Waypoint.class));
|
|
|
|
|
System.out.println("Attribute count: "+ database.countClass(Attribute.class));
|
|
|
|
|
|
2013-09-29 17:50:09 +02:00
|
|
|
fireEvent(new ProgressEvent("Database",
|
2013-09-27 20:30:35 +02:00
|
|
|
ProgressEvent.State.FINISHED,
|
|
|
|
|
"Caches are saved to Database"));
|
|
|
|
|
}
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2013-09-27 20:30:35 +02:00
|
|
|
@Override
|
2014-08-16 08:18:37 +02:00
|
|
|
public Cache findCacheById(Long id) {
|
|
|
|
|
Cache foundCache = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
OObjectDatabaseTx database = dbService.getDatabase();
|
|
|
|
|
String query = "select * from Cache where id="+id;
|
|
|
|
|
List<Cache> result = database.query(new OSQLSynchQuery<Cache>(query));
|
|
|
|
|
if (result != null && result.size() > 0) {
|
|
|
|
|
foundCache = result.get(0);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return foundCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-09-28 00:11:37 +02:00
|
|
|
public List<Cache> getAllCaches(CacheSortField sortField, SortDirection direction) {
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2013-09-27 20:30:35 +02:00
|
|
|
List<Cache> caches = new ArrayList<>();
|
2013-09-28 00:11:37 +02:00
|
|
|
try {
|
2014-08-16 01:49:39 +02:00
|
|
|
OObjectDatabaseTx database = dbService.getDatabase();
|
|
|
|
|
String query = "select * from Cache order by "+sortField.getFieldName()+" "+direction.toString();
|
|
|
|
|
List<Cache> result = database.query(new OSQLSynchQuery<Cache>(query));
|
2013-09-28 00:11:37 +02:00
|
|
|
if (result != null) {
|
|
|
|
|
caches = result;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|
2013-09-27 20:30:35 +02:00
|
|
|
|
2013-09-18 20:44:20 +02:00
|
|
|
return caches;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fireEvent(ProgressEvent event) {
|
2013-09-27 20:30:35 +02:00
|
|
|
listeners.stream().forEach((l) -> l.progress(event));
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-09 22:49:57 +02:00
|
|
|
/**
|
|
|
|
|
* Receive all cache lists
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<CacheList> getAllCacheLists() {
|
|
|
|
|
List<CacheList> lists = new ArrayList<>();
|
|
|
|
|
try {
|
2014-08-16 01:49:39 +02:00
|
|
|
OObjectDatabaseTx database = dbService.getDatabase();
|
|
|
|
|
String query = "select * from CacheList order by name";
|
|
|
|
|
List<CacheList> result = database.query(new OSQLSynchQuery<CacheList>(query));
|
2014-06-09 22:49:57 +02:00
|
|
|
if (result != null) {
|
|
|
|
|
lists = result;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2014-06-09 22:49:57 +02:00
|
|
|
return lists;
|
|
|
|
|
}
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2014-06-15 08:38:22 +02:00
|
|
|
@Override
|
|
|
|
|
public boolean doesCacheListNameExist(String name) {
|
|
|
|
|
boolean doesExist = false;
|
|
|
|
|
try {
|
2014-08-16 01:49:39 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2014-06-15 08:38:22 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return doesExist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void deleteCacheList(CacheList cacheList) {
|
2014-08-16 01:49:39 +02:00
|
|
|
OObjectDatabaseTx database = dbService.getDatabase();
|
2014-06-15 08:38:22 +02:00
|
|
|
try {
|
2014-08-16 01:49:39 +02:00
|
|
|
database.delete(cacheList);
|
2014-06-15 08:38:22 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-09 22:49:57 +02:00
|
|
|
@Override
|
|
|
|
|
public void storeCacheList(CacheList list) {
|
2014-08-16 01:49:39 +02:00
|
|
|
OObjectDatabaseTx database = dbService.getDatabase();
|
2014-06-09 22:49:57 +02:00
|
|
|
try {
|
2014-08-16 01:49:39 +02:00
|
|
|
database.save(list);
|
2014-06-09 22:49:57 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|