diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-12-16 00:28:26 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-16 00:28:26 +1300 |
commit | 835853422ce9f1c1fde15a6464c6f0f7eda4fed7 (patch) | |
tree | 049fa09050caec98242a0f642d478a533cee5898 /src/org/traccar/database | |
parent | f76c78a68f249ddb45a2bbae11dd95dd6db743b6 (diff) | |
parent | ef9148eea8dd404aba5707898f244a4b7bc26feb (diff) | |
download | trackermap-server-835853422ce9f1c1fde15a6464c6f0f7eda4fed7.tar.gz trackermap-server-835853422ce9f1c1fde15a6464c6f0f7eda4fed7.tar.bz2 trackermap-server-835853422ce9f1c1fde15a6464c6f0f7eda4fed7.zip |
Merge pull request #2686 from Abyss777/calendars
Calendars implementation
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r-- | src/org/traccar/database/CalendarManager.java | 112 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 43 | ||||
-rw-r--r-- | src/org/traccar/database/PermissionsManager.java | 6 | ||||
-rw-r--r-- | src/org/traccar/database/QueryBuilder.java | 30 |
4 files changed, 191 insertions, 0 deletions
diff --git a/src/org/traccar/database/CalendarManager.java b/src/org/traccar/database/CalendarManager.java new file mode 100644 index 000000000..3e95f6698 --- /dev/null +++ b/src/org/traccar/database/CalendarManager.java @@ -0,0 +1,112 @@ +/* + * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.database; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.traccar.helper.Log; +import org.traccar.model.Calendar; +import org.traccar.model.CalendarPermission; + +public class CalendarManager { + + private final DataManager dataManager; + + private final Map<Long, Calendar> calendars = new ConcurrentHashMap<>(); + private final Map<Long, Set<Long>> userCalendars = new ConcurrentHashMap<>(); + + public CalendarManager(DataManager dataManager) { + this.dataManager = dataManager; + refreshCalendars(); + } + + public final void refreshCalendars() { + if (dataManager != null) { + try { + calendars.clear(); + for (Calendar calendar : dataManager.getCalendars()) { + calendars.put(calendar.getId(), calendar); + } + } catch (SQLException error) { + Log.warning(error); + } + } + refreshUserCalendars(); + } + + private Set<Long> getUserCalendarIds(long userId) { + if (!userCalendars.containsKey(userId)) { + userCalendars.put(userId, new HashSet<Long>()); + } + return userCalendars.get(userId); + } + + public Collection<Calendar> getUserCalendars(long userId) { + ArrayList<Calendar> result = new ArrayList<>(); + for (long calendarId : getUserCalendarIds(userId)) { + result.add(calendars.get(calendarId)); + } + return result; + } + + public final void refreshUserCalendars() { + if (dataManager != null) { + try { + userCalendars.clear(); + for (CalendarPermission calendarsPermission : dataManager.getCalendarPermissions()) { + getUserCalendarIds(calendarsPermission.getUserId()).add(calendarsPermission.getCalendarId()); + } + } catch (SQLException error) { + Log.warning(error); + } + } + } + + public Calendar getCalendar(long calendarId) { + return calendars.get(calendarId); + } + + public final void addCalendar(Calendar calendar) throws SQLException { + dataManager.addCalendar(calendar); + calendars.put(calendar.getId(), calendar); + } + + public final void updateCalendar(Calendar calendar) throws SQLException { + dataManager.updateCalendar(calendar); + calendars.put(calendar.getId(), calendar); + } + + public final void removeCalendar(long calendarId) throws SQLException { + dataManager.removeCalendar(calendarId); + calendars.remove(calendarId); + refreshUserCalendars(); + } + + public Collection<Calendar> getAllCalendars() { + return calendars.values(); + } + + public boolean checkCalendar(long userId, long calendarId) { + return getUserCalendarIds(userId).contains(calendarId); + } +} diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 8be53ad7b..278109229 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -37,6 +37,8 @@ import liquibase.resource.ResourceAccessor; import org.traccar.Config; import org.traccar.helper.Log; import org.traccar.model.AttributeAlias; +import org.traccar.model.Calendar; +import org.traccar.model.CalendarPermission; import org.traccar.model.Device; import org.traccar.model.DevicePermission; import org.traccar.model.Event; @@ -484,4 +486,45 @@ public class DataManager { .executeUpdate()); } + public Collection<Calendar> getCalendars() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectCalendarsAll")) + .executeQuery(Calendar.class); + } + + public void addCalendar(Calendar calendar) throws SQLException { + calendar.setId(QueryBuilder.create(dataSource, getQuery("database.insertCalendar"), true) + .setObject(calendar) + .executeUpdate()); + } + + public void updateCalendar(Calendar calendar) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.updateCalendar")) + .setObject(calendar) + .executeUpdate(); + } + + public void removeCalendar(long calendarId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.deleteCalendar")) + .setLong("id", calendarId) + .executeUpdate(); + } + + public Collection<CalendarPermission> getCalendarPermissions() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectCalendarPermissions")) + .executeQuery(CalendarPermission.class); + } + + public void linkCalendar(long userId, long calendarId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.linkCalendar")) + .setLong("userId", userId) + .setLong("calendarId", calendarId) + .executeUpdate(); + } + + public void unlinkCalendar(long userId, long calendarId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.unlinkCalendar")) + .setLong("userId", userId) + .setLong("calendarId", calendarId) + .executeUpdate(); + } } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 078a5f935..6c0610655 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -207,6 +207,12 @@ public class PermissionsManager { } } + public void checkCalendar(long userId, long calendarId) throws SecurityException { + if (!Context.getCalendarManager().checkCalendar(userId, calendarId) && !isAdmin(userId)) { + throw new SecurityException("Calendar access denied"); + } + } + public Server getServer() { return server; } diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index 50d689a2a..201240f2f 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -240,6 +240,23 @@ public final class QueryBuilder { return this; } + public QueryBuilder setBlob(String name, byte[] value) throws SQLException { + for (int i : indexes(name)) { + try { + if (value == null) { + statement.setNull(i, Types.BLOB); + } else { + statement.setBytes(i, value); + } + } catch (SQLException error) { + statement.close(); + connection.close(); + throw error; + } + } + return this; + } + public QueryBuilder setObject(Object object) throws SQLException { Method[] methods = object.getClass().getMethods(); @@ -260,6 +277,8 @@ public final class QueryBuilder { setString(name, (String) method.invoke(object)); } else if (method.getReturnType().equals(Date.class)) { setDate(name, (Date) method.invoke(object)); + } else if (method.getReturnType().equals(byte[].class)) { + setBlob(name, (byte[]) method.invoke(object)); } else if (method.getReturnType().equals(Map.class)) { if (Context.getConfig().getBoolean("database.xml")) { setString(name, MiscFormatter.toXmlString((Map) method.invoke(object))); @@ -375,6 +394,17 @@ public final class QueryBuilder { } } }); + } else if (parameterType.equals(byte[].class)) { + processors.add(new ResultSetProcessor<T>() { + @Override + public void process(T object, ResultSet resultSet) throws SQLException { + try { + method.invoke(object, resultSet.getBytes(name)); + } catch (IllegalAccessException | InvocationTargetException error) { + Log.warning(error); + } + } + }); } } |