aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2013-01-02 14:44:52 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2013-01-02 14:44:52 +1300
commit7032c42fb017582f1343128466b8df51c155f78d (patch)
tree387e03f5ee6137513c6798c5e6558ed51af0d019 /src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java
parent65913d45863159b9652865bc4f23eb62120d9b62 (diff)
downloadetbsa-traccar-web-7032c42fb017582f1343128466b8df51c155f78d.tar.gz
etbsa-traccar-web-7032c42fb017582f1343128466b8df51c155f78d.tar.bz2
etbsa-traccar-web-7032c42fb017582f1343128466b8df51c155f78d.zip
Finished devices panel
Diffstat (limited to 'src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java')
-rw-r--r--src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java53
1 files changed, 45 insertions, 8 deletions
diff --git a/src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java b/src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java
index 8ee9d63..335cc17 100644
--- a/src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java
+++ b/src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java
@@ -19,6 +19,8 @@ import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class DatabaseServiceImpl extends RemoteServiceServlet implements DatabaseService {
+ private static final long serialVersionUID = 1;
+
private static final String PERSISTENCE_UNIT = "traccar";
private static final String ATTRIBUTE_USER = "user";
@@ -41,6 +43,11 @@ public class DatabaseServiceImpl extends RemoteServiceServlet implements Databas
}
@Override
+ public boolean authenticated() {
+ return (getUser() != null);
+ }
+
+ @Override
public boolean authenticate(String login, String password) {
TypedQuery<User> query = entityManager.createQuery(
"SELECT x FROM User x WHERE x.login = :login", User.class);
@@ -60,8 +67,11 @@ public class DatabaseServiceImpl extends RemoteServiceServlet implements Databas
user.setLogin(login);
user.setPassword(password);
entityManager.getTransaction().begin();
- entityManager.persist(user);
- entityManager.getTransaction().commit();
+ try {
+ entityManager.persist(user);
+ } finally {
+ entityManager.getTransaction().commit();
+ }
setUser(user);
return true;
}
@@ -69,14 +79,41 @@ public class DatabaseServiceImpl extends RemoteServiceServlet implements Databas
@Override
public List<Device> getDevices() {
User user = getUser();
- if (user != null) {
- List<Device> devices = new LinkedList<Device>();
- for (Device device : user.getDevices()) {
- devices.add(device);
+ List<Device> devices = new LinkedList<Device>();
+ for (Device device : user.getDevices()) {
+ devices.add(device);
+ }
+ return devices;
+ }
+
+ @Override
+ public boolean storeDevice(Device device) {
+ User user = getUser();
+ boolean isNew = device.getId() == 0;
+ entityManager.getTransaction().begin();
+ try {
+ device = entityManager.merge(device);
+ if (isNew) {
+ user.getDevices().add(device);
}
- return devices;
+ } finally {
+ entityManager.getTransaction().commit();
}
- return null;
+ return true;
+ }
+
+ @Override
+ public boolean removeDevice(Device device) {
+ User user = getUser();
+ entityManager.getTransaction().begin();
+ try {
+ device = entityManager.merge(device);
+ user.getDevices().remove(device);
+ entityManager.remove(device);
+ } finally {
+ entityManager.getTransaction().commit();
+ }
+ return true;
}
@Override