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-05-17 07:18:37 +02:00
|
|
|
import de.geofroggerfx.application.ProgressEvent;
|
|
|
|
|
import de.geofroggerfx.application.ProgressListener;
|
|
|
|
|
import de.geofroggerfx.model.Attribute;
|
|
|
|
|
import de.geofroggerfx.model.Cache;
|
|
|
|
|
import de.geofroggerfx.model.Log;
|
|
|
|
|
import de.geofroggerfx.model.TravelBug;
|
|
|
|
|
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-27 20:30:35 +02:00
|
|
|
import javax.persistence.EntityManager;
|
2013-09-18 20:44:20 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Andreas
|
|
|
|
|
*/
|
|
|
|
|
public class CacheServiceImpl implements CacheService {
|
|
|
|
|
|
2013-09-29 17:50:09 +02:00
|
|
|
private static final int TRANSACTION_SIZE = 100;
|
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) {
|
2013-09-27 20:30:35 +02:00
|
|
|
EntityManager em = dbService.getEntityManager();
|
|
|
|
|
|
2013-09-29 17:50:09 +02:00
|
|
|
try {
|
|
|
|
|
int transactionNumber = 0;
|
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
|
|
|
|
2013-09-29 17:50:09 +02:00
|
|
|
// begin transaction if the transaction counter is set to zero
|
2014-05-23 10:24:33 +02:00
|
|
|
if (transactionNumber == 0) { em.getTransaction().begin(); }
|
2013-09-29 17:50:09 +02:00
|
|
|
transactionNumber++;
|
|
|
|
|
|
2013-09-27 20:30:35 +02:00
|
|
|
em.merge(cache.getOwner());
|
|
|
|
|
em.merge(cache.getMainWayPoint());
|
2013-09-18 20:44:20 +02:00
|
|
|
|
2013-09-27 20:30:35 +02:00
|
|
|
for (Log log: cache.getLogs()) {
|
|
|
|
|
em.merge(log);
|
|
|
|
|
em.merge(log.getFinder());
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-27 20:30:35 +02:00
|
|
|
for (Attribute attribute: cache.getAttributes()) {
|
|
|
|
|
em.merge(attribute);
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-27 20:30:35 +02:00
|
|
|
for (TravelBug bug: cache.getTravelBugs()) {
|
|
|
|
|
em.merge(bug);
|
2013-09-18 20:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-27 20:30:35 +02:00
|
|
|
em.merge(cache);
|
2013-09-29 17:50:09 +02:00
|
|
|
|
|
|
|
|
// 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) {
|
2013-09-27 20:30:35 +02:00
|
|
|
em.getTransaction().commit();
|
|
|
|
|
}
|
2013-09-29 17:50:09 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
em.getTransaction().rollback();
|
|
|
|
|
}
|
2013-09-18 20:44:20 +02:00
|
|
|
|
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
|
2013-09-28 00:11:37 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
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 {
|
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|