Search in sources :

Example 1 with Group

use of org.traccar.model.Group in project traccar by tananaev.

the class PermissionsManager method refreshDeviceAndGroupPermissions.

public final void refreshDeviceAndGroupPermissions() {
    groupPermissions.clear();
    devicePermissions.clear();
    try {
        GroupTree groupTree = new GroupTree(Context.getGroupsManager().getItems(Context.getGroupsManager().getAllItems()), Context.getDeviceManager().getAllDevices());
        for (Permission groupPermission : dataManager.getPermissions(User.class, Group.class)) {
            Set<Long> userGroupPermissions = getGroupPermissions(groupPermission.getOwnerId());
            Set<Long> userDevicePermissions = getDevicePermissions(groupPermission.getOwnerId());
            userGroupPermissions.add(groupPermission.getPropertyId());
            for (Group group : groupTree.getGroups(groupPermission.getPropertyId())) {
                userGroupPermissions.add(group.getId());
            }
            for (Device device : groupTree.getDevices(groupPermission.getPropertyId())) {
                userDevicePermissions.add(device.getId());
            }
        }
        for (Permission devicePermission : dataManager.getPermissions(User.class, Device.class)) {
            getDevicePermissions(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
        }
        groupDevices.clear();
        for (long groupId : Context.getGroupsManager().getAllItems()) {
            for (Device device : groupTree.getDevices(groupId)) {
                getGroupDevices(groupId).add(device.getId());
            }
        }
    } catch (SQLException | ClassNotFoundException error) {
        Log.warning(error);
    }
    deviceUsers.clear();
    for (Map.Entry<Long, Set<Long>> entry : devicePermissions.entrySet()) {
        for (long deviceId : entry.getValue()) {
            getAllDeviceUsers(deviceId).add(entry.getKey());
        }
    }
}
Also used : Group(org.traccar.model.Group) Set(java.util.Set) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) Device(org.traccar.model.Device) Permission(org.traccar.model.Permission) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Group

use of org.traccar.model.Group in project traccar by tananaev.

the class DeviceManager method lookupAttribute.

private Object lookupAttribute(long deviceId, String attributeName, boolean lookupConfig) {
    Object result = null;
    Device device = getById(deviceId);
    if (device != null) {
        result = device.getAttributes().get(attributeName);
        if (result == null && lookupGroupsAttribute) {
            long groupId = device.getGroupId();
            while (groupId != 0) {
                Group group = Context.getGroupsManager().getById(groupId);
                if (group != null) {
                    result = group.getAttributes().get(attributeName);
                    if (result != null) {
                        break;
                    }
                    groupId = group.getGroupId();
                } else {
                    groupId = 0;
                }
            }
        }
        if (result == null) {
            if (lookupConfig) {
                result = Context.getConfig().getString(attributeName);
            } else {
                Server server = Context.getPermissionsManager().getServer();
                result = server.getAttributes().get(attributeName);
            }
        }
    }
    return result;
}
Also used : Group(org.traccar.model.Group) Server(org.traccar.model.Server) Device(org.traccar.model.Device)

Example 3 with Group

use of org.traccar.model.Group in project traccar by tananaev.

the class ExtendedObjectManager method refreshExtendedPermissions.

public void refreshExtendedPermissions() {
    if (getDataManager() != null) {
        try {
            Collection<Permission> databaseGroupPermissions = getDataManager().getPermissions(Group.class, getBaseClass());
            groupItems.clear();
            for (Permission groupPermission : databaseGroupPermissions) {
                getGroupItems(groupPermission.getOwnerId()).add(groupPermission.getPropertyId());
            }
            Collection<Permission> databaseDevicePermissions = getDataManager().getPermissions(Device.class, getBaseClass());
            deviceItems.clear();
            deviceItemsWithGroups.clear();
            for (Permission devicePermission : databaseDevicePermissions) {
                getDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
                getAllDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
            }
            for (Device device : Context.getDeviceManager().getAllDevices()) {
                long groupId = device.getGroupId();
                while (groupId != 0) {
                    getAllDeviceItems(device.getId()).addAll(getGroupItems(groupId));
                    Group group = Context.getGroupsManager().getById(groupId);
                    if (group != null) {
                        groupId = group.getGroupId();
                    } else {
                        groupId = 0;
                    }
                }
            }
        } catch (SQLException | ClassNotFoundException error) {
            Log.warning(error);
        }
    }
}
Also used : Group(org.traccar.model.Group) SQLException(java.sql.SQLException) Device(org.traccar.model.Device) Permission(org.traccar.model.Permission)

Example 4 with Group

use of org.traccar.model.Group in project traccar by tananaev.

the class Events method getExcel.

public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws SQLException, IOException {
    ReportUtils.checkPeriodLimit(from, to);
    ArrayList<DeviceReport> devicesEvents = new ArrayList<>();
    ArrayList<String> sheetNames = new ArrayList<>();
    HashMap<Long, String> geofenceNames = new HashMap<>();
    for (long deviceId : ReportUtils.getDeviceList(deviceIds, groupIds)) {
        Context.getPermissionsManager().checkDevice(userId, deviceId);
        Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to);
        boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS);
        for (Iterator<Event> iterator = events.iterator(); iterator.hasNext(); ) {
            Event event = iterator.next();
            if (all || types.contains(event.getType())) {
                long geofenceId = event.getGeofenceId();
                if (geofenceId != 0) {
                    if (Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) {
                        Geofence geofence = Context.getGeofenceManager().getById(geofenceId);
                        if (geofence != null) {
                            geofenceNames.put(geofenceId, geofence.getName());
                        }
                    } else {
                        iterator.remove();
                    }
                }
            } else {
                iterator.remove();
            }
        }
        DeviceReport deviceEvents = new DeviceReport();
        Device device = Context.getIdentityManager().getById(deviceId);
        deviceEvents.setDeviceName(device.getName());
        sheetNames.add(WorkbookUtil.createSafeSheetName(deviceEvents.getDeviceName()));
        if (device.getGroupId() != 0) {
            Group group = Context.getGroupsManager().getById(device.getGroupId());
            if (group != null) {
                deviceEvents.setGroupName(group.getName());
            }
        }
        deviceEvents.setObjects(events);
        devicesEvents.add(deviceEvents);
    }
    String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/");
    try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) {
        org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
        jxlsContext.putVar("devices", devicesEvents);
        jxlsContext.putVar("sheetNames", sheetNames);
        jxlsContext.putVar("geofenceNames", geofenceNames);
        jxlsContext.putVar("from", from);
        jxlsContext.putVar("to", to);
        ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
    }
}
Also used : Group(org.traccar.model.Group) HashMap(java.util.HashMap) Device(org.traccar.model.Device) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) Event(org.traccar.model.Event) DeviceReport(org.traccar.reports.model.DeviceReport) Geofence(org.traccar.model.Geofence)

Example 5 with Group

use of org.traccar.model.Group in project traccar by tananaev.

the class Stops method getExcel.

public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws SQLException, IOException {
    ReportUtils.checkPeriodLimit(from, to);
    ArrayList<DeviceReport> devicesStops = new ArrayList<>();
    ArrayList<String> sheetNames = new ArrayList<>();
    for (long deviceId : ReportUtils.getDeviceList(deviceIds, groupIds)) {
        Context.getPermissionsManager().checkDevice(userId, deviceId);
        Collection<StopReport> stops = detectStops(deviceId, from, to);
        DeviceReport deviceStops = new DeviceReport();
        Device device = Context.getIdentityManager().getById(deviceId);
        deviceStops.setDeviceName(device.getName());
        sheetNames.add(WorkbookUtil.createSafeSheetName(deviceStops.getDeviceName()));
        if (device.getGroupId() != 0) {
            Group group = Context.getGroupsManager().getById(device.getGroupId());
            if (group != null) {
                deviceStops.setGroupName(group.getName());
            }
        }
        deviceStops.setObjects(stops);
        devicesStops.add(deviceStops);
    }
    String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/");
    try (InputStream inputStream = new FileInputStream(templatePath + "/stops.xlsx")) {
        org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
        jxlsContext.putVar("devices", devicesStops);
        jxlsContext.putVar("sheetNames", sheetNames);
        jxlsContext.putVar("from", from);
        jxlsContext.putVar("to", to);
        ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
    }
}
Also used : Group(org.traccar.model.Group) Device(org.traccar.model.Device) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) StopReport(org.traccar.reports.model.StopReport) FileInputStream(java.io.FileInputStream) DeviceReport(org.traccar.reports.model.DeviceReport)

Aggregations

Group (org.traccar.model.Group)8 Device (org.traccar.model.Device)7 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 DeviceReport (org.traccar.reports.model.DeviceReport)4 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 Permission (org.traccar.model.Permission)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Event (org.traccar.model.Event)1 Geofence (org.traccar.model.Geofence)1 Position (org.traccar.model.Position)1 Server (org.traccar.model.Server)1 StopReport (org.traccar.reports.model.StopReport)1 TripReport (org.traccar.reports.model.TripReport)1