OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | Rev 156 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
80 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 java.util.ArrayList;
17
import java.util.List;
18
 
94 ilm 19
import org.openconcerto.utils.io.JSONConverter;
20
import org.openconcerto.utils.io.Transferable;
21
import net.minidev.json.JSONArray;
22
import net.minidev.json.JSONObject;
23
 
93 ilm 24
public class TableContent implements Transferable {
80 ilm 25
    private static final long serialVersionUID = 3648381615123520834L;
94 ilm 26
    private String tableId;
80 ilm 27
    private List<Row> rows;
28
 
29
    public TableContent() {
30
        // Serialization
31
    }
32
 
94 ilm 33
    public TableContent(final String tableId) {
142 ilm 34
        this.init(tableId, new ArrayList<Row>());
94 ilm 35
    }
36
 
37
    public TableContent(final String tableId, final List<Row> rows) {
38
        this.init(tableId, rows);
39
    }
40
 
41
    public TableContent(final JSONObject json) {
42
        this.fromJSON(json);
43
    }
44
 
142 ilm 45
    private final void init(final String tableId, final List<Row> rows) {
94 ilm 46
        this.tableId = tableId;
142 ilm 47
        this.rows = rows;
94 ilm 48
    }
49
 
142 ilm 50
    public final String getTableId() {
94 ilm 51
        return this.tableId;
52
    }
53
 
142 ilm 54
    public final void setTableId(final String tableId) {
94 ilm 55
        this.tableId = tableId;
56
    }
57
 
142 ilm 58
    public final synchronized Row getRow(final int index) {
59
        return this.rows.get(index);
80 ilm 60
    }
61
 
142 ilm 62
    public final synchronized int getRowsCount() {
63
        return this.rows.size();
64
    }
65
 
66
    public final synchronized boolean addRow(final Row row) {
67
        return this.rows.add(row);
68
    }
69
 
70
    public final synchronized Row setRow(final int index, final Row row) {
71
        return this.rows.set(index, row);
72
    }
73
 
74
    public final synchronized Row removeRow(final int index) {
75
        return this.rows.remove(index);
76
    }
77
 
78
    public final synchronized boolean removeRow(final Row row) {
79
        return this.rows.remove(row);
80
    }
81
 
82
    public final synchronized void setRows(List<Row> rows) {
80 ilm 83
        this.rows = rows;
84
    }
85
 
142 ilm 86
    public final synchronized void clearRows() {
87
        this.rows.clear();
88
    }
89
 
90
    /**
91
     * @return a copy of the list
92
     */
93
    public final synchronized List<Row> getRows(){
94
        return new ArrayList<Row>(this.rows);
95
    }
96
 
94 ilm 97
    @Override
142 ilm 98
    public synchronized String toString() {
99
        return "TableContent of " + this.tableId + " lines count : " + this.getRowsCount();
80 ilm 100
    }
101
 
102
    @Override
142 ilm 103
    public synchronized JSONObject toJSON() {
94 ilm 104
        final JSONObject result = new JSONObject();
105
        result.put("class", "TableContent");
106
        result.put("table-id", this.tableId);
107
        result.put("rows", JSONConverter.getJSON(this.rows));
108
        return result;
80 ilm 109
    }
93 ilm 110
 
111
    @Override
142 ilm 112
    public synchronized void fromJSON(final JSONObject json) {
113
        this.tableId = JSONConverter.getParameterFromJSON(json, "table-id", String.class);
114
        final JSONArray jsonRows = JSONConverter.getParameterFromJSON(json, "rows", JSONArray.class);
94 ilm 115
        if (jsonRows != null) {
142 ilm 116
            final List<Row> listRows = new ArrayList<Row>();
94 ilm 117
            for (final Object o : jsonRows) {
142 ilm 118
                listRows.add(new Row(JSONConverter.getObjectFromJSON(o, JSONObject.class)));
94 ilm 119
            }
142 ilm 120
            this.setRows(listRows);
94 ilm 121
        }
93 ilm 122
    }
80 ilm 123
}