diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2014-04-12 18:48:51 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2014-04-12 18:48:51 +1200 |
commit | 891abffcf8c76fff62b2c4a35a41818d3eab6bc2 (patch) | |
tree | a82c7cfb1942d6883d3cac0ead01dcd0c0c490fc /src/org/traccar/web/server/model/DataServiceImpl.java | |
parent | f97b868dc1bbf9698d05a9473da6ddd0c01640e8 (diff) | |
parent | 58a5627460c73d954cc9928b3df30827698638f6 (diff) | |
download | trackermap-web-891abffcf8c76fff62b2c4a35a41818d3eab6bc2.tar.gz trackermap-web-891abffcf8c76fff62b2c4a35a41818d3eab6bc2.tar.bz2 trackermap-web-891abffcf8c76fff62b2c4a35a41818d3eab6bc2.zip |
Merge pull request #161 from khfayzullaev/patch-2
Prevent adding devices with same ids
Diffstat (limited to 'src/org/traccar/web/server/model/DataServiceImpl.java')
-rw-r--r-- | src/org/traccar/web/server/model/DataServiceImpl.java | 102 |
1 files changed, 62 insertions, 40 deletions
diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 509bca4a..2c88800c 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -191,28 +191,26 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService if (currentUser.getAdmin()) { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { - entityManager.getTransaction().begin(); - try { - String login = user.getLogin(); - TypedQuery<User> query = entityManager.createQuery( - "SELECT x FROM User x WHERE x.login = :login", User.class); - query.setParameter("login", login); - List<User> results = query.getResultList(); - - if (results.isEmpty()) { - entityManager.persist(user); - entityManager.getTransaction().commit(); - return user; - } - else - { - throw new IllegalStateException(); - } - } catch (RuntimeException e) { - entityManager.getTransaction().rollback(); - throw e; - } - } + + String login = user.getLogin(); + TypedQuery<User> query = entityManager.createQuery("SELECT x FROM User x WHERE x.login = :login", User.class); + query.setParameter("login", login); + List<User> results = query.getResultList(); + + if (results.isEmpty()) { + entityManager.getTransaction().begin(); + try { + entityManager.persist(user); + entityManager.getTransaction().commit(); + return user; + } catch (RuntimeException e) { + entityManager.getTransaction().rollback(); + throw e; + } + } else { + throw new IllegalStateException(); + } + } } else { throw new SecurityException(); } @@ -301,34 +299,58 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService @Override public Device addDevice(Device device) { EntityManager entityManager = getSessionEntityManager(); - synchronized (entityManager) { + synchronized (entityManager) { + TypedQuery<Device> query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); + query.setParameter("id", device.getUniqueId()); + List<Device> results = query.getResultList(); + User user = getSessionUser(); - entityManager.getTransaction().begin(); - try { - entityManager.persist(device); - user.getDevices().add(device); - entityManager.getTransaction().commit(); - return device; - } catch (RuntimeException e) { - entityManager.getTransaction().rollback(); - throw e; + + if (results.isEmpty()) { + entityManager.getTransaction().begin(); + try { + entityManager.persist(device); + user.getDevices().add(device); + entityManager.getTransaction().commit(); + return device; + } catch (RuntimeException e) { + entityManager.getTransaction().rollback(); + throw e; + } } + else + { + throw new IllegalStateException(); + } } } + @Override public Device updateDevice(Device device) { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { - entityManager.getTransaction().begin(); - try { - device = entityManager.merge(device); - entityManager.getTransaction().commit(); - return device; - } catch (RuntimeException e) { - entityManager.getTransaction().rollback(); - throw e; + + TypedQuery<Device> query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); + query.setParameter("id", device.getUniqueId()); + List<Device> results = query.getResultList(); + + + if (results.isEmpty()) { + entityManager.getTransaction().begin(); + try { + device = entityManager.merge(device); + entityManager.getTransaction().commit(); + return device; + } catch (RuntimeException e) { + entityManager.getTransaction().rollback(); + throw e; + } } + else + { + throw new IllegalStateException(); + } } } |