OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Rev 132 | 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.io.Externalizable;
17
import java.io.IOException;
18
import java.io.ObjectInput;
19
import java.io.ObjectOutput;
94 ilm 20
import java.util.ArrayList;
21
import java.util.List;
80 ilm 22
 
94 ilm 23
import org.openconcerto.utils.io.JSONAble;
24
import org.openconcerto.utils.io.JSONConverter;
25
import net.minidev.json.JSONArray;
26
import net.minidev.json.JSONObject;
27
 
28
public class RowSelectionSpec implements Externalizable, JSONAble {
80 ilm 29
    private String tableId;
30
 
94 ilm 31
    private List<Long> ids;
32
 
80 ilm 33
    /**
34
     * Define selected ids of a table. ids are ids from selected lines
94 ilm 35
     */
80 ilm 36
    public RowSelectionSpec() {
37
        // Serialization
38
    }
39
 
94 ilm 40
    public RowSelectionSpec(final JSONObject json) {
41
        this.fromJSON(json);
42
    }
43
 
44
    public RowSelectionSpec(String tableId, List<Long> ids) {
45
        this.init(tableId, ids);
46
    }
47
 
48
    public RowSelectionSpec(String tableId) {
49
        this.init(tableId, null);
50
    }
51
 
52
    private void init(final String tableId, final List<Long> ids) {
80 ilm 53
        this.tableId = tableId;
94 ilm 54
        if(ids != null) {
55
            this.ids = ids;
56
        } else {
57
            this.ids = new ArrayList<Long>();
58
        }
80 ilm 59
    }
60
 
94 ilm 61
    public List<Long> getIds() {
62
        return this.ids;
80 ilm 63
    }
64
 
65
    public String getTableId() {
94 ilm 66
        return this.tableId;
80 ilm 67
    }
68
 
69
    @Override
70
    public String toString() {
94 ilm 71
        final StringBuilder r = new StringBuilder("RowSelectionSpec: ").append(this.tableId).append(" : ");
72
        final int idsSize = this.ids.size();
73
        for (int i = 0; i < idsSize; i++) {
74
            if (i < idsSize - 1) {
75
                r.append(this.ids.get(i)).append(", ");
80 ilm 76
            } else {
94 ilm 77
                r.append(this.ids.get(i));
80 ilm 78
            }
79
        }
94 ilm 80
        return r.toString();
80 ilm 81
    }
82
 
83
    @Override
84
    public void writeExternal(ObjectOutput out) throws IOException {
85
        try {
94 ilm 86
            out.writeUTF(this.tableId);
87
            out.writeObject(this.ids);
80 ilm 88
 
89
        } catch (Exception e) {
90
            e.printStackTrace();
91
            throw new IOException(e);
92
        }
93
    }
94
 
95
    @Override
96
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
94 ilm 97
        this.tableId = in.readUTF();
98
        this.ids = (List<Long>) in.readObject();
80 ilm 99
 
100
    }
101
 
94 ilm 102
    @Override
103
    public JSONObject toJSON() {
104
        final JSONObject json = new JSONObject();
105
        json.put("class", "RowSelectionSpec");
106
        json.put("table-id", this.tableId);
107
        json.put("ids", this.ids);
108
        return json;
109
    }
110
 
111
    @Override
112
    public void fromJSON(JSONObject json) {
113
        this.tableId = (String) JSONConverter.getParameterFromJSON(json, "table-id", String.class);
114
        final JSONArray jsonIds = (JSONArray) JSONConverter.getParameterFromJSON(json, "ids", JSONArray.class);
115
        this.ids = new ArrayList<Long>();
116
        for (final Object jsonId : jsonIds) {
117
            this.ids.add((Long) JSONConverter.getObjectFromJSON(jsonId, Long.class));
118
        }
119
    }
120
 
80 ilm 121
}