Salome HOME
Attribute DAO
[modules/gde.git] / projects / GDE_App / GDE-ejb / src / java / com / edf / gde / entities / AttributeGroup.java
index 53809f370a91ffcf4a8a964cee465afe4b682678..5fe68e6468e62494dff9652ce47fbeff799e998d 100644 (file)
@@ -1,20 +1,22 @@
 /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
+ * (C) 2015 EDF
  */
 package com.edf.gde.entities;
 
+import com.edf.gde.transferables.AttributeGroupTO;
+import com.edf.gde.transferables.AttributeTO;
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Objects;
 import javax.persistence.Basic;
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
-import javax.persistence.ManyToMany;
 import javax.persistence.NamedQueries;
 import javax.persistence.NamedQuery;
 import javax.persistence.OneToMany;
@@ -36,6 +38,7 @@ import javax.xml.bind.annotation.XmlTransient;
     @NamedQuery(name = "AttributeGroup.findById", query = "SELECT a FROM AttributeGroup a WHERE a.id = :id")
 })
 public class AttributeGroup implements Serializable {
+
     private static final long serialVersionUID = 1L;
     @Id
     @Basic(optional = false)
@@ -44,7 +47,8 @@ public class AttributeGroup implements Serializable {
     @SequenceGenerator(name = "SEQ_GEN_SEQUENCE", allocationSize = 50)
     @Column(name = "id")
     private long id;
-    @OneToMany(mappedBy = "groupId")
+    
+    @OneToMany (fetch = FetchType.EAGER, orphanRemoval = true, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
     private Collection<Attribute> attributeCollection;
 
     public AttributeGroup() {
@@ -71,6 +75,34 @@ public class AttributeGroup implements Serializable {
         this.attributeCollection = attributeCollection;
     }
 
+    public static AttributeGroup fromAttributeGroupTO(AttributeGroupTO agto) {
+        AttributeGroup group = new AttributeGroup();
+        group.id = agto.getId();
+        Collection<AttributeTO> attributes = agto.getAttributeCollection();
+        if (attributes != null) {
+            group.attributeCollection = new ArrayList<>();
+            for (AttributeTO ato : attributes) {
+                group.attributeCollection.add(Attribute.fromAttributeTO(ato));
+            }
+        }
+        return group;
+    }
+
+    public AttributeGroupTO toAttributeGroupTO() {
+        AttributeGroupTO agto = new AttributeGroupTO();
+        agto.setId(this.id);
+
+        Collection<AttributeTO> atos = new ArrayList<>();
+        if (attributeCollection != null) {
+            for (Attribute a : this.attributeCollection) {
+                atos.add(a.toAttributeTO());
+            }
+            agto.setAttributeCollection(atos);
+        }
+
+        return agto;
+    }
+
     @Override
     public String toString() {
         return "com.edf.gde.entities.AttributeGroup[ id=" + id + " ]";