OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 
174 ilm 16
import org.openconcerto.utils.io.JSONAble;
17
import org.openconcerto.utils.io.JSONConverter;
18
 
80 ilm 19
import java.io.Externalizable;
20
import java.io.IOException;
21
import java.io.ObjectInput;
22
import java.io.ObjectOutput;
23
import java.util.ArrayList;
24
import java.util.Collections;
25
import java.util.List;
26
 
94 ilm 27
import net.minidev.json.JSONArray;
28
import net.minidev.json.JSONObject;
29
 
93 ilm 30
public class RowsBulk implements Externalizable, JSONAble {
80 ilm 31
 
32
    private List<Row> rows;
83 ilm 33
    private int offset;
34
    private int total;
142 ilm 35
    private boolean remove;
80 ilm 36
 
37
    public RowsBulk() {// Serialization
38
    }
39
 
94 ilm 40
    public RowsBulk(final JSONObject json) {
41
        this.fromJSON(json);
42
    }
43
 
142 ilm 44
    public RowsBulk(List<Row> rows, int offset, int total, final boolean remove) {
80 ilm 45
        this.rows = rows;
83 ilm 46
        this.offset = offset;
47
        this.total = total;
142 ilm 48
        this.remove = remove;
80 ilm 49
    }
50
 
51
    // Sending by column : size gain is 5%
52
    @Override
53
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
54
        int rowCount = in.readInt();
55
        if (rowCount == 0) {
93 ilm 56
            this.rows = Collections.emptyList();
80 ilm 57
        } else {
156 ilm 58
            this.rows = new ArrayList<>(rowCount);
80 ilm 59
            int columnCount = in.readByte();
60
            // id
61
            for (int j = 0; j < rowCount; j++) {
156 ilm 62
                Row row = new Row((Number) in.readObject());
80 ilm 63
                this.rows.add(row);
64
            }
65
 
66
            for (int i = 0; i < columnCount; i++) {
67
                for (int j = 0; j < rowCount; j++) {
68
                    Object v = in.readObject();
69
                    this.rows.get(j).addValue(v);
70
                }
71
 
72
            }
73
 
74
        }
83 ilm 75
        this.offset = in.readInt();
76
        this.total = in.readInt();
142 ilm 77
        this.remove = in.readBoolean();
80 ilm 78
    }
79
 
80
    @Override
81
    public void writeExternal(ObjectOutput out) throws IOException {
82
        // nb rows
93 ilm 83
        int rowCount = this.rows.size();
80 ilm 84
        out.writeInt(rowCount);
85
        // content
156 ilm 86
        if (!this.rows.isEmpty()) {
80 ilm 87
            // nbcols
93 ilm 88
            int columnCount = this.rows.get(0).getValues().size();
80 ilm 89
            out.writeByte(columnCount);
90
            // ids
91
            for (int j = 0; j < rowCount; j++) {
93 ilm 92
                Row row = this.rows.get(j);
142 ilm 93
                out.writeObject(row.getId());
80 ilm 94
 
95
            }
96
 
97
            // send cols by cols
98
            for (int i = 0; i < columnCount; i++) {
99
 
100
                for (int j = 0; j < rowCount; j++) {
93 ilm 101
                    Row row = this.rows.get(j);
80 ilm 102
                    Object v = row.getValues().get(i);
103
                    out.writeObject(v);
104
                }
105
 
106
            }
107
 
108
        }
93 ilm 109
        out.writeInt(this.offset);
110
        out.writeInt(this.total);
142 ilm 111
        out.writeBoolean(this.remove);
80 ilm 112
    }
113
 
142 ilm 114
    public final List<Row> getRows() {
80 ilm 115
        return this.rows;
116
    }
117
 
142 ilm 118
    public final int getOffset() {
93 ilm 119
        return this.offset;
83 ilm 120
    }
121
 
142 ilm 122
    public final int getTotal() {
93 ilm 123
        return this.total;
83 ilm 124
    }
93 ilm 125
 
142 ilm 126
    public final boolean isRemove() {
127
        return this.remove;
128
    }
129
 
93 ilm 130
    @Override
94 ilm 131
    public JSONObject toJSON() {
132
        final JSONObject result = new JSONObject();
133
        result.put("class", "RowsBulk");
134
        result.put("rows", JSONConverter.getJSON(this.rows));
135
        result.put("offset", this.offset);
136
        result.put("total", this.total);
142 ilm 137
        result.put("remove", this.remove);
94 ilm 138
        return result;
139
    }
93 ilm 140
 
94 ilm 141
    @Override
142
    public void fromJSON(final JSONObject json) {
142 ilm 143
        this.offset = JSONConverter.getParameterFromJSON(json, "offset", Integer.class);
144
        this.total = JSONConverter.getParameterFromJSON(json, "total", Integer.class);
93 ilm 145
 
142 ilm 146
        final JSONArray jsonRows = JSONConverter.getParameterFromJSON(json, "rows", JSONArray.class);
174 ilm 147
        this.rows = new ArrayList<>();
94 ilm 148
        if (jsonRows != null) {
149
            for (final Object o : jsonRows) {
142 ilm 150
                this.rows.add(new Row(JSONConverter.getObjectFromJSON(o, JSONObject.class)));
94 ilm 151
            }
152
        }
93 ilm 153
    }
142 ilm 154
 
155
    @Override
156
    public String toString() {
174 ilm 157
        return this.rows.size() + " rows at offset " + this.offset + " (total:" + this.total + ") remove:" + this.remove;
142 ilm 158
    }
80 ilm 159
}