From e4702bdae9f0618eab30f07ba5f7ab814cc08806 Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Tue, 8 Apr 2014 16:48:32 -0400 Subject: Prevent adding devices with the equal Unique IDs --- src/org/traccar/web/client/controller/DeviceController.java | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/client/controller/DeviceController.java b/src/org/traccar/web/client/controller/DeviceController.java index 94c81ed3..95eb60d7 100644 --- a/src/org/traccar/web/client/controller/DeviceController.java +++ b/src/org/traccar/web/client/controller/DeviceController.java @@ -84,6 +84,10 @@ public class DeviceController implements ContentController, DeviceView.DeviceHan public void onSuccess(Device result) { deviceStore.add(result); } + @Override + public void onFailure(Throwable caught) { + new AlertMessageBox("Error", "Device with this Unique ID already exists").show(); + } }); } }).show(); @@ -99,6 +103,10 @@ public class DeviceController implements ContentController, DeviceView.DeviceHan public void onSuccess(Device result) { deviceStore.update(result); } + @Override + public void onFailure(Throwable caught) { + new AlertMessageBox("Error", "Device with this Unique ID already exists").show(); + } }); } }).show(); -- cgit v1.2.3 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(-) (limited to 'src/org/traccar/web') 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 From 3c0fa476056eb6c8cff57c8861cbc6fc63812ed5 Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Tue, 8 Apr 2014 19:25:46 -0400 Subject: Update DataServiceImpl.java --- src/org/traccar/web/server/model/DataServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index f0761271..af2b81fd 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -317,7 +317,7 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService } else { - throw new IllegalStateException() + throw new IllegalStateException(); } } catch (RuntimeException e) { entityManager.getTransaction().rollback(); -- cgit v1.2.3 From 37e994bafc08c1a54a4b996d4b1d568ff09f89be Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Tue, 8 Apr 2014 19:28:30 -0400 Subject: Update DeviceController.java --- src/org/traccar/web/client/controller/DeviceController.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/client/controller/DeviceController.java b/src/org/traccar/web/client/controller/DeviceController.java index 95eb60d7..b884537b 100644 --- a/src/org/traccar/web/client/controller/DeviceController.java +++ b/src/org/traccar/web/client/controller/DeviceController.java @@ -28,6 +28,7 @@ import com.google.gwt.core.client.GWT; import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.widget.core.client.ContentPanel; import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton; +import com.sencha.gxt.widget.core.client.box.AlertMessageBox; import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox; import com.sencha.gxt.widget.core.client.event.HideEvent; -- cgit v1.2.3 From 9c3f7f913766cbd6be85062179463eda751d5c3b Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Tue, 8 Apr 2014 19:40:50 -0400 Subject: Update DataServiceImpl.java --- src/org/traccar/web/server/model/DataServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index af2b81fd..338f2fef 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -305,7 +305,7 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService User user = getSessionUser(); entityManager.getTransaction().begin(); try { - TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.id = :id", Device.class); + TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); query.setParameter("id", device); List results = query.getResultList(); @@ -332,7 +332,7 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService synchronized (entityManager) { entityManager.getTransaction().begin(); try { - TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.id = :id", Device.class); + TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); query.setParameter("id", device); List results = query.getResultList(); if (results.isEmpty()) { -- cgit v1.2.3 From 438ee014d2b786e634049068b3e866e357e1aafe Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Tue, 8 Apr 2014 19:45:40 -0400 Subject: Update DataServiceImpl.java --- src/org/traccar/web/server/model/DataServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 338f2fef..9706eb1b 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -306,7 +306,7 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService entityManager.getTransaction().begin(); try { TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); - query.setParameter("id", device); + query.setParameter("id", device.getUniqueId()); List results = query.getResultList(); if (results.isEmpty()) { @@ -333,7 +333,7 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService entityManager.getTransaction().begin(); try { TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); - query.setParameter("id", device); + query.setParameter("id", device.getUniqueId()); List results = query.getResultList(); if (results.isEmpty()) { device = entityManager.merge(device); -- cgit v1.2.3 From f8bc5fb042fb2477ea159fc359fdc06b493f0f3e Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Thu, 10 Apr 2014 15:32:04 -0400 Subject: Update DataServiceImpl.java --- src/org/traccar/web/server/model/DataServiceImpl.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 9706eb1b..cc0b30e0 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -191,14 +191,15 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService if (currentUser.getAdmin()) { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { + + String login = user.getLogin(); + TypedQuery query = entityManager.createQuery( + "SELECT x FROM User x WHERE x.login = :login", User.class); + query.setParameter("login", login); + List results = query.getResultList(); + entityManager.getTransaction().begin(); try { - String login = user.getLogin(); - TypedQuery query = entityManager.createQuery( - "SELECT x FROM User x WHERE x.login = :login", User.class); - query.setParameter("login", login); - List results = query.getResultList(); - if (results.isEmpty()) { entityManager.persist(user); entityManager.getTransaction().commit(); -- cgit v1.2.3 From 4f0750af0eabce7b6689847d070a0237b1130955 Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Thu, 10 Apr 2014 15:34:17 -0400 Subject: Update DataServiceImpl.java --- src/org/traccar/web/server/model/DataServiceImpl.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index cc0b30e0..3164b102 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -304,12 +304,13 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { User user = getSessionUser(); + + TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); + query.setParameter("id", device.getUniqueId()); + List results = query.getResultList(); + entityManager.getTransaction().begin(); try { - TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); - query.setParameter("id", device.getUniqueId()); - List results = query.getResultList(); - if (results.isEmpty()) { entityManager.persist(device); user.getDevices().add(device); @@ -331,11 +332,13 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService public Device updateDevice(Device device) { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { + + TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); + query.setParameter("id", device.getUniqueId()); + List results = query.getResultList(); + entityManager.getTransaction().begin(); try { - TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); - query.setParameter("id", device.getUniqueId()); - List results = query.getResultList(); if (results.isEmpty()) { device = entityManager.merge(device); entityManager.getTransaction().commit(); -- cgit v1.2.3 From 99a02b06a63a3c48999575a1db2a6dc093812ee2 Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Thu, 10 Apr 2014 16:01:44 -0400 Subject: Update DataServiceImpl.java --- src/org/traccar/web/server/model/DataServiceImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 3164b102..7f4172e0 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -303,12 +303,11 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService public Device addDevice(Device device) { EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { - User user = getSessionUser(); - TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); query.setParameter("id", device.getUniqueId()); List results = query.getResultList(); + ser user = getSessionUser(); entityManager.getTransaction().begin(); try { if (results.isEmpty()) { -- cgit v1.2.3 From 90653a4d9ecd6739f548700c9284fd9f64a1a709 Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Thu, 10 Apr 2014 16:11:26 -0400 Subject: Update DataServiceImpl.java --- src/org/traccar/web/server/model/DataServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 7f4172e0..d4857d57 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -307,7 +307,7 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService query.setParameter("id", device.getUniqueId()); List results = query.getResultList(); - ser user = getSessionUser(); + User user = getSessionUser(); entityManager.getTransaction().begin(); try { if (results.isEmpty()) { -- cgit v1.2.3 From 58a5627460c73d954cc9928b3df30827698638f6 Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Thu, 10 Apr 2014 16:48:59 -0400 Subject: Update DataServiceImpl.java --- .../traccar/web/server/model/DataServiceImpl.java | 104 ++++++++++----------- 1 file changed, 52 insertions(+), 52 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index d4857d57..2c88800c 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -192,28 +192,25 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService EntityManager entityManager = getSessionEntityManager(); synchronized (entityManager) { - String login = user.getLogin(); - TypedQuery query = entityManager.createQuery( - "SELECT x FROM User x WHERE x.login = :login", User.class); - query.setParameter("login", login); - List results = query.getResultList(); - - entityManager.getTransaction().begin(); - try { - 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 query = entityManager.createQuery("SELECT x FROM User x WHERE x.login = :login", User.class); + query.setParameter("login", login); + List 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(); } @@ -302,55 +299,58 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService @Override public Device addDevice(Device device) { EntityManager entityManager = getSessionEntityManager(); - synchronized (entityManager) { + synchronized (entityManager) { TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); query.setParameter("id", device.getUniqueId()); List results = query.getResultList(); User user = getSessionUser(); - entityManager.getTransaction().begin(); - try { - 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; + + 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) { - TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); - query.setParameter("id", device.getUniqueId()); - List results = query.getResultList(); + TypedQuery query = entityManager.createQuery("SELECT x FROM Device x WHERE x.uniqueId = :id", Device.class); + query.setParameter("id", device.getUniqueId()); + List results = query.getResultList(); - entityManager.getTransaction().begin(); - try { - if (results.isEmpty()) { + + if (results.isEmpty()) { + entityManager.getTransaction().begin(); + try { device = entityManager.merge(device); entityManager.getTransaction().commit(); return device; - } - else - { - throw new IllegalStateException(); - } - } catch (RuntimeException e) { - entityManager.getTransaction().rollback(); - throw e; + } catch (RuntimeException e) { + entityManager.getTransaction().rollback(); + throw e; + } } + else + { + throw new IllegalStateException(); + } } } -- cgit v1.2.3