]> SALOME platform Git repositories - modules/gde.git/blob - projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/MetadataDAO.java
Salome HOME
Add study
[modules/gde.git] / projects / GDE_App / GDE-ejb / src / java / com / edf / gde / ejb / MetadataDAO.java
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package com.edf.gde.ejb;
7
8 import com.edf.gde.entities.Attribute;
9 import com.edf.gde.entities.AttributeGroup;
10 import com.edf.gde.transferables.AttributeGroupTO;
11 import com.edf.gde.transferables.AttributeTO;
12 import javax.ejb.Stateless;
13 import javax.ejb.LocalBean;
14 import javax.persistence.EntityManager;
15 import javax.persistence.PersistenceContext;
16
17 /**
18  *
19  * @author F62173
20  */
21 @Stateless
22 @LocalBean
23 public class MetadataDAO {
24     @PersistenceContext(unitName = "GDE-ejbPU")
25     private EntityManager em;
26
27     /* Attributes */
28     
29     public AttributeTO createAttribute(AttributeTO ato) {
30         Attribute a = Attribute.fromAttributeTO(ato);
31         em.persist(a);
32         return a.toAttributeTO();
33     }
34
35     public void deleteAttribute(AttributeTO ato) {
36         Attribute a = Attribute.fromAttributeTO(ato);
37         em.remove(a);
38     }
39  
40     public AttributeTO updateAttribute(AttributeTO ato) {
41         Attribute a = Attribute.fromAttributeTO(ato);
42         Attribute up = em.merge(a);
43         return up.toAttributeTO();
44     }
45     
46     public AttributeTO findAttribute(AttributeTO ato) {
47         Attribute found = em.find(Attribute.class, ato.getId());
48         return found.toAttributeTO();
49     }
50     
51     private <T> AttributeTO findBy(String queryName, String varName, T value) {
52         try {
53             Attribute found = (Attribute) em.createNamedQuery(queryName)
54                                           .setParameter(varName, value)
55                                           .getSingleResult();
56             return found.toAttributeTO();
57         }
58         catch (Exception e) {
59             return null;
60         }
61     }
62     
63     public AttributeTO findById(long id) {
64         return findBy("Attribute.findById", "id", id);
65     }
66     
67     public AttributeTO findByName(String name) {
68         return findBy("Attribute.findByName", "name", name);
69     }
70     
71     public AttributeTO findByType(String type) {
72         return findBy("Attribute.findByType", "type", type);
73     }
74     
75     public AttributeTO findByValue(String value) {
76         return findBy("Attribute.findByValue", "value", value);
77     }
78
79     /* Attributes Groups */
80     
81     public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) {
82         AttributeGroup group = AttributeGroup.fromAttributeGroupTO(agto);
83         em.persist(group);
84         return group.toAttributeGroupTO();
85     }
86
87     public void deleteAttributeGroup(AttributeGroupTO agto) {
88         AttributeGroup group = AttributeGroup.fromAttributeGroupTO(agto);
89         em.remove(group);
90     }
91  
92     public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) {
93         AttributeGroup group = AttributeGroup.fromAttributeGroupTO(agto);
94         AttributeGroup up = em.merge(group);
95         return up.toAttributeGroupTO();
96     }
97     
98     public AttributeGroupTO findAttributeGroup(AttributeGroupTO agto) {
99         AttributeGroup found = em.find(AttributeGroup.class, agto.getId());
100         return found.toAttributeGroupTO();
101     }
102     
103     public AttributeGroupTO findGroupById(long groupId) {
104         try {
105             AttributeGroup found = (AttributeGroup) em.createNamedQuery("AttributeGroup.findById")
106                                     .setParameter("id", groupId)
107                                     .getSingleResult();
108             return found.toAttributeGroupTO();
109         }
110         catch (Exception e) {
111             return null;
112         }
113     }
114     
115 }