aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2012-11-18 22:49:30 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2012-11-18 22:49:30 +1300
commite081b313c717c1fb60a93f07613253f73b8cc933 (patch)
tree510c896ad15a18e6c150cd5cd0914052e4a10ad8 /src/main/java/org
parent3d63b73061d7ae7b56f6c1f5bfc265ef41981bbb (diff)
downloadtrackermap-web-e081b313c717c1fb60a93f07613253f73b8cc933.tar.gz
trackermap-web-e081b313c717c1fb60a93f07613253f73b8cc933.tar.bz2
trackermap-web-e081b313c717c1fb60a93f07613253f73b8cc933.zip
Persistence experiments
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/traccar/web/client/MapPanel.java29
-rw-r--r--src/main/java/org/traccar/web/client/Style.java13
-rw-r--r--src/main/java/org/traccar/web/client/Traccar.java61
-rw-r--r--src/main/java/org/traccar/web/client/database/DatabaseService.java8
-rw-r--r--src/main/java/org/traccar/web/client/database/DatabaseServiceAsync.java11
-rw-r--r--src/main/java/org/traccar/web/client/login/LoginDialog.java63
-rw-r--r--src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java68
-rw-r--r--src/main/java/org/traccar/web/shared/model/Device.java11
-rw-r--r--src/main/java/org/traccar/web/shared/model/Position.java8
-rw-r--r--src/main/java/org/traccar/web/shared/model/User.java12
10 files changed, 219 insertions, 65 deletions
diff --git a/src/main/java/org/traccar/web/client/MapPanel.java b/src/main/java/org/traccar/web/client/MapPanel.java
index 72ed0d3b..81d842e7 100644
--- a/src/main/java/org/traccar/web/client/MapPanel.java
+++ b/src/main/java/org/traccar/web/client/MapPanel.java
@@ -1,35 +1,22 @@
package org.traccar.web.client;
-import com.smartgwt.client.widgets.layout.SectionStack;
-import com.smartgwt.client.widgets.layout.SectionStackSection;
-import com.smartgwt.client.widgets.Label;
-import com.smartgwt.client.widgets.Canvas;
-import com.smartgwt.client.widgets.WidgetCanvas;
-import com.smartgwt.client.widgets.events.ResizedEvent;
-import com.smartgwt.client.widgets.events.ResizedHandler;
-import com.smartgwt.client.widgets.events.DragResizeStopEvent;
-import com.smartgwt.client.widgets.events.DragResizeStopHandler;
-import com.smartgwt.client.widgets.events.DrawEvent;
-import com.smartgwt.client.widgets.events.DrawHandler;
-import com.smartgwt.client.types.Overflow;
-
-import com.google.gwt.core.client.Scheduler;
-
import org.gwtopenmaps.openlayers.client.LonLat;
import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.MapOptions;
import org.gwtopenmaps.openlayers.client.MapWidget;
import org.gwtopenmaps.openlayers.client.Projection;
-import org.gwtopenmaps.openlayers.client.control.LayerSwitcher;
-import org.gwtopenmaps.openlayers.client.control.OverviewMap;
import org.gwtopenmaps.openlayers.client.control.ScaleLine;
import org.gwtopenmaps.openlayers.client.layer.OSM;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
-
-import com.google.gwt.event.logical.shared.AttachEvent;
-import org.gwtopenmaps.openlayers.client.layer.*;
+import com.smartgwt.client.widgets.WidgetCanvas;
+import com.smartgwt.client.widgets.events.DrawEvent;
+import com.smartgwt.client.widgets.events.DrawHandler;
+import com.smartgwt.client.widgets.events.ResizedEvent;
+import com.smartgwt.client.widgets.events.ResizedHandler;
+import com.smartgwt.client.widgets.layout.SectionStack;
+import com.smartgwt.client.widgets.layout.SectionStackSection;
/**
* Map panel widget
@@ -90,7 +77,7 @@ public class MapPanel extends SectionStack {
e.getParentElement().getStyle().setProperty("width", "100%");
e.getParentElement().getStyle().setProperty("height", "100%");
}
- }
+ }
});
// Map resize handler
diff --git a/src/main/java/org/traccar/web/client/Style.java b/src/main/java/org/traccar/web/client/Style.java
new file mode 100644
index 00000000..83673906
--- /dev/null
+++ b/src/main/java/org/traccar/web/client/Style.java
@@ -0,0 +1,13 @@
+package org.traccar.web.client;
+
+public class Style {
+
+ public static int getPadding() {
+ return 10;
+ }
+
+ public static int getCellPadding() {
+ return 5;
+ }
+
+}
diff --git a/src/main/java/org/traccar/web/client/Traccar.java b/src/main/java/org/traccar/web/client/Traccar.java
index fdd8dac4..ca7cb3f4 100644
--- a/src/main/java/org/traccar/web/client/Traccar.java
+++ b/src/main/java/org/traccar/web/client/Traccar.java
@@ -1,7 +1,11 @@
package org.traccar.web.client;
+import java.util.List;
+
import org.traccar.web.client.database.DatabaseService;
import org.traccar.web.client.database.DatabaseServiceAsync;
+import org.traccar.web.client.login.LoginDialog;
+import org.traccar.web.shared.model.Device;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
@@ -20,25 +24,70 @@ public class Traccar implements EntryPoint {
/**
* Entry point method
*/
+ @Override
public void onModuleLoad() {
- DatabaseServiceAsync databaseService = GWT.create(DatabaseService.class);
+ final DatabaseServiceAsync databaseService = GWT.create(DatabaseService.class);
+
+ final AsyncCallback<List<Device>> callback2 = new AsyncCallback<List<Device>>() {
+ @Override
+ public void onFailure(Throwable caught) {
+ SC.say("onFailure2: " + caught.toString());
+ }
+ @Override
+ public void onSuccess(List<Device> result) {
+ String s = "";
+ if (result!= null) {
+ for (Device d : result) {
+ s += d.getName() + " ";
+ }
+ }
+ SC.say("Devices: " + s);
+ }
+ };
- AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
+ final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
+ @Override
public void onFailure(Throwable caught) {
SC.say("onFailure: " + caught.toString());
}
+ @Override
public void onSuccess(Boolean result) {
- SC.say("onSuccess: " + result);
+ databaseService.getDevices(callback2);
}
};
- databaseService.authenticate("test", "test", callback);
+ final LoginDialog loginDialog = new LoginDialog();
+
+ loginDialog.setLoginHandler(new LoginDialog.LoginHandler() {
+ private boolean validate(String login, String password) {
+ if (login == null || login.isEmpty() || password == null || password.isEmpty()) {
+ SC.warn("Login and password fields must not be blank");
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void onLogin(String login, String password) {
+ if (validate(login, password)) {
+ loginDialog.destroy();
+ databaseService.authenticate(login, password, callback);
+ }
+ }
+
+ @Override
+ public void onRegister(String login, String password) {
+ if (validate(login, password)) {
+ loginDialog.destroy();
+ databaseService.register(login, password, callback);
+ }
+ }
+ });
- /*LoginDialog loginDialog = new LoginDialog();
loginDialog.draw();
- loginDialog.centerInPage();*/
+ loginDialog.centerInPage();
/*devicePanel = new DevicePanel();
devicePanel.setWidth("20%");
diff --git a/src/main/java/org/traccar/web/client/database/DatabaseService.java b/src/main/java/org/traccar/web/client/database/DatabaseService.java
index 6d08a5ca..0a41ce90 100644
--- a/src/main/java/org/traccar/web/client/database/DatabaseService.java
+++ b/src/main/java/org/traccar/web/client/database/DatabaseService.java
@@ -1,5 +1,9 @@
package org.traccar.web.client.database;
+import java.util.List;
+
+import org.traccar.web.shared.model.Device;
+
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@@ -8,4 +12,8 @@ public interface DatabaseService extends RemoteService {
boolean authenticate(String login, String password);
+ boolean register(String login, String password);
+
+ List<Device> getDevices();
+
}
diff --git a/src/main/java/org/traccar/web/client/database/DatabaseServiceAsync.java b/src/main/java/org/traccar/web/client/database/DatabaseServiceAsync.java
index 471edf00..6cf6fc48 100644
--- a/src/main/java/org/traccar/web/client/database/DatabaseServiceAsync.java
+++ b/src/main/java/org/traccar/web/client/database/DatabaseServiceAsync.java
@@ -1,10 +1,17 @@
package org.traccar.web.client.database;
+import java.util.List;
+
+import org.traccar.web.shared.model.Device;
+
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface DatabaseServiceAsync {
- void authenticate(String login, String password,
- AsyncCallback<Boolean> callback);
+ void authenticate(String login, String password, AsyncCallback<Boolean> callback);
+
+ void register(String login, String password, AsyncCallback<Boolean> callback);
+
+ void getDevices(AsyncCallback<List<Device>> callback);
}
diff --git a/src/main/java/org/traccar/web/client/login/LoginDialog.java b/src/main/java/org/traccar/web/client/login/LoginDialog.java
index 3ac01ccd..ba00cabc 100644
--- a/src/main/java/org/traccar/web/client/login/LoginDialog.java
+++ b/src/main/java/org/traccar/web/client/login/LoginDialog.java
@@ -1,50 +1,77 @@
package org.traccar.web.client.login;
+import org.traccar.web.client.Style;
+
+import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
+import com.smartgwt.client.widgets.events.ClickEvent;
+import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
-import com.smartgwt.client.widgets.form.fields.ButtonItem;
-import com.smartgwt.client.types.Alignment;
-import com.smartgwt.client.types.TitleOrientation;
-import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.form.fields.ToolbarItem;
-import com.smartgwt.client.widgets.IButton;
-import com.smartgwt.client.widgets.Button;
+import com.smartgwt.client.widgets.layout.Layout;
public class LoginDialog extends Window {
- private DynamicForm form;
-
- public LoginDialog() {
+ public LoginDialog() {
- // Window properties
+ // Window properties
setTitle("User Authentication");
setShowCloseButton(false);
setShowMinimizeButton(false);
setAutoSize(true);
// Login form
- form = new DynamicForm();
+ final DynamicForm form = new DynamicForm();
form.setHeight100();
form.setWidth100();
- TextItem loginEdit = new TextItem();
+ final TextItem loginEdit = new TextItem();
loginEdit.setTitle("Login");
- PasswordItem passwordEdit = new PasswordItem();
+ final PasswordItem passwordEdit = new PasswordItem();
passwordEdit.setTitle("Password");
- ToolbarItem toolbarItem = new ToolbarItem();
- toolbarItem.setButtons(new IButton("Login"), new IButton("Register"));
+ final ToolbarItem toolbarItem = new ToolbarItem();
+ toolbarItem.setButtons(
+ new IButton("Login", new ClickHandler() {
+ @Override
+ public void onClick(ClickEvent event) {
+ if (loginHandler != null) {
+ loginHandler.onLogin(
+ loginEdit.getValueAsString(), passwordEdit.getValueAsString());
+ }
+ }
+ }),
+ new IButton("Register", new ClickHandler() {
+ @Override
+ public void onClick(ClickEvent event) {
+ if (loginHandler != null) {
+ loginHandler.onRegister(
+ loginEdit.getValueAsString(), passwordEdit.getValueAsString());
+ }
+ }
+ }));
form.setFields(loginEdit, passwordEdit, toolbarItem);
- form.setCellPadding(5);
+ form.setCellPadding(Style.getCellPadding());
- Layout layout = new Layout();
- layout.setPadding(10);
+ final Layout layout = new Layout();
+ layout.setPadding(Style.getPadding());
layout.addMember(form);
addItem(layout);
}
+ public interface LoginHandler {
+ public void onLogin(String login, String password);
+ public void onRegister(String login, String password);
+ }
+
+ private LoginHandler loginHandler;
+
+ public void setLoginHandler(LoginHandler loginHandler) {
+ this.loginHandler = loginHandler;
+ }
+
}
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 89f72e6c..372e5ddc 100644
--- a/src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java
+++ b/src/main/java/org/traccar/web/server/database/DatabaseServiceImpl.java
@@ -1,34 +1,80 @@
package org.traccar.web.server.database;
+import java.util.LinkedList;
+import java.util.List;
+
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
+import javax.persistence.TypedQuery;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpSession;
import org.traccar.web.client.database.DatabaseService;
+import org.traccar.web.shared.model.Device;
import org.traccar.web.shared.model.User;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class DatabaseServiceImpl extends RemoteServiceServlet implements DatabaseService {
- public boolean authenticate(String login, String password) {
+ private static final String PERSISTENCE_UNIT = "traccar";
+ private static final String ATTRIBUTE_USER = "user";
- EntityManager entityManager = Persistence.createEntityManagerFactory("traccar").createEntityManager();
- entityManager.getTransaction().begin();
+ private EntityManager entityManager;
- User user = new User();
- user.setLogin("test");
- user.setPassword("1");
+ @Override
+ public void init() throws ServletException {
+ super.init();
+ entityManager = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT).createEntityManager();
+ }
- entityManager.persist(user);
- entityManager.getTransaction().commit();
+ private void setUser(User user) {
+ HttpSession session = getThreadLocalRequest().getSession();
+ session.setAttribute(ATTRIBUTE_USER, user);
+ }
+
+ private User getUser() {
+ HttpSession session = getThreadLocalRequest().getSession();
+ return (User) session.getAttribute(ATTRIBUTE_USER);
+ }
- //User foundUser = entityManager.find(User.class, user.getId());
- entityManager.close();
+ @Override
+ public boolean authenticate(String login, String password) {
+ 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 (login.equals("test") && password.equals("test")) {
+ if (!results.isEmpty() && password.equals(results.get(0).getPassword())) {
+ setUser(results.get(0));
return true;
}
return false;
}
+ @Override
+ public boolean register(String login, String password) {
+ User user = new User();
+ user.setLogin(login);
+ user.setPassword(password);
+ entityManager.getTransaction().begin();
+ entityManager.persist(user);
+ entityManager.getTransaction().commit();
+ setUser(user);
+ return true;
+ }
+
+ @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);
+ }
+ return devices;
+ }
+ return null;
+ }
+
}
diff --git a/src/main/java/org/traccar/web/shared/model/Device.java b/src/main/java/org/traccar/web/shared/model/Device.java
index 17bfae29..15d5cd93 100644
--- a/src/main/java/org/traccar/web/shared/model/Device.java
+++ b/src/main/java/org/traccar/web/shared/model/Device.java
@@ -1,5 +1,7 @@
package org.traccar.web.shared.model;
+import java.io.Serializable;
+
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@@ -7,12 +9,15 @@ import javax.persistence.Id;
import javax.persistence.Table;
@Entity
-@Table(name="devices")
-public class Device {
+@Table(name = "devices")
+public class Device implements Serializable {
+
+ public Device() {
+ }
@Id
@GeneratedValue
- long id;
+ private long id;
@Column(unique = true)
private String uniqueId;
diff --git a/src/main/java/org/traccar/web/shared/model/Position.java b/src/main/java/org/traccar/web/shared/model/Position.java
index 4ceb1714..17db3713 100644
--- a/src/main/java/org/traccar/web/shared/model/Position.java
+++ b/src/main/java/org/traccar/web/shared/model/Position.java
@@ -1,5 +1,6 @@
package org.traccar.web.shared.model;
+import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
@@ -12,11 +13,14 @@ import org.hibernate.annotations.Index;
@Entity
@Table(name = "positions")
-public class Position {
+public class Position implements Serializable {
+
+ public Position() {
+ }
@Id
@GeneratedValue
- long id;
+ private long id;
@ManyToOne
@Index(name = "positionsIndex")
diff --git a/src/main/java/org/traccar/web/shared/model/User.java b/src/main/java/org/traccar/web/shared/model/User.java
index b82dd8e7..61d6a364 100644
--- a/src/main/java/org/traccar/web/shared/model/User.java
+++ b/src/main/java/org/traccar/web/shared/model/User.java
@@ -1,5 +1,6 @@
package org.traccar.web.shared.model;
+import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
@@ -11,11 +12,14 @@ import javax.persistence.Table;
@Entity
@Table(name="users")
-public class User {
+public class User implements Serializable {
+
+ public User() {
+ }
@Id
@GeneratedValue
- long id;
+ private long id;
@Column(unique = true)
private String login;
@@ -41,4 +45,8 @@ public class User {
@ManyToMany
private List<Device> devices;
+ public List<Device> getDevices() {
+ return devices;
+ }
+
}