changed the package name to the domain name

This commit is contained in:
2014-05-17 07:18:37 +02:00
parent 471b532290
commit 2af2f56ade
48 changed files with 116 additions and 127 deletions

View File

@@ -0,0 +1,42 @@
/*
* 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.
*/
package de.geofroggerfx.service;
import de.geofroggerfx.application.ProgressListener;
import de.geofroggerfx.model.Cache;
import java.util.List;
/**
* @author Andreas
*/
public interface CacheService {
void storeCaches(List<Cache> caches);
List<Cache> getAllCaches(CacheSortField sortField, SortDirection direction);
void addListener(ProgressListener listener);
}

View File

@@ -0,0 +1,120 @@
/*
* 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.
*/
package de.geofroggerfx.service;
import de.geofroggerfx.application.ProgressEvent;
import de.geofroggerfx.application.ProgressListener;
import de.geofroggerfx.application.ServiceManager;
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;
import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
/**
* @author Andreas
*/
public class CacheServiceImpl implements CacheService {
private static final int TRANSACTION_SIZE = 100;
private final DatabaseService dbService = ServiceManager.getInstance().getDatabaseService();
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) {
EntityManager em = dbService.getEntityManager();
try {
int transactionNumber = 0;
int currentCacheNumber = 0;
int numberOfCaches = caches.size();
for (Cache cache : caches) {
currentCacheNumber++;
fireEvent(new ProgressEvent("Database",
ProgressEvent.State.RUNNING,
"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();
}
} catch (Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
}
fireEvent(new ProgressEvent("Database",
ProgressEvent.State.FINISHED,
"Caches are saved to Database"));
}
@Override
@SuppressWarnings("unchecked")
public List<Cache> getAllCaches(CacheSortField sortField, SortDirection direction) {
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();
if (result != null) {
caches = result;
}
} catch (Exception e) {
e.printStackTrace();
}
return caches;
}
private void fireEvent(ProgressEvent event) {
listeners.stream().forEach((l) -> l.progress(event));
}
}

View File

@@ -0,0 +1,25 @@
package de.geofroggerfx.service;
/**
* Sort fields on cache object
*
* @author abi
*/
public enum CacheSortField {
NAME("name"),
TYPE("type"),
DIFFICULTY("difficulty"),
TERRAIN("terrain"),
PLACEDBY("placedBy"),
OWNER("owner");
private String fieldName;
private CacheSortField(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldName() {
return fieldName;
}
}

View File

@@ -0,0 +1,11 @@
package de.geofroggerfx.service;
/**
* direction of sorting
*
* @author abi
*/
public enum SortDirection {
ASC,
DESC
}

View File

@@ -0,0 +1,33 @@
/*
* 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.
*/
package de.geofroggerfx.service;
/**
* @author Andreas
*/
public interface UserService {
}