Kenmegne
6 days ago 908e81dd173f380879d5fabeb5794d5b7a76df0e
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * 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.fdxconvert.dao;
 
import com.megatim.fdxconvert.model.AlphaNumeriqueField;
import com.megatim.fdxconvert.model.ConversionModel;
import com.megatim.fdxconvert.model.Tache;
import com.megatim.fdxconvert.model.TypeFichier;
import com.megatim.fdxconvert.model.Validateur;
import java.util.List;
import java.util.Optional;
import org.hibernate.Session;
import org.hibernate.query.Query;
 
/**
 *
 * @author STEPHANIE
 */
public class TypeFichierDAO {
 
    private TypeFichierDAO() {
 
    }
 
    public static List<TypeFichier> getAllTypeFichWithoutValidateur() {
        try ( Session session = HibernateUtil.getSessionFactory().openSession()) {
            Query query = session.createQuery("select t from TypeFichier t where t.code not in"
                    + "(select t from TypeFichier t, Validateur v  where t.code = v.codeTypeFichier)");
            return query.getResultList();
        }
    }
 
    public static Optional<TypeFichier> findByCode(TypeFichier t) {
        try ( Session session = HibernateUtil.getSessionFactory().openSession()) {
            Query query = session.createQuery("select t from TypeFichier t where t.code = :code");
            query.setParameter("code", t.getCode());
            return query.uniqueResultOptional();
        }
    }
 
    public static Optional<TypeFichier> findByCode(String codeTypeFichier) {
        try ( Session session = HibernateUtil.getSessionFactory().openSession()) {
            Query query = session.createQuery("select t from TypeFichier t where t.code = :code");
            query.setParameter("code", codeTypeFichier);
            return query.uniqueResultOptional();
        }
    }
 
    public static List<TypeFichier> getAllTypeFichWithoutParaAuto() {
        try ( Session session = HibernateUtil.getSessionFactory().openSession()) {
            Query query = session.createQuery("select t from TypeFichier t where t.code not in"
                    + "(select t from TypeFichier t, ParametreAutomatique p  where t.code = p.codeTypeFichier)");
            return query.getResultList();
        }
    }
 
    public static List<TypeFichier> getAllTypeFichierWithValidateur() {
        try ( Session session = HibernateUtil.getSessionFactory().openSession()) {
            Query query = session.createQuery("select t from TypeFichier t, Validateur v where v.codeTypeFichier = t.code");
            return query.getResultList();
        }
    }
    
    public static List<TypeFichier> getAllTypeFichierWithModelJson() {
        try ( Session session = HibernateUtil.getSessionFactory().openSession()) {
            Query query = session.createQuery("select t from TypeFichier t, ModeleJson m where t.code = m.typeFichier.code");
            return query.getResultList();
        }
    }
 
    public static void delete(TypeFichier typeFichier) throws Exception {
        List<ConversionModel> conversionModels = ConversionModelDAO.findByTypeFichier(typeFichier);
        Optional<Validateur> opt = ValidateurDAO.findByCodeTypeFichier(typeFichier.getCode());
        List<Tache> taches = TacheDAO.getTacheByTypeFichier(typeFichier);
        List<AlphaNumeriqueField> alphaFields = AlphaNumeriqueFieldDAO.findByCodeTypeFichier(typeFichier.getCode());
 
        try ( Session session = HibernateUtil.getSessionFactory().openSession()) {
            session.beginTransaction();
 
            for (ConversionModel model : conversionModels) {
                session.delete(model);
            }
 
            for(AlphaNumeriqueField field : alphaFields) {
                session.delete(field);
            }
            
            if (opt.isPresent()) {
                session.delete(opt.get());
            }
 
            for (Tache tache : taches) {
                session.delete(tache);
            }
            
            session.delete(typeFichier);
 
            session.getTransaction().commit();
        }
    }
 
}