2013-09-18 20:44:20 +02:00
|
|
|
/*
|
2014-08-29 00:39:27 +02:00
|
|
|
* Copyright (c) 2013, Andreas Billmann <abi@geofroggerfx.de>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2013-09-18 20:44:20 +02:00
|
|
|
*/
|
2014-05-17 07:18:37 +02:00
|
|
|
package de.geofroggerfx.service;
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2014-08-29 00:39:27 +02:00
|
|
|
import com.orientechnologies.orient.core.intent.OIntentMassiveInsert;
|
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 {
|
2014-08-29 00:39:27 +02:00
|
|
|
long start = System.currentTimeMillis();
|
2013-09-27 20:30:35 +02:00
|
|
|
for (Cache cache : caches) {
|
2014-08-16 08:18:37 +02:00
|
|
|
Cache existingCache = findCacheById(cache.getId());
|
|
|
|
|
if (existingCache != null) {
|
|
|
|
|
database.delete(existingCache);
|
|
|
|
|
}
|
2014-08-29 00:39:27 +02:00
|
|
|
}
|
2014-08-16 08:18:37 +02:00
|
|
|
|
2014-08-29 00:39:27 +02:00
|
|
|
long deletes = System.currentTimeMillis();
|
2014-08-16 08:18:37 +02:00
|
|
|
|
2014-08-29 00:39:27 +02:00
|
|
|
int currentCacheNumber = 0;
|
|
|
|
|
int numberOfCaches = caches.size();
|
|
|
|
|
database.declareIntent( new OIntentMassiveInsert() );
|
|
|
|
|
for (Cache cache : caches) {
|
|
|
|
|
currentCacheNumber++;
|
|
|
|
|
fireEvent(new ProgressEvent("Database",
|
|
|
|
|
ProgressEvent.State.RUNNING,
|
|
|
|
|
"Save caches to Database " + currentCacheNumber + " / " + numberOfCaches,
|
|
|
|
|
(double) currentCacheNumber / (double) numberOfCaches));
|
|
|
|
|
database.save(cache);
|
2013-09-27 20:30:35 +02:00
|
|
|
}
|
2014-08-29 00:39:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
long saves = System.currentTimeMillis();
|
|
|
|
|
|
|
|
|
|
System.out.println("Time to delete: "+(deletes-start));
|
|
|
|
|
System.out.println("Time to save: "+(saves-deletes));
|
|
|
|
|
System.out.println("Time total: "+(saves-start));
|
|
|
|
|
|
2013-09-29 17:50:09 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
2014-08-29 00:39:27 +02:00
|
|
|
} finally {
|
|
|
|
|
database.declareIntent( null );
|
2013-09-29 17:50:09 +02:00
|
|
|
}
|
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));
|
|
|
|
|
|
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
|
|
|
}
|