OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Details | Compare with Previous | 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.utils;
15
 
16
import org.openconcerto.sql.PropsConfiguration;
17
import org.openconcerto.sql.changer.Change;
18
import org.openconcerto.sql.changer.Changer;
19
import org.openconcerto.sql.model.DBRoot;
20
import org.openconcerto.sql.model.SQLBase;
21
import org.openconcerto.sql.model.SQLSyntax;
22
import org.openconcerto.utils.CollectionUtils;
23
 
24
import java.io.File;
25
import java.io.IOException;
26
import java.lang.reflect.Method;
27
import java.sql.SQLException;
28
import java.util.Arrays;
29
import java.util.Collections;
30
 
31
/**
32
 * @author Sylvain CUAZ
33
 */
34
public abstract class ChangeBase {
35
 
36
    private final DBRoot root;
37
 
38
    public ChangeBase(final DBRoot r) {
39
        this.root = r;
40
    }
41
 
42
    public ChangeBase() throws IOException {
43
        this(new PropsConfiguration(new File("changeBase.properties"), Change.props).getRoot());
44
    }
45
 
46
    protected final SQLSyntax getSyntax() {
47
        return SQLSyntax.get(this.getRoot());
48
    }
49
 
50
    protected final void call(String converter) {
51
        this.call(converter, new String[0]);
52
    }
53
 
54
    protected final void call(String[] nameNparams) {
55
        final String[] params = new String[nameNparams.length - 1];
56
        System.arraycopy(nameNparams, 1, params, 0, params.length);
57
        this.call(nameNparams[0], params);
58
    }
59
 
60
    /**
61
     * Appelle la méthode spécifiée.
62
     *
63
     * @param converter la méthode à appeler.
64
     * @param params parameters to pass to the converter.
65
     */
66
    protected final void call(String converter, String[] params) {
67
        final Class<? extends Changer> c = this.getChange() == null ? null : this.getChange().findClass(converter);
68
        if (c != null) {
69
            try {
83 ilm 70
                this.getChange().exec(getRoot(), c, params);
17 ilm 71
            } catch (SQLException e) {
72
                e.printStackTrace();
73
            }
74
        } else {
75
            try {
76
                final Class[] types = new Class[params.length];
77
                Arrays.fill(types, String.class);
78
                this.call(this.getClass().getMethod(converter, types), params);
79
            } catch (NoSuchMethodException exn) {
80
                final String copies = CollectionUtils.join(Collections.nCopies(params.length, "String"), ", ");
81
                System.err.println("Le convertisseur '" + converter + "(" + copies + ")' n'existe pas.");
82
            }
83
        }
84
    }
85
 
86
    abstract protected Change getChange();
87
 
88
    private void call(Method converter, Object[] params) {
89
        try {
90
            converter.invoke(this, params);
91
        } catch (Exception exn) {
92
            exn.printStackTrace();
93
        }
94
    }
95
 
96
    protected final SQLBase getBase() {
97
        return this.getRoot().getBase();
98
    }
99
 
100
    protected final DBRoot getRoot() {
101
        return this.root;
102
    }
103
}