OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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