aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/web/server
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2013-02-19 22:09:27 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2013-02-19 22:09:27 +1300
commit0e766c9024e2457320f64353e29bb5e2d8294b28 (patch)
tree35965494f8f52fefa994c397198d18854bd51a43 /src/org/traccar/web/server
parent9f80fe9b90d9ddb1132c139072cfe3bac5a42491 (diff)
downloadtrackermap-web-0e766c9024e2457320f64353e29bb5e2d8294b28.tar.gz
trackermap-web-0e766c9024e2457320f64353e29bb5e2d8294b28.tar.bz2
trackermap-web-0e766c9024e2457320f64353e29bb5e2d8294b28.zip
Add application settings
Diffstat (limited to 'src/org/traccar/web/server')
-rw-r--r--src/org/traccar/web/server/model/DataServiceImpl.java76
1 files changed, 69 insertions, 7 deletions
diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java
index 3df7b686..28c61678 100644
--- a/src/org/traccar/web/server/model/DataServiceImpl.java
+++ b/src/org/traccar/web/server/model/DataServiceImpl.java
@@ -30,6 +30,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import org.traccar.web.client.model.DataService;
+import org.traccar.web.shared.model.ApplicationSettings;
import org.traccar.web.shared.model.Device;
import org.traccar.web.shared.model.Position;
import org.traccar.web.shared.model.User;
@@ -43,7 +44,8 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
private static final String PERSISTENCE_DATASTORE = "java:/DefaultDS";
private static final String PERSISTENCE_UNIT_DEBUG = "debug";
private static final String PERSISTENCE_UNIT_RELEASE = "release";
- private static final String ATTRIBUTE_USER = "user";
+ private static final String ATTRIBUTE_USER = "traccar.user";
+ private static final String ATTRIBUTE_SETTINGS = "traccar.settings";
private EntityManagerFactory entityManagerFactory;
@@ -131,12 +133,16 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
@Override
public boolean register(String login, String password) {
- User user = new User();
- user.setLogin(login);
- user.setPassword(password);
- createUser(user);
- setUser(user);
- return true;
+ if (getApplicationSettings().getRegistrationEnabled()) {
+ User user = new User();
+ user.setLogin(login);
+ user.setPassword(password);
+ createUser(user);
+ setUser(user);
+ return true;
+ } else {
+ throw new SecurityException();
+ }
}
private void createUser(User user) {
@@ -260,4 +266,60 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
}
}
+ private ApplicationSettings getApplicationSettings() {
+ ApplicationSettings applicationSettings = (ApplicationSettings) getServletContext().getAttribute(ATTRIBUTE_SETTINGS);
+ if (applicationSettings == null) {
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
+ try {
+ TypedQuery<ApplicationSettings> query = entityManager.createQuery("SELECT x FROM ApplicationSettings x", ApplicationSettings.class);
+ List<ApplicationSettings> resultList = query.getResultList();
+ if (resultList == null || resultList.isEmpty()) {
+ applicationSettings = new ApplicationSettings();
+ entityManager.getTransaction().begin();
+ try {
+ entityManager.persist(applicationSettings);
+ entityManager.getTransaction().commit();
+ } catch (RuntimeException e) {
+ entityManager.getTransaction().rollback();
+ throw e;
+ }
+ } else {
+ applicationSettings = resultList.get(0);
+ }
+ getServletContext().setAttribute(ATTRIBUTE_SETTINGS, applicationSettings);
+ } finally {
+ entityManager.close();
+ }
+ }
+ return applicationSettings;
+ }
+
+ @Override
+ public ApplicationSettings updateApplicationSettings(ApplicationSettings applicationSettings) {
+ if (applicationSettings == null) {
+ return getApplicationSettings();
+ } else {
+ EntityManager entityManager = entityManagerFactory.createEntityManager();
+ try {
+ User user = getUser();
+ if (user.getAdmin()) {
+ entityManager.getTransaction().begin();
+ try {
+ entityManager.merge(applicationSettings);
+ entityManager.getTransaction().commit();
+ getServletContext().setAttribute(ATTRIBUTE_SETTINGS, applicationSettings);
+ return applicationSettings;
+ } catch (RuntimeException e) {
+ entityManager.getTransaction().rollback();
+ throw e;
+ }
+ } else {
+ throw new SecurityException();
+ }
+ } finally {
+ entityManager.close();
+ }
+ }
+ }
+
}