Search in sources :

Example 1 with TeamMetricProcessor

use of com.cpjd.roblu.ui.teamsSorting.TeamMetricProcessor in project Roblu by wdavies973.

the class LoadTeamsTask method run.

/**
 * Performs loading, sorting, searching, etc.
 */
@Override
public void run() {
    /*
         * Verify that the event actually exists
         */
    if (ioWeakReference.get() == null || !ioWeakReference.get().doesEventExist(eventID)) {
        quit();
        return;
    }
    /*
         * Next, take care of "reload", if reload is true, teams must be reloaded from the file system
         */
    if (teams == null || teams.size() == 0) {
        RTeam[] local = ioWeakReference.get().loadTeams(eventID);
        // no teams were found locally
        if (local == null || local.length == 0) {
            Log.d("RBS", "LoadTeamsTask found no teams locally stored.");
            teams = null;
            listener.teamsListLoaded(null, false);
            quit();
            return;
        }
        // teams were found locally, so attach them to the teams array
        teams = new ArrayList<>(Arrays.asList(local));
        Log.d("RBS", "LoadTeamsTask loaded " + teams.size() + " teams");
    }
    // threads) finish.
    for (RTeam team : teams) {
        if (team == null) {
            quit();
            return;
        }
    }
    /*
         * Next, each RTeam contains a variable named filter, it's a sort of "helper" variable.
         * Each RTeam has a compareTo method that will allow it to easily be sorted, the filter variable within
         * the team will let the team now how it's to behave when compareTo() is called in it
         */
    for (RTeam team : teams) team.setFilter(filter);
    /*
         * If filter is ANYTHING but SORT_TYPE.SEARCH or SORT_TYPE.CUSTOM_SORT, just run a standard sort.
         * And actually, that also means that we're done with this method, return the teams!
         */
    if (filter == TeamsView.SORT_TYPE.LAST_EDIT || filter == TeamsView.SORT_TYPE.NUMERICAL || filter == TeamsView.SORT_TYPE.ALPHABETICAL) {
        // reset the filter tag
        for (RTeam team : this.teams) team.setFilterTag("");
        try {
            Collections.sort(teams);
            if (filter == TeamsView.SORT_TYPE.LAST_EDIT)
                Collections.reverse(teams);
            listener.teamsListLoaded(this.teams, false);
            quit();
        } catch (Exception e) {
            Log.d("RBS", "A problem occurred while attempting to sort teams with SORT_TYPE = " + filter);
        }
    } else /*
         * Okay, the user actually wants to search, possibly.
         * Let's handle PURE searching next (no custom sort)
         */
    if (filter == TeamsView.SORT_TYPE.SEARCH) {
        for (RTeam team : teams) {
            team.setCustomRelevance(0);
            team.setFilterTag("");
            // assign search relevance to the team
            processTeamForSearch(team);
        }
        try {
            Collections.sort(this.teams);
            Collections.reverse(this.teams);
            listener.teamsListLoaded(this.teams, true);
            quit();
        } catch (Exception e) {
            Log.d("RBS", "A problem occurred while attempting to sort teams with SORT_TYPE = " + filter);
        }
    } else /*
         * Alright, the user actually wants to custom sort, so, let's custom sort, also
         * check to see if query is not null, because searching is also allowing during custom sorting
         */
    if (filter == TeamsView.SORT_TYPE.CUSTOM_SORT) {
        /*
             * Elements Processor is a powerful helper utility. How it works is the following:
             * -Elements Processor will be passed an input (int method defined by TeamMetricProcessor.PROCESS_METHOD, and ID of the metric to analyze
             * -Elements Processor will attach a string to filterTag and a relevance to customRelevance and return the team object, then just sort
             */
        TeamMetricProcessor teamMetricProcessor = new TeamMetricProcessor();
        /*
             * TeamMetricProcessor requires all inputted teams to be synced with the form, so let's do that
             */
        RForm form = ioWeakReference.get().loadForm(eventID);
        for (RTeam team : teams) {
            team.verify(form);
            ioWeakReference.get().saveTeam(eventID, team);
        }
        /*
             * Decode the method process and metric ID from the customSortToken string
             */
        int methodID = Integer.parseInt(customSortToken.split(":")[0]);
        int metricID = Integer.parseInt(customSortToken.split(":")[1]);
        Log.d("RBS", "Custom sorting with methodID: " + methodID + ", metricID: " + metricID);
        /*
             * Make sure to set the extra "matchTitle" parameter if processMethod==PROCESS_METHOD.IN_MATCH
             */
        // for the "In match" option, zero relevance items should be hidden
        boolean shouldHideZeroRelevance = false;
        if (methodID == TeamMetricProcessor.PROCESS_METHOD.OTHER && metricID == TeamMetricProcessor.PROCESS_METHOD.OTHER_METHOD.IN_MATCH && customSortToken.split(":").length == 3) {
            teamMetricProcessor.setInMatchTitle(customSortToken.split(":")[2]);
            Log.d("RBS", "In match title: " + teamMetricProcessor.getInMatchTitle());
            shouldHideZeroRelevance = true;
        } else if (methodID == TeamMetricProcessor.PROCESS_METHOD.MATCHES && customSortToken.split(":").length == 3) {
            teamMetricProcessor.setInMatchTitle(customSortToken.split(":")[2]);
            Log.d("RBS", "In match title: " + teamMetricProcessor.getInMatchTitle());
        }
        /*
             * Next, perform the operation
             */
        for (RTeam team : teams) {
            team.setFilter(filter);
            // reset old filter tags
            team.setFilterTag("");
            // reset any old relevance
            team.setCustomRelevance(0);
            teamMetricProcessor.process(team, methodID, metricID);
        }
        /*
             * Finally, check to see if the user also wants to sort through the array
             */
        for (RTeam team : teams) processTeamForSearch(team);
        try {
            Collections.sort(teams);
            // don't reverse array if sorting by "In matches" since in matches uses numerical sub sort
            if (!(methodID == TeamMetricProcessor.PROCESS_METHOD.OTHER && metricID == TeamMetricProcessor.PROCESS_METHOD.OTHER_METHOD.IN_MATCH))
                Collections.reverse(teams);
            listener.teamsListLoaded(this.teams, shouldHideZeroRelevance);
            quit();
        } catch (Exception e) {
            Log.d("RBS", "A problem occurred while attempting to sort teams with SORT_TYPE = " + filter);
        }
    }
}
Also used : RForm(com.cpjd.roblu.models.RForm) RTeam(com.cpjd.roblu.models.RTeam) TeamMetricProcessor(com.cpjd.roblu.ui.teamsSorting.TeamMetricProcessor)

Aggregations

RForm (com.cpjd.roblu.models.RForm)1 RTeam (com.cpjd.roblu.models.RTeam)1 TeamMetricProcessor (com.cpjd.roblu.ui.teamsSorting.TeamMetricProcessor)1