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