[Lada-commits] [PATCH 6 of 8] Added concrete class to lock objects based on the tree_modified timestamp

Wald Commits scm-commit at wald.intevation.org
Wed Mar 25 16:26:28 CET 2015


# HG changeset patch
# User Raimund Renkert <raimund.renkert at intevation.de>
# Date 1427296714 -3600
# Node ID b2af88e8eb15fb1b19e399a9c2dd9712ae8c98ce
# Parent  ec13e741ec7127e9881a21d86c5da3850e65b50a
Added concrete class to lock objects based on the tree_modified timestamp.

diff -r ec13e741ec71 -r b2af88e8eb15 src/main/java/de/intevation/lada/lock/TimestampLocker.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/intevation/lada/lock/TimestampLocker.java	Wed Mar 25 16:18:34 2015 +0100
@@ -0,0 +1,94 @@
+package de.intevation.lada.lock;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.Timestamp;
+
+import javax.inject.Inject;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.lada.model.land.LMessung;
+import de.intevation.lada.model.land.LProbe;
+import de.intevation.lada.util.annotation.RepositoryConfig;
+import de.intevation.lada.util.data.Repository;
+import de.intevation.lada.util.data.RepositoryType;
+import de.intevation.lada.util.rest.Response;
+
+ at LockConfig(type=LockType.TIMESTAMP)
+public class TimestampLocker implements ObjectLocker {
+
+    @Inject
+    private Logger logger;
+
+    @Inject
+    @RepositoryConfig(type=RepositoryType.RO)
+    Repository repository;
+
+    @Override
+    public boolean isLocked(Object o) {
+        if (o instanceof LProbe) {
+            LProbe newProbe = (LProbe)o;
+            LProbe oldProbe = (LProbe)repository.getById(
+                LProbe.class,
+                newProbe.getId(),
+                "land").getData();
+            logger.debug("old: " + oldProbe.getTreeModified().getTime());
+            logger.debug("new: " + newProbe.getTreeModified().getTime());
+            if (oldProbe.getTreeModified().getTime() >
+                    newProbe.getTreeModified().getTime()) {
+                return true;
+            }
+        }
+        else {
+            Method[] methods = o.getClass().getMethods();
+            for (Method m: methods) {
+                if (m.getName().equals("getProbeId")) {
+                    Integer id;
+                    try {
+                        id = (Integer) m.invoke(o);
+                    } catch (IllegalAccessException | IllegalArgumentException
+                            | InvocationTargetException e) {
+                        return true;
+                    }
+                    Response response =
+                        repository.getById(LProbe.class, id, "land");
+                    LProbe probe = (LProbe)response.getData();
+                    return isNewer(o, probe.getTreeModified());
+                }
+                if (m.getName().equals("getMessungsId")) {
+                    Integer id;
+                    try {
+                        id = (Integer) m.invoke(o);
+                    } catch (IllegalAccessException | IllegalArgumentException
+                            | InvocationTargetException e) {
+                        return true;
+                    }
+                    Response mResponse =
+                        repository.getById(LMessung.class, id, "land");
+                    LMessung messung = (LMessung)mResponse.getData();
+                    Response pResponse =
+                        repository.getById(LProbe.class, messung.getProbeId(), "land");
+                    LProbe probe = (LProbe)pResponse.getData();
+                    boolean newerMessung = isNewer(o, messung.getTreeModified());
+                    boolean newerProbe = isNewer(o, probe.getTreeModified());
+                    return newerMessung || newerProbe;
+                }
+            }
+        }
+        return false;
+    }
+
+    private boolean isNewer(Object o, Timestamp t) {
+        Method m;
+        try {
+            m = o.getClass().getMethod("getTreeModified");
+            Timestamp ot = (Timestamp)m.invoke(o);
+            return t.getTime() > ot.getTime();
+        } catch (NoSuchMethodException | SecurityException |
+                 IllegalAccessException | IllegalArgumentException|
+                 InvocationTargetException e) {
+            return true;
+        }
+    }
+}


More information about the Lada-commits mailing list