added attributes to database
This commit is contained in:
@@ -26,12 +26,14 @@
|
|||||||
package de.geofroggerfx.dao.jdbc;
|
package de.geofroggerfx.dao.jdbc;
|
||||||
|
|
||||||
import de.geofroggerfx.dao.CacheDAO;
|
import de.geofroggerfx.dao.CacheDAO;
|
||||||
|
import de.geofroggerfx.model.Attribute;
|
||||||
import de.geofroggerfx.model.Cache;
|
import de.geofroggerfx.model.Cache;
|
||||||
import de.geofroggerfx.model.CacheListEntry;
|
import de.geofroggerfx.model.CacheListEntry;
|
||||||
import de.geofroggerfx.model.Waypoint;
|
import de.geofroggerfx.model.Waypoint;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.w3c.dom.Attr;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -54,7 +56,7 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
private static final Logger LOGGER = Logger.getLogger("JdbcCacheDAO");
|
private static final Logger LOGGER = Logger.getLogger("JdbcCacheDAO");
|
||||||
private static final String CACHE_TABLE = "cache";
|
private static final String CACHE_TABLE = "cache";
|
||||||
private static final String WAYPOINT_TABLE = "waypoint";
|
private static final String WAYPOINT_TABLE = "waypoint";
|
||||||
|
private static final String ATTRIBUTE_TABLE = "attribute";
|
||||||
|
|
||||||
private String[] columnListCache = new String[] {
|
private String[] columnListCache = new String[] {
|
||||||
"id",
|
"id",
|
||||||
@@ -88,6 +90,9 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
"symbol",
|
"symbol",
|
||||||
"type"};
|
"type"};
|
||||||
|
|
||||||
|
private String[] columnListAttribute = new String[] {
|
||||||
|
"id",
|
||||||
|
"cache_id"};
|
||||||
|
|
||||||
private JdbcTemplate jdbcTemplate;
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
@@ -100,6 +105,7 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
public void save(List<Cache> listOfCaches) {
|
public void save(List<Cache> listOfCaches) {
|
||||||
deleteExistingCaches(listOfCaches);
|
deleteExistingCaches(listOfCaches);
|
||||||
batchInsertCaches(listOfCaches);
|
batchInsertCaches(listOfCaches);
|
||||||
|
batchInsertAttributes(listOfCaches);
|
||||||
batchInsertWaypoints(listOfCaches);
|
batchInsertWaypoints(listOfCaches);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +120,7 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
@Override
|
@Override
|
||||||
public List<CacheListEntry> getAllCacheEntriesSortBy(String name, String asc) {
|
public List<CacheListEntry> getAllCacheEntriesSortBy(String name, String asc) {
|
||||||
return this.jdbcTemplate.query(
|
return this.jdbcTemplate.query(
|
||||||
"SELECT c.id, c.name AS name, c.name AS code, c.difficulty, c.terrain, c.type FROM "+CACHE_TABLE+" c ORDER BY "+name+" "+asc,
|
"SELECT c.id, c.name AS name, c.name AS code, c.difficulty, c.terrain, c.type FROM " + CACHE_TABLE + " c ORDER BY " + name + " " + asc,
|
||||||
(rs, rowNum) -> {
|
(rs, rowNum) -> {
|
||||||
return new CacheListEntry(
|
return new CacheListEntry(
|
||||||
rs.getLong("id"),
|
rs.getLong("id"),
|
||||||
@@ -136,6 +142,8 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
Waypoint waypoint = getWaypointForCacheId(cache.getId());
|
Waypoint waypoint = getWaypointForCacheId(cache.getId());
|
||||||
cache.setMainWayPoint(waypoint);
|
cache.setMainWayPoint(waypoint);
|
||||||
waypoint.setCache(cache);
|
waypoint.setCache(cache);
|
||||||
|
List<Attribute> attributes = getAttributesForCacheId(cache.getId());
|
||||||
|
cache.getAttributes().setAll(attributes);
|
||||||
return cache;
|
return cache;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -218,6 +226,33 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
LOGGER.info("try to save "+listOfCaches.size()+" waypoints -> done in "+(endInsert-startInsert)+" ms");
|
LOGGER.info("try to save "+listOfCaches.size()+" waypoints -> done in "+(endInsert-startInsert)+" ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void batchInsertAttributes(List<Cache> listOfCaches) {
|
||||||
|
long startInsert = System.currentTimeMillis();
|
||||||
|
|
||||||
|
List<Object[]> batch = listOfCaches.
|
||||||
|
parallelStream().
|
||||||
|
flatMap(cache -> cache.getAttributes().stream().map(attribute -> valuesFromAttribute(cache, attribute))).
|
||||||
|
collect(Collectors.toList());
|
||||||
|
LOGGER.info("try to save "+batch.size()+" attributes");
|
||||||
|
|
||||||
|
try {
|
||||||
|
String insertSQL = generateInsertSQL(ATTRIBUTE_TABLE, columnListAttribute);
|
||||||
|
int[] updateCounts = jdbcTemplate.batchUpdate(
|
||||||
|
insertSQL,
|
||||||
|
batch);
|
||||||
|
int updatedRows = 0;
|
||||||
|
for (int count:updateCounts) {
|
||||||
|
updatedRows += count;
|
||||||
|
}
|
||||||
|
LOGGER.info("batch inserted "+updatedRows+" attributes");
|
||||||
|
} catch (Throwable e) {
|
||||||
|
LOGGER.log(Level.SEVERE, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
long endInsert = System.currentTimeMillis();
|
||||||
|
LOGGER.info("try to save "+batch.size()+" attributes -> done in "+(endInsert-startInsert)+" ms");
|
||||||
|
}
|
||||||
|
|
||||||
private Waypoint getWaypointForCacheId(long cacheId) {
|
private Waypoint getWaypointForCacheId(long cacheId) {
|
||||||
return this.jdbcTemplate.queryForObject(
|
return this.jdbcTemplate.queryForObject(
|
||||||
generateSelectSQL(WAYPOINT_TABLE, columnListWaypoint) + " WHERE cache_id=?",
|
generateSelectSQL(WAYPOINT_TABLE, columnListWaypoint) + " WHERE cache_id=?",
|
||||||
@@ -228,6 +263,17 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<Attribute> getAttributesForCacheId(long cacheId) {
|
||||||
|
return this.jdbcTemplate.query(
|
||||||
|
generateSelectSQL(ATTRIBUTE_TABLE, columnListAttribute) + " WHERE cache_id=?",
|
||||||
|
new Object[]{cacheId},
|
||||||
|
(rs, rowNum) -> {
|
||||||
|
return getAttributeFromResultSet(rs);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private Cache getCacheFromResultSet(ResultSet rs) throws SQLException {
|
private Cache getCacheFromResultSet(ResultSet rs) throws SQLException {
|
||||||
Cache cache = new Cache();
|
Cache cache = new Cache();
|
||||||
@@ -288,6 +334,12 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
return waypoint;
|
return waypoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Attribute getAttributeFromResultSet(ResultSet rs) throws SQLException {
|
||||||
|
Attribute attribute = Attribute.getAttributeById(rs.getInt("id"));
|
||||||
|
return attribute;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private Object[] valuesFromWaypoint(Cache cache) {
|
private Object[] valuesFromWaypoint(Cache cache) {
|
||||||
return new Object[]{
|
return new Object[]{
|
||||||
cache.getMainWayPoint().getName(),
|
cache.getMainWayPoint().getName(),
|
||||||
@@ -301,18 +353,13 @@ public class JdbcCacheDAO implements CacheDAO {
|
|||||||
cache.getMainWayPoint().getSymbol(),
|
cache.getMainWayPoint().getSymbol(),
|
||||||
null
|
null
|
||||||
};
|
};
|
||||||
/*
|
}
|
||||||
"name",
|
|
||||||
"cache_id",
|
private Object[] valuesFromAttribute(Cache cache, Attribute attribute) {
|
||||||
"latitude",
|
return new Object[]{
|
||||||
"longitude",
|
attribute.getId(),
|
||||||
"time",
|
cache.getId()
|
||||||
"description",
|
};
|
||||||
"url",
|
|
||||||
"urlName",
|
|
||||||
"symbol",
|
|
||||||
"type"
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -298,8 +298,6 @@ public enum Attribute {
|
|||||||
GEOTOUR_FALSE(-67);
|
GEOTOUR_FALSE(-67);
|
||||||
|
|
||||||
|
|
||||||
private final static List<Long> attributes = new ArrayList<>();
|
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
private Attribute(int id) {
|
private Attribute(int id) {
|
||||||
@@ -322,16 +320,16 @@ public enum Attribute {
|
|||||||
idToCompare = -idToCompare;
|
idToCompare = -idToCompare;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return getAttributeById(idToCompare);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Attribute getAttributeById(int id) {
|
||||||
for (Attribute t : Attribute.values()) {
|
for (Attribute t : Attribute.values()) {
|
||||||
if (t.getId() == idToCompare) {
|
if (t.getId() == id) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
throw new IllegalArgumentException("unknown attribute id:" + id);
|
||||||
if (!attributes.contains(id)) {
|
|
||||||
attributes.add(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IllegalArgumentException("unknown attribute id:" + id + " inc:" + inc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
CREATE TABLE attribute (
|
||||||
|
id INT,
|
||||||
|
cache_id INT,
|
||||||
|
PRIMARY KEY (id, cache_id),
|
||||||
|
FOREIGN KEY(cache_id) REFERENCES cache(id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO version(version) values(2);
|
||||||
Reference in New Issue
Block a user