OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 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.erp.panel;
15
 
156 ilm 16
import org.openconcerto.erp.core.common.element.SocieteCommonSQLElement;
18 ilm 17
import org.openconcerto.erp.utils.ActionDB;
18
import org.openconcerto.erp.utils.StatusListener;
19
import org.openconcerto.sql.Configuration;
67 ilm 20
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
80 ilm 21
import org.openconcerto.sql.model.DBRoot;
67 ilm 22
import org.openconcerto.sql.model.SQLDataSource;
80 ilm 23
import org.openconcerto.sql.model.SQLName;
18 ilm 24
import org.openconcerto.sql.model.SQLRow;
25
import org.openconcerto.sql.model.SQLRowValues;
26
import org.openconcerto.sql.model.SQLSelect;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.Where;
29
import org.openconcerto.ui.JLabelBold;
20 ilm 30
import org.openconcerto.utils.ExceptionHandler;
18 ilm 31
 
32
import java.awt.GridBagConstraints;
33
import java.awt.GridBagLayout;
34
import java.sql.PreparedStatement;
35
import java.sql.SQLException;
36
import java.util.List;
37
import java.util.Map;
38
 
39
import javax.swing.JFrame;
40
import javax.swing.JLabel;
41
import javax.swing.JPanel;
42
import javax.swing.JProgressBar;
43
import javax.swing.SwingUtilities;
44
 
45
public class ChargementCreationSocietePanel extends JPanel implements StatusListener {
46
 
47
    private JProgressBar progressBar;
48
    private JLabel label;
49
 
50
    public ChargementCreationSocietePanel(final int idSoc, final int typePCG) {
51
 
52
        super();
53
        this.progressBar = new JProgressBar();
54
        this.setLayout(new GridBagLayout());
55
        GridBagConstraints c = new GridBagConstraints();
56
        c.anchor = GridBagConstraints.WEST;
57
        c.fill = GridBagConstraints.HORIZONTAL;
58
        c.gridheight = 1;
59
        c.gridwidth = 1;
60
        c.gridx = 0;
61
        c.gridy = 0;
62
        c.weightx = 1;
63
        c.weighty = 0;
64
 
65
        JLabel label1 = new JLabelBold("Création de la base de données de la nouvelle société");
66
        this.add(label1, c);
67
        c.gridy++;
68
 
69
        JLabel label2 = new JLabel("Cette opération peut prendre plusieurs minutes.");
70
        this.add(label2, c);
71
        c.gridy++;
72
        this.label = new JLabel(" ");
73
        this.add(this.label, c);
74
        c.gridy++;
75
        this.add(this.progressBar, c);
76
        this.progressBar.setIndeterminate(true);
77
 
20 ilm 78
        new Thread("Creation de societe") {
18 ilm 79
            public void run() {
20 ilm 80
                try {
81
                    creationBase(idSoc);
18 ilm 82
 
20 ilm 83
                    statusChanged("Importation du plan comptable");
84
                    importationPlanComptable(idSoc, typePCG);
85
                    statusChanged("Création terminée!");
86
                    ChargementCreationSocietePanel.this.progressBar.setIndeterminate(false);
87
                    ChargementCreationSocietePanel.this.progressBar.setString(null);
18 ilm 88
 
20 ilm 89
                    SwingUtilities.invokeLater(new Runnable() {
90
                        public void run() {
91
                            ((JFrame) SwingUtilities.getRoot(ChargementCreationSocietePanel.this)).dispose();
92
                        }
93
                    });
94
                } catch (Throwable e) {
95
                    ExceptionHandler.handle("Erreur pendant la création de la base!", e);
96
                }
18 ilm 97
            }
98
        }.start();
99
    }
100
 
20 ilm 101
    private void creationBase(int id) throws SQLException {
18 ilm 102
 
103
        System.err.println("Création de la base");
104
 
19 ilm 105
        ActionDB.dupliqueDB("Default", "OpenConcerto" + id, this);
18 ilm 106
 
107
        statusChanged("Mise à jour des sociétés");
108
        SQLRowValues rowVals = new SQLRowValues(Configuration.getInstance().getBase().getTable("SOCIETE_COMMON"));
109
        rowVals.put("DATABASE_NAME", "OpenConcerto" + id);
110
 
20 ilm 111
        rowVals.update(id);
112
 
18 ilm 113
    }
114
 
115
    private void importationPlanComptable(int id, int typePCG) {
116
 
117
        SQLRow rowSociete = Configuration.getInstance().getBase().getTable("SOCIETE_COMMON").getRow(id);
118
 
67 ilm 119
        // FIXME by Sylvain comme on a déjà accede à la base, les nouvelles tables n'ont pas été
18 ilm 120
        // rechargées
121
 
80 ilm 122
        final DBRoot baseNewSociete = SocieteCommonSQLElement.getRoot(rowSociete);
123
 
18 ilm 124
        SQLTable tableComptePCG = baseNewSociete.getTable("COMPTE_PCG");
67 ilm 125
        final SQLSelect sel = new SQLSelect();
18 ilm 126
        sel.addSelect(tableComptePCG.getField("NUMERO"));
127
        sel.addSelect(tableComptePCG.getField("NOM"));
128
        sel.addSelect(tableComptePCG.getField("INFOS"));
129
 
130
        if (typePCG == 0) {
131
            sel.setWhere(new Where(tableComptePCG.getField("ID_TYPE_COMPTE_PCG_BASE"), "!=", 1));
67 ilm 132
        } else if (typePCG == 1) {
133
            sel.setWhere(new Where(tableComptePCG.getField("ID_TYPE_COMPTE_PCG_AB"), "!=", 1));
18 ilm 134
        }
135
 
136
        try {
67 ilm 137
            baseNewSociete.getBase().getDataSource().useConnection(new ConnectionHandlerNoSetup<Object, SQLException>() {
18 ilm 138
 
67 ilm 139
                @Override
140
                public Object handle(SQLDataSource ds) throws SQLException, SQLException {
80 ilm 141
                    String insert = "INSERT INTO " + new SQLName(baseNewSociete.getName(), "COMPTE_PCE").quote() + " (\"NUMERO\", \"NOM\", \"INFOS\") VALUES (?, ?, ?)";
67 ilm 142
                    PreparedStatement stmt = ds.getConnection().prepareStatement(insert);
143
                    List tmpCpt = baseNewSociete.getBase().getDataSource().execute(sel.asString());
144
                    for (int i = 0; i < tmpCpt.size(); i++) {
145
                        Map tmp = (Map) tmpCpt.get(i);
146
                        String numero = (tmp.get("NUMERO") == null) ? "" : tmp.get("NUMERO").toString();
147
                        stmt.setString(1, numero);
148
                        String nom = (tmp.get("NOM") == null) ? "" : tmp.get("NOM").toString();
149
                        stmt.setString(2, nom);
150
                        String infos = (tmp.get("INFOS") == null) ? "" : tmp.get("INFOS").toString();
151
                        stmt.setString(3, infos);
152
                        stmt.executeUpdate();
18 ilm 153
 
67 ilm 154
                    }
155
                    return null;
156
                }
157
            });
18 ilm 158
 
159
        } catch (SQLException e) {
160
            e.printStackTrace();
161
        }
162
 
163
    }
164
 
165
    public void statusChanged(final String message) {
166
        SwingUtilities.invokeLater(new Runnable() {
167
 
168
            public void run() {
169
                ChargementCreationSocietePanel.this.label.setText(message);
170
 
171
            }
172
        });
173
 
174
    }
175
}