Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.fdxconsultation.core.impl.integration;
 
import com.megatim.fdxcommons.tools.database.connection.DBConnection;
import com.megatim.fdxcommons.tools.database.contrat.AppColumnDefinition;
import com.megatim.fdxcommons.tools.database.tables.FdxConsultationTable;
import com.megatim.fdxcommons.tools.database.tables.appcolumns.EnCoursProductionColumnDefinition;
import com.megatim.fdxconsultation.core.ifaces.integration.AppColumnDeleter;
import com.megatim.fdxconsultation.core.ifaces.integration.ColumnDefinitionManager;
import com.megatim.fdxconsultation.tools.context.AppCommonContext;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.persistence.Tuple;
import javax.sql.DataSource;
 
/**
 *
 * @author ASUS
 */
@Dependent
public class AppColumnDeleterImpl implements AppColumnDeleter {
 
    @Inject
    ColumnDefinitionManager columnDefinitionManager;
 
    @Resource(lookup = AppCommonContext.JNDI_NAME)
    private DataSource dataSource;
 
    @Override
    public void delete() {
        try ( Connection connection = new DBConnection(dataSource).connection()) {
            List<Tuple> refVersionAndTypeFichier = columnDefinitionManager.byRefVersionAndByTypeFichier();
 
            refVersionAndTypeFichier.stream().forEach(r -> {
                FdxConsultationTable consTable = new FdxConsultationTable((String) r.get(0), (String) r.get(1));
 
                try {
                    for (AppColumnDefinition appColumn : columnsToDrop()) {
                        if (consTable.checkIfTableExists(connection)) {
                            if (consTable.checkIfConstraintExists(connection)) {
                                consTable.dropPkConstraint(connection);
                            }
                            if (consTable.checkIfColumnExists(connection, appColumn.name())) {
                                consTable.dropAppColumn(connection, appColumn);
                            }
                            consTable.addPkConstraint(connection);
                        }
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(AppColumnCreatorImpl.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
                }
            });
        } catch (SQLException ex) {
            Logger.getLogger(AppColumnCreatorImpl.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
 
    private List<AppColumnDefinition> columnsToDrop() {
        return new ArrayList<>(Arrays.asList(
                new EnCoursProductionColumnDefinition()
        ));
    }
}