소스 검색

Added objectMapper

Lukas Cerny 4 년 전
부모
커밋
ea81fff85d
2개의 변경된 파일98개의 추가작업 그리고 13개의 파일을 삭제
  1. 62 0
      src/main/java/io/senslog/core/ObjectMapper.java
  2. 36 13
      src/main/java/io/senslog/service/DataService.java

+ 62 - 0
src/main/java/io/senslog/core/ObjectMapper.java

@@ -0,0 +1,62 @@
+package io.senslog.core;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+public class ObjectMapper {
+
+    private static class Tuple<F, T> {
+        private final F value1;
+        private final T value2;
+        public static <F, T> Tuple<F, T> of(F v1, T v2) {
+            return new Tuple<>(v1, v2);
+        }
+        private Tuple(F v1, T v2) {
+            this.value1 = v1;
+            this.value2 = v2;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+            Tuple<?, ?> tuple = (Tuple<?, ?>) o;
+            return Objects.equals(value1, tuple.value1) &&
+                    Objects.equals(value2, tuple.value2);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(value1, value2);
+        }
+    }
+
+    public static ObjectMapper create() {
+        return new ObjectMapper(new HashMap<>());
+    }
+
+    private final Map<Tuple<?, ?>, Function<?, ?>> registeredMappings;
+
+    private ObjectMapper(Map<Tuple<?, ?>, Function<?, ?>> mappings) {
+        this.registeredMappings = mappings;
+    }
+
+    public <F, T> ObjectMapper register(Class<F> fromClass, Class<T> toClass, Function<F, T> mapper) {
+        this.registeredMappings.put(Tuple.of(fromClass, toClass), mapper);
+        return this;
+    }
+
+    @SuppressWarnings("unchecked")
+    public <F, T> T mapTo(F object, Class<T> toClass) {
+        Function<F, T> fnc = (Function<F, T>) registeredMappings.get(Tuple.of(object.getClass(), toClass));
+        if (fnc != null) {
+            return fnc.apply(object);
+        }
+
+        throw new IllegalArgumentException(
+                "Can not find any mappings from the class: " + object.getClass() + " to the class: " + toClass
+        );
+    }
+}

+ 36 - 13
src/main/java/io/senslog/service/DataService.java

@@ -1,5 +1,6 @@
 package io.senslog.service;
 
+import io.senslog.core.ObjectMapper;
 import io.senslog.database.DBException;
 import io.senslog.database.DBRepositoryPool;
 import io.senslog.database.model.DBObservation;
@@ -23,6 +24,25 @@ public class DataService {
     // TODO where to get this value?
     private static final boolean USE_TRACKS = false;
 
+    private static final ObjectMapper mapper;
+
+    static {
+        mapper = ObjectMapper.create()
+                // WSObservation <--> DBObservation
+                .register(WSObservation.class, DBObservation.class,
+                        o -> new DBObservation(o.getUnitId(), o.getSensorId(), o.getValue(), o.getTimestamp())
+                )
+                .register(DBObservation.class, WSObservation.class,
+                        o -> new WSObservation(o.getUnitId(), o.getSensorId(), o.getValue(), o.getTimestamp())
+                )
+                // WSPosition  <--> DBPosition
+                .register(WSPosition.class, DBPosition.class,
+                        p -> new DBPosition(p.getUnitId(), p.getLatitude(), p.getLongitude(),
+                                p.getTimestamp(), p.getAltitude(), p.getSpeed(), p.getDop(), 4326
+                        )
+                );
+    }
+
     private final ObservationRepository observationRep;
     private final PositionRepository positionRep;
     private final SensorRepository unitRep;
@@ -54,32 +74,35 @@ public class DataService {
             // TODO solve
         }
 
-        DBObservation dbObservation = new DBObservation(
-                wsObservation.getUnitId(), wsObservation.getSensorId(),
-                wsObservation.getValue(), wsObservation.getTimestamp()
-        );
-
         try {
+            DBObservation dbObservation = mapper.mapTo(wsObservation, DBObservation.class);
             return observationRep.insert(dbObservation);
         } catch (DBException e) {
             throw new WSException(500, e.getMessage());
         }
     }
 
+    public boolean saveObservations(List<WSObservation> wsObservations) {
+
+        try {
+            List<DBObservation> dbObservations = new ArrayList<>(wsObservations.size());
+            for (WSObservation wsO : wsObservations) {
+                dbObservations.add(mapper.mapTo(wsO, DBObservation.class));
+            }
+            return observationRep.insert(dbObservations);
+        } catch (DBException e) {
+            throw new WSException(500, e.getMessage());
+        }
+    }
+
     public boolean savePosition(WSPosition wsPosition) {
 
         if (USE_TRACKS) {
             // TODO solve(wsPosition);
         }
 
-
-        DBPosition dbPosition = new DBPosition(
-                wsPosition.getUnitId(), wsPosition.getLatitude(), wsPosition.getLongitude(),
-                wsPosition.getTimestamp(), wsPosition.getAltitude(), wsPosition.getSpeed(),
-                wsPosition.getDop(), 4326
-        );
-
         try {
+            DBPosition dbPosition = mapper.mapTo(wsPosition, DBPosition.class);
             return positionRep.insert(dbPosition);
         } catch (DBException e) {
             throw new WSException(500, e.getMessage());
@@ -107,7 +130,7 @@ public class DataService {
 
             List<WSObservation> result = new ArrayList<>(observations.size());
             for (DBObservation dbO : observations) {
-                result.add(new WSObservation(dbO.getUnitId(), dbO.getSensorId(), dbO.getValue(), dbO.getTimestamp()));
+                result.add(mapper.mapTo(dbO, WSObservation.class));
             }
             return result;
         } catch (DBException e) {