OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
174 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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.ui.light;
15
 
16
import org.openconcerto.utils.io.Transferable;
17
 
18
import java.util.ArrayList;
19
import java.util.List;
20
 
21
import net.minidev.json.JSONArray;
22
import net.minidev.json.JSONObject;
23
 
24
public class TableSearchParameter implements Transferable {
25
    private String id;
26
    private String label;
27
    private List<TableSearchParameterType> types = new ArrayList<>();
28
 
29
    public TableSearchParameter() {
30
        // For serialization
31
    }
32
 
33
    public String getId() {
34
        return this.id;
35
    }
36
 
37
    public String getLabel() {
38
        return this.label;
39
    }
40
 
41
    public List<TableSearchParameterType> getTypes() {
42
        return this.types;
43
    }
44
 
45
    public TableSearchParameter(String id, String label) {
46
        this.id = id;
47
        this.label = label;
48
    }
49
 
50
    public void add(TableSearchParameterType type) {
51
        this.types.add(type);
52
    }
53
 
54
    public void remove(TableSearchParameterType type) {
55
        this.types.remove(type);
56
    }
57
 
58
    @Override
59
    public JSONObject toJSON() {
60
        if (this.id == null) {
61
            throw new IllegalStateException("null id");
62
        }
63
        if (this.label == null) {
64
            throw new IllegalStateException("null label");
65
        }
66
        final JSONObject obj = new JSONObject();
67
        obj.put("id", this.id);
68
        obj.put("label", this.label);
69
        if (!this.types.isEmpty()) {
70
            final JSONArray array = new JSONArray();
71
            for (TableSearchParameterType type : this.types) {
72
                array.add(type.toJSON());
73
            }
74
            obj.put("types", array);
75
        }
76
        return obj;
77
    }
78
 
79
    @Override
80
    public void fromJSON(JSONObject json) {
81
        this.id = json.getAsString("id");
82
        this.label = json.getAsString("label");
83
        if (this.id == null) {
84
            throw new IllegalStateException("null id");
85
        }
86
        if (this.label == null) {
87
            throw new IllegalStateException("null label");
88
        }
89
        this.types.clear();
90
        if (json.containsKey("types")) {
91
            final JSONArray array = (JSONArray) json.get("types");
92
            for (Object object : array) {
93
                final TableSearchParameterType t = new TableSearchParameterType();
94
                t.fromJSON(((JSONObject) object));
95
                this.types.add(t);
96
            }
97
        }
98
 
99
    }
100
}