@Path("{id}")
 @PUT
 public Response update(Geofence entity) throws SQLException {
   Context.getPermissionsManager().checkReadonly(getUserId());
   Context.getPermissionsManager().checkGeofence(getUserId(), entity.getId());
   Context.getGeofenceManager().updateGeofence(entity);
   return Response.ok(entity).build();
 }
 @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();
   return Response.ok(entity).build();
 }
 @Path("{id}")
 @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();
   return Response.noContent().build();
 }
  @GET
  public Collection<Geofence> get(
      @QueryParam("all") boolean all,
      @QueryParam("userId") long userId,
      @QueryParam("groupId") long groupId,
      @QueryParam("deviceId") long deviceId,
      @QueryParam("refresh") boolean refresh)
      throws SQLException {

    GeofenceManager geofenceManager = Context.getGeofenceManager();
    if (refresh) {
      geofenceManager.refreshGeofences();
    }

    Set<Long> result;
    if (all) {
      Context.getPermissionsManager().checkAdmin(getUserId());
      result = new HashSet<>(geofenceManager.getAllGeofencesIds());
    } else {
      if (userId == 0) {
        userId = getUserId();
      }
      Context.getPermissionsManager().checkUser(getUserId(), userId);
      result = new HashSet<>(geofenceManager.getUserGeofencesIds(userId));
    }

    if (groupId != 0) {
      Context.getPermissionsManager().checkGroup(getUserId(), groupId);
      result.retainAll(geofenceManager.getGroupGeofencesIds(groupId));
    }

    if (deviceId != 0) {
      Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
      result.retainAll(geofenceManager.getDeviceGeofencesIds(deviceId));
    }
    return geofenceManager.getGeofences(result);
  }