OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 57 | Go to most recent revision | Details | 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.core.finance.accounting.element;
15
 
16
import org.openconcerto.erp.core.common.element.ComptaSQLConfElement;
17
import org.openconcerto.erp.core.finance.accounting.ui.AjouterAxeAnalytiqueFrame;
18
import org.openconcerto.erp.core.finance.accounting.ui.RepartitionAxeAnalytiquePanel;
19
import org.openconcerto.erp.core.finance.accounting.ui.ValiderSuppressionAxeFrame;
20
import org.openconcerto.erp.element.objet.Axe;
21
import org.openconcerto.erp.element.objet.Poste;
22
import org.openconcerto.erp.element.objet.Repartition;
23
import org.openconcerto.erp.element.objet.RepartitionElement;
24
import org.openconcerto.sql.element.BaseSQLComponent;
25
import org.openconcerto.sql.element.SQLComponent;
26
import org.openconcerto.sql.model.SQLBase;
27
import org.openconcerto.sql.model.SQLRow;
28
import org.openconcerto.sql.model.SQLRowValues;
29
import org.openconcerto.sql.model.SQLSelect;
30
import org.openconcerto.sql.model.SQLTable;
31
import org.openconcerto.ui.DefaultGridBagConstraints;
32
 
33
import java.awt.Component;
34
import java.awt.Container;
35
import java.awt.GridBagConstraints;
36
import java.awt.GridBagLayout;
37
import java.awt.Insets;
38
import java.awt.event.ActionEvent;
39
import java.awt.event.ActionListener;
40
import java.awt.event.KeyAdapter;
41
import java.awt.event.KeyEvent;
42
import java.awt.event.MouseAdapter;
43
import java.awt.event.MouseEvent;
44
import java.sql.SQLException;
45
import java.util.ArrayList;
46
import java.util.HashMap;
47
import java.util.List;
48
import java.util.Map;
49
import java.util.Vector;
50
 
51
import javax.swing.AbstractAction;
52
import javax.swing.JButton;
53
import javax.swing.JFrame;
54
import javax.swing.JPopupMenu;
55
import javax.swing.JTabbedPane;
56
import javax.swing.JTextField;
57
import javax.swing.JWindow;
58
import javax.swing.SwingUtilities;
59
import javax.swing.event.AncestorEvent;
60
import javax.swing.event.AncestorListener;
61
 
62
import org.apache.commons.dbutils.handlers.ArrayListHandler;
63
 
64
public class AnalytiqueSQLElement extends ComptaSQLConfElement {
65
 
66
    // association onglet - Axe
67
    private Vector vecteurTabAxe;
68
 
69
    private final List<Axe> axes = new ArrayList<Axe>();
70
    private List<List<Repartition>> repartitionsAxe;
71
    private List<List<RepartitionElement>> repartitionElemsAxe;
72
    private List<List<Poste>> postesAxe;
73
 
74
    private AnalytiqueSQLElement a;
75
 
76
    private JTabbedPane tabAxes;
77
 
78
    // Map utilisée pour les ID, association IDtmp/IDReal
79
    private Map<Integer, Integer> mapPostes;
80
    private Map<Integer, Integer> mapRepartitons;
81
 
82
    // Window utilisée pour changer le nom des onglets ou des colonnes
83
    private JWindow windowChangeNom;
84
 
85
    private AjouterAxeAnalytiqueFrame ajoutAxeFrame = null;
86
    private JTextField text;
87
    private int editedAxeIndex = -1;
88
 
89
    public AnalytiqueSQLElement() {
90
        super("AXE_ANALYTIQUE", "un axe analytique", "axes analytiques");
91
        this.a = this;
92
    }
93
 
94
    protected List getListFields() {
95
        final List<String> l = new ArrayList<String>();
96
        l.add("NOM");
97
        return l;
98
    }
99
 
100
    protected List getComboFields() {
101
        final List<String> l = new ArrayList<String>();
102
        l.add("NOM");
103
        return l;
104
    }
105
 
106
    /*
107
     * (non-Javadoc)
108
     *
109
     * @see org.openconcerto.devis.SQLElement#getComponent()
110
     */
111
    public SQLComponent createComponent() {
112
        return new BaseSQLComponent(this) {
113
            public void addViews() {
114
                this.setLayout(new GridBagLayout());
115
                final GridBagConstraints c = new DefaultGridBagConstraints();
116
                c.anchor = GridBagConstraints.NORTHWEST;
117
                c.gridwidth = 4;
118
                c.gridheight = 8;
119
 
120
                vecteurTabAxe = new Vector();
121
 
122
                repartitionsAxe = new Vector();
123
                repartitionElemsAxe = new Vector();
124
                postesAxe = new Vector();
125
 
126
                tabAxes = new JTabbedPane();
127
 
128
                /***********************************************************************************
129
                 * * CREATION DES ONGLETS
130
                 **********************************************************************************/
131
 
132
                // on recupere les axes existant
133
                // SELECT ID, NOM FROM AXE
134
                SQLBase base = getTable().getBase();
135
                SQLSelect sel = new SQLSelect(base);
136
                sel.addSelect("AXE_ANALYTIQUE.ID");
137
                sel.addSelect("AXE_ANALYTIQUE.NOM");
138
                sel.addRawOrder("AXE_ANALYTIQUE.NOM");
139
                String req = sel.asString();
140
 
141
                Object ob = getTable().getBase().getDataSource().execute(req, new ArrayListHandler());
142
 
143
                List myList = (List) ob;
144
                if (myList.size() != 0) {
145
 
146
                    // on crée les onglets et on stocke les axes
147
                    for (int i = 0; i < myList.size(); i++) {
148
 
149
                        // ID, nom
150
                        Object[] objTmp = (Object[]) myList.get(i);
151
 
152
                        axes.add(new Axe(Integer.parseInt(objTmp[0].toString()), objTmp[1].toString()));
153
 
154
                        // on recupere les repartitions et les élements associés à l'axe
155
                        RepartitionAxeAnalytiquePanel repAxeComp = new RepartitionAxeAnalytiquePanel(((Axe) axes.get(i)).getId());
156
 
157
                        repartitionsAxe.add(repAxeComp.getRepartitions());
158
                        repartitionElemsAxe.add(repAxeComp.getRepartitionElems());
159
                        postesAxe.add(repAxeComp.getPostes());
160
                        tabAxes.addTab(((Axe) axes.get(i)).getNom(), repAxeComp);
161
 
162
                        vecteurTabAxe.add(i, new String(String.valueOf(i)));
163
                    }
164
 
165
                    System.out.println("Size ----> " + axes.size());
166
                } else {
167
                    ajouterAxe("Nouvel Axe");
168
                }
169
 
170
                c.fill = GridBagConstraints.BOTH;
171
                c.weightx = 1;
172
                c.weighty = 1;
173
                this.add(tabAxes, c);
174
 
175
                tabAxes.addMouseListener(new MouseAdapter() {
176
 
177
                    public void mousePressed(final MouseEvent e) {
178
 
179
                        final int index = tabAxes.indexAtLocation(e.getX(), e.getY());
180
 
181
                        validAxeText();
182
 
183
                        if (e.getClickCount() == 2) {
184
 
185
                            actionModifierAxe(e, index);
186
                        }
187
 
188
                        if (e.getButton() == MouseEvent.BUTTON3) {
189
                            actionDroitOnglet(index, e);
190
                        }
191
                    }
192
                });
193
 
194
                tabAxes.addAncestorListener(new AncestorListener() {
195
 
196
                    public void ancestorAdded(AncestorEvent event) {
197
 
198
                        validAxeText();
199
 
200
                    }
201
 
202
                    public void ancestorRemoved(AncestorEvent event) {
203
 
204
                        validAxeText();
205
 
206
                    }
207
 
208
                    public void ancestorMoved(AncestorEvent event) {
209
 
210
                        validAxeText();
211
 
212
                    }
213
                });
214
 
215
                /***********************************************************************************
216
                 * * AJOUT D'UN AXE
217
                 **********************************************************************************/
218
                JButton boutonAddAxe = new JButton("Ajouter un axe");
219
                boutonAddAxe.addActionListener(new ActionListener() {
220
 
221
                    public void actionPerformed(ActionEvent arg0) {
222
 
223
                        if (ajoutAxeFrame == null) {
224
                            ajoutAxeFrame = new AjouterAxeAnalytiqueFrame(a);
225
                        }
226
                        ajoutAxeFrame.pack();
227
                        ajoutAxeFrame.setVisible(true);
228
                    }
229
                });
230
 
231
                c.gridx += 4;
232
                c.gridwidth = 1;
233
                c.gridheight = 1;
234
                c.weightx = 0;
235
                c.weighty = 0;
236
                c.fill = GridBagConstraints.HORIZONTAL;
237
                this.add(boutonAddAxe, c);
238
 
239
                /***********************************************************************************
240
                 * * SUPPRESSION D'UN AXE
241
                 **********************************************************************************/
242
                JButton boutonDelAxe = new JButton("Supprimer un axe");
243
                boutonDelAxe.addActionListener(new ActionListener() {
244
 
245
                    public void actionPerformed(ActionEvent arg0) {
246
 
247
                        supprimerAxe(Integer.parseInt(vecteurTabAxe.get(tabAxes.getSelectedIndex()).toString()));
248
 
249
                    }
250
                });
251
 
252
                c.gridy++;
253
                this.add(boutonDelAxe, c);
254
 
255
                JButton boutonSet = new JButton("SetVal");
256
                boutonSet.addActionListener(new ActionListener() {
257
 
258
                    public void actionPerformed(ActionEvent arg0) {
259
                        setVal();
260
                    }
261
                });
262
 
263
                c.gridy++;
264
                this.add(boutonSet, c);
265
 
266
                JButton boutonCheck = new JButton("Check");
267
                boutonCheck.addActionListener(new ActionListener() {
268
 
269
                    public void actionPerformed(ActionEvent arg0) {
270
                        checkID();
271
                    }
272
                });
273
 
274
                c.gridy++;
275
                this.add(boutonCheck, c);
276
            }
277
        };
278
    }
279
 
280
    private void actionDroitOnglet(final int index, final MouseEvent e) {
281
 
282
        JPopupMenu pop = new JPopupMenu();
283
        pop.add(new AbstractAction("Ajouter un axe") {
284
 
285
            public void actionPerformed(ActionEvent e) {
286
                if (ajoutAxeFrame == null) {
287
                    ajoutAxeFrame = new AjouterAxeAnalytiqueFrame(a);
288
                }
289
                ajoutAxeFrame.pack();
290
                ajoutAxeFrame.setVisible(true);
291
            }
292
        });
293
 
294
        // si un onglet est selectionné
295
        if (index != -1) {
296
            pop.add(new AbstractAction("Supprimer l'axe") {
297
 
298
                public void actionPerformed(ActionEvent e) {
299
 
300
                    supprimerAxe(index);
301
                }
302
            });
303
 
304
            pop.add(new AbstractAction("Modifier le nom") {
305
 
306
                public void actionPerformed(ActionEvent aE) {
307
 
308
                    actionModifierAxe(e, index);
309
                }
310
            });
311
        }
312
 
313
        pop.show(e.getComponent(), e.getX(), e.getY());
314
 
315
        System.out.println("Click droit onglet");
316
    }
317
 
318
    private void actionModifierAxe(MouseEvent e, final int index) {
319
 
320
        if (index == -1)
321
            return;
322
 
323
        Component comp = (Component) e.getSource();
324
        JFrame frame = (JFrame) SwingUtilities.getRoot(comp);
325
 
326
        this.windowChangeNom = new JWindow(frame);
327
        Container container = this.windowChangeNom.getContentPane();
328
        container.setLayout(new GridBagLayout());
329
 
330
        final GridBagConstraints c = new DefaultGridBagConstraints();
331
        c.insets = new Insets(0, 0, 0, 0);
332
        c.weightx = 1;
333
 
334
        this.editedAxeIndex = index;
335
        this.text = new JTextField(" " + this.tabAxes.getTitleAt(index) + " ");
336
        this.text.setEditable(true);
337
        container.add(this.text, c);
338
        this.text.setBorder(null);
339
        // text.setBackground(this.tabAxes.getBackground());
340
        this.text.addKeyListener(new KeyAdapter() {
341
 
342
            public void keyPressed(KeyEvent event) {
343
 
344
                if (event.getKeyCode() == KeyEvent.VK_ENTER) {
345
                    validAxeText();
346
                }
347
            }
348
 
349
        });
350
 
351
        this.windowChangeNom.pack();
352
 
353
        int ecartY = this.tabAxes.getBoundsAt(index).height - this.text.getBounds().height + 2;
354
        int ecartX = this.tabAxes.getBoundsAt(index).width - this.text.getBounds().width;
355
 
356
        this.windowChangeNom.setLocation(comp.getLocationOnScreen().x + this.tabAxes.getBoundsAt(index).getLocation().x + ecartX / 2, comp.getLocationOnScreen().y
357
                + this.tabAxes.getBoundsAt(index).getLocation().y + ecartY / 2);
358
 
359
        this.windowChangeNom.setVisible(true);
360
    }
361
 
362
    /**
363
     * @param index
364
     * @param text
365
     */
366
    private synchronized void validAxeText() {
367
        if (this.text == null || this.windowChangeNom == null)
368
            return;
369
 
370
        modifierAxe(this.editedAxeIndex, this.text.getText());
371
        this.windowChangeNom.setVisible(false);
372
        this.windowChangeNom.dispose();
373
        this.editedAxeIndex = -1;
374
        this.text = null;
375
    }
376
 
377
    /**
378
     * modifier le nom de l'onglet
379
     *
380
     * @param axeIndex : numero de l'onglet
381
     * @param nom : nouveau nom
382
     */
383
    private void modifierAxe(int axeIndex, String nom) {
384
        if (axeIndex < 0)
385
            return;
386
        ((Axe) this.axes.get(Integer.parseInt(this.vecteurTabAxe.get(axeIndex).toString()))).setNom(nom);
387
        this.tabAxes.setTitleAt(axeIndex, nom);
388
 
389
    }
390
 
391
    /**
392
     * Ajouter un onglet
393
     *
394
     * @param nom
395
     */
396
    public void ajouterAxe(String nom) {
397
 
398
        RepartitionAxeAnalytiquePanel repAxePanel = new RepartitionAxeAnalytiquePanel(1);
399
 
400
        if (nom.trim().length() == 0) {
401
            this.tabAxes.addTab("Nouvel axe", repAxePanel);
402
        } else {
403
            this.tabAxes.addTab(nom, repAxePanel);
404
        }
405
 
406
        this.repartitionsAxe.add(repAxePanel.getRepartitions());
407
        this.repartitionElemsAxe.add(repAxePanel.getRepartitionElems());
408
        this.postesAxe.add(repAxePanel.getPostes());
409
 
410
        this.vecteurTabAxe.add(this.tabAxes.getTabCount() - 1, new String(String.valueOf(this.axes.size())));
411
        this.axes.add(new Axe(1, this.tabAxes.getTitleAt(this.tabAxes.getTabCount() - 1), true));
412
    }
413
 
414
    /**
415
     * Supprimer un onglet
416
     *
417
     * @param axeSelect : numero de l'axe
418
     */
419
    private void supprimerAxe(int axeSelect) {
420
 
421
        // int axeSelect =
422
        // Integer.parseInt(this.vecteurTabAxe.get(this.tabAxes.getSelectedIndex()).toString());
423
 
424
        // Test si l'axe contient une repartition deja associe à un compte
425
        // SELECT ID, ID_COMPTE FROM ASSOCIATION WHERE ID.AXE = id
426
        SQLTable assocTable = getTable().getBase().getTable("ASSOCIATION_COMPTE_ANALYTIQUE");
427
 
428
        SQLBase base = getTable().getBase();
429
        SQLSelect selAssoc = new SQLSelect(base);
430
        selAssoc.addSelect(assocTable.getField("ID"));
431
        selAssoc.addSelect(assocTable.getField("ID_COMPTE_PCE"));
432
        selAssoc.setWhere("ASSOCIATION_COMPTE_ANALYTIQUE.ID_AXE_ANALYTIQUE", "=", ((Axe) this.axes.get(axeSelect)).getId());
433
 
434
        String reqAssoc = selAssoc.asString();
435
        Object obAssoc = getTable().getBase().getDataSource().execute(reqAssoc, new ArrayListHandler());
436
 
437
        List myListAssoc = (List) obAssoc;
438
 
439
        if (myListAssoc.size() != 0) {
440
            System.out.println("La répartition est affectée à un compte.");
441
            ValiderSuppressionAxeFrame validFrame = new ValiderSuppressionAxeFrame(this.a, axeSelect);
442
 
443
            validFrame.pack();
444
            validFrame.setVisible(true);
445
 
446
        } else {
447
            deleteAxe(axeSelect);
448
        }
449
    }
450
 
451
    public void deleteAxe(int axeSelect) {
452
        int j;
453
        ((Axe) this.axes.get(axeSelect)).setSuppression(true);
454
 
455
        for (j = this.tabAxes.getSelectedIndex(); j < this.tabAxes.getTabCount() - 1; j++) {
456
 
457
            this.vecteurTabAxe.set(j, new String(this.vecteurTabAxe.get(j + 1).toString()));
458
        }
459
 
460
        // on recupere le vecteur de repartition associé à l'axe
461
        Vector repartitions = (Vector) this.repartitionsAxe.get(axeSelect);
462
 
463
        for (j = 0; j < repartitions.size(); j++) {
464
            ((Repartition) repartitions.get(j)).setSuppression(true);
465
        }
466
 
467
        // on recupere les éléments de répartitions de l'axe
468
        Vector repartitionElems = (Vector) this.repartitionElemsAxe.get(axeSelect);
469
 
470
        for (j = 0; j < repartitionElems.size(); j++) {
471
            ((RepartitionElement) repartitionElems.get(j)).setSuppression(true);
472
        }
473
 
474
        // on recupere les postes de l'axe
475
        Vector post = (Vector) this.postesAxe.get(axeSelect);
476
 
477
        for (j = 0; j < post.size(); j++) {
478
            ((Poste) post.get(j)).setSuppression(true);
479
        }
480
 
481
        this.tabAxes.remove(this.tabAxes.getSelectedIndex());
482
        // tabAxes.setEnabledAt(tabAxes.getSelectedIndex(), false);
483
 
484
        System.out.println("suppression de tab numero --> " + this.tabAxes.getSelectedIndex() + " numero Axe : " + axeSelect);
485
    }
486
 
487
    /***********************************************************************************************
488
     * * Validation des données dans la table
489
     **********************************************************************************************/
490
    private void supprimerAxeTable(Axe axe) {
491
 
492
        SQLRowValues vals = new SQLRowValues(this.getTable());
493
        vals.put(this.getTable().getArchiveField().getName(), new Integer(1));
494
        try {
495
            vals.update(axe.getId());
496
        } catch (SQLException e) {
497
            System.out.println("Erreur suppression Axe " + axe.getId());
498
        }
499
 
500
        System.out.println("Axe Supprimé  -->" + axe.toString());
501
    }
502
 
503
    private void creerAxeTable(Axe axe) {
504
 
505
        // axe.setId (INSERT INTO AXE_ANALYTIQUE (colonnes) VALUES ())
506
 
507
        Map m = new HashMap();
508
        m.put("NOM", axe.getNom());
509
 
510
        SQLRowValues val = new SQLRowValues(this.getTable(), m);
511
 
512
        try {
513
 
514
            SQLRow row = val.insert();
515
            System.out.println("Row ID -->" + row.getID());
516
            axe.setId(row.getInt("ID"));
517
        } catch (SQLException e) {
518
            System.out.println("Error insert row in " + this.getTable().toString());
519
        }
520
 
521
        // mise à jour du vecteur
522
        axe.setCreation(false);
523
        axe.setModif(false);
524
 
525
        System.out.println("Axe Nouveau  -->" + axe.toString());
526
    }
527
 
528
    private void modifierAxeTable(Axe axe) {
529
 
530
        // UPDATE AXE_ANALYTIQUE SET column = axe.get...(), ... WHERE id = axe.getId()
531
        SQLRowValues vals = new SQLRowValues(this.getTable());
532
        vals.put("NOM", axe.getNom());
533
 
534
        try {
535
            vals.update(axe.getId());
536
        } catch (SQLException e) {
537
 
538
            System.err.println("Erreur modification Axe " + axe.getId());
539
            e.printStackTrace();
540
        }
541
 
542
        // mise à jour du vecteur
543
        axe.setCreation(false);
544
        axe.setModif(false);
545
 
546
        System.out.println("Axe Modifié  -->" + axe.toString());
547
    }
548
 
549
    private void supprimerRepartitionTable(Repartition rep) {
550
 
551
        SQLRowValues vals = new SQLRowValues(getTable().getBase().getTable("REPARTITION_ANALYTIQUE"));
552
        vals.put(getTable().getBase().getTable("REPARTITION_ANALYTIQUE").getArchiveField().getName(), new Integer(1));
553
 
554
        // Test si la repartition n'est pas deja associe à un compte
555
        // SELECT ID, ID_COMPTE FROM ASSOCIATION WHERE ID.REP = id
556
        SQLTable assocTable = getTable().getBase().getTable("ASSOCIATION_COMPTE_ANALYTIQUE");
557
        SQLBase base = assocTable.getBase();
558
        SQLSelect selAssoc = new SQLSelect(base);
559
        selAssoc.addSelect(assocTable.getField("ID"));
560
        selAssoc.addSelect(assocTable.getField("ID_COMPTE_PCE"));
561
        selAssoc.setWhere("ASSOCIATION_COMPTE_ANALYTIQUE.ID_REPARTITION_ANALYTIQUE", "=", rep.getId());
562
 
563
        String reqAssoc = selAssoc.asString();
564
        Object obAssoc = getTable().getBase().getDataSource().execute(reqAssoc, new ArrayListHandler());
565
 
566
        List myListAssoc = (List) obAssoc;
567
 
568
        if (myListAssoc.size() != 0) {
569
            SQLRowValues valsAssoc = new SQLRowValues(getTable().getBase().getTable("ASSOCIATION_COMPTE_ANALYTIQUE"));
570
            valsAssoc.put("ARCHIVE", 1);
571
            try {
572
 
573
                for (int i = 0; i < myListAssoc.size(); i++) {
574
                    Object[] objTmp = (Object[]) myListAssoc.get(i);
575
                    valsAssoc.update(Integer.parseInt(objTmp[0].toString()));
576
                }
577
 
578
            } catch (SQLException e) {
579
                System.err.println("Erreur suppression association ");
580
                e.printStackTrace();
581
            }
582
        }
583
 
584
        try {
585
            vals.update(rep.getId());
586
        } catch (SQLException e) {
587
            System.err.println("Erreur suppression Repartition " + rep.getId());
588
            e.printStackTrace();
589
        }
590
 
591
        System.out.println("Rep Supprimée  -->" + rep.toString());
592
    }
593
 
594
    private void creerRepartitionTable(Repartition rep) {
595
        // int new_id = INSERT INTO REPARTITION_ANALYTIQUE (column) VALUES
596
        // mettre à jour le vecteur
597
        // mapRepartitons.put(String.valueOf(rep.getId()), new_id);
598
 
599
        Map m = new HashMap();
600
        m.put("NOM", rep.getNom());
601
 
602
        SQLRowValues val = new SQLRowValues(getTable().getBase().getTable("REPARTITION_ANALYTIQUE"), m);
603
 
604
        try {
605
 
606
            SQLRow row = val.insert();
607
            System.out.println("Row ID -->" + row.getID());
608
            this.mapRepartitons.put(new Integer(rep.getId()), new Integer(row.getID()));
609
            rep.setId(row.getID());
610
        } catch (SQLException e) {
611
            System.err.println("Error insert row in " + val.getTable().getName());
612
            e.printStackTrace();
613
        }
614
 
615
        // mise à jour du vecteur
616
        rep.setCreation(false);
617
        rep.setModif(false);
618
 
619
        System.out.println("Rep Crée  -->" + rep.toString());
620
    }
621
 
622
    private void modifierRepartitionTable(Repartition rep) {
623
 
624
        SQLRowValues vals = new SQLRowValues(getTable().getBase().getTable("REPARTITION_ANALYTIQUE"));
625
        vals.put("NOM", rep.getNom());
626
        try {
627
            vals.update(rep.getId());
628
        } catch (SQLException e) {
629
            System.err.println("Erreur modification Repartition " + rep.getId());
630
            e.printStackTrace();
631
        }
632
 
633
        // mise à jour du vecteur
634
        rep.setCreation(false);
635
        rep.setModif(false);
636
 
637
        System.out.println("Répartition Modifié  -->" + rep.toString());
638
    }
639
 
640
    private void creerPosteTable(Poste p, int id_axe) {
641
 
642
        final Map<String, Object> m = new HashMap<String, Object>();
643
        m.put("NOM", p.getNom());
644
        m.put("ID_AXE_ANALYTIQUE", new Integer(id_axe));
645
 
646
        final SQLRowValues val = new SQLRowValues(getTable().getBase().getTable("POSTE_ANALYTIQUE"), m);
647
 
648
        try {
649
 
650
            final SQLRow row = val.insert();
651
 
652
            this.mapPostes.put(p.getId(), row.getID());
653
 
654
            p.setId(row.getID());
655
            p.setIdAxe(id_axe);
656
        } catch (SQLException e) {
657
            System.err.println("Error insert row in " + val.getTable().getName());
658
            e.printStackTrace();
659
        }
660
 
661
        // mise à jour du vecteur
662
        p.setCreation(false);
663
        p.setModif(false);
664
 
665
        System.out.println("Poste Crée  -->" + p.toString());
666
    }
667
 
668
    private void supprimerPosteTable(Poste p) {
669
 
670
        SQLRowValues vals = new SQLRowValues(getTable().getBase().getTable("POSTE_ANALYTIQUE"));
671
        vals.put(getTable().getBase().getTable("POSTE_ANALYTIQUE").getArchiveField().getName(), new Integer(1));
672
        try {
673
            vals.update(p.getId());
674
        } catch (SQLException e) {
675
            System.err.println("Erreur suppression Poste " + p.getId());
676
            e.printStackTrace();
677
        }
678
 
679
        System.out.println("Poste Supprimée  -->" + p.toString());
680
    }
681
 
682
    private void modifierPosteTable(Poste p) {
683
 
684
        SQLRowValues vals = new SQLRowValues(getTable().getBase().getTable("POSTE_ANALYTIQUE"));
685
        vals.put("NOM", p.getNom());
686
        try {
687
            vals.update(p.getId());
688
        } catch (SQLException e) {
689
            System.err.println("Erreur modification Poste " + p.getId());
690
            e.printStackTrace();
691
        }
692
 
693
        // mise à jour du vecteur
694
        p.setCreation(false);
695
        p.setModif(false);
696
 
697
        System.out.println("Poste Modifié  -->" + p.toString());
698
    }
699
 
700
    private void creerRepartitionElementTable(RepartitionElement repElem) {
701
 
702
        Map m = new HashMap();
703
        m.put("ID_REPARTITION_ANALYTIQUE", this.mapRepartitons.get(new Integer(repElem.getIdRep())));
704
        m.put("ID_POSTE_ANALYTIQUE", this.mapPostes.get(new Integer(repElem.getIdPoste())));
705
        m.put("TAUX", new Float(repElem.getTaux()));
706
 
707
        SQLRowValues val = new SQLRowValues(getTable().getBase().getTable("REPARTITION_ANALYTIQUE_ELEMENT"), m);
708
 
709
        try {
710
 
711
            if (val.getInvalid() == null) {
712
                SQLRow row = val.insert();
713
                System.out.println("Row ID -->" + row.getID());
714
                repElem.setId(row.getID());
715
                repElem.setIdRep(((Integer) this.mapRepartitons.get(new Integer(repElem.getIdRep()))).intValue());
716
                repElem.setIdPoste(((Integer) this.mapPostes.get(new Integer(repElem.getIdPoste()))).intValue());
717
            } else {
718
                System.out.println("--------------------------> Element supprimé");
719
                repElem.setDeleted(true);
720
            }
721
 
722
        } catch (SQLException e) {
723
            System.err.println("Error insert row in " + val.getTable().getName());
724
            e.printStackTrace();
725
        }
726
 
727
        // mise à jour du vecteur
728
        repElem.setCreation(false);
729
        repElem.setModif(false);
730
 
731
        System.out.println("RepElement Crée  -->" + repElem.toString());
732
    }
733
 
734
    private void supprimerRepartitionElementTable(RepartitionElement repElem) {
735
 
736
        SQLRowValues vals = new SQLRowValues(getTable().getBase().getTable("REPARTITION_ANALYTIQUE_ELEMENT"));
737
        vals.put(getTable().getBase().getTable("REPARTITION_ANALYTIQUE_ELEMENT").getArchiveField().getName(), new Integer(1));
738
        try {
739
            vals.update(repElem.getId());
740
        } catch (SQLException e) {
741
            System.err.println("Erreur suppression Repartition Elem " + repElem.getId());
742
            e.printStackTrace();
743
        }
744
 
745
        System.out.println("RepElement Supprimée  -->" + repElem.toString());
746
    }
747
 
748
    private void modifierRepartitionElementTable(RepartitionElement repElem) {
749
 
750
        SQLRowValues vals = new SQLRowValues(getTable().getBase().getTable("REPARTITION_ANALYTIQUE_ELEMENT"));
751
        vals.put("TAUX", new Float(repElem.getTaux()));
752
        try {
753
            vals.update(repElem.getId());
754
        } catch (SQLException e) {
755
            System.err.println("Erreur suppression Repartition Elem " + repElem.getId());
756
            e.printStackTrace();
757
        }
758
 
759
        // mise à jour du vecteur
760
        repElem.setCreation(false);
761
        repElem.setModif(false);
762
 
763
        System.out.println("RepElement Modifié  -->" + repElem.toString());
764
    }
765
 
766
    private void setVal() {
767
 
768
        for (int i = 0; i < this.axes.size(); i++) {
769
            // on récupére un axe
770
            Axe axe = this.axes.get(i);
771
            this.mapPostes = new HashMap<Integer, Integer>();
772
            this.mapRepartitons = new HashMap<Integer, Integer>();
773
 
774
            if ((!(axe.getSuppression() && axe.getCreation())) || (!axe.getDeleted())) {
775
                if (axe.getSuppression()) {
776
 
777
                    supprimerAxeTable(axe);
778
                } else if (axe.getCreation()) {
779
 
780
                    creerAxeTable(axe);
781
                } else {
782
 
783
                    if (axe.getModif()) {
784
 
785
                        modifierAxeTable(axe);
786
                    }
787
                }
788
            }
789
 
790
            // on recupere le vecteur de repartition associé à l'axe
791
            List<Repartition> repartitions = this.repartitionsAxe.get(i);
792
 
793
            for (int j = 0; j < repartitions.size(); j++) {
794
                Repartition rep = repartitions.get(j);
795
 
796
                this.mapRepartitons.put(new Integer(rep.getId()), new Integer(rep.getId()));
797
 
798
                if ((!(rep.getSuppression() && rep.getCreation())) || (!rep.getDeleted())) {
799
                    if (rep.getCreation()) {
800
 
801
                        creerRepartitionTable(rep);
802
                    } else {
803
                        if (rep.getSuppression()) {
804
 
805
                            supprimerRepartitionTable(rep);
806
                        } else {
807
                            if (rep.getModif()) {
808
 
809
                                modifierRepartitionTable(rep);
810
                            }
811
                        }
812
                    }
813
                }
814
 
815
                // System.out.println("Map répartition : " + idtmp + " --> " +
816
                // this.mapRepartitons.get(Integer.valueOf(idtmp)));
817
            }
818
 
819
            // on recupere les postes de l'axe
820
            final List<Poste> postes = this.postesAxe.get(i);
821
 
822
            for (int j = 0; j < postes.size(); j++) {
823
                Poste p = postes.get(j);
824
 
825
                this.mapPostes.put(p.getId(), p.getId());
826
 
827
                if ((!(p.getSuppression() && p.getCreation())) || (!p.getDeleted())) {
828
                    if (p.getCreation()) {
829
 
830
                        creerPosteTable(p, axe.getId());
831
                    } else {
832
                        if (p.getSuppression()) {
833
 
834
                            supprimerPosteTable(p);
835
                        } else {
836
                            if (p.getModif()) {
837
 
838
                                modifierPosteTable(p);
839
                            }
840
                        }
841
                    }
842
                }
843
 
844
                // System.out.println("Map postes : " + idtmp + " --> " +
845
                // this.mapPostes.get(Integer.valueOf(idtmp)));
846
            }
847
 
848
            // on recupere les éléments de répartitions de l'axe
849
            final List<RepartitionElement> repartitionElems = this.repartitionElemsAxe.get(i);
850
 
851
            for (int j = 0; j < repartitionElems.size(); j++) {
852
                RepartitionElement repElem = repartitionElems.get(j);
853
 
854
                if ((!(repElem.getSuppression() && repElem.getCreation())) || (!repElem.getDeleted())) {
855
                    if (repElem.getCreation()) {
856
 
857
                        creerRepartitionElementTable(repElem);
858
                    } else {
859
                        if (repElem.getSuppression()) {
860
 
861
                            supprimerRepartitionElementTable(repElem);
862
                        } else {
863
                            if (repElem.getModif()) {
864
 
865
                                modifierRepartitionElementTable(repElem);
866
                            }
867
                        }
868
                    }
869
                }
870
            }
871
        }
872
    }
873
 
874
    private void checkID() {
875
 
876
        final int size = this.axes.size();
877
        for (int i = 0; i < size; i++) {
878
 
879
            // on recupere les éléments de répartitions de l'axe
880
            final List<RepartitionElement> repartitionElems = this.repartitionElemsAxe.get(i);
881
            final int repartitionElemsCount = repartitionElems.size();
882
            final List<Poste> postes = this.postesAxe.get(i);
883
            final int postesCount = postes.size();
884
            final List<Repartition> repartitions = this.repartitionsAxe.get(i);
885
            final int repartitionsCount = repartitions.size();
886
 
887
            for (int j = 0; j < repartitionElemsCount; j++) {
888
                final RepartitionElement repElem = repartitionElems.get(j);
889
                if (!repElem.getSuppression() || !repElem.getDeleted()) {
890
 
891
                    for (int k = 0; k < postesCount; k++) {
892
                        final Poste poste = postes.get(k);
893
                        if (repElem.getIdPoste() == poste.getId() && poste.getSuppression() && poste.getDeleted()) {
894
                            System.out.println("Probleme repElem " + repElem.toString());
895
                        }
896
                    }
897
 
898
                    for (int k = 0; k < repartitionsCount; k++) {
899
                        final Repartition repartition = repartitions.get(k);
900
                        if (repElem.getIdRep() == repartition.getId() && repartition.getSuppression() && repartition.getDeleted()) {
901
                            System.out.println("Probleme repElem " + repElem.toString());
902
                        }
903
                    }
904
                }
905
            }
906
        }
907
    }
908
}