OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 182
Line 1... Line 1...
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-2019 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.
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.sql.model;
14
 package org.openconcerto.sql.model;
15
 
15
 
16
import org.openconcerto.sql.Log;
16
import org.openconcerto.sql.Log;
-
 
17
import org.openconcerto.sql.model.SQLTable.VirtualFields;
17
import org.openconcerto.sql.model.graph.DatabaseGraph;
18
import org.openconcerto.sql.model.graph.DatabaseGraph;
18
import org.openconcerto.sql.model.graph.Link;
19
import org.openconcerto.sql.model.graph.Link;
19
import org.openconcerto.sql.model.graph.Link.Direction;
20
import org.openconcerto.sql.model.graph.Link.Direction;
20
import org.openconcerto.sql.model.graph.Step;
21
import org.openconcerto.sql.model.graph.Step;
21
import org.openconcerto.utils.NumberUtils;
22
import org.openconcerto.utils.NumberUtils;
Line 24... Line 25...
24
import org.openconcerto.utils.convertor.StringClobConvertor;
25
import org.openconcerto.utils.convertor.StringClobConvertor;
25
 
26
 
26
import java.math.BigDecimal;
27
import java.math.BigDecimal;
27
import java.sql.Clob;
28
import java.sql.Clob;
28
import java.text.DateFormat;
29
import java.text.DateFormat;
-
 
30
import java.util.ArrayList;
29
import java.util.Calendar;
31
import java.util.Calendar;
30
import java.util.Collection;
32
import java.util.Collection;
31
import java.util.Collections;
33
import java.util.Collections;
32
import java.util.Date;
34
import java.util.Date;
33
import java.util.HashSet;
35
import java.util.HashSet;
Line 194... Line 196...
194
    public abstract int getID();
196
    public abstract int getID();
195
 
197
 
196
    public abstract Number getIDNumber();
198
    public abstract Number getIDNumber();
197
 
199
 
198
    /**
200
    /**
-
 
201
     * Get the row reference.
-
 
202
     * 
-
 
203
     * @return the row reference, <code>null</code> if some fields of the
-
 
204
     *         {@link SQLTable#getPrimaryKeyFields() primary key} are missing or <code>null</code>.
-
 
205
     */
-
 
206
    public final RowRef getRowRef() {
-
 
207
        final List<String> pkFields = this.getTable().getPKsNames();
-
 
208
        final List<Object> pk = new ArrayList<>(pkFields.size());
-
 
209
        for (final String f : pkFields) {
-
 
210
            final Object o = this.getObject(f);
-
 
211
            // Don't need contains() because a PK cannot contain NULL in the DB.
-
 
212
            if (o == null)
-
 
213
                return null;
-
 
214
            pk.add(o);
-
 
215
        }
-
 
216
        return new RowRef(this.getTable(), Collections.unmodifiableList(pk), true);
-
 
217
    }
-
 
218
 
-
 
219
    /**
199
     * Whether this row is the undefined row. Return <code>false</code> if both the
220
     * Whether this row is the undefined row. Return <code>false</code> if both the
200
     * {@link #getIDNumber() ID} and {@link SQLTable#getUndefinedIDNumber()} are <code>null</code>
221
     * {@link #getIDNumber() ID} and {@link SQLTable#getUndefinedIDNumber()} are <code>null</code>
201
     * since no row can have <code>null</code> primary key in the database. IOW when
222
     * since no row can have <code>null</code> primary key in the database. IOW when
202
     * {@link SQLTable#getUndefinedIDNumber()} is <code>null</code> the empty
223
     * {@link SQLTable#getUndefinedIDNumber()} is <code>null</code> the empty
203
     * <strong>foreign</strong> keys are <code>null</code>.
224
     * <strong>foreign</strong> keys are <code>null</code>.
Line 229... Line 250...
229
            return ((Boolean) archiveVal).booleanValue();
250
            return ((Boolean) archiveVal).booleanValue();
230
        else
251
        else
231
            return ((Number) archiveVal).intValue() > 0;
252
            return ((Number) archiveVal).intValue() > 0;
232
    }
253
    }
233
 
254
 
-
 
255
    public abstract SQLRowAccessor toImmutable();
-
 
256
 
-
 
257
    public abstract boolean isFrozen();
-
 
258
 
234
    /**
259
    /**
235
     * Creates an SQLRow from these values, without any DB access.
260
     * Creates an SQLRow from these values, without any DB access.
236
     * 
261
     * 
237
     * @return an SQLRow with the same values as this.
262
     * @return an SQLRow with the same values as this.
238
     */
263
     */
239
    public abstract SQLRow asRow();
264
    public final SQLRow asRow() {
-
 
265
        return this.asRow(null);
-
 
266
    }
-
 
267
 
-
 
268
    public abstract SQLRow asRow(final Boolean immutable);
-
 
269
 
-
 
270
    /**
-
 
271
     * Return an immutable SQLRow with only the passed fields retained. I.e. the returned instance
-
 
272
     * can consume less memory.
-
 
273
     * 
-
 
274
     * @param fields which fields to retain.
-
 
275
     * @return a {@link #isFrozen() frozen} SQLRow.
-
 
276
     */
-
 
277
    public final SQLRow trimmedRow(final VirtualFields fields) {
-
 
278
        final Set<String> fieldsNames = this.getTable().getFieldsNames(fields);
-
 
279
        if (this instanceof SQLRow && this.isFrozen() && fieldsNames.containsAll(this.getFields()))
-
 
280
            return (SQLRow) this;
-
 
281
        return SQLRow.trim(this, SQLRowAccessor::getValues, fieldsNames);
-
 
282
    }
-
 
283
 
-
 
284
    public final SQLRow fetchNewRow() {
-
 
285
        return this.fetchNewRow(true);
-
 
286
    }
-
 
287
 
-
 
288
    /**
-
 
289
     * Return a new instance with up-to-date values.
-
 
290
     * 
-
 
291
     * @param useCache <code>true</code> to use the {@link SQLDataSource#isCacheEnabled() cache}.
-
 
292
     * @return a new instance.
-
 
293
     */
-
 
294
    public final SQLRow fetchNewRow(final boolean useCache) {
-
 
295
        return new SQLRow(this.getTable(), this.getID()).fetchValues(useCache);
-
 
296
    }
240
 
297
 
241
    /**
298
    /**
242
     * Creates an SQLRowValues from these values, without any DB access.
299
     * Creates an SQLRowValues from these values, without any DB access.
243
     * 
300
     * 
244
     * @return an SQLRowValues with the same values as this.
301
     * @return an SQLRowValues with the same values as this.
245
     */
302
     */
246
    public abstract SQLRowValues asRowValues();
303
    public final SQLRowValues asRowValues() {
-
 
304
        return this.asRowValues(null);
-
 
305
    }
-
 
306
 
-
 
307
    /**
-
 
308
     * Creates an SQLRowValues from these values, without any DB access.
-
 
309
     * 
-
 
310
     * @param immutable <code>true</code> if the result must be
-
 
311
     *        {@link SQLRowValuesCluster#isFrozen() frozen}, <code>false</code> if it must not,
-
 
312
     *        <code>null</code> if the caller doesn't care and just wants the fastest result.
-
 
313
     * @return an SQLRowValues with the same values as this.
-
 
314
     */
-
 
315
    public abstract SQLRowValues asRowValues(final Boolean immutable);
247
 
316
 
248
    /**
317
    /**
249
     * Creates an SQLRowValues with just this ID, and no other values.
318
     * Creates an SQLRowValues with just this ID, and no other values.
250
     * 
319
     * 
251
     * @return an empty SQLRowValues.
320
     * @return an empty SQLRowValues.
Line 265... Line 334...
265
        return this.getFields().contains(fieldName);
334
        return this.getFields().contains(fieldName);
266
    }
335
    }
267
 
336
 
268
    public abstract Object getObject(String fieldName);
337
    public abstract Object getObject(String fieldName);
269
 
338
 
-
 
339
    public abstract Object getObjectNoCheck(String fieldName);
-
 
340
 
270
    /**
341
    /**
271
     * Return the value for the passed field only if already present in this instance.
342
     * Return the value for the passed field only if already present in this instance.
272
     * 
343
     * 
273
     * @param fieldName a field name.
344
     * @param fieldName a field name.
274
     * @return the existing value for the passed field.
345
     * @return the existing value for the passed field.