From fd7aa110eaf07540b45ab360f655b59669352ced Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Tue, 8 Apr 2014 16:51:43 -0400 Subject: Prevent adding devices with the equal Unique IDs --- .../traccar/web/server/model/DataServiceImpl.java | 33 +++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 509bca4a..f0761271 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -305,10 +305,20 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService User user = getSessionUser(); entityManager.getTransaction().begin(); try { - entityManager.persist(device); - user.getDevices().add(device); - entityManager.getTransaction().commit(); - return device; + TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.id = :id", Device.class); + query.setParameter("id", device); + List results = query.getResultList(); + + if (results.isEmpty()) { + entityManager.persist(device); + user.getDevices().add(device); + entityManager.getTransaction().commit(); + return device; + } + else + { + throw new IllegalStateException() + } } catch (RuntimeException e) { entityManager.getTransaction().rollback(); throw e; @@ -322,9 +332,18 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService synchronized (entityManager) { entityManager.getTransaction().begin(); try { - device = entityManager.merge(device); - entityManager.getTransaction().commit(); - return device; + TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.id = :id", Device.class); + query.setParameter("id", device); + List results = query.getResultList(); + if (results.isEmpty()) { + device = entityManager.merge(device); + entityManager.getTransaction().commit(); + return device; + } + else + { + throw new IllegalStateException(); + } } catch (RuntimeException e) { entityManager.getTransaction().rollback(); throw e; -- cgit v1.2.3