OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 174
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.ui.light;
14
 package org.openconcerto.ui.light;
15
 
15
 
16
import org.openconcerto.utils.io.JSONConverter;
16
import org.openconcerto.utils.io.JSONConverter;
17
import org.openconcerto.utils.io.Transferable;
17
import org.openconcerto.utils.io.Transferable;
18
 
18
 
19
import java.io.Externalizable;
-
 
20
import java.io.IOException;
19
import java.util.ArrayList;
21
import java.io.ObjectInput;
20
import java.util.List;
22
import java.io.ObjectOutput;
-
 
23
 
21
 
-
 
22
import net.minidev.json.JSONArray;
24
import net.minidev.json.JSONObject;
23
import net.minidev.json.JSONObject;
25
 
24
 
26
public class TableSpec implements Transferable, Externalizable {
25
public class TableSpec implements Transferable {
27
    private String id;
26
    private String id;
28
    private ColumnsSpec columns;
27
    private ColumnsSpec columns;
29
    private TableContent content;
28
    private TableContent content;
30
    private RowSelectionSpec selection;
29
    private RowSelectionSpec selection;
-
 
30
    private List<TableSearchParameter> searchParameters = new ArrayList<>();
31
    private SearchSpec search;
31
    private UserSearch search;
32
    private Boolean variableColumnsCount = false;
32
    private Boolean variableColumnsCount = false;
33
 
33
 
34
    public TableSpec() {
34
    public TableSpec() {
35
        // Serialization
35
        // Serialization
36
    }
36
    }
37
 
37
 
38
    public TableSpec(final String tableId, final RowSelectionSpec selection, final ColumnsSpec columns) {
38
    public TableSpec(final String tableId, final RowSelectionSpec selection, final ColumnsSpec columns) {
39
        this.id = tableId + ".spec";
39
        this.id = tableId + ".spec";
40
        if (selection == null) {
40
        if (selection == null) {
41
            throw new IllegalArgumentException("null RowSelectionSpec");
41
            throw new IllegalArgumentException("null RowSelectionSpec");
42
        }
42
        }
43
        if (columns == null) {
43
        if (columns == null) {
44
            throw new IllegalArgumentException("null ColumnsSpec");
44
            throw new IllegalArgumentException("null ColumnsSpec");
45
        }
45
        }
46
        this.selection = selection;
46
        this.selection = selection;
47
        this.columns = columns;
47
        this.columns = columns;
48
        this.content = new TableContent(tableId);
48
        this.content = new TableContent(tableId);
49
    }
49
    }
50
 
50
 
51
    public TableSpec(final JSONObject json) {
51
    public TableSpec(final JSONObject json) {
52
        this.fromJSON(json);
52
        this.fromJSON(json);
53
    }
53
    }
54
 
54
 
55
    public String getId() {
55
    public String getId() {
56
        return this.id;
56
        return this.id;
57
    }
57
    }
58
 
58
 
59
    public void setId(final String id) {
59
    public void setId(final String id) {
60
        this.id = id;
60
        this.id = id;
61
    }
61
    }
62
 
62
 
63
    public ColumnsSpec getColumns() {
63
    public ColumnsSpec getColumns() {
64
        return this.columns;
64
        return this.columns;
65
    }
65
    }
66
 
66
 
67
    public void setColumns(final ColumnsSpec columns) {
67
    public void setColumns(final ColumnsSpec columns) {
68
        this.columns = columns;
68
        this.columns = columns;
69
    }
69
    }
70
 
70
 
71
    public TableContent getContent() {
71
    public TableContent getContent() {
72
        return this.content;
72
        return this.content;
73
    }
73
    }
74
 
74
 
75
    public void setContent(final TableContent content) {
75
    public void setContent(final TableContent content) {
76
        this.content = content;
76
        this.content = content;
77
    }
77
    }
78
 
78
 
79
    public RowSelectionSpec getSelection() {
79
    public RowSelectionSpec getSelection() {
80
        return this.selection;
80
        return this.selection;
81
    }
81
    }
82
 
82
 
83
    public void setSelection(final RowSelectionSpec selection) {
83
    public void setSelection(final RowSelectionSpec selection) {
84
        this.selection = selection;
84
        this.selection = selection;
85
    }
85
    }
86
 
86
 
87
    public SearchSpec getSearch() {
87
    public UserSearch getSearch() {
88
        return this.search;
88
        return this.search;
89
    }
89
    }
90
 
90
 
91
    public void setSearch(final SearchSpec search) {
91
    public void setSearch(final UserSearch search) {
92
        this.search = search;
92
        this.search = search;
93
    }
93
    }
94
 
94
 
95
    public Boolean isVariableColumnsCount() {
95
    public Boolean isVariableColumnsCount() {
96
        return this.variableColumnsCount;
96
        return this.variableColumnsCount;
97
    }
97
    }
98
 
98
 
99
    public void setVariableColumnsCount(final Boolean variableColumnsCount) {
99
    public void setVariableColumnsCount(final Boolean variableColumnsCount) {
100
        this.variableColumnsCount = variableColumnsCount;
100
        this.variableColumnsCount = variableColumnsCount;
101
    }
101
    }
102
 
102
 
-
 
103
    public void addSearchParameter(TableSearchParameter p) {
-
 
104
        this.searchParameters.add(p);
-
 
105
    }
-
 
106
 
-
 
107
    public void removeSearchParameter(TableSearchParameter p) {
-
 
108
        this.searchParameters.remove(p);
-
 
109
    }
-
 
110
 
103
    @Override
111
    @Override
104
    public JSONObject toJSON() {
112
    public JSONObject toJSON() {
105
        final JSONObject result = new JSONObject();
113
        final JSONObject result = new JSONObject();
106
        result.put("class", "TableSpec");
114
        result.put("class", "TableSpec");
107
        result.put("id", this.id);
115
        result.put("id", this.id);
108
        if (this.columns != null) {
116
        if (this.columns != null) {
109
            result.put("columns", JSONConverter.getJSON(this.columns));
117
            result.put("columns", JSONConverter.getJSON(this.columns));
110
        }
118
        }
111
        if (this.content != null) {
119
        if (this.content != null) {
112
            result.put("content", JSONConverter.getJSON(this.content));
120
            result.put("content", JSONConverter.getJSON(this.content));
113
        }
121
        }
114
        if (this.selection != null) {
122
        if (this.selection != null) {
115
            result.put("selection", JSONConverter.getJSON(this.selection));
123
            result.put("selection", JSONConverter.getJSON(this.selection));
116
        }
124
        }
-
 
125
 
117
        if (this.search != null) {
126
        if (this.search != null) {
118
            result.put("search", JSONConverter.getJSON(this.search));
127
            result.put("search", JSONConverter.getJSON(this.search));
119
        }
128
        }
120
        if (this.variableColumnsCount) {
129
        if (this.variableColumnsCount) {
121
            result.put("variable-columns-count", JSONConverter.getJSON(true));
130
            result.put("variable-columns-count", JSONConverter.getJSON(true));
122
        }
131
        }
123
 
132
 
-
 
133
        if (!this.searchParameters.isEmpty()) {
-
 
134
            JSONArray a = new JSONArray();
-
 
135
            for (TableSearchParameter p : this.searchParameters) {
-
 
136
                a.add(p.toJSON());
-
 
137
            }
-
 
138
            result.put("search-params", a);
-
 
139
        }
124
        return result;
140
        return result;
125
    }
141
    }
126
 
142
 
127
    @Override
143
    @Override
128
    public void fromJSON(final JSONObject json) {
144
    public void fromJSON(final JSONObject json) {
129
        this.id = (String) JSONConverter.getParameterFromJSON(json, "id", String.class);
145
        this.id = json.getAsString("id");
130
 
146
 
131
        final JSONObject jsonColumns = (JSONObject) JSONConverter.getParameterFromJSON(json, "columns", JSONObject.class);
147
        final JSONObject jsonColumns = JSONConverter.getParameterFromJSON(json, "columns", JSONObject.class);
132
        if (jsonColumns != null) {
148
        if (jsonColumns != null) {
133
            this.columns = new ColumnsSpec(jsonColumns);
149
            this.columns = new ColumnsSpec(jsonColumns);
134
        }
150
        }
135
 
151
 
136
        final JSONObject jsonContent = (JSONObject) JSONConverter.getParameterFromJSON(json, "content", JSONObject.class);
152
        final JSONObject jsonContent = JSONConverter.getParameterFromJSON(json, "content", JSONObject.class);
137
        if (jsonContent != null) {
153
        if (jsonContent != null) {
138
            this.content = new TableContent(jsonContent);
154
            this.content = new TableContent(jsonContent);
139
        }
155
        }
140
        final JSONObject jsonSelection = (JSONObject) JSONConverter.getParameterFromJSON(json, "selection", JSONObject.class);
156
        final JSONObject jsonSelection = JSONConverter.getParameterFromJSON(json, "selection", JSONObject.class);
141
        if (jsonSelection != null) {
157
        if (jsonSelection != null) {
142
            this.selection = new RowSelectionSpec(jsonSelection);
158
            this.selection = new RowSelectionSpec(jsonSelection);
143
        } else {
159
        } else {
144
            throw new IllegalArgumentException("null selection");
160
            throw new IllegalArgumentException("null selection");
145
        }
161
        }
146
 
162
 
147
        final JSONObject jsonSearch = (JSONObject) JSONConverter.getParameterFromJSON(json, "search", JSONObject.class);
163
        final JSONObject jsonSearch = JSONConverter.getParameterFromJSON(json, "search", JSONObject.class);
148
        if (jsonSearch != null) {
164
        if (jsonSearch != null) {
149
            this.search = new SearchSpec(jsonSearch);
165
            this.search = new UserSearch(jsonSearch);
150
        }
166
        }
151
 
167
 
152
        this.variableColumnsCount = (Boolean) JSONConverter.getParameterFromJSON(json, "variable-columns-count", Boolean.class);
168
        this.variableColumnsCount = JSONConverter.getParameterFromJSON(json, "variable-columns-count", Boolean.class);
-
 
169
        this.searchParameters.clear();
-
 
170
        if (json.containsKey("search-params")) {
-
 
171
            final JSONArray array = (JSONArray) json.get("search-params");
-
 
172
            for (Object o : array) {
-
 
173
                final TableSearchParameter p = new TableSearchParameter();
-
 
174
                p.fromJSON((JSONObject) o);
-
 
175
                this.searchParameters.add(p);
153
    }
176
            }
154
 
-
 
155
    @Override
-
 
156
    public void writeExternal(ObjectOutput out) throws IOException {
-
 
157
        out.writeUTF(id);
-
 
158
        this.columns.writeExternal(out);
-
 
159
        this.content.writeExternal(out);
-
 
160
 
-
 
161
        this.selection.writeExternal(out);
-
 
162
        this.search.writeExternal(out);
-
 
163
 
-
 
164
        out.writeBoolean(this.variableColumnsCount);
-
 
165
 
-
 
166
    }
177
        }
167
 
178
 
168
    @Override
-
 
169
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
-
 
170
        this.id = in.readUTF();
-
 
171
        this.columns = new ColumnsSpec();
-
 
172
        this.columns.readExternal(in);
-
 
173
        this.content = new TableContent();
-
 
174
        this.content.readExternal(in);
-
 
175
        this.selection = new RowSelectionSpec();
-
 
176
        this.selection.readExternal(in);
-
 
177
        this.search = new SearchSpec();
-
 
178
        this.search.readExternal(in);
-
 
179
        this.variableColumnsCount = in.readBoolean();
-
 
180
    }
179
    }
181
 
180
 
182
}
181
}