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 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.pos.model;
15
 
16
import java.util.ArrayList;
65 ilm 17
import java.util.Collections;
18
import java.util.Comparator;
182 ilm 19
import java.util.HashSet;
18 ilm 20
import java.util.List;
182 ilm 21
import java.util.Set;
18 ilm 22
 
23
public class Categorie {
144 ilm 24
    private static List<Categorie> topLevelCategories = new ArrayList<>();
25
    private static List<Categorie> allCategories = new ArrayList<>();
149 ilm 26
    private static List<Article> favoriteProducts = new ArrayList<>();
18 ilm 27
    private String name;
28
    // Sous catégories
144 ilm 29
    private List<Categorie> l = new ArrayList<>();
18 ilm 30
    // Articles dans cette categorie
182 ilm 31
    private Set<Article> articles = new HashSet<>();
18 ilm 32
    private Categorie parent;
149 ilm 33
    private boolean isUnknown = false;
18 ilm 34
 
35
    public Categorie(String string) {
36
        this(string, false);
37
    }
38
 
39
    public Categorie(String string, boolean top) {
40
        this.name = string;
65 ilm 41
        if (top) {
18 ilm 42
            topLevelCategories.add(this);
65 ilm 43
        }
83 ilm 44
        allCategories.add(this);
18 ilm 45
    }
46
 
149 ilm 47
    public void setUnknown() {
48
        this.isUnknown = true;
49
    }
50
 
51
    public boolean isUnknown() {
52
        return isUnknown;
53
    }
54
 
18 ilm 55
    @Override
56
    public String toString() {
57
        return name;
58
    }
59
 
60
    public void add(Categorie s) {
61
        l.add(s);
62
        s.setParentCategorie(this);
63
    }
64
 
65
    private void setParentCategorie(Categorie categorie) {
66
        this.parent = categorie;
67
    }
68
 
69
    public Categorie getParent() {
70
        return parent;
71
    }
72
 
73
    void addArticle(Article a) {
182 ilm 74
        this.articles.add(a);
18 ilm 75
    }
76
 
77
    public static List<Categorie> getTopLevelCategories() {
78
        return topLevelCategories;
79
    }
80
 
83 ilm 81
    public static List<Categorie> getAllCategories() {
82
        return allCategories;
83
    }
84
 
18 ilm 85
    public List<Categorie> getSubCategories() {
86
        return l;
87
    }
88
 
89
    public String getName() {
90
        return name;
91
    }
92
 
93
    public List<Article> getArticles() {
182 ilm 94
        int size = articles.size();
95
        for (Categorie c : l) {
96
            size += c.getArticles().size();
97
        }
98
        final List<Article> result = new ArrayList<>(size);
18 ilm 99
        result.addAll(articles);
100
        for (Categorie c : l) {
101
            result.addAll(c.getArticles());
102
        }
65 ilm 103
        Collections.sort(result, new Comparator<Article>() {
104
            @Override
105
            public int compare(Article o1, Article o2) {
106
                return o1.getName().compareTo(o2.getName());
107
            }
108
        });
18 ilm 109
        return result;
110
    }
149 ilm 111
 
112
    public static void setFavoriteProducts(List<Article> products) {
113
        favoriteProducts = products;
114
    }
115
 
116
    public static List<Article> getFavoriteProducts() {
117
        return favoriteProducts;
118
    }
119
 
120
    public static void toggleFavoriteState(Article product) {
121
        if (favoriteProducts.contains(product)) {
122
            favoriteProducts.remove(product);
123
        } else {
124
            favoriteProducts.add(product);
125
        }
126
 
127
    }
18 ilm 128
}