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