- found attribute is calculated at reading time from file, to avoid lazy loading of all the log entries from database

- kind of batch writing to database for better performance
This commit is contained in:
frosch95
2013-09-29 17:50:09 +02:00
parent 2116f4f88c
commit c0a64e2627
7 changed files with 175 additions and 29 deletions

View File

@@ -118,7 +118,7 @@ public class CacheListCell extends ListCell<Cache> {
name.setText(cache.getName());
dt.setText("D: " + cache.getDifficulty() + " / T:" + cache.getTerrain());
if (CacheUtils.hasUserFoundCache(cache, new Long(3906456))) {
if (cache.isFound()) {
foundIcon.setImage(IconManager.getIcon("/icons/iconmonstr-check-mark-11-icon.png", IconManager.IconSize.SMALL));
} else {
foundIcon.setImage(null);
@@ -129,7 +129,7 @@ public class CacheListCell extends ListCell<Cache> {
}
private void setStyleClassDependingOnFoundState(Cache cache) {
if (CacheUtils.hasUserFoundCache(cache, new Long(3906456))) {
if (cache.isFound()) {
addClasses(this, CACHE_LIST_FOUND_CLASS);
removeClasses(this, CACHE_LIST_NOT_FOUND_CLASS);
} else {

View File

@@ -165,19 +165,24 @@ public class GroundspeakGPXReader implements GPXReader {
private void parseContent() {
modelList = new ArrayList<>();
final Element root = content.getRootElement();
final List<Element> waypoints = root.getChildren(WPT, defaultNamespace);
try {
final Element root = content.getRootElement();
final List<Element> waypoints = root.getChildren(WPT, defaultNamespace);
int totalNumberOfCaches = waypoints.size();
int currentNumber = 0;
for (final Element waypointElement : waypoints) {
currentNumber++;
fireEvent(new ProgressEvent("GPX Reader",
ProgressEvent.State.RUNNING,
"Parse " + currentNumber + " of " + totalNumberOfCaches + " caches.",
(double) currentNumber / (double) totalNumberOfCaches));
final Cache cache = parseWaypointElement(waypointElement);
modelList.add(cache);
int totalNumberOfCaches = waypoints.size();
int currentNumber = 0;
for (final Element waypointElement : waypoints) {
currentNumber++;
fireEvent(new ProgressEvent("GPX Reader",
ProgressEvent.State.RUNNING,
"Parse " + currentNumber + " of " + totalNumberOfCaches + " caches.",
(double) currentNumber / (double) totalNumberOfCaches));
final Cache cache = parseWaypointElement(waypointElement);
cache.setFound(CacheUtils.hasUserFoundCache(cache, 3906456l));
modelList.add(cache);
}
} catch (Exception e) {
e.printStackTrace();
}
}

View File

@@ -38,6 +38,7 @@ public class Cache {
private Long id;
private boolean available;
private boolean archived;
private boolean found;
private String name;
private String placedBy;
private User owner;
@@ -231,6 +232,14 @@ public class Cache {
this.travelBugs = travelBugs;
}
public boolean isFound() {
return found;
}
public void setFound(final boolean found) {
this.found = found;
}
@Override
public String toString() {
return "Cache{"

View File

@@ -33,6 +33,8 @@ import java.util.List;
public class CacheUtils {
public static final String FOUND_IT = "Found it";
public static final String ATTENDED = "Attended";
public static final String WEBCAM_PHOTO = "Webcam Photo Taken";
public static boolean hasUserFoundCache(Cache cache, Long currentUser) {
boolean foundIt = false;
@@ -55,7 +57,7 @@ public class CacheUtils {
}
private static boolean isLogTypeFoundIt(Log log) {
return log.getType().equals(FOUND_IT);
return log.getType().equals(FOUND_IT) || log.getType().equals(ATTENDED) || log.getType().equals(WEBCAM_PHOTO);
}
}

View File

@@ -23,8 +23,8 @@ import java.util.List;
*/
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
@@ -38,6 +38,8 @@ public class CacheServiceImpl implements CacheService {
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) {
@@ -47,7 +49,10 @@ public class CacheServiceImpl implements CacheService {
"Save caches to Database " + currentCacheNumber + " / " + numberOfCaches,
(double) currentCacheNumber / (double) numberOfCaches));
em.getTransaction().begin();
// 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());
@@ -65,10 +70,24 @@ public class CacheServiceImpl implements CacheService {
}
em.merge(cache);
em.getTransaction().commit();
// comit every X caches
if (transactionNumber == TRANSACTION_SIZE) {
em.getTransaction().commit();
transactionNumber = 0;
}
}
fireEvent(new ProgressEvent("Database",
// 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"));
}