OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | Rev 94 | 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
 
93 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
 
93 ilm 27
public class RowsBulk implements Externalizable, JSONAble {
80 ilm 28
 
29
    private List<Row> rows;
83 ilm 30
    private int offset;
31
    private int total;
80 ilm 32
 
33
    public RowsBulk() {// Serialization
34
    }
35
 
83 ilm 36
    public RowsBulk(List<Row> rows, int offset, int total) {
80 ilm 37
        this.rows = rows;
83 ilm 38
        this.offset = offset;
39
        this.total = total;
80 ilm 40
    }
41
 
42
    // Sending by column : size gain is 5%
43
    @Override
44
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
45
        int rowCount = in.readInt();
46
        if (rowCount == 0) {
93 ilm 47
            this.rows = Collections.emptyList();
80 ilm 48
        } else {
49
            this.rows = new ArrayList<Row>(rowCount);// colcount
50
            int columnCount = in.readByte();
51
            // id
52
            for (int j = 0; j < rowCount; j++) {
53
                Row row = new Row(in.readLong(), columnCount);
54
                this.rows.add(row);
55
            }
56
 
57
            for (int i = 0; i < columnCount; i++) {
58
                for (int j = 0; j < rowCount; j++) {
59
                    Object v = in.readObject();
60
                    this.rows.get(j).addValue(v);
61
                }
62
 
63
            }
64
 
65
        }
83 ilm 66
        this.offset = in.readInt();
67
        this.total = in.readInt();
80 ilm 68
    }
69
 
70
    @Override
71
    public void writeExternal(ObjectOutput out) throws IOException {
72
        // nb rows
93 ilm 73
        int rowCount = this.rows.size();
80 ilm 74
        out.writeInt(rowCount);
75
        // content
93 ilm 76
        if (this.rows.size() > 0) {
80 ilm 77
            // nbcols
93 ilm 78
            int columnCount = this.rows.get(0).getValues().size();
80 ilm 79
            out.writeByte(columnCount);
80
            // ids
81
            for (int j = 0; j < rowCount; j++) {
93 ilm 82
                Row row = this.rows.get(j);
80 ilm 83
                out.writeLong(row.getId());
84
 
85
            }
86
 
87
            // send cols by cols
88
            for (int i = 0; i < columnCount; i++) {
89
 
90
                for (int j = 0; j < rowCount; j++) {
93 ilm 91
                    Row row = this.rows.get(j);
80 ilm 92
                    Object v = row.getValues().get(i);
93
                    out.writeObject(v);
94
                }
95
 
96
            }
97
 
98
        }
93 ilm 99
        out.writeInt(this.offset);
100
        out.writeInt(this.total);
80 ilm 101
    }
102
 
103
    public List<Row> getRows() {
104
        return this.rows;
105
    }
106
 
83 ilm 107
    public int getOffset() {
93 ilm 108
        return this.offset;
83 ilm 109
    }
110
 
111
    public int getTotal() {
93 ilm 112
        return this.total;
83 ilm 113
    }
93 ilm 114
 
115
    @Override
116
    public String toJSON() {
117
        final StringBuilder result = new StringBuilder("{");
118
 
119
        result.append("\"rows\":" + JSONconverter.getJSON(this.rows) + ",");
120
        result.append("\"offset\":" + JSONconverter.getJSON(this.offset) + ",");
121
        result.append("\"total\":" + JSONconverter.getJSON(this.total));
122
 
123
        result.append("}");
124
        return result.toString();
125
    }
80 ilm 126
}