diff options
69 files changed, 785 insertions, 2591 deletions
diff --git a/setup/default.xml b/setup/default.xml index 04530104a..f0f9ef4a3 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -60,7 +60,7 @@ WHERE email = :email </entry> - <entry key='database.selectUsersAll'> + <entry key='database.selectUsers'> SELECT * FROM users </entry> @@ -103,15 +103,15 @@ DELETE FROM users WHERE id = :id </entry> - <entry key='database.selectDevicePermissions'> + <entry key='database.selectUserDevices'> SELECT userId, deviceId FROM user_device </entry> - <entry key='database.selectGroupPermissions'> + <entry key='database.selectUserGroups'> SELECT userId, groupId FROM user_group </entry> - <entry key='database.selectDevicesAll'> + <entry key='database.selectDevices'> SELECT * FROM devices </entry> @@ -141,15 +141,15 @@ DELETE FROM devices WHERE id = :id </entry> - <entry key='database.linkDevice'> + <entry key='database.linkUserDevice'> INSERT INTO user_device (userId, deviceId) VALUES (:userId, :deviceId) </entry> - <entry key='database.unlinkDevice'> + <entry key='database.unlinkUserDevice'> DELETE FROM user_device WHERE userId = :userId AND deviceId = :deviceId </entry> - <entry key='database.selectGroupsAll'> + <entry key='database.selectGroups'> SELECT * FROM groups </entry> @@ -165,11 +165,11 @@ DELETE FROM groups WHERE id = :id </entry> - <entry key='database.linkGroup'> + <entry key='database.linkUserGroup'> INSERT INTO user_group (userId, groupId) VALUES (:userId, :groupId) </entry> - <entry key='database.unlinkGroup'> + <entry key='database.unlinkUserGroup'> DELETE FROM user_group WHERE userId = :userId AND groupId = :groupId </entry> @@ -207,7 +207,7 @@ SELECT * FROM events WHERE deviceId = :deviceId AND serverTime BETWEEN :from AND :to ORDER BY serverTime </entry> - <entry key='database.selectGeofencesAll'> + <entry key='database.selectGeofences'> SELECT * FROM geofences </entry> @@ -230,15 +230,15 @@ DELETE FROM geofences WHERE id = :id </entry> - <entry key='database.selectGeofencePermissions'> + <entry key='database.selectUserGeofences'> SELECT userId, geofenceId FROM user_geofence </entry> - <entry key='database.linkGeofence'> + <entry key='database.linkUserGeofence'> INSERT INTO user_geofence (userId, geofenceId) VALUES (:userId, :geofenceId) </entry> - <entry key='database.unlinkGeofence'> + <entry key='database.unlinkUserGeofence'> DELETE FROM user_geofence WHERE userId = :userId AND geofenceId = :geofenceId </entry> @@ -328,7 +328,7 @@ VALUES (:captureTime, :activeUsers, :activeDevices, :requests, :messagesReceived, :messagesStored, :mailSent, :smsSent, :geocoderRequests, :geolocationRequests, :attributes) </entry> - <entry key='database.selectCalendarsAll'> + <entry key='database.selectCalendars'> SELECT * FROM calendars </entry> @@ -349,27 +349,27 @@ DELETE FROM calendars WHERE id = :id </entry> - <entry key='database.selectCalendarPermissions'> + <entry key='database.selectUserCalendars'> SELECT userId, calendarId FROM user_calendar </entry> - <entry key='database.linkCalendar'> + <entry key='database.linkUserCalendar'> INSERT INTO user_calendar (userId, calendarId) VALUES (:userId, :calendarId) </entry> - <entry key='database.unlinkCalendar'> + <entry key='database.unlinkUserCalendar'> DELETE FROM user_calendar WHERE userId = :userId AND calendarId = :calendarId </entry> - <entry key='database.selectUserPermissions'> + <entry key='database.selectUserUsers'> SELECT userId, managedUserId FROM user_user </entry> - <entry key='database.linkUser'> + <entry key='database.linkUserManagedUser'> INSERT INTO user_user (userId, managedUserId) VALUES (:userId, :managedUserId) </entry> - <entry key='database.unlinkUser'> + <entry key='database.unlinkUserManagedUser'> DELETE FROM user_user WHERE userId = :userId AND managedUserId = :managedUserId </entry> @@ -395,15 +395,15 @@ DELETE FROM attributes WHERE id = :id </entry> - <entry key='database.selectAttributePermissions'> + <entry key='database.selectUserAttributes'> SELECT userId, attributeId FROM user_attribute </entry> - <entry key='database.linkAttribute'> + <entry key='database.linkUserAttribute'> INSERT INTO user_attribute (userId, attributeId) VALUES (:userId, :attributeId) </entry> - <entry key='database.unlinkAttribute'> + <entry key='database.unlinkUserAttribute'> DELETE FROM user_attribute WHERE userId = :userId AND attributeId = :attributeId </entry> @@ -452,15 +452,15 @@ DELETE FROM drivers WHERE id = :id </entry> - <entry key='database.selectDriverPermissions'> + <entry key='database.selectUserDrivers'> SELECT userId, driverId FROM user_driver </entry> - <entry key='database.linkDriver'> + <entry key='database.linkUserDriver'> INSERT INTO user_driver (userId, driverId) VALUES (:userId, :driverId) </entry> - <entry key='database.unlinkDriver'> + <entry key='database.unlinkUserDriver'> DELETE FROM user_driver WHERE userId = :userId AND driverId = :driverId </entry> diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index cde858671..436ef9b02 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -52,9 +52,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { if (defaultGroupId != 0) { Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); } return device.getId(); diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java index 44ef33c53..cc272df9c 100644 --- a/src/org/traccar/api/BaseResource.java +++ b/src/org/traccar/api/BaseResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/org/traccar/api/resource/AttributePermissionResource.java b/src/org/traccar/api/resource/AttributePermissionResource.java deleted file mode 100644 index 1924bcdf1..000000000 --- a/src/org/traccar/api/resource/AttributePermissionResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.AttributePermission; - -@Path("permissions/attributes") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class AttributePermissionResource extends BaseResource { - - @POST - public Response add(AttributePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); - Context.getDataManager().linkAttribute(entity.getUserId(), entity.getAttributeId()); - Context.getAttributesManager().refreshUserAttributes(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(AttributePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); - Context.getDataManager().unlinkAttribute(entity.getUserId(), entity.getAttributeId()); - Context.getAttributesManager().refreshUserAttributes(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java index 4d326779b..7b81d3ffb 100644 --- a/src/org/traccar/api/resource/AttributeResource.java +++ b/src/org/traccar/api/resource/AttributeResource.java @@ -38,6 +38,7 @@ import org.traccar.api.BaseResource; import org.traccar.database.AttributesManager; import org.traccar.model.Attribute; import org.traccar.model.Position; +import org.traccar.model.User; import org.traccar.processing.ComputedAttributesHandler; @Path("attributes/computed") @@ -52,42 +53,43 @@ public class AttributeResource extends BaseResource { AttributesManager attributesManager = Context.getAttributesManager(); if (refresh) { - attributesManager.refreshAttributes(); + attributesManager.refreshItems(); } Set<Long> result = new HashSet<>(); if (all) { if (Context.getPermissionsManager().isAdmin(getUserId())) { - result.addAll(attributesManager.getAllAttributes()); + result.addAll(attributesManager.getAllItems()); } else { Context.getPermissionsManager().checkManager(getUserId()); - result.addAll(attributesManager.getManagedAttributes(getUserId())); + result.addAll(attributesManager.getManagedItems(getUserId())); } } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - result.addAll(attributesManager.getUserAttributes(userId)); + result.addAll(attributesManager.getUserItems(userId)); } if (groupId != 0) { Context.getPermissionsManager().checkGroup(getUserId(), groupId); - result.retainAll(attributesManager.getGroupAttributes(groupId)); + result.retainAll(attributesManager.getGroupItems(groupId)); } if (deviceId != 0) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - result.retainAll(attributesManager.getDeviceAttributes(deviceId)); + result.retainAll(attributesManager.getDeviceItems(deviceId)); } - return attributesManager.getAttributes(result); + return attributesManager.getItems(Attribute.class, result); } private Response add(Attribute entity) throws SQLException { - Context.getAttributesManager().addAttribute(entity); - Context.getDataManager().linkAttribute(getUserId(), entity.getId()); - Context.getAttributesManager().refreshUserAttributes(); + Context.getPermissionsManager().checkReadonly(getUserId()); + Context.getAttributesManager().addItem(entity); + Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); + Context.getAttributesManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -127,8 +129,8 @@ public class AttributeResource extends BaseResource { @PUT public Response update(Attribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getId()); - Context.getAttributesManager().updateAttribute(entity); + Context.getPermissionsManager().checkPermission(Attribute.class, getUserId(), entity.getId()); + Context.getAttributesManager().updateItem(entity); return Response.ok(entity).build(); } @@ -136,8 +138,8 @@ public class AttributeResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkAttribute(getUserId(), id); - Context.getAttributesManager().removeAttribute(id); + Context.getPermissionsManager().checkPermission(Attribute.class, getUserId(), id); + Context.getAttributesManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/CalendarPermissionResource.java b/src/org/traccar/api/resource/CalendarPermissionResource.java deleted file mode 100644 index a49254b6b..000000000 --- a/src/org/traccar/api/resource/CalendarPermissionResource.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.CalendarPermission; - -@Path("permissions/calendars") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class CalendarPermissionResource extends BaseResource { - - @POST - public Response add(CalendarPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkCalendar(getUserId(), entity.getCalendarId()); - Context.getDataManager().linkCalendar(entity.getUserId(), entity.getCalendarId()); - Context.getCalendarManager().refreshUserCalendars(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(CalendarPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkCalendar(getUserId(), entity.getCalendarId()); - Context.getDataManager().unlinkCalendar(entity.getUserId(), entity.getCalendarId()); - Context.getCalendarManager().refreshUserCalendars(); - return Response.noContent().build(); - } -} diff --git a/src/org/traccar/api/resource/CalendarResource.java b/src/org/traccar/api/resource/CalendarResource.java index 641d3b4b5..a0f8656af 100644 --- a/src/org/traccar/api/resource/CalendarResource.java +++ b/src/org/traccar/api/resource/CalendarResource.java @@ -33,7 +33,9 @@ import javax.ws.rs.core.Response; import org.traccar.Context; import org.traccar.api.BaseResource; +import org.traccar.database.CalendarManager; import org.traccar.model.Calendar; +import org.traccar.model.User; @Path("calendars") @Produces(MediaType.APPLICATION_JSON) @@ -44,28 +46,29 @@ public class CalendarResource extends BaseResource { public Collection<Calendar> get( @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException { + CalendarManager calendarManager = Context.getCalendarManager(); if (all) { if (Context.getPermissionsManager().isAdmin(getUserId())) { - return Context.getCalendarManager().getAllCalendars(); + return calendarManager.getItems(Calendar.class, calendarManager.getAllItems()); } else { Context.getPermissionsManager().checkManager(getUserId()); - return Context.getCalendarManager().getManagedCalendars(getUserId()); + return calendarManager.getItems(Calendar.class, calendarManager.getManagedItems(getUserId())); } } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - return Context.getCalendarManager().getUserCalendars(userId); + return calendarManager.getItems(Calendar.class, calendarManager.getUserItems(userId)); } } @POST public Response add(Calendar entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getCalendarManager().addCalendar(entity); - Context.getDataManager().linkCalendar(getUserId(), entity.getId()); - Context.getCalendarManager().refreshUserCalendars(); + Context.getCalendarManager().addItem(entity); + Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); + Context.getCalendarManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -73,8 +76,8 @@ public class CalendarResource extends BaseResource { @PUT public Response update(Calendar entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkCalendar(getUserId(), entity.getId()); - Context.getCalendarManager().updateCalendar(entity); + Context.getPermissionsManager().checkPermission(Calendar.class, getUserId(), entity.getId()); + Context.getCalendarManager().updateItem(entity); return Response.ok(entity).build(); } @@ -82,8 +85,8 @@ public class CalendarResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkCalendar(getUserId(), id); - Context.getCalendarManager().removeCalendar(id); + Context.getPermissionsManager().checkPermission(Calendar.class, getUserId(), id); + Context.getCalendarManager().removeItem(id); return Response.noContent().build(); } } diff --git a/src/org/traccar/api/resource/DeviceAttributeResource.java b/src/org/traccar/api/resource/DeviceAttributeResource.java deleted file mode 100644 index 82d17bcc6..000000000 --- a/src/org/traccar/api/resource/DeviceAttributeResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.DeviceAttribute; - -@Path("devices/attributes") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DeviceAttributeResource extends BaseResource { - - @POST - public Response add(DeviceAttribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); - Context.getDataManager().linkDeviceAttribute(entity.getDeviceId(), entity.getAttributeId()); - Context.getAttributesManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DeviceAttribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); - Context.getDataManager().unlinkDeviceAttribute(entity.getDeviceId(), entity.getAttributeId()); - Context.getAttributesManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DeviceDriverResource.java b/src/org/traccar/api/resource/DeviceDriverResource.java deleted file mode 100644 index 341c50909..000000000 --- a/src/org/traccar/api/resource/DeviceDriverResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.DeviceDriver; - -@Path("devices/drivers") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DeviceDriverResource extends BaseResource { - - @POST - public Response add(DeviceDriver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); - Context.getDataManager().linkDeviceDriver(entity.getDeviceId(), entity.getDriverId()); - Context.getDriversManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DeviceDriver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); - Context.getDataManager().unlinkDeviceDriver(entity.getDeviceId(), entity.getDriverId()); - Context.getDriversManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DeviceGeofenceResource.java b/src/org/traccar/api/resource/DeviceGeofenceResource.java deleted file mode 100644 index 6254fe3cf..000000000 --- a/src/org/traccar/api/resource/DeviceGeofenceResource.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@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.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.DeviceGeofence; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import java.sql.SQLException; - -@Path("devices/geofences") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DeviceGeofenceResource extends BaseResource { - - @POST - public Response add(DeviceGeofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); - Context.getDataManager().linkDeviceGeofence(entity.getDeviceId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DeviceGeofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); - Context.getDataManager().unlinkDeviceGeofence(entity.getDeviceId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DevicePermissionResource.java b/src/org/traccar/api/resource/DevicePermissionResource.java deleted file mode 100644 index 6e00dc47f..000000000 --- a/src/org/traccar/api/resource/DevicePermissionResource.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2015 - 2017 Anton Tananaev (anton@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.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.DevicePermission; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.sql.SQLException; - -@Path("permissions/devices") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DevicePermissionResource extends BaseResource { - - @POST - public Response add(DevicePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getDataManager().linkDevice(entity.getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DevicePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - if (getUserId() != entity.getUserId()) { - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - } else { - Context.getPermissionsManager().checkAdmin(getUserId()); - } - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getDataManager().unlinkDevice(entity.getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 2957d623c..6cf7a6219 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -19,6 +19,7 @@ import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.Device; import org.traccar.model.DeviceTotalDistance; +import org.traccar.model.User; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -83,11 +84,9 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDeviceReadonly(getUserId()); Context.getPermissionsManager().checkDeviceLimit(getUserId()); Context.getDeviceManager().addDevice(entity); - Context.getDataManager().linkDevice(getUserId(), entity.getId()); + Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); } @@ -99,9 +98,7 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDevice(getUserId(), entity.getId()); Context.getDeviceManager().updateDevice(entity); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); } @@ -113,9 +110,7 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDevice(getUserId(), id); Context.getDeviceManager().removeDevice(id); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); Context.getAliasesManager().removeDevice(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/DriverPermissionResource.java b/src/org/traccar/api/resource/DriverPermissionResource.java deleted file mode 100644 index fd1ca7c6d..000000000 --- a/src/org/traccar/api/resource/DriverPermissionResource.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; - -import org.traccar.model.DriverPermission; - -@Path("permissions/drivers") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DriverPermissionResource extends BaseResource { - - @POST - public Response add(DriverPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); - Context.getDataManager().linkDriver(entity.getUserId(), entity.getDriverId()); - Context.getDriversManager().refreshUserDrivers(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DriverPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); - Context.getDataManager().unlinkDriver(entity.getUserId(), entity.getDriverId()); - Context.getDriversManager().refreshUserDrivers(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DriverResource.java b/src/org/traccar/api/resource/DriverResource.java index 7fe0af473..72eadb711 100644 --- a/src/org/traccar/api/resource/DriverResource.java +++ b/src/org/traccar/api/resource/DriverResource.java @@ -37,6 +37,7 @@ import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.database.DriversManager; import org.traccar.model.Driver; +import org.traccar.model.User; @Path("drivers") @Produces(MediaType.APPLICATION_JSON) @@ -50,44 +51,44 @@ public class DriverResource extends BaseResource { DriversManager driversManager = Context.getDriversManager(); if (refresh) { - driversManager.refreshDrivers(); + driversManager.refreshItems(); } Set<Long> result = new HashSet<>(); if (all) { if (Context.getPermissionsManager().isAdmin(getUserId())) { - result.addAll(driversManager.getAllDrivers()); + result.addAll(driversManager.getAllItems()); } else { Context.getPermissionsManager().checkManager(getUserId()); - result.addAll(driversManager.getManagedDrivers(getUserId())); + result.addAll(driversManager.getManagedItems(getUserId())); } } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - result.addAll(driversManager.getUserDrivers(userId)); + result.addAll(driversManager.getUserItems(userId)); } if (groupId != 0) { Context.getPermissionsManager().checkGroup(getUserId(), groupId); - result.retainAll(driversManager.getGroupDrivers(groupId)); + result.retainAll(driversManager.getGroupItems(groupId)); } if (deviceId != 0) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - result.retainAll(driversManager.getDeviceDrivers(deviceId)); + result.retainAll(driversManager.getDeviceItems(deviceId)); } - return driversManager.getDrivers(result); + return driversManager.getItems(Driver.class, result); } @POST public Response add(Driver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getDriversManager().addDriver(entity); - Context.getDataManager().linkDriver(getUserId(), entity.getId()); - Context.getDriversManager().refreshUserDrivers(); + Context.getDriversManager().addItem(entity); + Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); + Context.getDriversManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -95,8 +96,8 @@ public class DriverResource extends BaseResource { @PUT public Response update(Driver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getId()); - Context.getDriversManager().updateDriver(entity); + Context.getPermissionsManager().checkPermission(Driver.class, getUserId(), entity.getId()); + Context.getDriversManager().updateItem(entity); return Response.ok(entity).build(); } @@ -104,8 +105,8 @@ public class DriverResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDriver(getUserId(), id); - Context.getDriversManager().removeDriver(id); + Context.getPermissionsManager().checkPermission(Driver.class, getUserId(), id); + Context.getDriversManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java index 0ef5456af..b7fda6f73 100644 --- a/src/org/traccar/api/resource/EventResource.java +++ b/src/org/traccar/api/resource/EventResource.java @@ -12,6 +12,7 @@ import javax.ws.rs.core.MediaType; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.Event; +import org.traccar.model.Geofence; @Path("events") @Produces(MediaType.APPLICATION_JSON) @@ -25,7 +26,7 @@ public class EventResource extends BaseResource { Event event = Context.getDataManager().getEvent(id); Context.getPermissionsManager().checkDevice(getUserId(), event.getDeviceId()); if (event.getGeofenceId() != 0) { - Context.getPermissionsManager().checkGeofence(getUserId(), event.getGeofenceId()); + Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), event.getGeofenceId()); } return event; } diff --git a/src/org/traccar/api/resource/GeofencePermissionResource.java b/src/org/traccar/api/resource/GeofencePermissionResource.java deleted file mode 100644 index 8faa63d85..000000000 --- a/src/org/traccar/api/resource/GeofencePermissionResource.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@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.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GeofencePermission; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.sql.SQLException; - -@Path("permissions/geofences") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GeofencePermissionResource extends BaseResource { - - @POST - public Response add(GeofencePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); - Context.getDataManager().linkGeofence(entity.getUserId(), entity.getGeofenceId()); - Context.getGeofenceManager().refreshUserGeofences(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GeofencePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); - Context.getDataManager().unlinkGeofence(entity.getUserId(), entity.getGeofenceId()); - Context.getGeofenceManager().refreshUserGeofences(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java index d5acf106a..47f889e9c 100644 --- a/src/org/traccar/api/resource/GeofenceResource.java +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -19,6 +19,7 @@ import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.database.GeofenceManager; import org.traccar.model.Geofence; +import org.traccar.model.User; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -49,44 +50,44 @@ public class GeofenceResource extends BaseResource { GeofenceManager geofenceManager = Context.getGeofenceManager(); if (refresh) { - geofenceManager.refreshGeofences(); + geofenceManager.refreshItems(); } Set<Long> result = new HashSet<>(); if (all) { if (Context.getPermissionsManager().isAdmin(getUserId())) { - result.addAll(geofenceManager.getAllGeofencesIds()); + result.addAll(geofenceManager.getAllItems()); } else { Context.getPermissionsManager().checkManager(getUserId()); - result.addAll(geofenceManager.getManagedGeofencesIds(getUserId())); + result.addAll(geofenceManager.getManagedItems(getUserId())); } } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - result.addAll(geofenceManager.getUserGeofencesIds(userId)); + result.addAll(geofenceManager.getUserItems(userId)); } if (groupId != 0) { Context.getPermissionsManager().checkGroup(getUserId(), groupId); - result.retainAll(geofenceManager.getGroupGeofencesIds(groupId)); + result.retainAll(geofenceManager.getGroupItems(groupId)); } if (deviceId != 0) { Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - result.retainAll(geofenceManager.getDeviceGeofencesIds(deviceId)); + result.retainAll(geofenceManager.getDeviceItems(deviceId)); } - return geofenceManager.getGeofences(result); + return geofenceManager.getItems(Geofence.class, result); } @POST public Response add(Geofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getDataManager().addGeofence(entity); - Context.getDataManager().linkGeofence(getUserId(), entity.getId()); - Context.getGeofenceManager().refreshGeofences(); + Context.getGeofenceManager().addItem(entity); + Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); + Context.getGeofenceManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -94,8 +95,8 @@ public class GeofenceResource extends BaseResource { @PUT public Response update(Geofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getId()); - Context.getGeofenceManager().updateGeofence(entity); + Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), entity.getId()); + Context.getGeofenceManager().updateItem(entity); return Response.ok(entity).build(); } @@ -103,9 +104,8 @@ public class GeofenceResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGeofence(getUserId(), id); - Context.getDataManager().removeGeofence(id); - Context.getGeofenceManager().refreshGeofences(); + Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), id); + Context.getGeofenceManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GroupAttributeResource.java b/src/org/traccar/api/resource/GroupAttributeResource.java deleted file mode 100644 index d8b8b58c9..000000000 --- a/src/org/traccar/api/resource/GroupAttributeResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GroupAttribute; - -@Path("groups/attributes") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GroupAttributeResource extends BaseResource { - - @POST - public Response add(GroupAttribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); - Context.getDataManager().linkGroupAttribute(entity.getGroupId(), entity.getAttributeId()); - Context.getAttributesManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GroupAttribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkAttribute(getUserId(), entity.getAttributeId()); - Context.getDataManager().unlinkGroupAttribute(entity.getGroupId(), entity.getAttributeId()); - Context.getAttributesManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GroupDriverResource.java b/src/org/traccar/api/resource/GroupDriverResource.java deleted file mode 100644 index 76fc2892c..000000000 --- a/src/org/traccar/api/resource/GroupDriverResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GroupDriver; - -@Path("groups/drivers") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GroupDriverResource extends BaseResource { - - @POST - public Response add(GroupDriver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); - Context.getDataManager().linkGroupDriver(entity.getGroupId(), entity.getDriverId()); - Context.getDriversManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GroupDriver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkDriver(getUserId(), entity.getDriverId()); - Context.getDataManager().unlinkGroupDriver(entity.getGroupId(), entity.getDriverId()); - Context.getDriversManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GroupGeofenceResource.java b/src/org/traccar/api/resource/GroupGeofenceResource.java deleted file mode 100644 index 81fd4e45f..000000000 --- a/src/org/traccar/api/resource/GroupGeofenceResource.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@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.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GroupGeofence; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.sql.SQLException; - -@Path("groups/geofences") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GroupGeofenceResource extends BaseResource { - - @POST - public Response add(GroupGeofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); - Context.getDataManager().linkGroupGeofence(entity.getGroupId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GroupGeofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkGeofence(getUserId(), entity.getGeofenceId()); - Context.getDataManager().unlinkGroupGeofence(entity.getGroupId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GroupPermissionResource.java b/src/org/traccar/api/resource/GroupPermissionResource.java deleted file mode 100644 index 61a725222..000000000 --- a/src/org/traccar/api/resource/GroupPermissionResource.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2016 - 2017 Anton Tananaev (anton@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.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GroupPermission; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.sql.SQLException; - -@Path("permissions/groups") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GroupPermissionResource extends BaseResource { - - @POST - public Response add(GroupPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getDataManager().linkGroup(entity.getUserId(), entity.getGroupId()); - Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GroupPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getDataManager().unlinkGroup(entity.getUserId(), entity.getGroupId()); - Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index ceba69105..f20c95327 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -18,6 +18,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.Group; +import org.traccar.model.User; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -61,11 +62,9 @@ public class GroupResource extends BaseResource { public Response add(Group entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getDeviceManager().addGroup(entity); - Context.getDataManager().linkGroup(getUserId(), entity.getId()); + Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); } @@ -75,9 +74,7 @@ public class GroupResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), entity.getId()); Context.getDeviceManager().updateGroup(entity); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); } @@ -88,9 +85,7 @@ public class GroupResource extends BaseResource { Context.getPermissionsManager().checkGroup(getUserId(), id); Context.getDeviceManager().removeGroup(id); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/PermissionsResource.java b/src/org/traccar/api/resource/PermissionsResource.java new file mode 100644 index 000000000..9b9f65ad1 --- /dev/null +++ b/src/org/traccar/api/resource/PermissionsResource.java @@ -0,0 +1,79 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.api.resource; + +import java.sql.SQLException; +import java.util.LinkedHashMap; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.traccar.Context; +import org.traccar.api.BaseResource; +import org.traccar.model.Device; +import org.traccar.model.Permission; +import org.traccar.model.User; + +@Path("permissions") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class PermissionsResource extends BaseResource { + + private void checkPermission(Permission permission, boolean link) { + if (!link && permission.getOwnerClass().equals(User.class) + && permission.getPropertyClass().equals(Device.class)) { + if (getUserId() != permission.getOwnerId()) { + Context.getPermissionsManager().checkUser(getUserId(), permission.getOwnerId()); + } else { + Context.getPermissionsManager().checkAdmin(getUserId()); + } + } else { + Context.getPermissionsManager().checkPermission( + permission.getOwnerClass(), getUserId(), permission.getOwnerId()); + } + Context.getPermissionsManager().checkPermission( + permission.getPropertyClass(), getUserId(), permission.getPropertyId()); + } + + @POST + public Response add(LinkedHashMap<String, Long> entity) throws SQLException, ClassNotFoundException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Permission permission = new Permission(entity); + checkPermission(permission, true); + Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), + permission.getPropertyClass(), permission.getPropertyId(), true); + Context.getPermissionsManager().refreshPermissions(permission); + return Response.noContent().build(); + } + + @DELETE + public Response remove(LinkedHashMap<String, Long> entity) throws SQLException, ClassNotFoundException { + Context.getPermissionsManager().checkReadonly(getUserId()); + Permission permission = new Permission(entity); + checkPermission(permission, false); + Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(), + permission.getPropertyClass(), permission.getPropertyId(), false); + Context.getPermissionsManager().refreshPermissions(permission); + return Response.noContent().build(); + } + +} diff --git a/src/org/traccar/api/resource/UserPermissionResource.java b/src/org/traccar/api/resource/UserPermissionResource.java deleted file mode 100644 index a97c4a665..000000000 --- a/src/org/traccar/api/resource/UserPermissionResource.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.UserPermission; - -@Path("permissions/users") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class UserPermissionResource extends BaseResource { - - @POST - public Response add(UserPermission entity) throws SQLException { - Context.getPermissionsManager().checkAdmin(getUserId()); - if (entity.getUserId() != entity.getManagedUserId()) { - Context.getDataManager().linkUser(entity.getUserId(), entity.getManagedUserId()); - Context.getPermissionsManager().refreshUserPermissions(); - } - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(UserPermission entity) throws SQLException { - Context.getPermissionsManager().checkAdmin(getUserId()); - Context.getDataManager().unlinkUser(entity.getUserId(), entity.getManagedUserId()); - Context.getPermissionsManager().refreshUserPermissions(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index 4d8a8b3a4..98395e3cc 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -17,6 +17,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; +import org.traccar.model.ManagedUser; import org.traccar.model.User; import javax.annotation.security.PermitAll; @@ -74,7 +75,7 @@ public class UserResource extends BaseResource { } Context.getPermissionsManager().addUser(entity); if (Context.getPermissionsManager().isManager(getUserId())) { - Context.getDataManager().linkUser(getUserId(), entity.getId()); + Context.getDataManager().linkObject(User.class, getUserId(), ManagedUser.class, entity.getId(), true); } Context.getPermissionsManager().refreshUserPermissions(); if (Context.getNotificationManager() != null) { @@ -104,7 +105,7 @@ public class UserResource extends BaseResource { Context.getPermissionsManager().checkUser(getUserId(), id); Context.getPermissionsManager().removeUser(id); if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refreshUserGeofences(); + Context.getGeofenceManager().refreshUserItems(); } if (Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); diff --git a/src/org/traccar/database/AttributesManager.java b/src/org/traccar/database/AttributesManager.java index 362d6130f..266fc5526 100644 --- a/src/org/traccar/database/AttributesManager.java +++ b/src/org/traccar/database/AttributesManager.java @@ -17,183 +17,27 @@ package org.traccar.database; import java.sql.SQLException; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import org.traccar.Context; -import org.traccar.helper.Log; -import org.traccar.model.AttributePermission; import org.traccar.model.Attribute; -import org.traccar.model.Device; -import org.traccar.model.DeviceAttribute; -import org.traccar.model.GroupAttribute; +import org.traccar.model.BaseModel; -public class AttributesManager { - - private final DataManager dataManager; - - private final Map<Long, Attribute> attributes = new ConcurrentHashMap<>(); - private final Map<Long, Set<Long>> deviceAttributes = new ConcurrentHashMap<>(); - private final Map<Long, Set<Long>> deviceAttributesWithGroups = new ConcurrentHashMap<>(); - private final Map<Long, Set<Long>> groupAttributes = new ConcurrentHashMap<>(); - private final Map<Long, Set<Long>> userAttributes = new ConcurrentHashMap<>(); +public class AttributesManager extends ExtendedObjectManager { public AttributesManager(DataManager dataManager) { - this.dataManager = dataManager; - refreshAttributes(); - } - - public Set<Long> getUserAttributes(long userId) { - if (!userAttributes.containsKey(userId)) { - userAttributes.put(userId, new HashSet<Long>()); - } - return userAttributes.get(userId); - } - - public Set<Long> getGroupAttributes(long groupId) { - if (!groupAttributes.containsKey(groupId)) { - groupAttributes.put(groupId, new HashSet<Long>()); - } - return groupAttributes.get(groupId); - } - - public Set<Long> getDeviceAttributes(long deviceId) { - return getDeviceAttributes(deviceAttributes, deviceId); - } - - public Set<Long> getAllDeviceAttributes(long deviceId) { - return getDeviceAttributes(deviceAttributesWithGroups, deviceId); + super(dataManager, Attribute.class); + refreshItems(); + refreshExtendedPermissions(); } - private Set<Long> getDeviceAttributes(Map<Long, Set<Long>> deviceAttributes, long deviceId) { - if (!deviceAttributes.containsKey(deviceId)) { - deviceAttributes.put(deviceId, new HashSet<Long>()); - } - return deviceAttributes.get(deviceId); - } - - public final void refreshAttributes() { - if (dataManager != null) { - try { - attributes.clear(); - for (Attribute attribute : dataManager.getAttributes()) { - attributes.put(attribute.getId(), attribute); - } - } catch (SQLException error) { - Log.warning(error); - } - } - refreshUserAttributes(); - refresh(); - } - - public final void refreshUserAttributes() { - if (dataManager != null) { - try { - userAttributes.clear(); - for (AttributePermission attributePermission : dataManager.getAttributePermissions()) { - getUserAttributes(attributePermission.getUserId()).add(attributePermission.getAttributeId()); - } - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public final void refresh() { - if (dataManager != null) { - try { - - Collection<GroupAttribute> databaseGroupAttributes = dataManager.getGroupAttributes(); - - groupAttributes.clear(); - for (GroupAttribute groupAttribute : databaseGroupAttributes) { - getGroupAttributes(groupAttribute.getGroupId()).add(groupAttribute.getAttributeId()); - } - - Collection<DeviceAttribute> databaseDeviceAttributes = dataManager.getDeviceAttributes(); - Collection<Device> allDevices = Context.getDeviceManager().getAllDevices(); - - deviceAttributes.clear(); - deviceAttributesWithGroups.clear(); - - for (DeviceAttribute deviceAttribute : databaseDeviceAttributes) { - getDeviceAttributes(deviceAttribute.getDeviceId()) - .add(deviceAttribute.getAttributeId()); - getAllDeviceAttributes(deviceAttribute.getDeviceId()) - .add(deviceAttribute.getAttributeId()); - } - - for (Device device : allDevices) { - long groupId = device.getGroupId(); - while (groupId != 0) { - getAllDeviceAttributes(device.getId()).addAll(getGroupAttributes(groupId)); - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } - } - - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public void addAttribute(Attribute attribute) throws SQLException { - dataManager.addAttribute(attribute); - attributes.put(attribute.getId(), attribute); - } - - public void updateAttribute(Attribute attribute) throws SQLException { - dataManager.updateAttribute(attribute); - Attribute cachedAttribute = attributes.get(attribute.getId()); + @Override + public void updateItem(BaseModel item) throws SQLException { + Attribute attribute = (Attribute) item; + getDataManager().updateObject(attribute); + Attribute cachedAttribute = (Attribute) getById(item.getId()); cachedAttribute.setDescription(attribute.getDescription()); cachedAttribute.setAttribute(attribute.getAttribute()); cachedAttribute.setExpression(attribute.getExpression()); cachedAttribute.setType(attribute.getType()); } - public void removeAttribute(long computedAttributeId) throws SQLException { - dataManager.removeAttribute(computedAttributeId); - attributes.remove(computedAttributeId); - refreshUserAttributes(); - refresh(); - } - - public boolean checkAttribute(long userId, long attributeId) { - return getUserAttributes(userId).contains(attributeId); - } - - public Attribute getAttribute(long id) { - return attributes.get(id); - } - - public final Collection<Attribute> getAttributes(Set<Long> attributeIds) { - Collection<Attribute> result = new LinkedList<>(); - for (long attributeId : attributeIds) { - result.add(getAttribute(attributeId)); - } - return result; - } - - public final Set<Long> getAllAttributes() { - return attributes.keySet(); - } - - public final Set<Long> getManagedAttributes(long userId) { - Set<Long> attributes = new HashSet<>(); - attributes.addAll(getUserAttributes(userId)); - for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { - attributes.addAll(getUserAttributes(managedUserId)); - } - return attributes; - } - } diff --git a/src/org/traccar/database/CalendarManager.java b/src/org/traccar/database/CalendarManager.java index 31d484327..80bb79db2 100644 --- a/src/org/traccar/database/CalendarManager.java +++ b/src/org/traccar/database/CalendarManager.java @@ -16,107 +16,13 @@ */ 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.Context; -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 class CalendarManager extends SimpleObjectManager { 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 Collection<Calendar> getManagedCalendars(long userId) { - ArrayList<Calendar> result = new ArrayList<>(); - result.addAll(getUserCalendars(userId)); - for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { - result.addAll(getUserCalendars(managedUserId)); - } - 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(); + super(dataManager, Calendar.class); + refreshItems(); } - 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 1c2a66bcf..80b9f98e9 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -23,6 +23,7 @@ import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; +import java.util.Map; import javax.naming.InitialContext; import javax.sql.DataSource; @@ -38,31 +39,13 @@ import liquibase.resource.ResourceAccessor; import org.traccar.Config; import org.traccar.helper.Log; import org.traccar.model.AttributeAlias; -import org.traccar.model.AttributePermission; -import org.traccar.model.Calendar; -import org.traccar.model.CalendarPermission; -import org.traccar.model.Attribute; import org.traccar.model.Device; -import org.traccar.model.DeviceAttribute; -import org.traccar.model.DeviceDriver; -import org.traccar.model.DevicePermission; -import org.traccar.model.Driver; -import org.traccar.model.DriverPermission; import org.traccar.model.Event; -import org.traccar.model.Geofence; -import org.traccar.model.Group; -import org.traccar.model.GroupAttribute; -import org.traccar.model.GroupDriver; -import org.traccar.model.GroupGeofence; -import org.traccar.model.GroupPermission; -import org.traccar.model.Notification; +import org.traccar.model.BaseModel; import org.traccar.model.Position; import org.traccar.model.Server; import org.traccar.model.Statistics; import org.traccar.model.User; -import org.traccar.model.UserPermission; -import org.traccar.model.DeviceGeofence; -import org.traccar.model.GeofencePermission; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -166,17 +149,6 @@ public class DataManager { } } - public Collection<User> getUsers() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectUsersAll")) - .executeQuery(User.class); - } - - public void addUser(User user) throws SQLException { - user.setId(QueryBuilder.create(dataSource, getQuery("database.insertUser"), true) - .setObject(user) - .executeUpdate()); - } - public void updateUser(User user) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.updateUser")) .setObject(user) @@ -188,102 +160,12 @@ public class DataManager { } } - public void removeUser(long userId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteUser")) - .setLong("id", userId) - .executeUpdate(); - } - - public Collection<DevicePermission> getDevicePermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDevicePermissions")) - .executeQuery(DevicePermission.class); - } - - public Collection<GroupPermission> getGroupPermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupPermissions")) - .executeQuery(GroupPermission.class); - } - - public Collection<Device> getAllDevices() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDevicesAll")) - .executeQuery(Device.class); - } - - public void addDevice(Device device) throws SQLException { - device.setId(QueryBuilder.create(dataSource, getQuery("database.insertDevice"), true) - .setObject(device) - .executeUpdate()); - } - - public void updateDevice(Device device) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateDevice")) - .setObject(device) - .executeUpdate(); - } - public void updateDeviceStatus(Device device) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.updateDeviceStatus")) .setObject(device) .executeUpdate(); } - public void removeDevice(long deviceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteDevice")) - .setLong("id", deviceId) - .executeUpdate(); - } - - public void linkDevice(long userId, long deviceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDevice")) - .setLong("userId", userId) - .setLong("deviceId", deviceId) - .executeUpdate(); - } - - public void unlinkDevice(long userId, long deviceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDevice")) - .setLong("userId", userId) - .setLong("deviceId", deviceId) - .executeUpdate(); - } - - public Collection<Group> getAllGroups() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupsAll")) - .executeQuery(Group.class); - } - - public void addGroup(Group group) throws SQLException { - group.setId(QueryBuilder.create(dataSource, getQuery("database.insertGroup"), true) - .setObject(group) - .executeUpdate()); - } - - public void updateGroup(Group group) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateGroup")) - .setObject(group) - .executeUpdate(); - } - - public void removeGroup(long groupId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteGroup")) - .setLong("id", groupId) - .executeUpdate(); - } - - public void linkGroup(long userId, long groupId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGroup")) - .setLong("userId", userId) - .setLong("groupId", groupId) - .executeUpdate(); - } - - public void unlinkGroup(long userId, long groupId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGroup")) - .setLong("userId", userId) - .setLong("groupId", groupId) - .executeUpdate(); - } - public Collection<Position> getPositions(long deviceId, Date from, Date to) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectPositions")) .setLong("deviceId", deviceId) @@ -336,24 +218,12 @@ public class DataManager { .executeQuerySingle(Server.class); } - public void updateServer(Server server) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateServer")) - .setObject(server) - .executeUpdate(); - } - public Event getEvent(long eventId) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectEvent")) .setLong("id", eventId) .executeQuerySingle(Event.class); } - public void addEvent(Event event) throws SQLException { - event.setId(QueryBuilder.create(dataSource, getQuery("database.insertEvent"), true) - .setObject(event) - .executeUpdate()); - } - public Collection<Event> getEvents(long deviceId, Date from, Date to) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectEvents")) .setLong("deviceId", deviceId) @@ -362,109 +232,6 @@ public class DataManager { .executeQuery(Event.class); } - public Collection<Geofence> getGeofences() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGeofencesAll")) - .executeQuery(Geofence.class); - } - - public void addGeofence(Geofence geofence) throws SQLException { - geofence.setId(QueryBuilder.create(dataSource, getQuery("database.insertGeofence"), true) - .setObject(geofence) - .executeUpdate()); - } - - public void updateGeofence(Geofence geofence) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateGeofence")) - .setObject(geofence) - .executeUpdate(); - } - - public void removeGeofence(long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteGeofence")) - .setLong("id", geofenceId) - .executeUpdate(); - } - - public Collection<GeofencePermission> getGeofencePermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGeofencePermissions")) - .executeQuery(GeofencePermission.class); - } - - public void linkGeofence(long userId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGeofence")) - .setLong("userId", userId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public void unlinkGeofence(long userId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGeofence")) - .setLong("userId", userId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public Collection<GroupGeofence> getGroupGeofences() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupGeofences")) - .executeQuery(GroupGeofence.class); - } - - public void linkGroupGeofence(long groupId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGroupGeofence")) - .setLong("groupId", groupId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public void unlinkGroupGeofence(long groupId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGroupGeofence")) - .setLong("groupId", groupId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public Collection<DeviceGeofence> getDeviceGeofences() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDeviceGeofences")) - .executeQuery(DeviceGeofence.class); - } - - public void linkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDeviceGeofence")) - .setLong("deviceId", deviceId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public void unlinkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceGeofence")) - .setLong("deviceId", deviceId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public Collection<Notification> getNotifications() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectNotifications")) - .executeQuery(Notification.class); - } - - public void addNotification(Notification notification) throws SQLException { - notification.setId(QueryBuilder.create(dataSource, getQuery("database.insertNotification"), true) - .setObject(notification) - .executeUpdate()); - } - - public void updateNotification(Notification notification) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateNotification")) - .setObject(notification) - .executeUpdate(); - } - - public void removeNotification(Notification notification) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteNotification")) - .setLong("id", notification.getId()) - .executeUpdate(); - } - public Collection<AttributeAlias> getAttributeAliases() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectAttributeAliases")) .executeQuery(AttributeAlias.class); @@ -501,224 +268,55 @@ public class DataManager { .executeUpdate()); } - public Collection<Calendar> getCalendars() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectCalendarsAll")) - .executeQuery(Calendar.class); + public static Class<?> getClassByName(String name) throws ClassNotFoundException { + return Class.forName("org.traccar.model." + + name.substring(0, 1).toUpperCase() + name.replace("Id", "").substring(1)); } - 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 static String makeNameId(Class<?> clazz) { + String name = clazz.getSimpleName(); + return name.substring(0, 1).toLowerCase() + name.substring(1) + (name.indexOf("Id") == -1 ? "Id" : ""); } - public Collection<CalendarPermission> getCalendarPermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectCalendarPermissions")) - .executeQuery(CalendarPermission.class); - } + public void linkObject(Class<?> owner, long ownerId, Class<?> property, long propertyId, boolean link) + throws SQLException { + String query = "database." + (link ? "link" : "unlink") + owner.getSimpleName() + property.getSimpleName(); + QueryBuilder queryBuilder = QueryBuilder.create(dataSource, getQuery(query)); - public void linkCalendar(long userId, long calendarId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkCalendar")) - .setLong("userId", userId) - .setLong("calendarId", calendarId) - .executeUpdate(); + queryBuilder.setLong(makeNameId(owner), ownerId); + queryBuilder.setLong(makeNameId(property), propertyId); + queryBuilder.executeUpdate(); } - public void unlinkCalendar(long userId, long calendarId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkCalendar")) - .setLong("userId", userId) - .setLong("calendarId", calendarId) - .executeUpdate(); + public <T> Collection<T> getObjects(Class<T> clazz) throws SQLException { + String query = "database.select" + clazz.getSimpleName() + "s"; + return QueryBuilder.create(dataSource, getQuery(query)).executeQuery(clazz); } - public Collection<UserPermission> getUserPermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectUserPermissions")) - .executeQuery(UserPermission.class); + public Collection<Map<String, Long>> getPermissions(Class<? extends BaseModel> owner, + Class<? extends BaseModel> property) throws SQLException { + String query = "database.select" + owner.getSimpleName() + property.getSimpleName() + "s"; + return QueryBuilder.create(dataSource, getQuery(query)).executeMapQuery(Long.class); } - public void linkUser(long userId, long managedUserId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkUser")) - .setLong("userId", userId) - .setLong("managedUserId", managedUserId) - .executeUpdate(); - } - - public void unlinkUser(long userId, long managedUserId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkUser")) - .setLong("userId", userId) - .setLong("managedUserId", managedUserId) - .executeUpdate(); - } - - public Collection<Attribute> getAttributes() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectAttributes")) - .executeQuery(Attribute.class); - } - - public void addAttribute(Attribute attribute) throws SQLException { - attribute.setId(QueryBuilder.create(dataSource, getQuery("database.insertAttribute"), true) - .setObject(attribute) + public void addObject(BaseModel entity) throws SQLException { + String query = "database.insert" + entity.getClass().getSimpleName(); + entity.setId(QueryBuilder.create(dataSource, getQuery(query), true) + .setObject(entity) .executeUpdate()); } - public void updateAttribute(Attribute attribute) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateAttribute")) - .setObject(attribute) + public void updateObject(BaseModel entity) throws SQLException { + String query = "database.update" + entity.getClass().getSimpleName(); + QueryBuilder.create(dataSource, getQuery(query)) + .setObject(entity) .executeUpdate(); } - public void removeAttribute(long computedAttributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteAttribute")) - .setLong("id", computedAttributeId) - .executeUpdate(); - } - - public Collection<AttributePermission> getAttributePermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectAttributePermissions")) - .executeQuery(AttributePermission.class); - } - - public void linkAttribute(long userId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkAttribute")) - .setLong("userId", userId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void unlinkAttribute(long userId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkAttribute")) - .setLong("userId", userId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public Collection<GroupAttribute> getGroupAttributes() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupAttributes")) - .executeQuery(GroupAttribute.class); - } - - public void linkGroupAttribute(long groupId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGroupAttribute")) - .setLong("groupId", groupId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void unlinkGroupAttribute(long groupId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGroupAttribute")) - .setLong("groupId", groupId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public Collection<DeviceAttribute> getDeviceAttributes() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDeviceAttributes")) - .executeQuery(DeviceAttribute.class); - } - - public void linkDeviceAttribute(long deviceId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDeviceAttribute")) - .setLong("deviceId", deviceId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void unlinkDeviceAttribute(long deviceId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceAttribute")) - .setLong("deviceId", deviceId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public Collection<Driver> getDrivers() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDrivers")) - .executeQuery(Driver.class); - } - - public void addDriver(Driver driver) throws SQLException { - driver.setId(QueryBuilder.create(dataSource, getQuery("database.insertDriver"), true) - .setObject(driver) - .executeUpdate()); - } - - public void updateDriver(Driver driver) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateDriver")) - .setObject(driver) - .executeUpdate(); - } - - public void removeDriver(long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteDriver")) - .setLong("id", driverId) - .executeUpdate(); - } - - public Collection<DriverPermission> getDriverPermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDriverPermissions")) - .executeQuery(DriverPermission.class); - } - - public void linkDriver(long userId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDriver")) - .setLong("userId", userId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public void unlinkDriver(long userId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDriver")) - .setLong("userId", userId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public Collection<GroupDriver> getGroupDrivers() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupDrivers")) - .executeQuery(GroupDriver.class); - } - - public void linkGroupDriver(long groupId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGroupDriver")) - .setLong("groupId", groupId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public void unlinkGroupDriver(long groupId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGroupDriver")) - .setLong("groupId", groupId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public Collection<DeviceDriver> getDeviceDrivers() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDeviceDrivers")) - .executeQuery(DeviceDriver.class); - } - - public void linkDeviceDriver(long deviceId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDeviceDriver")) - .setLong("deviceId", deviceId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public void unlinkDeviceDriver(long deviceId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceDriver")) - .setLong("deviceId", deviceId) - .setLong("driverId", driverId) + public void removeObject(Class<? extends BaseModel> clazz, long entityId) throws SQLException { + String query = "database.delete" + clazz.getSimpleName(); + QueryBuilder.create(dataSource, getQuery(query)) + .setLong("id", entityId) .executeUpdate(); } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index f2a2dd565..ba360f165 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -85,7 +85,7 @@ public class DeviceManager implements IdentityManager { if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay) && devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { GeofenceManager geofenceManager = Context.getGeofenceManager(); - Collection<Device> databaseDevices = dataManager.getAllDevices(); + Collection<Device> databaseDevices = dataManager.getObjects(Device.class); if (devicesById == null) { devicesById = new ConcurrentHashMap<>(databaseDevices.size()); } @@ -199,7 +199,7 @@ public class DeviceManager implements IdentityManager { } public void addDevice(Device device) throws SQLException { - dataManager.addDevice(device); + dataManager.addObject(device); devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); @@ -209,7 +209,7 @@ public class DeviceManager implements IdentityManager { } public void updateDevice(Device device) throws SQLException { - dataManager.updateDevice(device); + dataManager.updateObject(device); devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); @@ -227,7 +227,7 @@ public class DeviceManager implements IdentityManager { } public void removeDevice(long deviceId) throws SQLException { - dataManager.removeDevice(deviceId); + dataManager.removeObject(Device.class, deviceId); if (devicesById.containsKey(deviceId)) { String deviceUniqueId = devicesById.get(deviceId).getUniqueId(); @@ -289,7 +289,7 @@ public class DeviceManager implements IdentityManager { long lastUpdate = groupsLastUpdate.get(); if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay) && groupsLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { - Collection<Group> databaseGroups = dataManager.getAllGroups(); + Collection<Group> databaseGroups = dataManager.getObjects(Group.class); if (groupsById == null) { groupsById = new ConcurrentHashMap<>(databaseGroups.size()); } @@ -359,18 +359,18 @@ public class DeviceManager implements IdentityManager { public void addGroup(Group group) throws SQLException { checkGroupCycles(group); - dataManager.addGroup(group); + dataManager.addObject(group); groupsById.put(group.getId(), group); } public void updateGroup(Group group) throws SQLException { checkGroupCycles(group); - dataManager.updateGroup(group); + dataManager.updateObject(group); groupsById.put(group.getId(), group); } public void removeGroup(long groupId) throws SQLException { - dataManager.removeGroup(groupId); + dataManager.removeObject(Group.class, groupId); groupsById.remove(groupId); } diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java index e89d59311..0dc2b102d 100644 --- a/src/org/traccar/database/DriversManager.java +++ b/src/org/traccar/database/DriversManager.java @@ -17,147 +17,50 @@ package org.traccar.database; import java.sql.SQLException; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedList; import java.util.Map; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import org.traccar.Context; import org.traccar.helper.Log; -import org.traccar.model.Device; -import org.traccar.model.DeviceDriver; import org.traccar.model.Driver; -import org.traccar.model.DriverPermission; -import org.traccar.model.GroupDriver; +import org.traccar.model.BaseModel; -public class DriversManager { +public class DriversManager extends ExtendedObjectManager { - private final DataManager dataManager; - - private final Map<Long, Driver> drivers = new ConcurrentHashMap<>(); private final Map<String, Driver> driversByUniqueId = new ConcurrentHashMap<>(); - private final Map<Long, Set<Long>> deviceDrivers = new ConcurrentHashMap<>(); - private final Map<Long, Set<Long>> deviceDriversWithGroups = new ConcurrentHashMap<>(); - private final Map<Long, Set<Long>> groupDrivers = new ConcurrentHashMap<>(); - private final Map<Long, Set<Long>> userDrivers = new ConcurrentHashMap<>(); public DriversManager(DataManager dataManager) { - this.dataManager = dataManager; - refreshDrivers(); - } - - public Set<Long> getUserDrivers(long userId) { - if (!userDrivers.containsKey(userId)) { - userDrivers.put(userId, new HashSet<Long>()); - } - return userDrivers.get(userId); - } - - public Set<Long> getGroupDrivers(long groupId) { - if (!groupDrivers.containsKey(groupId)) { - groupDrivers.put(groupId, new HashSet<Long>()); - } - return groupDrivers.get(groupId); - } - - public Set<Long> getDeviceDrivers(long deviceId) { - return getDeviceDrivers(deviceDrivers, deviceId); - } - - public Set<Long> getAllDeviceDrivers(long deviceId) { - return getDeviceDrivers(deviceDriversWithGroups, deviceId); - } - - private Set<Long> getDeviceDrivers(Map<Long, Set<Long>> deviceDrivers, long deviceId) { - if (!deviceDrivers.containsKey(deviceId)) { - deviceDrivers.put(deviceId, new HashSet<Long>()); - } - return deviceDrivers.get(deviceId); + super(dataManager, Driver.class); + refreshItems(); + refreshExtendedPermissions(); } - public final void refreshDrivers() { - if (dataManager != null) { + @Override + public void refreshItems() { + if (getDataManager() != null) { try { - drivers.clear(); - driversByUniqueId.clear(); - for (Driver driver : dataManager.getDrivers()) { - drivers.put(driver.getId(), driver); - driversByUniqueId.put(driver.getUniqueId(), driver); + clearItems(); + for (BaseModel item : getDataManager().getObjects(getBaseClass())) { + putItem(item.getId(), item); + driversByUniqueId.put(((Driver) item).getUniqueId(), (Driver) item); } } catch (SQLException error) { Log.warning(error); } } - refreshUserDrivers(); - refresh(); + refreshUserItems(); } - public final void refreshUserDrivers() { - if (dataManager != null) { - try { - userDrivers.clear(); - for (DriverPermission driverPermission : dataManager.getDriverPermissions()) { - getUserDrivers(driverPermission.getUserId()).add(driverPermission.getDriverId()); - } - } catch (SQLException error) { - Log.warning(error); - } - } + @Override + public void addItem(BaseModel item) throws SQLException { + super.addItem(item); + driversByUniqueId.put(((Driver) item).getUniqueId(), (Driver) item); } - public final void refresh() { - if (dataManager != null) { - try { - - Collection<GroupDriver> databaseGroupDrivers = dataManager.getGroupDrivers(); - - groupDrivers.clear(); - for (GroupDriver groupDriver : databaseGroupDrivers) { - getGroupDrivers(groupDriver.getGroupId()).add(groupDriver.getDriverId()); - } - - Collection<DeviceDriver> databaseDeviceDrivers = dataManager.getDeviceDrivers(); - Collection<Device> allDevices = Context.getDeviceManager().getAllDevices(); - - deviceDrivers.clear(); - deviceDriversWithGroups.clear(); - - for (DeviceDriver deviceAttribute : databaseDeviceDrivers) { - getDeviceDrivers(deviceAttribute.getDeviceId()) - .add(deviceAttribute.getDriverId()); - getAllDeviceDrivers(deviceAttribute.getDeviceId()) - .add(deviceAttribute.getDriverId()); - } - - for (Device device : allDevices) { - long groupId = device.getGroupId(); - while (groupId != 0) { - getAllDeviceDrivers(device.getId()).addAll(getGroupDrivers(groupId)); - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } - } - - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public void addDriver(Driver driver) throws SQLException { - dataManager.addDriver(driver); - drivers.put(driver.getId(), driver); - driversByUniqueId.put(driver.getUniqueId(), driver); - } - - public void updateDriver(Driver driver) throws SQLException { - dataManager.updateDriver(driver); - Driver cachedDriver = drivers.get(driver.getId()); + @Override + public void updateItem(BaseModel item) throws SQLException { + Driver driver = (Driver) item; + getDataManager().updateObject(driver); + Driver cachedDriver = (Driver) getById(driver.getId()); cachedDriver.setName(driver.getName()); if (!driver.getUniqueId().equals(cachedDriver.getUniqueId())) { driversByUniqueId.remove(cachedDriver.getUniqueId()); @@ -167,47 +70,20 @@ public class DriversManager { cachedDriver.setAttributes(driver.getAttributes()); } - public void removeDriver(long driverId) throws SQLException { - dataManager.removeDriver(driverId); - if (drivers.containsKey(driverId)) { - String driverUniqueId = drivers.get(driverId).getUniqueId(); - drivers.remove(driverId); + @Override + public void removeItem(long driverId) throws SQLException { + Driver cachedDriver = (Driver) getById(driverId); + getDataManager().removeObject(Driver.class, driverId); + if (cachedDriver != null) { + String driverUniqueId = cachedDriver.getUniqueId(); + removeCachedItem(driverId); driversByUniqueId.remove(driverUniqueId); } - refreshUserDrivers(); - refresh(); - } - - public boolean checkDriver(long userId, long driverId) { - return getUserDrivers(userId).contains(driverId); - } - - public Driver getDriver(long id) { - return drivers.get(id); + refreshUserItems(); + refreshExtendedPermissions(); } public Driver getDriverByUniqueId(String uniqueId) { return driversByUniqueId.get(uniqueId); } - - public final Collection<Driver> getDrivers(Set<Long> driverIds) { - Collection<Driver> result = new LinkedList<>(); - for (long driverId : driverIds) { - result.add(getDriver(driverId)); - } - return result; - } - - public final Set<Long> getAllDrivers() { - return drivers.keySet(); - } - - public final Set<Long> getManagedDrivers(long userId) { - Set<Long> drivers = new HashSet<>(); - drivers.addAll(getUserDrivers(userId)); - for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { - drivers.addAll(getUserDrivers(managedUserId)); - } - return drivers; - } } diff --git a/src/org/traccar/database/ExtendedObjectManager.java b/src/org/traccar/database/ExtendedObjectManager.java new file mode 100644 index 000000000..483c3a09e --- /dev/null +++ b/src/org/traccar/database/ExtendedObjectManager.java @@ -0,0 +1,121 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.Device; +import org.traccar.model.Group; +import org.traccar.model.BaseModel; + +public abstract class ExtendedObjectManager extends SimpleObjectManager { + + private final Map<Long, Set<Long>> deviceItems = new ConcurrentHashMap<>(); + private final Map<Long, Set<Long>> deviceItemsWithGroups = new ConcurrentHashMap<>(); + private final Map<Long, Set<Long>> groupItems = new ConcurrentHashMap<>(); + + protected ExtendedObjectManager(DataManager dataManager, Class<? extends BaseModel> baseClass) { + super(dataManager, baseClass); + } + + public final Set<Long> getGroupItems(long groupId) { + if (!groupItems.containsKey(groupId)) { + groupItems.put(groupId, new HashSet<Long>()); + } + return groupItems.get(groupId); + } + + protected final void clearGroupItems() { + groupItems.clear(); + } + + public final Set<Long> getDeviceItems(long deviceId) { + if (!deviceItems.containsKey(deviceId)) { + deviceItems.put(deviceId, new HashSet<Long>()); + } + return deviceItems.get(deviceId); + } + + protected final void clearDeviceItems() { + deviceItems.clear(); + } + + public Set<Long> getAllDeviceItems(long deviceId) { + if (!deviceItemsWithGroups.containsKey(deviceId)) { + deviceItemsWithGroups.put(deviceId, new HashSet<Long>()); + } + return deviceItemsWithGroups.get(deviceId); + } + + @Override + public void removeItem(long itemId) throws SQLException { + super.removeItem(itemId); + refreshExtendedPermissions(); + } + + public void refreshExtendedPermissions() { + if (getDataManager() != null) { + try { + + Collection<Map<String, Long>> databaseGroupPermissions = + getDataManager().getPermissions(Group.class, getBaseClass()); + + clearGroupItems(); + for (Map<String, Long> groupPermission : databaseGroupPermissions) { + getGroupItems(groupPermission.get(DataManager.makeNameId(Group.class))) + .add(groupPermission.get(getBaseClassIdName())); + } + + Collection<Map<String, Long>> databaseDevicePermissions = + getDataManager().getPermissions(Device.class, getBaseClass()); + Collection<Device> allDevices = Context.getDeviceManager().getAllDevices(); + + clearDeviceItems(); + deviceItemsWithGroups.clear(); + + for (Map<String, Long> devicePermission : databaseDevicePermissions) { + getDeviceItems(devicePermission.get(DataManager.makeNameId(Device.class))) + .add(devicePermission.get(getBaseClassIdName())); + getAllDeviceItems(devicePermission.get(DataManager.makeNameId(Device.class))) + .add(devicePermission.get(getBaseClassIdName())); + } + + for (Device device : allDevices) { + long groupId = device.getGroupId(); + while (groupId != 0) { + getAllDeviceItems(device.getId()).addAll(getGroupItems(groupId)); + if (Context.getDeviceManager().getGroupById(groupId) != null) { + groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } + } + } + + } catch (SQLException error) { + Log.warning(error); + } + } + } +} diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index b8e6a5d73..bc2c27a65 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -15,290 +15,54 @@ */ package org.traccar.database; -import java.sql.SQLException; import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; import org.traccar.Context; -import org.traccar.helper.Log; import org.traccar.model.Device; -import org.traccar.model.DeviceGeofence; import org.traccar.model.Geofence; -import org.traccar.model.GeofencePermission; -import org.traccar.model.GroupGeofence; import org.traccar.model.Position; -public class GeofenceManager { - - private final DataManager dataManager; - - private final Map<Long, Geofence> geofences = new HashMap<>(); - private final Map<Long, Set<Long>> userGeofences = new HashMap<>(); - private final Map<Long, Set<Long>> groupGeofences = new HashMap<>(); - - private final Map<Long, Set<Long>> deviceGeofencesWithGroups = new HashMap<>(); - private final Map<Long, Set<Long>> deviceGeofences = new HashMap<>(); - - private final ReadWriteLock deviceGeofencesLock = new ReentrantReadWriteLock(); - private final ReadWriteLock geofencesLock = new ReentrantReadWriteLock(); - private final ReadWriteLock groupGeofencesLock = new ReentrantReadWriteLock(); - private final ReadWriteLock userGeofencesLock = new ReentrantReadWriteLock(); +public class GeofenceManager extends ExtendedObjectManager { public GeofenceManager(DataManager dataManager) { - this.dataManager = dataManager; - refreshGeofences(); - } - - private Set<Long> getUserGeofences(long userId) { - if (!userGeofences.containsKey(userId)) { - userGeofences.put(userId, new HashSet<Long>()); - } - return userGeofences.get(userId); - } - - public Set<Long> getUserGeofencesIds(long userId) { - userGeofencesLock.readLock().lock(); - try { - return getUserGeofences(userId); - } finally { - userGeofencesLock.readLock().unlock(); - } - } - - private Set<Long> getGroupGeofences(long groupId) { - if (!groupGeofences.containsKey(groupId)) { - groupGeofences.put(groupId, new HashSet<Long>()); - } - return groupGeofences.get(groupId); - } - - public Set<Long> getGroupGeofencesIds(long groupId) { - groupGeofencesLock.readLock().lock(); - try { - return getGroupGeofences(groupId); - } finally { - groupGeofencesLock.readLock().unlock(); - } - } - - public Set<Long> getAllDeviceGeofences(long deviceId) { - deviceGeofencesLock.readLock().lock(); - try { - return getDeviceGeofences(deviceGeofencesWithGroups, deviceId); - } finally { - deviceGeofencesLock.readLock().unlock(); - } + super(dataManager, Geofence.class); + refreshItems(); + refreshExtendedPermissions(); } - public Set<Long> getDeviceGeofencesIds(long deviceId) { - deviceGeofencesLock.readLock().lock(); - try { - return getDeviceGeofences(deviceGeofences, deviceId); - } finally { - deviceGeofencesLock.readLock().unlock(); - } + @Override + public final void refreshExtendedPermissions() { + super.refreshExtendedPermissions(); + recalculateDevicesGeofences(); } - private Set<Long> getDeviceGeofences(Map<Long, Set<Long>> deviceGeofences, long deviceId) { - if (!deviceGeofences.containsKey(deviceId)) { - deviceGeofences.put(deviceId, new HashSet<Long>()); - } - return deviceGeofences.get(deviceId); - } - - public final void refreshGeofences() { - if (dataManager != null) { - try { - geofencesLock.writeLock().lock(); - try { - geofences.clear(); - for (Geofence geofence : dataManager.getGeofences()) { - geofences.put(geofence.getId(), geofence); - } - } finally { - geofencesLock.writeLock().unlock(); - } - } catch (SQLException error) { - Log.warning(error); - } - } - refreshUserGeofences(); - refresh(); - } - - public final void refreshUserGeofences() { - if (dataManager != null) { - try { - userGeofencesLock.writeLock().lock(); - try { - userGeofences.clear(); - for (GeofencePermission geofencePermission : dataManager.getGeofencePermissions()) { - getUserGeofences(geofencePermission.getUserId()).add(geofencePermission.getGeofenceId()); - } - } finally { - userGeofencesLock.writeLock().unlock(); - } - } catch (SQLException error) { - Log.warning(error); - } - } - } - - public final void refresh() { - if (dataManager != null) { - try { - - Collection<GroupGeofence> databaseGroupGeofences = dataManager.getGroupGeofences(); - groupGeofencesLock.writeLock().lock(); - try { - groupGeofences.clear(); - for (GroupGeofence groupGeofence : databaseGroupGeofences) { - getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId()); - } - } finally { - groupGeofencesLock.writeLock().unlock(); - } - - Collection<DeviceGeofence> databaseDeviceGeofences = dataManager.getDeviceGeofences(); - Collection<Device> allDevices = Context.getDeviceManager().getAllDevices(); - - groupGeofencesLock.readLock().lock(); - deviceGeofencesLock.writeLock().lock(); - try { - deviceGeofences.clear(); - deviceGeofencesWithGroups.clear(); - - for (DeviceGeofence deviceGeofence : databaseDeviceGeofences) { - getDeviceGeofences(deviceGeofences, deviceGeofence.getDeviceId()) - .add(deviceGeofence.getGeofenceId()); - getDeviceGeofences(deviceGeofencesWithGroups, deviceGeofence.getDeviceId()) - .add(deviceGeofence.getGeofenceId()); - } - - for (Device device : allDevices) { - long groupId = device.getGroupId(); - while (groupId != 0) { - getDeviceGeofences(deviceGeofencesWithGroups, - device.getId()).addAll(getGroupGeofences(groupId)); - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } - List<Long> deviceGeofenceIds = device.getGeofenceIds(); - if (deviceGeofenceIds == null) { - deviceGeofenceIds = new ArrayList<>(); - } else { - deviceGeofenceIds.clear(); - } - Position lastPosition = Context.getIdentityManager().getLastPosition(device.getId()); - if (lastPosition != null && deviceGeofencesWithGroups.containsKey(device.getId())) { - for (long geofenceId : deviceGeofencesWithGroups.get(device.getId())) { - Geofence geofence = getGeofence(geofenceId); - if (geofence != null && geofence.getGeometry() - .containsPoint(lastPosition.getLatitude(), lastPosition.getLongitude())) { - deviceGeofenceIds.add(geofenceId); - } - } - } - device.setGeofenceIds(deviceGeofenceIds); - } - - } finally { - deviceGeofencesLock.writeLock().unlock(); - groupGeofencesLock.readLock().unlock(); - } - - } catch (SQLException error) { - Log.warning(error); + public List<Long> getCurrentDeviceGeofences(Position position) { + List<Long> result = new ArrayList<>(); + for (long geofenceId : getAllDeviceItems(position.getDeviceId())) { + Geofence geofence = (Geofence) getById(geofenceId); + if (geofence != null && geofence.getGeometry() + .containsPoint(position.getLatitude(), position.getLongitude())) { + result.add(geofenceId); } } + return result; } - public final Collection<Geofence> getAllGeofences() { - geofencesLock.readLock().lock(); - try { - return geofences.values(); - } finally { - geofencesLock.readLock().unlock(); - } - } - - public final Set<Long> getAllGeofencesIds() { - geofencesLock.readLock().lock(); - try { - return geofences.keySet(); - } finally { - geofencesLock.readLock().unlock(); - } - } - - public final Set<Long> getManagedGeofencesIds(long userId) { - Set<Long> geofences = new HashSet<>(); - geofences.addAll(getUserGeofencesIds(userId)); - for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { - geofences.addAll(getUserGeofencesIds(managedUserId)); - } - return geofences; - } - - public final Collection<Geofence> getGeofences(Set<Long> geofencesIds) { - geofencesLock.readLock().lock(); - try { - Collection<Geofence> result = new LinkedList<>(); - for (long geofenceId : geofencesIds) { - result.add(getGeofence(geofenceId)); + public void recalculateDevicesGeofences() { + for (Device device : Context.getDeviceManager().getAllDevices()) { + List<Long> deviceGeofenceIds = device.getGeofenceIds(); + if (deviceGeofenceIds == null) { + deviceGeofenceIds = new ArrayList<>(); + } else { + deviceGeofenceIds.clear(); } - return result; - } finally { - geofencesLock.readLock().unlock(); - } - } - - public final Geofence getGeofence(long geofenceId) { - geofencesLock.readLock().lock(); - try { - return geofences.get(geofenceId); - } finally { - geofencesLock.readLock().unlock(); - } - } - - public final void updateGeofence(Geofence geofence) { - geofencesLock.writeLock().lock(); - try { - geofences.put(geofence.getId(), geofence); - } finally { - geofencesLock.writeLock().unlock(); - } - try { - dataManager.updateGeofence(geofence); - } catch (SQLException error) { - Log.warning(error); - } - } - - public boolean checkGeofence(long userId, long geofenceId) { - return getUserGeofencesIds(userId).contains(geofenceId); - } - - public List<Long> getCurrentDeviceGeofences(Position position) { - List<Long> result = new ArrayList<>(); - for (long geofenceId : getAllDeviceGeofences(position.getDeviceId())) { - if (getGeofence(geofenceId).getGeometry().containsPoint(position.getLatitude(), position.getLongitude())) { - result.add(geofenceId); + Position lastPosition = Context.getIdentityManager().getLastPosition(device.getId()); + if (lastPosition != null && getAllDeviceItems(device.getId()) != null) { + deviceGeofenceIds.addAll(getCurrentDeviceGeofences(lastPosition)); } + device.setGeofenceIds(deviceGeofenceIds); } - return result; } } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 48caa615c..0987f255b 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -49,7 +49,7 @@ public class NotificationManager { public void updateEvent(Event event, Position position) { try { - dataManager.addEvent(event); + dataManager.addObject(event); } catch (SQLException error) { Log.warning(error); } @@ -57,7 +57,7 @@ public class NotificationManager { Set<Long> users = Context.getPermissionsManager().getDeviceUsers(event.getDeviceId()); for (long userId : users) { if (event.getGeofenceId() == 0 || Context.getGeofenceManager() != null - && Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId())) { + && Context.getGeofenceManager().checkItemPermission(userId, event.getGeofenceId())) { Notification notification = getUserNotificationByType(userId, event.getType()); if (notification != null) { if (notification.getWeb()) { @@ -105,7 +105,7 @@ public class NotificationManager { notificationsLock.writeLock().lock(); try { userNotifications.clear(); - for (Notification notification : dataManager.getNotifications()) { + for (Notification notification : dataManager.getObjects(Notification.class)) { getUserNotificationsUnsafe(notification.getUserId()).add(notification); } } finally { @@ -139,7 +139,7 @@ public class NotificationManager { || cachedNotification.getSms() != notification.getSms()) { if (!notification.getWeb() && !notification.getMail() && !notification.getSms()) { try { - dataManager.removeNotification(cachedNotification); + dataManager.removeObject(Notification.class, cachedNotification.getId()); } catch (SQLException error) { Log.warning(error); } @@ -160,7 +160,7 @@ public class NotificationManager { notificationsLock.writeLock().unlock(); } try { - dataManager.updateNotification(cachedNotification); + dataManager.updateObject(cachedNotification); } catch (SQLException error) { Log.warning(error); } @@ -170,7 +170,7 @@ public class NotificationManager { } } else if (notification.getWeb() || notification.getMail() || notification.getSms()) { try { - dataManager.addNotification(notification); + dataManager.addObject(notification); } catch (SQLException error) { Log.warning(error); } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 82ca6af49..b9cdfc36b 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -17,13 +17,16 @@ package org.traccar.database; import org.traccar.Context; import org.traccar.helper.Log; +import org.traccar.model.Attribute; +import org.traccar.model.Calendar; import org.traccar.model.Device; -import org.traccar.model.DevicePermission; +import org.traccar.model.Driver; +import org.traccar.model.Geofence; import org.traccar.model.Group; -import org.traccar.model.GroupPermission; +import org.traccar.model.ManagedUser; +import org.traccar.model.Permission; import org.traccar.model.Server; import org.traccar.model.User; -import org.traccar.model.UserPermission; import java.lang.reflect.Method; import java.sql.SQLException; @@ -98,7 +101,7 @@ public class PermissionsManager { usersTokens.clear(); try { server = dataManager.getServer(); - for (User user : dataManager.getUsers()) { + for (User user : dataManager.getObjects(User.class)) { users.put(user.getId(), user); if (user.getToken() != null) { usersTokens.put(user.getToken(), user.getId()); @@ -112,8 +115,9 @@ public class PermissionsManager { public final void refreshUserPermissions() { userPermissions.clear(); try { - for (UserPermission permission : dataManager.getUserPermissions()) { - getUserPermissions(permission.getUserId()).add(permission.getManagedUserId()); + for (Map<String, Long> permission : dataManager.getPermissions(User.class, User.class)) { + getUserPermissions(permission.get(DataManager.makeNameId(User.class))) + .add(permission.get(DataManager.makeNameId(ManagedUser.class))); } } catch (SQLException error) { Log.warning(error); @@ -126,19 +130,23 @@ public class PermissionsManager { try { GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(), Context.getDeviceManager().getAllDevices()); - for (GroupPermission permission : dataManager.getGroupPermissions()) { - Set<Long> userGroupPermissions = getGroupPermissions(permission.getUserId()); - Set<Long> userDevicePermissions = getDevicePermissions(permission.getUserId()); - userGroupPermissions.add(permission.getGroupId()); - for (Group group : groupTree.getGroups(permission.getGroupId())) { + for (Map<String, Long> groupPermission : dataManager.getPermissions(User.class, Group.class)) { + Set<Long> userGroupPermissions = getGroupPermissions(groupPermission + .get(DataManager.makeNameId(User.class))); + Set<Long> userDevicePermissions = getDevicePermissions(groupPermission + .get(DataManager.makeNameId(User.class))); + userGroupPermissions.add(groupPermission.get(DataManager.makeNameId(Group.class))); + for (Group group : groupTree.getGroups(groupPermission.get(DataManager.makeNameId(Group.class)))) { userGroupPermissions.add(group.getId()); } - for (Device device : groupTree.getDevices(permission.getGroupId())) { + for (Device device : groupTree.getDevices(groupPermission.get(DataManager.makeNameId(Group.class)))) { userDevicePermissions.add(device.getId()); } } - for (DevicePermission permission : dataManager.getDevicePermissions()) { - getDevicePermissions(permission.getUserId()).add(permission.getDeviceId()); + + for (Map<String, Long> devicePermission : dataManager.getPermissions(User.class, Device.class)) { + getDevicePermissions(devicePermission.get(DataManager.makeNameId(User.class))) + .add(devicePermission.get(DataManager.makeNameId(Device.class))); } groupDevices.clear(); @@ -298,52 +306,72 @@ public class PermissionsManager { } } - public void checkGeofence(long userId, long geofenceId) throws SecurityException { - if (!Context.getGeofenceManager().checkGeofence(userId, geofenceId) && !isAdmin(userId)) { - checkManager(userId); - for (long managedUserId : getUserPermissions(userId)) { - if (Context.getGeofenceManager().checkGeofence(managedUserId, geofenceId)) { - return; - } - } - throw new SecurityException("Geofence access denied"); + public void checkPermission(Class<?> object, long userId, long objectId) + throws SecurityException { + SimpleObjectManager manager = null; + + if (object.equals(Device.class)) { + checkDevice(userId, objectId); + } else if (object.equals(Group.class)) { + checkGroup(userId, objectId); + } else if (object.equals(User.class) || object.equals(ManagedUser.class)) { + checkUser(userId, objectId); + } else if (object.equals(Geofence.class)) { + manager = Context.getGeofenceManager(); + } else if (object.equals(Attribute.class)) { + manager = Context.getAttributesManager(); + } else if (object.equals(Driver.class)) { + manager = Context.getDriversManager(); + } else if (object.equals(Calendar.class)) { + manager = Context.getCalendarManager(); + } else { + throw new IllegalArgumentException("Unknown object type"); } - } - public void checkAttribute(long userId, long attributeId) throws SecurityException { - if (!Context.getAttributesManager().checkAttribute(userId, attributeId) && !isAdmin(userId)) { + if (manager != null && !manager.checkItemPermission(userId, objectId) && !isAdmin(userId)) { checkManager(userId); for (long managedUserId : getUserPermissions(userId)) { - if (Context.getAttributesManager().checkAttribute(managedUserId, attributeId)) { + if (manager.checkItemPermission(managedUserId, objectId)) { return; } } - throw new SecurityException("Attribute access denied"); - } - } - - public void checkDriver(long userId, long driverId) throws SecurityException { - if (!Context.getDriversManager().checkDriver(userId, driverId) && !isAdmin(userId)) { - checkManager(userId); - for (long managedUserId : getUserPermissions(userId)) { - if (Context.getDriversManager().checkDriver(managedUserId, driverId)) { - return; - } + throw new SecurityException("Type " + object + " access denied"); + } + } + + public void refreshAllExtendedPermissions() { + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refreshExtendedPermissions(); + } + Context.getDriversManager().refreshExtendedPermissions(); + Context.getAttributesManager().refreshExtendedPermissions(); + } + + public void refreshPermissions(Permission permission) { + if (permission.getOwnerClass().equals(User.class)) { + if (permission.getPropertyClass().equals(Device.class) + || permission.getPropertyClass().equals(Group.class)) { + refreshPermissions(); + refreshAllExtendedPermissions(); + } else if (permission.getPropertyClass().equals(ManagedUser.class)) { + refreshUserPermissions(); + } else if (permission.getPropertyClass().equals(Geofence.class) && Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refreshUserItems(); + } else if (permission.getPropertyClass().equals(Driver.class)) { + Context.getDriversManager().refreshUserItems(); + } else if (permission.getPropertyClass().equals(Attribute.class)) { + Context.getAttributesManager().refreshUserItems(); + } else if (permission.getPropertyClass().equals(Calendar.class)) { + Context.getCalendarManager().refreshUserItems(); } - throw new SecurityException("Driver access denied"); - } - } - - - public void checkCalendar(long userId, long calendarId) throws SecurityException { - if (!Context.getCalendarManager().checkCalendar(userId, calendarId) && !isAdmin(userId)) { - checkManager(userId); - for (long managedUserId : getUserPermissions(userId)) { - if (Context.getCalendarManager().checkCalendar(managedUserId, calendarId)) { - return; - } + } else if (permission.getOwnerClass().equals(Device.class) || permission.getOwnerClass().equals(Group.class)) { + if (permission.getPropertyClass().equals(Geofence.class) && Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refreshExtendedPermissions(); + } else if (permission.getPropertyClass().equals(Driver.class)) { + Context.getDriversManager().refreshExtendedPermissions(); + } else if (permission.getPropertyClass().equals(Attribute.class)) { + Context.getAttributesManager().refreshExtendedPermissions(); } - throw new SecurityException("Calendar access denied"); } } @@ -352,7 +380,7 @@ public class PermissionsManager { } public void updateServer(Server server) throws SQLException { - dataManager.updateServer(server); + dataManager.updateObject(server); this.server = server; } @@ -379,7 +407,7 @@ public class PermissionsManager { } public void addUser(User user) throws SQLException { - dataManager.addUser(user); + dataManager.addObject(user); users.put(user.getId(), user); if (user.getToken() != null) { usersTokens.put(user.getToken(), user.getId()); @@ -401,7 +429,7 @@ public class PermissionsManager { } public void removeUser(long userId) throws SQLException { - dataManager.removeUser(userId); + dataManager.removeObject(User.class, userId); usersTokens.remove(users.get(userId).getToken()); users.remove(userId); refreshPermissions(); diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index a24e6f0bf..440690a76 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -35,6 +35,7 @@ import java.sql.Types; import java.util.Collection; import java.util.Date; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -487,4 +488,28 @@ public final class QueryBuilder { return 0; } + public <T> Collection<Map<String, T>> executeMapQuery(Class<T> clazz) throws SQLException { + List<Map<String, T>> result = new LinkedList<>(); + if (query != null) { + try { + try (ResultSet resultSet = statement.executeQuery()) { + ResultSetMetaData resultMetaData = resultSet.getMetaData(); + while (resultSet.next()) { + LinkedHashMap<String, T> map = new LinkedHashMap<>(); + for (int i = 1; i <= resultMetaData.getColumnCount(); i++) { + String label = resultMetaData.getColumnLabel(i); + map.put(label, resultSet.getObject(label, clazz)); + } + result.add(map); + } + } + } finally { + statement.close(); + connection.close(); + } + } + + return result; + } + } diff --git a/src/org/traccar/database/SimpleObjectManager.java b/src/org/traccar/database/SimpleObjectManager.java new file mode 100644 index 000000000..0db8af658 --- /dev/null +++ b/src/org/traccar/database/SimpleObjectManager.java @@ -0,0 +1,159 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.BaseModel; +import org.traccar.model.User; + +public abstract class SimpleObjectManager { + + private final DataManager dataManager; + + private final Map<Long, BaseModel> items = new ConcurrentHashMap<>(); + private final Map<Long, Set<Long>> userItems = new ConcurrentHashMap<>(); + + private Class<? extends BaseModel> baseClass; + private String baseClassIdName; + + protected SimpleObjectManager(DataManager dataManager, Class<? extends BaseModel> baseClass) { + this.dataManager = dataManager; + this.baseClass = baseClass; + baseClassIdName = DataManager.makeNameId(baseClass); + } + + protected final DataManager getDataManager() { + return dataManager; + } + + protected final Class<? extends BaseModel> getBaseClass() { + return baseClass; + } + + protected final String getBaseClassIdName() { + return baseClassIdName; + } + + public final BaseModel getById(long itemId) { + return items.get(itemId); + } + + protected final void clearItems() { + items.clear(); + } + + protected final void putItem(long itemId, BaseModel item) { + items.put(itemId, item); + } + + protected final void removeCachedItem(long itemId) { + items.remove(itemId); + } + + public final Set<Long> getUserItems(long userId) { + if (!userItems.containsKey(userId)) { + userItems.put(userId, new HashSet<Long>()); + } + return userItems.get(userId); + } + + protected final void clearUserItems() { + userItems.clear(); + } + + public final boolean checkItemPermission(long userId, long itemId) { + return getUserItems(userId).contains(itemId); + } + + public void refreshItems() { + if (dataManager != null) { + try { + clearItems(); + for (BaseModel item : dataManager.getObjects(this.baseClass)) { + putItem(item.getId(), item); + } + } catch (SQLException error) { + Log.warning(error); + } + } + refreshUserItems(); + } + + public final void refreshUserItems() { + if (dataManager != null) { + try { + clearUserItems(); + for (Map<String, Long> permission : dataManager.getPermissions(User.class, baseClass)) { + getUserItems(permission.get(DataManager.makeNameId(User.class))) + .add(permission.get(baseClassIdName)); + } + } catch (SQLException error) { + Log.warning(error); + } + } + } + + public void addItem(BaseModel item) throws SQLException { + dataManager.addObject(item); + putItem(item.getId(), item); + } + + public void updateItem(BaseModel item) throws SQLException { + dataManager.updateObject(item); + putItem(item.getId(), item); + } + + public void removeItem(long itemId) throws SQLException { + BaseModel item = getById(itemId); + if (item != null) { + dataManager.removeObject(baseClass, itemId); + removeCachedItem(itemId); + } + refreshUserItems(); + } + + public final <T> Collection<T> getItems(Class<T> clazz, Set<Long> itemIds) { + Collection<T> result = new LinkedList<>(); + for (long itemId : itemIds) { + result.add((T) getById(itemId)); + } + return result; + } + + public final Set<Long> getAllItems() { + return items.keySet(); + } + + public final Set<Long> getManagedItems(long userId) { + Set<Long> result = new HashSet<>(); + result.addAll(getUserItems(userId)); + for (long managedUserId : Context.getPermissionsManager().getUserPermissions(userId)) { + result.addAll(getUserItems(managedUserId)); + } + return result; + } + +} diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index fbec932b1..516df9d69 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -22,8 +22,10 @@ import java.util.List; import org.traccar.BaseEventHandler; import org.traccar.Context; import org.traccar.database.GeofenceManager; +import org.traccar.model.Calendar; import org.traccar.model.Device; import org.traccar.model.Event; +import org.traccar.model.Geofence; import org.traccar.model.Position; public class GeofenceEventHandler extends BaseEventHandler { @@ -57,18 +59,18 @@ public class GeofenceEventHandler extends BaseEventHandler { Collection<Event> events = new ArrayList<>(); for (long geofenceId : newGeofences) { - long calendarId = geofenceManager.getGeofence(geofenceId).getCalendarId(); - if (calendarId == 0 || Context.getCalendarManager().getCalendar(calendarId) == null - || Context.getCalendarManager().getCalendar(calendarId).checkMoment(position.getFixTime())) { + long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); + Calendar calendar = calendarId != 0 ? (Calendar) Context.getCalendarManager().getById(calendarId) : null; + if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } for (long geofenceId : oldGeofences) { - long calendarId = geofenceManager.getGeofence(geofenceId).getCalendarId(); - if (calendarId == 0 || Context.getCalendarManager().getCalendar(calendarId) == null - || Context.getCalendarManager().getCalendar(calendarId).checkMoment(position.getFixTime())) { + long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); + Calendar calendar = calendarId != 0 ? (Calendar) Context.getCalendarManager().getById(calendarId) : null; + if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); diff --git a/src/org/traccar/model/Attribute.java b/src/org/traccar/model/Attribute.java index 9c3b5e43b..45d40b3ec 100644 --- a/src/org/traccar/model/Attribute.java +++ b/src/org/traccar/model/Attribute.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class Attribute { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class Attribute extends BaseModel { private String description; diff --git a/src/org/traccar/model/AttributeAlias.java b/src/org/traccar/model/AttributeAlias.java index 2835c0558..1599ab939 100644 --- a/src/org/traccar/model/AttributeAlias.java +++ b/src/org/traccar/model/AttributeAlias.java @@ -16,17 +16,7 @@ */ package org.traccar.model; -public class AttributeAlias { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class AttributeAlias extends BaseModel { private long deviceId; diff --git a/src/org/traccar/model/AttributePermission.java b/src/org/traccar/model/AttributePermission.java deleted file mode 100644 index fe2fe7b6e..000000000 --- a/src/org/traccar/model/AttributePermission.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.model; - -public class AttributePermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } - - private long attributeId; - - public long getAttributeId() { - return attributeId; - } - - public void setAttributeId(long attributeId) { - this.attributeId = attributeId; - } - -} diff --git a/src/org/traccar/model/DriverPermission.java b/src/org/traccar/model/BaseModel.java index cdbcc4a50..8bdb916e8 100644 --- a/src/org/traccar/model/DriverPermission.java +++ b/src/org/traccar/model/BaseModel.java @@ -16,25 +16,16 @@ */ package org.traccar.model; -public class DriverPermission { +public class BaseModel { - private long userId; + private long id; - public long getUserId() { - return userId; + public final long getId() { + return id; } - public void setUserId(long userId) { - this.userId = userId; + public final void setId(long id) { + this.id = id; } - private long driverId; - - public long getDriverId() { - return driverId; - } - - public void setDriverId(long driverId) { - this.driverId = driverId; - } } diff --git a/src/org/traccar/model/Calendar.java b/src/org/traccar/model/Calendar.java index 55f696d50..3d855b1be 100644 --- a/src/org/traccar/model/Calendar.java +++ b/src/org/traccar/model/Calendar.java @@ -34,7 +34,7 @@ import net.fortuna.ical4j.model.Period; import net.fortuna.ical4j.model.component.CalendarComponent; import org.traccar.database.QueryIgnore; -public class Calendar extends Extensible { +public class Calendar extends ExtendedModel { private String name; diff --git a/src/org/traccar/model/CalendarPermission.java b/src/org/traccar/model/CalendarPermission.java deleted file mode 100644 index 59f54e07b..000000000 --- a/src/org/traccar/model/CalendarPermission.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.model; - -public class CalendarPermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } - - private long calendarId; - - public long getCalendarId() { - return calendarId; - } - - public void setCalendarId(long calendarId) { - this.calendarId = calendarId; - } -} diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index 2324da532..6a13c2b77 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -18,7 +18,7 @@ package org.traccar.model; import java.util.Date; import java.util.List; -public class Device extends Extensible { +public class Device extends ExtendedModel { private String name; diff --git a/src/org/traccar/model/DeviceAttribute.java b/src/org/traccar/model/DeviceAttribute.java deleted file mode 100644 index e0ac6dd98..000000000 --- a/src/org/traccar/model/DeviceAttribute.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.model; - -public class DeviceAttribute { - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } - - private long attributeId; - - public long getAttributeId() { - return attributeId; - } - - public void setAttributeId(long attributeId) { - this.attributeId = attributeId; - } -} diff --git a/src/org/traccar/model/DeviceDriver.java b/src/org/traccar/model/DeviceDriver.java deleted file mode 100644 index 2008aaddf..000000000 --- a/src/org/traccar/model/DeviceDriver.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.model; - -public class DeviceDriver { - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } - - private long driverId; - - public long getDriverId() { - return driverId; - } - - public void setDriverId(long driverId) { - this.driverId = driverId; - } - -} diff --git a/src/org/traccar/model/DeviceGeofence.java b/src/org/traccar/model/DeviceGeofence.java deleted file mode 100644 index 00c99add6..000000000 --- a/src/org/traccar/model/DeviceGeofence.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@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.model; - -public class DeviceGeofence { - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } - - private long geofenceId; - - public long getGeofenceId() { - return geofenceId; - } - - public void setGeofenceId(long geofenceId) { - this.geofenceId = geofenceId; - } - -} diff --git a/src/org/traccar/model/DevicePermission.java b/src/org/traccar/model/DevicePermission.java deleted file mode 100644 index c62173132..000000000 --- a/src/org/traccar/model/DevicePermission.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2015 - 2016 Anton Tananaev (anton@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.model; - -public class DevicePermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } - - private long deviceId; - - public long getDeviceId() { - return deviceId; - } - - public void setDeviceId(long deviceId) { - this.deviceId = deviceId; - } - -} diff --git a/src/org/traccar/model/Driver.java b/src/org/traccar/model/Driver.java index 7a51d9151..05f52fd4d 100644 --- a/src/org/traccar/model/Driver.java +++ b/src/org/traccar/model/Driver.java @@ -16,7 +16,7 @@ */ package org.traccar.model; -public class Driver extends Extensible { +public class Driver extends ExtendedModel { private String name; diff --git a/src/org/traccar/model/Extensible.java b/src/org/traccar/model/ExtendedModel.java index b7953d8a6..a4bf00e70 100644 --- a/src/org/traccar/model/Extensible.java +++ b/src/org/traccar/model/ExtendedModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,17 +18,7 @@ package org.traccar.model; import java.util.LinkedHashMap; import java.util.Map; -public class Extensible { - - private long id; - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } +public class ExtendedModel extends BaseModel { private Map<String, Object> attributes = new LinkedHashMap<>(); diff --git a/src/org/traccar/model/Geofence.java b/src/org/traccar/model/Geofence.java index dfb888852..21c196da9 100644 --- a/src/org/traccar/model/Geofence.java +++ b/src/org/traccar/model/Geofence.java @@ -26,7 +26,7 @@ import org.traccar.geofence.GeofencePolyline; import com.fasterxml.jackson.annotation.JsonIgnore; -public class Geofence extends Extensible { +public class Geofence extends ExtendedModel { public static final String TYPE_GEOFENCE_CILCLE = "geofenceCircle"; public static final String TYPE_GEOFENCE_POLYGON = "geofencePolygon"; diff --git a/src/org/traccar/model/GeofencePermission.java b/src/org/traccar/model/GeofencePermission.java deleted file mode 100644 index 464f4e9eb..000000000 --- a/src/org/traccar/model/GeofencePermission.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@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.model; - -public class GeofencePermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } - - private long geofenceId; - - public long getGeofenceId() { - return geofenceId; - } - - public void setGeofenceId(long geofenceId) { - this.geofenceId = geofenceId; - } - -} diff --git a/src/org/traccar/model/Group.java b/src/org/traccar/model/Group.java index c21d43127..aad206aad 100644 --- a/src/org/traccar/model/Group.java +++ b/src/org/traccar/model/Group.java @@ -15,7 +15,7 @@ */ package org.traccar.model; -public class Group extends Extensible { +public class Group extends ExtendedModel { private String name; diff --git a/src/org/traccar/model/GroupAttribute.java b/src/org/traccar/model/GroupAttribute.java deleted file mode 100644 index a7e8a80bc..000000000 --- a/src/org/traccar/model/GroupAttribute.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.model; - -public class GroupAttribute { - - private long groupId; - - public long getGroupId() { - return groupId; - } - - public void setGroupId(long groupId) { - this.groupId = groupId; - } - - private long attributeId; - - public long getAttributeId() { - return attributeId; - } - - public void setAttributeId(long attributeId) { - this.attributeId = attributeId; - } -} diff --git a/src/org/traccar/model/GroupGeofence.java b/src/org/traccar/model/GroupGeofence.java deleted file mode 100644 index 736e6c704..000000000 --- a/src/org/traccar/model/GroupGeofence.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@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.model; - -public class GroupGeofence { - - private long groupId; - - public long getGroupId() { - return groupId; - } - - public void setGroupId(long groupId) { - this.groupId = groupId; - } - - private long geofenceId; - - public long getGeofenceId() { - return geofenceId; - } - - public void setGeofenceId(long geofenceId) { - this.geofenceId = geofenceId; - } - -} diff --git a/src/org/traccar/model/GroupPermission.java b/src/org/traccar/model/GroupPermission.java deleted file mode 100644 index 59b41b049..000000000 --- a/src/org/traccar/model/GroupPermission.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@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.model; - -public class GroupPermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } - - private long groupId; - - public long getGroupId() { - return groupId; - } - - public void setGroupId(long groupId) { - this.groupId = groupId; - } - -} diff --git a/src/org/traccar/model/GroupDriver.java b/src/org/traccar/model/ManagedUser.java index 91b205923..03c5ef48d 100644 --- a/src/org/traccar/model/GroupDriver.java +++ b/src/org/traccar/model/ManagedUser.java @@ -16,26 +16,6 @@ */ package org.traccar.model; -public class GroupDriver { - - private long groupId; - - public long getGroupId() { - return groupId; - } - - public void setGroupId(long groupId) { - this.groupId = groupId; - } - - private long driverId; - - public long getDriverId() { - return driverId; - } - - public void setDriverId(long driverId) { - this.driverId = driverId; - } +public class ManagedUser extends User { } diff --git a/src/org/traccar/model/Message.java b/src/org/traccar/model/Message.java index ab472202b..dad9c20f0 100644 --- a/src/org/traccar/model/Message.java +++ b/src/org/traccar/model/Message.java @@ -15,7 +15,7 @@ */ package org.traccar.model; -public class Message extends Extensible { +public class Message extends ExtendedModel { private long deviceId; diff --git a/src/org/traccar/model/Notification.java b/src/org/traccar/model/Notification.java index 6c61cafaf..e7bb69903 100644 --- a/src/org/traccar/model/Notification.java +++ b/src/org/traccar/model/Notification.java @@ -15,7 +15,7 @@ */ package org.traccar.model; -public class Notification extends Extensible { +public class Notification extends ExtendedModel { private long userId; diff --git a/src/org/traccar/model/Permission.java b/src/org/traccar/model/Permission.java new file mode 100644 index 000000000..1006b1c47 --- /dev/null +++ b/src/org/traccar/model/Permission.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 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.model; + +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.traccar.database.DataManager; + +public class Permission { + + private Class<?> ownerClass; + private long ownerId; + private Class<?> propertyClass; + private long propertyId; + + public Permission(LinkedHashMap<String, Long> permissionMap) throws ClassNotFoundException { + Iterator<Map.Entry<String, Long>> iterator = permissionMap.entrySet().iterator(); + String owner = iterator.next().getKey(); + ownerClass = DataManager.getClassByName(owner); + String property = iterator.next().getKey(); + propertyClass = DataManager.getClassByName(property); + ownerId = permissionMap.get(owner); + propertyId = permissionMap.get(property); + } + + public Class<?> getOwnerClass() { + return ownerClass; + } + + public long getOwnerId() { + return ownerId; + } + + public Class<?> getPropertyClass() { + return propertyClass; + } + + public long getPropertyId() { + return propertyId; + } +} diff --git a/src/org/traccar/model/Server.java b/src/org/traccar/model/Server.java index 4ded65204..cd5b0624e 100644 --- a/src/org/traccar/model/Server.java +++ b/src/org/traccar/model/Server.java @@ -19,7 +19,7 @@ import java.util.TimeZone; import org.traccar.helper.Log; -public class Server extends Extensible { +public class Server extends ExtendedModel { public String getVersion() { return Log.getAppVersion(); diff --git a/src/org/traccar/model/Statistics.java b/src/org/traccar/model/Statistics.java index c7ae5af7a..2acf8514f 100644 --- a/src/org/traccar/model/Statistics.java +++ b/src/org/traccar/model/Statistics.java @@ -17,7 +17,7 @@ package org.traccar.model; import java.util.Date; -public class Statistics extends Extensible { +public class Statistics extends ExtendedModel { private Date captureTime; diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java index 366ced503..6e6a079c2 100644 --- a/src/org/traccar/model/User.java +++ b/src/org/traccar/model/User.java @@ -21,7 +21,7 @@ import org.traccar.helper.Hashing; import java.util.Date; import java.util.TimeZone; -public class User extends Extensible { +public class User extends ExtendedModel { private String name; diff --git a/src/org/traccar/model/UserPermission.java b/src/org/traccar/model/UserPermission.java deleted file mode 100644 index 39ead5ef1..000000000 --- a/src/org/traccar/model/UserPermission.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.model; - -public class UserPermission { - - private long userId; - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } - - private long managedUserId; - - public long getManagedUserId() { - return managedUserId; - } - - public void setManagedUserId(long managedUserId) { - this.managedUserId = managedUserId; - } - -} diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index bd7cfc0c5..08f55df75 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -75,7 +75,7 @@ public final class EventForwarder { } } if (event.getGeofenceId() != 0) { - Geofence geofence = Context.getGeofenceManager().getGeofence(event.getGeofenceId()); + Geofence geofence = (Geofence) Context.getGeofenceManager().getById(event.getGeofenceId()); if (geofence != null) { data.put(KEY_GEOFENCE, geofence); } diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index a30023fdc..cd9959671 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -51,7 +51,7 @@ public final class NotificationFormatter { velocityContext.put("speedUnits", ReportUtils.getSpeedUnit(userId)); } if (event.getGeofenceId() != 0) { - velocityContext.put("geofence", Context.getGeofenceManager().getGeofence(event.getGeofenceId())); + velocityContext.put("geofence", Context.getGeofenceManager().getById(event.getGeofenceId())); } String driverUniqueId = event.getString(Position.KEY_DRIVER_UNIQUE_ID); if (driverUniqueId != null) { diff --git a/src/org/traccar/notification/PropertiesProvider.java b/src/org/traccar/notification/PropertiesProvider.java index e7cac8d0f..2fea901af 100644 --- a/src/org/traccar/notification/PropertiesProvider.java +++ b/src/org/traccar/notification/PropertiesProvider.java @@ -16,27 +16,27 @@ package org.traccar.notification; import org.traccar.Config; -import org.traccar.model.Extensible; +import org.traccar.model.ExtendedModel; public class PropertiesProvider { private Config config; - private Extensible extensible; + private ExtendedModel extendedModel; public PropertiesProvider(Config config) { this.config = config; } - public PropertiesProvider(Extensible extensible) { - this.extensible = extensible; + public PropertiesProvider(ExtendedModel extendedModel) { + this.extendedModel = extendedModel; } public String getString(String key) { if (config != null) { return config.getString(key); } else { - return extensible.getString(key); + return extendedModel.getString(key); } } diff --git a/src/org/traccar/processing/ComputedAttributesHandler.java b/src/org/traccar/processing/ComputedAttributesHandler.java index 536e39ec4..d6a762535 100644 --- a/src/org/traccar/processing/ComputedAttributesHandler.java +++ b/src/org/traccar/processing/ComputedAttributesHandler.java @@ -86,8 +86,8 @@ public class ComputedAttributesHandler extends BaseDataHandler { @Override protected Position handlePosition(Position position) { - Collection<Attribute> attributes = Context.getAttributesManager().getAttributes( - Context.getAttributesManager().getAllDeviceAttributes(position.getDeviceId())); + Collection<Attribute> attributes = Context.getAttributesManager().getItems(Attribute.class, + Context.getAttributesManager().getAllDeviceItems(position.getDeviceId())); for (Attribute attribute : attributes) { if (attribute.getAttribute() != null) { Object result = null; diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 0706f1382..3a111944c 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -50,7 +50,7 @@ public final class Events { for (Event event : events) { if (all || types.contains(event.getType())) { long geofenceId = event.getGeofenceId(); - if (geofenceId == 0 || Context.getGeofenceManager().checkGeofence(userId, geofenceId)) { + if (geofenceId == 0 || Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) { result.add(event); } } @@ -74,8 +74,8 @@ public final class Events { if (all || types.contains(event.getType())) { long geofenceId = event.getGeofenceId(); if (geofenceId != 0) { - if (Context.getGeofenceManager().checkGeofence(userId, geofenceId)) { - Geofence geofence = Context.getGeofenceManager().getGeofence(geofenceId); + if (Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) { + Geofence geofence = (Geofence) Context.getGeofenceManager().getById(geofenceId); if (geofence != null) { geofenceNames.put(geofenceId, geofence.getName()); } |