OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.sales.pos.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Categorie {
    private static List<Categorie> topLevelCategories = new ArrayList<>();
    private static List<Categorie> allCategories = new ArrayList<>();
    private static List<Article> favoriteProducts = new ArrayList<>();
    private String name;
    // Sous catégories
    private List<Categorie> l = new ArrayList<>();
    // Articles dans cette categorie
    private Set<Article> articles = new HashSet<>();
    private Categorie parent;
    private boolean isUnknown = false;

    public Categorie(String string) {
        this(string, false);
    }

    public Categorie(String string, boolean top) {
        this.name = string;
        if (top) {
            topLevelCategories.add(this);
        }
        allCategories.add(this);
    }

    public void setUnknown() {
        this.isUnknown = true;
    }

    public boolean isUnknown() {
        return isUnknown;
    }

    @Override
    public String toString() {
        return name;
    }

    public void add(Categorie s) {
        l.add(s);
        s.setParentCategorie(this);
    }

    private void setParentCategorie(Categorie categorie) {
        this.parent = categorie;
    }

    public Categorie getParent() {
        return parent;
    }

    void addArticle(Article a) {
        this.articles.add(a);
    }

    public static List<Categorie> getTopLevelCategories() {
        return topLevelCategories;
    }

    public static List<Categorie> getAllCategories() {
        return allCategories;
    }

    public List<Categorie> getSubCategories() {
        return l;
    }

    public String getName() {
        return name;
    }

    public List<Article> getArticles() {
        int size = articles.size();
        for (Categorie c : l) {
            size += c.getArticles().size();
        }
        final List<Article> result = new ArrayList<>(size);
        result.addAll(articles);
        for (Categorie c : l) {
            result.addAll(c.getArticles());
        }
        Collections.sort(result, new Comparator<Article>() {
            @Override
            public int compare(Article o1, Article o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        return result;
    }

    public static void setFavoriteProducts(List<Article> products) {
        favoriteProducts = products;
    }

    public static List<Article> getFavoriteProducts() {
        return favoriteProducts;
    }

    public static void toggleFavoriteState(Article product) {
        if (favoriteProducts.contains(product)) {
            favoriteProducts.remove(product);
        } else {
            favoriteProducts.add(product);
        }

    }
}