OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 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.sql.model;
15
 
16
import org.openconcerto.utils.cc.IPredicate;
17
import org.openconcerto.utils.checks.EmptyObjFromVO;
18
 
19
import java.sql.SQLException;
20
import java.util.HashMap;
21
import java.util.Map;
22
 
23
/**
24
 * Store inserted rows. This class allows to avoid having huge, slow {@link SQLRowValuesCluster} by
25
 * replacing a foreign link with its ID.
26
 *
27
 * @author Sylvain CUAZ
28
 * @param <T> type of source object.
29
 */
30
public abstract class SQLRowValuesMap<T> {
31
 
32
    private final SQLTable t;
33
    private final Map<SQLRowValues, SQLRow> map;
34
    private final IPredicate<? super T> emptyPredicate;
35
 
36
    public SQLRowValuesMap(final SQLTable t) {
37
        this(t, EmptyObjFromVO.getDefaultPredicate());
38
    }
39
 
40
    public SQLRowValuesMap(final SQLTable t, final IPredicate<? super T> emptyPredicate) {
41
        this.t = t;
42
        this.map = new HashMap<SQLRowValues, SQLRow>();
43
        this.emptyPredicate = emptyPredicate;
44
    }
45
 
46
    protected abstract void fill(final SQLRowValues vals, T obj);
47
 
48
    /**
49
     * Return a non SQLRowValues value, thus avoiding linking two graphs together. If
50
     * <code>obj</code> is empty, returns {@link SQLRowValues#SQL_EMPTY_LINK}, else calls
51
     * {@link #fill(SQLRowValues, Object)} and if these values haven't already been inserted, insert
52
     * them, finally return the ID.
53
     *
54
     * @param obj the source object.
55
     * @return the ID or SQL_EMPTY_LINK.
56
     * @throws SQLException if an error occurs while inserting.
57
     */
58
    public final Object getValue(final T obj) throws SQLException {
59
        if (this.emptyPredicate.evaluateChecked(obj)) {
60
            return SQLRowValues.SQL_EMPTY_LINK;
61
        } else {
62
            final SQLRowValues key = new SQLRowValues(this.t);
63
            this.fill(key, obj);
64
            SQLRow res = this.map.get(key);
65
            if (res == null) {
66
                res = key.insert();
67
                this.map.put(key, res);
68
            }
69
            return res.getIDNumber();
70
        }
71
    }
72
}