<commit_before>/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2011 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.web.planner;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.libreplan.business.labels.entities.Label;
import org.libreplan.business.planner.entities.GenericResourceAllocation;
import org.libreplan.business.planner.entities.ResourceAllocation;
import org.libreplan.business.planner.entities.SpecificResourceAllocation;
import org.libreplan.business.planner.entities.TaskElement;
import org.libreplan.business.resources.entities.Criterion;
import org.libreplan.business.resources.entities.Resource;
import org.libreplan.web.common.components.finders.FilterPair;
import org.libreplan.web.common.components.finders.TaskElementFilterEnum;
import org.zkoss.ganttz.IPredicate;

/**
 * Checks if {@link TaskElement} matches with the different filters.
 *
 * @author <NAME> <<EMAIL>>
 */
public class TaskElementPredicate implements IPredicate {

    private List<FilterPair> filters;

    private Date startDate;

    private Date finishDate;

    private String name;

    private boolean ignoreLabelsInheritance;

    public TaskElementPredicate(List<FilterPair> filters, Date startDate,
            Date finishDate, String name, boolean ignoreLabelsInheritance) {
        this.filters = filters;
        this.startDate = startDate;
        this.finishDate = finishDate;
        this.name = name;
        this.ignoreLabelsInheritance = ignoreLabelsInheritance;
    }

    @Override
    public boolean accepts(Object object) {
        final TaskElement taskElement = (TaskElement) object;
        return accepts(taskElement) || accepts(taskElement.getAllChildren());
    }

    private boolean accepts(TaskElement taskElement) {
        if (taskElement == null) {
            return false;
        }
        if (acceptFilters(taskElement) && acceptFiltersDates(taskElement)
                && acceptFilterName(taskElement)) {
            return true;
        }
        return false;
    }

    private boolean accepts(List<TaskElement> taskElements) {
        for (TaskElement taskElement : taskElements) {
            if (accepts(taskElement)) {
                return true;
            }
        }
        return false;
    }

    private boolean acceptFilters(TaskElement taskElement) {
        if ((filters == null) || (filters.isEmpty())) {
            return true;
        }
        for (FilterPair filter : filters) {
            if (!acceptFilter(filter, taskElement)) {
                return false;
            }
        }
        return true;
    }

    private boolean acceptFilter(FilterPair filter, TaskElement taskElement) {
        switch ((TaskElementFilterEnum) filter.getType()) {
        case Criterion:
            return acceptCriterion(filter, taskElement);
        case Resource:
            return acceptResource(filter, taskElement);
        case Label:
            return acceptLabel(filter, taskElement);
        }
        return false;
    }

    private boolean acceptCriterion(FilterPair filter, TaskElement taskElement) {
        Criterion filterCriterion = (Criterion) filter.getValue();
        return existCriterionInTaskElementResourceAllocations(filterCriterion,
                taskElement);
    }

    private boolean existCriterionInTaskElementResourceAllocations(
            Criterion filterCriterion, TaskElement taskElement) {
        for (ResourceAllocation<?> each : taskElement
                .getAllResourceAllocations()) {
            if (acceptsCriterionInResourceAllocation(filterCriterion, each)) {
                return true;
            }
        }
        return false;
    }

    private boolean acceptsCriterionInResourceAllocation(
            Criterion filterCriterion, ResourceAllocation<?> resourceAllocation) {
        if (resourceAllocation instanceof GenericResourceAllocation) {
            Set<Criterion> criteria = ((GenericResourceAllocation) resourceAllocation)
                    .getCriterions();
            for (Criterion criterion : criteria) {
                if (criterion.getId().equals(filterCriterion.getId())) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean acceptResource(FilterPair filter, TaskElement taskElement) {
        Resource filterResource = (Resource) filter.getValue();
        return existResourceInTaskElementResourceAllocations(filterResource,
                taskElement);
    }

    private boolean existResourceInTaskElementResourceAllocations(
            Resource filterResource, TaskElement taskElement) {
        for (ResourceAllocation<?> each : taskElement
                .getAllResourceAllocations()) {
            if (acceptsResourceInResourceAllocation(filterResource, each)) {
                return true;
            }
        }
        return false;
    }

    private boolean acceptsResourceInResourceAllocation(
            Resource filterResource, ResourceAllocation<?> resourceAllocation) {
        if (resourceAllocation instanceof SpecificResourceAllocation) {
            Resource resource = ((SpecificResourceAllocation) resourceAllocation)
                    .getResource();
            if (resource.getId().equals(filterResource.getId())) {
                return true;
            }
        }
        return false;
    }

    private boolean acceptLabel(FilterPair filter, TaskElement taskElement) {
        Label filterLabel = (Label) filter.getValue();
        return existLabelInTaskElement(filterLabel, taskElement);
    }

    private boolean existLabelInTaskElement(Label filterLabel,
            TaskElement taskElement) {
        Set<Label> labels;
        if (ignoreLabelsInheritance) {
            labels = taskElement.getOrderElement().getLabels();
        } else {
            labels = taskElement.getOrderElement().getAllLabels();
        }
        for (Label label : labels) {<commit_msg>Bug #1466: Fix NPE when there are milestones in the planning<commit_after>/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2011 Igalia, S.L.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.web.planner;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.libreplan.business.labels.entities.Label;
import org.libreplan.business.orders.entities.OrderElement;
import org.libreplan.business.planner.entities.GenericResourceAllocation;
import org.libreplan.business.planner.entities.ResourceAllocation;
import org.libreplan.business.planner.entities.SpecificResourceAllocation;
import org.libreplan.business.planner.entities.TaskElement;
import org.libreplan.business.resources.entities.Criterion;
import org.libreplan.business.resources.entities.Resource;
import org.libreplan.web.common.components.finders.FilterPair;
import org.libreplan.web.common.components.finders.TaskElementFilterEnum;
import org.zkoss.ganttz.IPredicate;

/**
 * Checks if {@link TaskElement} matches with the different filters.
 *
 * @author <NAME> <<EMAIL>>
 */
public class TaskElementPredicate implements IPredicate {

    private List<FilterPair> filters;

    private Date startDate;

    private Date finishDate;

    private String name;

    private boolean ignoreLabelsInheritance;

    public TaskElementPredicate(List<FilterPair> filters, Date startDate,
            Date finishDate, String name, boolean ignoreLabelsInheritance) {
        this.filters = filters;
        this.startDate = startDate;
        this.finishDate = finishDate;
        this.name = name;
        this.ignoreLabelsInheritance = ignoreLabelsInheritance;
    }

    @Override
    public boolean accepts(Object object) {
        final TaskElement taskElement = (TaskElement) object;
        return accepts(taskElement) || accepts(taskElement.getAllChildren());
    }

    private boolean accepts(TaskElement taskElement) {
        if (taskElement == null) {
            return false;
        }
        if (acceptFilters(taskElement) && acceptFiltersDates(taskElement)
                && acceptFilterName(taskElement)) {
            return true;
        }
        return false;
    }

    private boolean accepts(List<TaskElement> taskElements) {
        for (TaskElement taskElement : taskElements) {
            if (accepts(taskElement)) {
                return true;
            }
        }
        return false;
    }

    private boolean acceptFilters(TaskElement taskElement) {
        if ((filters == null) || (filters.isEmpty())) {
            return true;
        }
        for (FilterPair filter : filters) {
            if (!acceptFilter(filter, taskElement)) {
                return false;
            }
        }
        return true;
    }

    private boolean acceptFilter(FilterPair filter, TaskElement taskElement) {
        switch ((TaskElementFilterEnum) filter.getType()) {
        case Criterion:
            return acceptCriterion(filter, taskElement);
        case Resource:
            return acceptResource(filter, taskElement);
        case Label:
            return acceptLabel(filter, taskElement);
        }
        return false;
    }

    private boolean acceptCriterion(FilterPair filter, TaskElement taskElement) {
        Criterion filterCriterion = (Criterion) filter.getValue();
        return existCriterionInTaskElementResourceAllocations(filterCriterion,
                taskElement);
    }

    private boolean existCriterionInTaskElementResourceAllocations(
            Criterion filterCriterion, TaskElement taskElement) {
        for (ResourceAllocation<?> each : taskElement
                .getAllResourceAllocations()) {
            if (acceptsCriterionInResourceAllocation(filterCriterion, each)) {
                return true;
            }
        }
        return false;
    }

    private boolean acceptsCriterionInResourceAllocation(
            Criterion filterCriterion, ResourceAllocation<?> resourceAllocation) {
        if (resourceAllocation instanceof GenericResourceAllocation) {
            Set<Criterion> criteria = ((GenericResourceAllocation) resourceAllocation)
                    .getCriterions();
            for (Criterion criterion : criteria) {
                if (criterion.getId().equals(filterCriterion.getId())) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean acceptResource(FilterPair filter, TaskElement taskElement) {
        Resource filterResource = (Resource) filter.getValue();
        return existResourceInTaskElementResourceAllocations(filterResource,
                taskElement);
    }

    private boolean existResourceInTaskElementResourceAllocations(
            Resource filterResource, TaskElement taskElement) {
        for (ResourceAllocation<?> each : taskElement
                .getAllResourceAllocations()) {
            if (acceptsResourceInResourceAllocation(filterResource, each)) {
                return true;
            }
        }
        return false;
    }

    private boolean acceptsResourceInResourceAllocation(
            Resource filterResource, ResourceAllocation<?> resourceAllocation) {
        if (resourceAllocation instanceof SpecificResourceAllocation) {
            Resource resource = ((SpecificResourceAllocation) resourceAllocation)
                    .getResource();
            if (resource.getId().equals(filterResource.getId())) {
                return true;
            }
        }
        return false;
    }

    private boolean acceptLabel(FilterPair filter, TaskElement taskElement) {
        Label filterLabel = (Label) filter.getValue();
        return existLabelInTaskElement(filterLabel, taskElement);
    }

    private boolean existLabelInTaskElement(Label filterLabel,
            TaskElement taskElement) {
        OrderElement orderElement = taskElement.getOrderElement();
        if (orderElement ==  null) {
            return false;
        }

        Set<Label> labels;
        if (ignoreLabelsInheritance) {
            labels = orderElement.getLabels();
        } else {
            labels = orderElement.getAllLabels();
        }
        for (Label label : labels) {
{'fraction_non_alphanumeric': 0.07796810395747195, 'fraction_numerical': 0.0010336680448907265, 'mean_word_length': 2.6737184703010577, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '2773389', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3294, 'n_words': 884}