diff options
author | Khurshid Fayzullaev <khurshidfayzullaev@yahoo.com> | 2014-04-08 16:51:43 -0400 |
---|---|---|
committer | Khurshid Fayzullaev <khurshidfayzullaev@yahoo.com> | 2014-04-08 16:51:43 -0400 |
commit | fd7aa110eaf07540b45ab360f655b59669352ced (patch) | |
tree | 50f5ef846f8c08a54b1fab475772ebad84979472 /src/org/traccar/web/server/model | |
parent | f97b868dc1bbf9698d05a9473da6ddd0c01640e8 (diff) | |
download | trackermap-web-fd7aa110eaf07540b45ab360f655b59669352ced.tar.gz trackermap-web-fd7aa110eaf07540b45ab360f655b59669352ced.tar.bz2 trackermap-web-fd7aa110eaf07540b45ab360f655b59669352ced.zip |
Prevent adding devices with the equal Unique IDs
Diffstat (limited to 'src/org/traccar/web/server/model')
-rw-r--r-- | src/org/traccar/web/server/model/DataServiceImpl.java | 33 |
1 files 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<Device> query = entityManager.createQuery("SELECT x FROM Device x WHERE x.id = :id", Device.class); + query.setParameter("id", device); + List<Device> 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<Device> query = entityManager.createQuery("SELECT x FROM Device x WHERE x.id = :id", Device.class); + query.setParameter("id", device); + List<Device> 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; |