OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 73 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 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.sql.Configuration;
17
 
18
import java.sql.SQLException;
19
import java.util.ArrayList;
20
import java.util.HashMap;
21
import java.util.Map;
22
 
23
public class SQLInjector {
24
 
25
    final private SQLTable tableSrc, tableDest;
26
    final private ArrayList<SQLField> from = new ArrayList<SQLField>();
27
    final private ArrayList<SQLField> to = new ArrayList<SQLField>();
28
    private Map<SQLField, Object> values = new HashMap<SQLField, Object>();
29
    private static Map<SQLTable, Map<SQLTable, SQLInjector>> injectors = new HashMap<SQLTable, Map<SQLTable, SQLInjector>>();
30
 
31
    public SQLInjector(final DBRoot r, final String src, final String dest) {
32
        this(r.findTable(src), r.findTable(dest));
33
    }
34
 
35
    public SQLInjector(SQLTable src, SQLTable dest) {
36
        this.tableDest = dest;
37
        this.tableSrc = src;
38
        Map<SQLTable, SQLInjector> srcs = injectors.get(src);
39
        if (srcs == null) {
40
            srcs = new HashMap<SQLTable, SQLInjector>();
41
            injectors.put(src, srcs);
42
        }
43
        srcs.put(dest, this);
44
    }
45
 
46
    public SQLRowValues createRowValuesFrom(int idSrc) {
47
        return createRowValuesFrom(getSource().getRow(idSrc));
48
    }
49
 
50
    private static final SQLSystem dbSystem = Configuration.getInstance().getBase().getServer().getSQLSystem();
51
 
52
    public SQLRowValues createRowValuesFrom(final SQLRowAccessor srcRow) {
53
        if (!srcRow.getTable().equals(getSource()))
54
            throw new IllegalArgumentException("Row not from source table : " + srcRow);
55
        SQLRowValues rowVals = new SQLRowValues(getDestination());
56
 
57
        for (SQLField field : this.values.keySet()) {
58
 
59
            rowVals.put(field.getName(), this.values.get(field));
60
        }
61
 
62
        for (int i = 0; i < getFrom().size(); i++) {
63
 
64
            final SQLField sqlFieldFrom = getFrom().get(i);
65
            final SQLField sqlFieldTo = getTo().get(i);
66
            final Object o = srcRow.getObject(sqlFieldFrom.getName());
67
 
68
            // Probleme avec H2 Primary Key en Long et foreignKey en Int
69
            if (dbSystem == SQLSystem.H2 && sqlFieldFrom.getType().getJavaType() == Long.class && sqlFieldTo.getType().getJavaType() == Integer.class) {
70
                rowVals.put(sqlFieldTo.getName(), ((Long) o).intValue());
71
            } else {
72
 
73
                rowVals.put(sqlFieldTo.getName(), o);
74
            }
75
        }
76
 
77
        return rowVals;
78
    }
79
 
80
    public SQLRow insertFrom(final SQLRowAccessor srcRow) throws SQLException {
81
        return createRowValuesFrom(srcRow).insert();
82
    }
83
 
84
    // TODO gettable()..getName()..equalsIgnoreCase( by .getTable().equals(
85
    /**
86
     * mettre une valeur par défaut pour un champ donné
87
     *
88
     * @param fieldDest
89
     * @param defaultValue
90
     */
91
    protected final void mapDefaultValues(SQLField fieldDest, Object defaultValue) {
92
        if (fieldDest.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
93
            this.values.put(fieldDest, defaultValue);
94
        } else {
95
            throw new IllegalArgumentException("SQLField " + fieldDest + " is not a field of table " + this.tableDest);
96
        }
97
    }
98
 
99
    protected final void map(SQLField from, SQLField to) throws IllegalArgumentException {
100
        // Verification de la validité des SQLField
101
        if (!from.getTable().getName().equalsIgnoreCase(this.tableSrc.getName())) {
102
            throw new IllegalArgumentException("SQLField " + from + " is not a field of table " + this.tableSrc);
103
        } else {
104
            if (!to.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
105
                throw new IllegalArgumentException("SQLField " + to + " is not a field of table " + this.tableDest);
106
            }
107
        }
108
 
109
        int index = this.from.indexOf(from);
110
        if (index > 0) {
111
            this.to.set(index, to);
112
        } else {
113
            this.from.add(from);
114
            this.to.add(to);
115
        }
116
    }
117
 
118
    protected final void remove(SQLField from, SQLField to) throws IllegalArgumentException {
119
        // Verification de la validité des SQLField
120
        if (!from.getTable().getName().equalsIgnoreCase(this.tableSrc.getName())) {
121
            throw new IllegalArgumentException("SQLField " + from + " is not a field of table " + this.tableSrc);
122
        } else {
123
            if (!to.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
124
                throw new IllegalArgumentException("SQLField " + to + " is not a field of table " + this.tableDest);
125
            }
126
        }
127
 
128
        int index = this.from.indexOf(from);
129
        if (this.to.get(index).getName().equalsIgnoreCase(to.getName())) {
130
            this.to.remove(to);
131
            this.from.remove(from);
132
        }
133
    }
134
 
135
    /**
136
     * Créer l'association entre les champs portant le nom dans les deux tables
137
     *
138
     */
139
    public void createDefaultMap() {
140
        for (SQLField field : this.tableSrc.getContentFields()) {
141
 
142
            if (this.tableDest.contains(field.getName())) {
143
                map(field, this.tableDest.getField(field.getName()));
144
            }
145
        }
146
    }
147
 
148
    public ArrayList<SQLField> getFrom() {
149
        return this.from;
150
    }
151
 
152
    public ArrayList<SQLField> getTo() {
153
        return this.to;
154
    }
155
 
156
    public static SQLInjector getInjector(SQLTable src, SQLTable dest) {
157
 
158
        Map<SQLTable, SQLInjector> m = injectors.get(src);
159
        if (m != null) {
160
            return m.get(dest);
161
        } else {
162
            return null;
163
        }
164
    }
165
 
166
    /**
167
     * Creer un SQLInjector par défaut si aucun n'est déja défini
168
     *
169
     * @param src
170
     * @param dest
171
     * @return un SQLInjector par défaut si aucun n'est déja défini
172
     */
173
    public static SQLInjector createDefaultInjector(SQLTable src, SQLTable dest) {
174
        if (getInjector(src, dest) == null) {
175
            System.err.println("No SQLInjector defined for " + src + " , " + dest + ". SQLInjector created automatically.");
176
            SQLInjector injector = new SQLInjector(src, dest);
177
            injector.createDefaultMap();
178
            return injector;
179
        } else {
180
            return getInjector(src, dest);
181
        }
182
    }
183
 
184
    public SQLTable getDestination() {
185
        return this.tableDest;
186
    }
187
 
188
    public SQLTable getSource() {
189
        return this.tableSrc;
190
    }
191
}