<commit_before>/*
 * Autopsy Forensic Browser
 *
 * Copyright 2019-2020 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> 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.sleuthkit.autopsy.geolocation;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import javafx.util.Pair;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.geolocation.datamodel.GeoLocationDataException;
import org.sleuthkit.autopsy.geolocation.datamodel.Track;
import org.sleuthkit.autopsy.geolocation.datamodel.Waypoint;
import org.sleuthkit.autopsy.geolocation.datamodel.WaypointBuilder;
import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;

/**
 * The business logic for filtering waypoints.
 */
abstract class AbstractWaypointFetcher implements WaypointBuilder.WaypointFilterQueryCallBack {

    private static final Logger logger = Logger.getLogger(AbstractWaypointFetcher.class.getName());

    private final GeoFilterPanel.GeoFilter filters;

    /**
     * Constructs the Waypoint Runner
     *
     * @param filters
     */
    AbstractWaypointFetcher(GeoFilterPanel.GeoFilter filters) {
        this.filters = filters;
    }

    /**
     * Gets the waypoints based in the current GeoFilter.
     *
     * This function kicks off a process that will send with
     * handleFilteredWaypointSet being called. Subclasses must implement
     * handleFitleredWayoiintSet to get the final results.
     *
     * @throws GeoLocationDataException
     */
    void getWaypoints() throws GeoLocationDataException {
        Case currentCase = Case.getCurrentCase();
        WaypointBuilder.getAllWaypoints(currentCase.getSleuthkitCase(),
                filters.getDataSources(),
                filters.getArtifactTypes(),
                filters.showAllWaypoints(),
                filters.getMostRecentNumDays(),
                filters.showWaypointsWithoutTimeStamp(),
                this);

    }

    /**
     * Called after all of the MapWaypoints are created from all of the
     * TSK_GPS_XXX objects.
     *
     * @param mapWaypoints List of filtered MapWaypoints.
     */
    abstract void handleFilteredWaypointSet(Set<MapWaypoint> mapWaypoints, List<Set<MapWaypoint>> tracks);

    @Override
    public void process(List<Waypoint> waypoints) {
        List<Track> tracks = new ArrayList<>();
        if (filters.getArtifactTypes().contains(ARTIFACT_TYPE.TSK_GPS_TRACK)) {
            try {
                tracks = Track.getTracks(Case.getCurrentCase().getSleuthkitCase(), filters.getDataSources());
            } catch (GeoLocationDataException ex) {
                logger.log(Level.WARNING, "Exception thrown while retrieving list of Tracks", ex);
            }
        }
        Pair<List<Waypoint>, List<List<Waypoint>>> waypointsAndTracks = createWaypointList(waypoints, tracks);
        
        final Set<MapWaypoint> pointSet = MapWaypoint.getWaypoints(waypointsAndTracks.getKey());
        final List<Set<MapWaypoint>> trackSets = new ArrayList<>();
        for (List<Waypoint> t : waypointsAndTracks.getValue()) {
            trackSets.add(MapWaypoint.getWaypoints(t));
        }

        handleFilteredWaypointSet(pointSet, trackSets);
    }

    /**
     * Returns a complete list of waypoints including the tracks. Takes into
     * account the current filters and includes waypoints as approprate.
     *
     * @param waypoints List of waypoints
     * @param tracks    List of tracks
     *
     * @return A list of waypoints including the tracks based on the current
     *         filters.
     */
    private Pair<List<Waypoint>, List<List<Waypoint>>> createWaypointList(List<Waypoint> waypoints, List<Track> tracks) {
        final List<Waypoint> completeList = new ArrayList<>();
        List<List<Waypoint>> filteredTracks = new ArrayList<>();

        if (tracks != null) {
            Long timeRangeEnd;
            Long timeRangeStart;
            if (!filters.showAllWaypoints()) {
                // Figure out what the most recent time is given the filtered
                // waypoints and the tracks.
                timeRangeEnd = getMostRecent(waypoints, tracks);
                timeRangeStart = timeRangeEnd - (86400 * filters.getMostRecentNumDays());

                completeList.addAll(getWaypointsInRange(timeRangeStart, timeRangeEnd, waypoints));
                
                filteredTracks = getTracksInRange(timeRangeStart, timeRangeEnd, tracks);
                for (List<Waypoint> filteredTrack : filteredTracks) {
                    completeList.addAll(filteredTrack);<commit_msg>Fixed date filter NPE when no waypoints or tracks have timestamps<commit_after>/*
 * Autopsy Forensic Browser
 *
 * Copyright 2019-2020 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> 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.sleuthkit.autopsy.geolocation;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import javafx.util.Pair;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.geolocation.datamodel.GeoLocationDataException;
import org.sleuthkit.autopsy.geolocation.datamodel.Track;
import org.sleuthkit.autopsy.geolocation.datamodel.Waypoint;
import org.sleuthkit.autopsy.geolocation.datamodel.WaypointBuilder;
import org.sleuthkit.datamodel.BlackboardArtifact.ARTIFACT_TYPE;

/**
 * The business logic for filtering waypoints.
 */
abstract class AbstractWaypointFetcher implements WaypointBuilder.WaypointFilterQueryCallBack {

    private static final Logger logger = Logger.getLogger(AbstractWaypointFetcher.class.getName());

    private final GeoFilterPanel.GeoFilter filters;

    /**
     * Constructs the Waypoint Runner
     *
     * @param filters
     */
    AbstractWaypointFetcher(GeoFilterPanel.GeoFilter filters) {
        this.filters = filters;
    }

    /**
     * Gets the waypoints based in the current GeoFilter.
     *
     * This function kicks off a process that will send with
     * handleFilteredWaypointSet being called. Subclasses must implement
     * handleFitleredWayoiintSet to get the final results.
     *
     * @throws GeoLocationDataException
     */
    void getWaypoints() throws GeoLocationDataException {
        Case currentCase = Case.getCurrentCase();
        WaypointBuilder.getAllWaypoints(currentCase.getSleuthkitCase(),
                filters.getDataSources(),
                filters.getArtifactTypes(),
                filters.showAllWaypoints(),
                filters.getMostRecentNumDays(),
                filters.showWaypointsWithoutTimeStamp(),
                this);

    }

    /**
     * Called after all of the MapWaypoints are created from all of the
     * TSK_GPS_XXX objects.
     *
     * @param mapWaypoints List of filtered MapWaypoints.
     */
    abstract void handleFilteredWaypointSet(Set<MapWaypoint> mapWaypoints, List<Set<MapWaypoint>> tracks);

    @Override
    public void process(List<Waypoint> waypoints) {
        List<Track> tracks = new ArrayList<>();
        if (filters.getArtifactTypes().contains(ARTIFACT_TYPE.TSK_GPS_TRACK)) {
            try {
                tracks = Track.getTracks(Case.getCurrentCase().getSleuthkitCase(), filters.getDataSources());
            } catch (GeoLocationDataException ex) {
                logger.log(Level.WARNING, "Exception thrown while retrieving list of Tracks", ex);
            }
        }
        Pair<List<Waypoint>, List<List<Waypoint>>> waypointsAndTracks = createWaypointList(waypoints, tracks);
        
        final Set<MapWaypoint> pointSet = MapWaypoint.getWaypoints(waypointsAndTracks.getKey());
        final List<Set<MapWaypoint>> trackSets = new ArrayList<>();
        for (List<Waypoint> t : waypointsAndTracks.getValue()) {
            trackSets.add(MapWaypoint.getWaypoints(t));
        }

        handleFilteredWaypointSet(pointSet, trackSets);
    }

    /**
     * Returns a complete list of waypoints including the tracks. Takes into
     * account the current filters and includes waypoints as approprate.
     *
     * @param waypoints List of waypoints
     * @param tracks    List of tracks
     *
     * @return A list of waypoints including the tracks based on the current
     *         filters.
     */
    private Pair<List<Waypoint>, List<List<Waypoint>>> createWaypointList(List<Waypoint> waypoints, List<Track> tracks) {
        final List<Waypoint> completeList = new ArrayList<>();
        List<List<Waypoint>> filteredTracks = new ArrayList<>();

        if (tracks != null) {
            Long timeRangeEnd;
            Long timeRangeStart;
            if (!filters.showAllWaypoints()) {
                // Figure out what the most recent time is given the filtered
                // waypoints and the tracks.
                timeRangeEnd = getMostRecent(waypoints, tracks);
                if (timeRangeEnd != null) {
                    timeRangeStart = timeRangeEnd - (86400 * filters.getMostRecentNumDays());

                    completeList.addAll(getWaypointsInRange(timeRangeStart, timeRangeEnd, waypoints));

                    filteredTracks = getTracksInRange(timeRangeStart, timeRangeEnd, tracks);
                    for (List<Waypoint> filteredTrack : filteredTracks) {
                        completeList.addAll(filteredTrack);
                    }
{'fraction_non_alphanumeric': 0.08555133079847908, 'fraction_numerical': 0.003231939163498099, 'mean_word_length': 2.9389741669786598, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4671405', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2798, 'n_words': 756}