|
|
@@ -39,20 +39,29 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Future<Integer> updateDriverAction(DriverAction data) {
|
|
|
+ public Future<Integer> updateActionByDriverUnit(DriverAction data) {
|
|
|
Tuple params = Tuple.of(data.getTimestamp(), data.getDriverId(), data.getActionId(), data.getUnitId());
|
|
|
- return client.withTransaction(client -> client
|
|
|
- .preparedQuery("UPDATE maplog.driver_to_action SET to_time=$1 " +
|
|
|
- "WHERE driver_id = $2 AND action_id = $3 AND unitId = $4 AND to_time IS NULL AND from_time < $1")
|
|
|
+ return client.withTransaction(conn -> conn.preparedQuery("UPDATE maplog.driver_to_action SET to_time = $1 " +
|
|
|
+ "WHERE driver_id = $2 AND action_id = $3 AND unit_id = $4 AND to_time IS NULL AND from_time < $1 RETURNING id")
|
|
|
.execute(params)
|
|
|
- .flatMap(res -> {
|
|
|
- if (res.rowCount() <= 0) {
|
|
|
- return client.preparedQuery("INSERT INTO maplog.driver_to_action(from_time, driverId, actionId, unitId) VALUES ($1, $2, $3, $4)")
|
|
|
+ .flatMap(r1 -> {
|
|
|
+ if (r1.rowCount() <= 0) {
|
|
|
+ return conn.preparedQuery("INSERT INTO maplog.driver_to_action(from_time, driver_id, action_id, unit_id) " +
|
|
|
+ "SELECT $1, $2, $3, $4 WHERE EXISTS(SELECT action_id FROM maplog.action WHERE action_id = $3) RETURNING id")
|
|
|
.execute(params)
|
|
|
- .map(RowSet::rowCount)
|
|
|
- .onFailure(logger::catching);
|
|
|
+ .flatMap(r2 -> {
|
|
|
+ if (r2.rowCount() <= 0) {
|
|
|
+ return conn.preparedQuery("WITH rows AS (INSERT INTO maplog.action(action_id, name) VALUES ($3, 'GENERATED-ACTION') RETURNING action_id) " +
|
|
|
+ "INSERT INTO maplog.driver_to_action(from_time, driver_id, action_id, unit_id) " +
|
|
|
+ "SELECT $1, $2, rows.action_id, $4 FROM rows RETURNING id;")
|
|
|
+ .execute(params)
|
|
|
+ .map(RowSet::rowCount);
|
|
|
+ } else {
|
|
|
+ return Future.succeededFuture(r2.rowCount());
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
- return Future.succeededFuture(res.rowCount());
|
|
|
+ return Future.succeededFuture(r1.rowCount());
|
|
|
}));
|
|
|
}
|
|
|
|
|
|
@@ -68,16 +77,9 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
data.getLocation().getAltitude(),
|
|
|
data.getLocation().getAngle(),
|
|
|
data.getSpeed(),
|
|
|
- data.getObservedValues()
|
|
|
- ))
|
|
|
- .onComplete(res -> {
|
|
|
- if (res.succeeded()) {
|
|
|
- logger.info(res.result());
|
|
|
- } else {
|
|
|
- logger.error(res.cause());
|
|
|
- }
|
|
|
- })
|
|
|
- .map(rs -> rs.iterator().next().getInteger(0));
|
|
|
+ data.getObservedValues()))
|
|
|
+ .map(RowSet::iterator)
|
|
|
+ .map(it -> it.hasNext() ? it.next().getInteger(0) : 0);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -309,24 +311,24 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
|
|
|
@Override
|
|
|
public Future<List<Driver>> allDrivers() {
|
|
|
- return client.query("SELECT id, name FROM maplog.driver ORDER BY id")
|
|
|
+ return client.query("SELECT driver_id, name FROM maplog.driver ORDER BY driver_id")
|
|
|
.execute()
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(row -> Driver.of(
|
|
|
- row.getInteger("id"),
|
|
|
+ row.getInteger("driver_id"),
|
|
|
row.getString("name")
|
|
|
)).collect(toList())
|
|
|
);
|
|
|
}
|
|
|
|
|
|
private static final Function<Row, Driver> ROW_TO_DRIVER = (row) -> Driver.of(
|
|
|
- row.getInteger("id"),
|
|
|
+ row.getInteger("driver_id"),
|
|
|
row.getString("name")
|
|
|
);
|
|
|
|
|
|
@Override
|
|
|
public Future<Driver> findDriverById(int driverId) {
|
|
|
- return client.preparedQuery("SELECT id, name FROM maplog.driver WHERE id = $1")
|
|
|
+ return client.preparedQuery("SELECT driver_id, name FROM maplog.driver WHERE driver_id = $1")
|
|
|
.execute(Tuple.of(driverId))
|
|
|
.map(RowSet::iterator)
|
|
|
.map(iterator -> iterator.hasNext() ? ROW_TO_DRIVER.apply(iterator.next()) : null)
|
|
|
@@ -336,13 +338,13 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
|
|
|
@Override
|
|
|
public Future<List<Driver>> findDriversByUnitId(long unitId) {
|
|
|
- return client.preparedQuery("SELECT d.id, d.name FROM maplog.driver AS d " +
|
|
|
- "JOIN maplog.driver_to_action AS dta ON dta.driver_id = d.id " +
|
|
|
+ return client.preparedQuery("SELECT d.driver_id, d.name FROM maplog.driver AS d " +
|
|
|
+ "JOIN maplog.driver_to_action AS dta ON dta.driver_id = d.driver_id " +
|
|
|
"WHERE dta.unit_id = $1")
|
|
|
.execute(Tuple.of(unitId))
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(row -> Driver.of(
|
|
|
- row.getInteger("id"),
|
|
|
+ row.getInteger("driver_id"),
|
|
|
row.getString("name")
|
|
|
)).collect(toList())
|
|
|
);
|
|
|
@@ -350,13 +352,13 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
|
|
|
@Override
|
|
|
public Future<List<Action>> findActionsByDriverIdAndUnitId(int driverId, long unitId) {
|
|
|
- return client.preparedQuery("SELECT a.id, a.name FROM maplog.action AS a " +
|
|
|
- "JOIN maplog.driver_to_action AS dta ON a.id = dta.action_id " +
|
|
|
- "WHERE dta.driver_id = $1 AND dta.unit_id = $2 ORDER BY a.id")
|
|
|
+ return client.preparedQuery("SELECT a.action_id, a.name FROM maplog.action AS a " +
|
|
|
+ "JOIN maplog.driver_to_action AS dta ON a.action_id = dta.action_id " +
|
|
|
+ "WHERE dta.driver_id = $1 AND dta.unit_id = $2 ORDER BY a.action_id")
|
|
|
.execute(Tuple.of(driverId, unitId))
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(row -> Action.of(
|
|
|
- row.getInteger("id"),
|
|
|
+ row.getInteger("action_id"),
|
|
|
row.getString("name")
|
|
|
)).collect(toList())
|
|
|
);
|
|
|
@@ -364,28 +366,28 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
|
|
|
@Override
|
|
|
public Future<List<Action>> findActionsByDriverId(int driverId) {
|
|
|
- return client.preparedQuery("SELECT a.id, a.name FROM maplog.action AS a " +
|
|
|
- "JOIN maplog.driver_to_action AS dta ON a.id = dta.action_id " +
|
|
|
- "WHERE dta.driver_id = $1 ORDER BY a.id")
|
|
|
+ return client.preparedQuery("SELECT a.action_id, a.name FROM maplog.action AS a " +
|
|
|
+ "JOIN maplog.driver_to_action AS dta ON a.action_id = dta.action_id " +
|
|
|
+ "WHERE dta.driver_id = $1 ORDER BY a.action_id")
|
|
|
.execute(Tuple.of(driverId))
|
|
|
.map(rs -> StreamSupport.stream(rs.spliterator(), false)
|
|
|
.map(row -> Action.of(
|
|
|
- row.getInteger("id"),
|
|
|
+ row.getInteger("action_id"),
|
|
|
row.getString("name")
|
|
|
)).collect(toList())
|
|
|
);
|
|
|
}
|
|
|
|
|
|
private static final Function<Row, Action> ROW_TO_ACTION = (row) -> Action.of(
|
|
|
- row.getInteger("id"),
|
|
|
+ row.getInteger("action_id"),
|
|
|
row.getString("name")
|
|
|
);
|
|
|
|
|
|
@Override
|
|
|
public Future<Action> findActionByIdAndDriverId(int actionId, int driverId) {
|
|
|
- return client.preparedQuery("SELECT a.id, a.name FROM maplog.action AS a " +
|
|
|
- "JOIN maplog.driver_to_action AS dta ON a.id = dta.action_id " +
|
|
|
- "WHERE a.id = $1 AND dta.driver_id = $2")
|
|
|
+ return client.preparedQuery("SELECT a.action_id, a.name FROM maplog.action AS a " +
|
|
|
+ "JOIN maplog.driver_to_action AS dta ON a.action_id = dta.action_id " +
|
|
|
+ "WHERE a.action_id = $1 AND dta.driver_id = $2")
|
|
|
.execute(Tuple.of(actionId, driverId))
|
|
|
.map(RowSet::iterator)
|
|
|
.map(iterator -> iterator.hasNext() ? ROW_TO_ACTION.apply(iterator.next()) : null)
|
|
|
@@ -395,9 +397,9 @@ public class MapLogRepository implements SensLogRepository {
|
|
|
|
|
|
@Override
|
|
|
public Future<Action> findActionByIdAndDriverIdAndUnitId(int actionId, int driverId, long unitId) {
|
|
|
- return client.preparedQuery("SELECT a.id, a.name FROM maplog.action AS a " +
|
|
|
- "JOIN maplog.driver_to_action AS dta ON a.id = dta.action_id " +
|
|
|
- "WHERE a.id = $1 AND dta.driver_id = $2 AND dta.unit_id = $3")
|
|
|
+ return client.preparedQuery("SELECT a.action_id, a.name FROM maplog.action AS a " +
|
|
|
+ "JOIN maplog.driver_to_action AS dta ON a.action_id = dta.action_id " +
|
|
|
+ "WHERE a.action_id = $1 AND dta.driver_id = $2 AND dta.unit_id = $3")
|
|
|
.execute(Tuple.of(actionId, driverId, unitId))
|
|
|
.map(RowSet::iterator)
|
|
|
.map(iterator -> iterator.hasNext() ? ROW_TO_ACTION.apply(iterator.next()) : null)
|