Salome HOME
Attribute DAO
authorBojnourdi <kavoos.bojnourdi@edf.fr>
Thu, 13 Aug 2015 13:04:27 +0000 (15:04 +0200)
committerBojnourdi <kavoos.bojnourdi@edf.fr>
Thu, 13 Aug 2015 13:04:27 +0000 (15:04 +0200)
projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java [new file with mode: 0644]
projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/MetadataDao.java [deleted file]
projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/MetadataEJB.java
projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java
projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java
projects/GDE_App/GDE-ejb/src/java/com/edf/gde/transferables/AttributeTO.java
projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java
projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java
projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java
projects/GDE_App/src/GDE_DB_Init.sql

diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java
new file mode 100644 (file)
index 0000000..eaf8e69
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ * (C) 2015 EDF
+ */
+package com.edf.gde.dao;
+
+import com.edf.gde.entities.Attribute;
+import com.edf.gde.entities.AttributeGroup;
+import com.edf.gde.transferables.AttributeGroupTO;
+import com.edf.gde.transferables.AttributeTO;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import javax.persistence.EntityManager;
+
+/**
+ *
+ * @author Kavoos
+ */
+public class AttributeDao {
+
+    private EntityManager em;
+
+    public AttributeDao(EntityManager em) {
+        this.em = em;
+    }
+
+    public AttributeTO createAttribute(AttributeTO ato) {
+        Attribute a = Attribute.fromAttributeTO(ato);
+        em.persist(a);
+        return a.toAttributeTO();
+    }
+
+    public void deleteAttribute(long attributeId) {
+        Attribute a = em.find(Attribute.class, attributeId);
+        em.remove(a);
+    }
+
+    public AttributeTO updateAttribute(AttributeTO ato) {
+        Attribute a = Attribute.fromAttributeTO(ato);
+        Attribute up = em.merge(a);
+        return up.toAttributeTO();
+    }
+
+    public AttributeTO readAttribute(long attributeId) {
+        Attribute found = em.find(Attribute.class, attributeId);
+        return found.toAttributeTO();
+    }
+
+    private <T> AttributeTO findBy(String queryName, String varName, T value) {
+        Attribute found = (Attribute) em.createNamedQuery(queryName)
+                .setParameter(varName, value)
+                .getSingleResult();
+        return found.toAttributeTO();
+    }
+
+    public AttributeTO findById(long id) {
+        return findBy("Attribute.findById", "id", id);
+    }
+
+    public AttributeTO findByName(String name) {
+        return findBy("Attribute.findByName", "name", name);
+    }
+
+    public AttributeTO findByType(String type) {
+        return findBy("Attribute.findByType", "type", type);
+    }
+
+    public AttributeTO findByValue(String value) {
+        return findBy("Attribute.findByValue", "value", value);
+    }
+
+    /**
+     *
+     * @param agto
+     * @return
+     */
+    public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) {
+        AttributeGroup group = AttributeGroup.fromAttributeGroupTO(agto);
+        AttributeGroupTO ret = new AttributeGroupTO();
+        em.persist(group);
+        ret.setId(group.getId());
+
+        Collection<AttributeTO> l = agto.getAttributeCollection();
+        List<AttributeTO> newAttributes = new ArrayList<>();
+
+        if (l != null) {
+            for (AttributeTO attributeTO : l) {
+                AttributeTO ato = createAttribute(attributeTO);
+                newAttributes.add(ato);
+            }
+        }
+        ret.setAttributeCollection(newAttributes);
+        return ret;
+    }
+
+    public void deleteAttributeGroup(long id) {
+        AttributeGroup group = em.find(AttributeGroup.class, id);
+        em.remove(group);
+        em.flush();
+        em.clear();
+    }
+
+    public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) {
+        AttributeGroup group = AttributeGroup.fromAttributeGroupTO(agto);
+        AttributeGroup up = em.merge(group);
+        Collection<AttributeTO> attributeCollection = agto.getAttributeCollection();
+        
+        return up.toAttributeGroupTO();
+    }
+
+    public AttributeGroupTO readAttributeGroup(long groupId) {
+        AttributeGroup found = (AttributeGroup) em.createNamedQuery("AttributeGroup.findById")
+                .setParameter("id", groupId)
+                .getSingleResult();
+        return found.toAttributeGroupTO();
+    }
+}
diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/MetadataDao.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/MetadataDao.java
deleted file mode 100644 (file)
index bdfc239..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * (C) 2015 EDF
- */
-package com.edf.gde.dao;
-
-import com.edf.gde.entities.Attribute;
-import com.edf.gde.entities.AttributeGroup;
-import com.edf.gde.transferables.AttributeGroupTO;
-import com.edf.gde.transferables.AttributeTO;
-import javax.persistence.EntityManager;
-
-/**
- *
- * @author Kavoos
- */
-public class MetadataDao {
-
-    private EntityManager em;
-
-    public MetadataDao(EntityManager em) {
-        this.em = em;
-    }
-
-    public AttributeTO createAttribute(AttributeTO ato) {
-        Attribute a = Attribute.fromAttributeTO(ato);
-        em.persist(a);
-        return a.toAttributeTO();
-    }
-
-    public void deleteAttribute(long attributeId ) {
-        Attribute a = new Attribute(attributeId);
-        em.remove(a);
-    }
-
-    public AttributeTO updateAttribute(AttributeTO ato) {
-        Attribute a = Attribute.fromAttributeTO(ato);
-        Attribute up = em.merge(a);
-        return up.toAttributeTO();
-    }
-
-    public AttributeTO readAttribute(long attributeId) {
-        Attribute found = em.find(Attribute.class, attributeId);
-        return found.toAttributeTO();
-    }
-
-    private <T> AttributeTO findBy(String queryName, String varName, T value) {
-        Attribute found = (Attribute) em.createNamedQuery(queryName)
-                .setParameter(varName, value)
-                .getSingleResult();
-        return found.toAttributeTO();
-    }
-
-    public AttributeTO findById(long id) {
-        return findBy("Attribute.findById", "id", id);
-    }
-
-    public AttributeTO findByName(String name) {
-        return findBy("Attribute.findByName", "name", name);
-    }
-
-    public AttributeTO findByType(String type) {
-        return findBy("Attribute.findByType", "type", type);
-    }
-
-    public AttributeTO findByValue(String value) {
-        return findBy("Attribute.findByValue", "value", value);
-    }
-
-    /* Attributes Groups */
-    public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) {
-        AttributeGroup group = AttributeGroup.fromAttributeGroupTO(agto);
-        em.persist(group);
-        return group.toAttributeGroupTO();
-    }
-
-    public void deleteAttributeGroup(long id) {
-        AttributeGroup group = new AttributeGroup(id);
-        em.remove(group);
-    }
-
-    public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) {
-        AttributeGroup group = AttributeGroup.fromAttributeGroupTO(agto);
-        AttributeGroup up = em.merge(group);
-        return up.toAttributeGroupTO();
-    }
-
-    public AttributeGroupTO readAttributeGroup(long groupId) {
-        AttributeGroup found = (AttributeGroup) em.createNamedQuery("AttributeGroup.findById")
-                .setParameter("id", groupId)
-                .getSingleResult();
-        return found.toAttributeGroupTO();
-    }
-}
index fb94d72c005aa4059375a62162d888f6789b0fcb..3fad94b0f50e02887e69d5bb38b393f27ac242b2 100644 (file)
@@ -3,7 +3,7 @@
  */
 package com.edf.gde.ejb;
 
-import com.edf.gde.dao.MetadataDao;
+import com.edf.gde.dao.AttributeDao;
 import com.edf.gde.transferables.AttributeGroupTO;
 import com.edf.gde.transferables.AttributeTO;
 import javax.ejb.Stateless;
@@ -24,63 +24,63 @@ public class MetadataEJB {
 
     /* Attributes */
     public AttributeTO createAttribute(AttributeTO ato) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.createAttribute(ato);
     }
 
     public void deleteAttribute(long attributeId) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         dao.deleteAttribute(attributeId);
     }
 
     public AttributeTO updateAttribute(AttributeTO ato) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.updateAttribute(ato);
     }
 
     public AttributeTO readAttribute(long attributeId) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.readAttribute(attributeId);
     }
 
     public AttributeTO findById(long id) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.findById(id);
     }
 
     public AttributeTO findByName(String name) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.findByName(name);
     }
 
     public AttributeTO findByType(String type) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.findByType(type);
     }
 
     public AttributeTO findByValue(String value) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.findByValue(value);
     }
 
     /* Attributes Groups */
     public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.createAttributeGroup(agto);
     }
 
     public void deleteAttributeGroup(long id) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         dao.deleteAttributeGroup(id);
     }
 
     public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.updateAttributeGroup(agto);
     }
 
     public AttributeGroupTO readAttributeGroup(long groupId) {
-        MetadataDao dao = new MetadataDao(em);
+        AttributeDao dao = new AttributeDao(em);
         return dao.readAttributeGroup(groupId);
     }
 
index ae6d036a25d49066c822afe9ea1058ce05d10a5d..2935df67bb31c515478047d2e532b646c582de22 100644 (file)
@@ -32,7 +32,8 @@ import javax.xml.bind.annotation.XmlRootElement;
     @NamedQuery(name = "Attribute.findById", query = "SELECT a FROM Attribute a WHERE a.id = :id"),
     @NamedQuery(name = "Attribute.findByName", query = "SELECT a FROM Attribute a WHERE a.name = :name"),
     @NamedQuery(name = "Attribute.findByType", query = "SELECT a FROM Attribute a WHERE a.type = :type"),
-    @NamedQuery(name = "Attribute.findByValue", query = "SELECT a FROM Attribute a WHERE a.value = :value")
+    @NamedQuery(name = "Attribute.findByValue", query = "SELECT a FROM Attribute a WHERE a.value = :value"),
+    @NamedQuery(name = "Attribute.findByGroupId", query = "SELECT a FROM Attribute a WHERE a.attributeGroupId = :attributeGroupId")
 })
 public class Attribute implements Serializable {
     private static final long serialVersionUID = 1L;
index 3a068efd9098abd950eaeea03f52b77a5eb9650a..5fe68e6468e62494dff9652ce47fbeff799e998d 100644 (file)
@@ -38,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)
@@ -46,7 +47,8 @@ public class AttributeGroup implements Serializable {
     @SequenceGenerator(name = "SEQ_GEN_SEQUENCE", allocationSize = 50)
     @Column(name = "id")
     private long id;
-    @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, cascade = {CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.REFRESH})
+    
+    @OneToMany (fetch = FetchType.EAGER, orphanRemoval = true, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
     private Collection<Attribute> attributeCollection;
 
     public AttributeGroup() {
@@ -76,28 +78,31 @@ public class AttributeGroup implements Serializable {
     public static AttributeGroup fromAttributeGroupTO(AttributeGroupTO agto) {
         AttributeGroup group = new AttributeGroup();
         group.id = agto.getId();
-        
-        group.attributeCollection = new ArrayList<>();
         Collection<AttributeTO> attributes = agto.getAttributeCollection();
-        for (AttributeTO ato : attributes) {
-            group.attributeCollection.add(Attribute.fromAttributeTO(ato));
+        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<>();
-        for (Attribute a : this.attributeCollection) {
-            atos.add(a.toAttributeTO());
+        if (attributeCollection != null) {
+            for (Attribute a : this.attributeCollection) {
+                atos.add(a.toAttributeTO());
+            }
+            agto.setAttributeCollection(atos);
         }
-        agto.setAttributeCollection(atos);
+
         return agto;
     }
-    
+
     @Override
     public String toString() {
         return "com.edf.gde.entities.AttributeGroup[ id=" + id + " ]";
index 5c5cd68cdadd3e4f4a7818b2873b68b2f44f3f87..4cfd5116efa2437f4c32e5600928a3243d65b92c 100644 (file)
@@ -4,6 +4,7 @@
 package com.edf.gde.transferables;
 
 import java.io.Serializable;
+import java.util.Objects;
 
 /**
  *
@@ -17,12 +18,12 @@ public class AttributeTO implements Serializable {
     private String type;
     private String value;
     private long groupId;
-    private Boolean mandatory;
+    private boolean mandatory;
 
     public AttributeTO() {
     }
 
-    public AttributeTO(long id, String name, String type, String value, long groupId, Boolean mandatory) {
+    public AttributeTO(long id, String name, String type, String value, long groupId, boolean mandatory) {
         this.id = id;
         this.name = name;
         this.type = type;
@@ -71,12 +72,39 @@ public class AttributeTO implements Serializable {
         this.groupId = groupId;
     }
 
-    public Boolean getMandatory() {
+    public boolean getMandatory() {
         return mandatory;
     }
 
-    public void setMandatory(Boolean mandatory) {
+    public void setMandatory(boolean mandatory) {
         this.mandatory = mandatory;
     }
+
+    @Override
+    public int hashCode() {
+        int hash = 3;
+        hash = 67 * hash + (int) (this.id ^ (this.id >>> 32));
+        hash = 67 * hash + Objects.hashCode(this.name);
+        hash = 67 * hash + Objects.hashCode(this.type);
+        hash = 67 * hash + Objects.hashCode(this.value);
+        hash = 67 * hash + (int) (this.groupId ^ (this.groupId >>> 32));
+        hash = 67 * hash + (this.mandatory ? 1 : 0);
+        return hash;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final AttributeTO other = (AttributeTO) obj;
+        if (this.id != other.id) {
+            return false;
+        }
+        return true;
+    }
     
 }
index 4a058854265397864b405c529c5409bc633b4811..cd10869273c22a39e686d0074b4027a24ad9594d 100644 (file)
@@ -73,7 +73,7 @@ public class AttributesService extends BaseService {
                 }
                 break;
                 case DELETEATTRIBUTEGROUP: {
-                    long attributeGroupId = commandTO.getLong("attributeLongId");
+                    long attributeGroupId = commandTO.getLong("attributeGroupId");
                     mjb.deleteAttributeGroup(attributeGroupId);
                 }
                 break;
index 7fb87e056d5848b3fa95c43b7ecbaeb32239b733..244bff5e861f17af8fb04f4c11b2044d485e3811 100644 (file)
@@ -86,8 +86,6 @@ public abstract class BaseDao extends SimpleRestApi {
     protected <T> T postCommand(CommandTO commandTO, Object object, Class<T> classOf) throws IOException {
         if (object != null) {
             commandTO.setData(toJson(object));
-        } else {
-            commandTO = null;
         }
         if (postAsJSonData(commandTO, daoResponseHandler)) {
             CommandResultTO resultTO = daoResponseHandler.getResultTO();
@@ -108,8 +106,6 @@ public abstract class BaseDao extends SimpleRestApi {
     protected void postCommand(CommandTO commandTO, Object object) throws IOException {
         if (object != null) {
             commandTO.setData(toJson(object));
-        } else {
-            commandTO = null;
         }
         if (postAsJSonData(commandTO, daoResponseHandler)) {
             CommandResultTO resultTO = daoResponseHandler.getResultTO();
index 91fde671ea2b8411137f6e32f45b1014b43517d5..48d825ee8fc80bbdc062f30e3d938ecfa719f94f 100644 (file)
@@ -5,8 +5,14 @@ package com.edf.gde.test.dao;
 
 import com.edf.gde.dao.AttributeDaoClient;
 import com.edf.gde.test.base.BaseTest;
+import com.edf.gde.transferables.AttributeGroupTO;
+import com.edf.gde.transferables.AttributeTO;
 import org.junit.After;
 import org.junit.AfterClass;
+import org.junit.Assert;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -35,7 +41,35 @@ public class AttributeDaoTest extends BaseTest {
 
     @Test
     public void testCreateAttribute() throws Exception {
-        testName("createAttribute");
         AttributeDaoClient daoClient = new AttributeDaoClient();
+        testName("createAttribute");
+        
+        /* Create an instance of AttributeGroup*/
+        AttributeGroupTO attributeGroup = new AttributeGroupTO();
+        AttributeGroupTO resultAttributeGroup = daoClient.createAttributeGroup(attributeGroup);
+        assertNotNull(resultAttributeGroup);
+        assertTrue(resultAttributeGroup.getId()>0);
+        AttributeTO ato = new AttributeTO();
+        ato.setName("Attribute1");
+        ato.setType("integer");
+        ato.setValue("12");
+        ato.setGroupId(resultAttributeGroup.getId());
+        AttributeTO newAttribute = daoClient.createAttribute(ato);
+        
+        assertNotNull(newAttribute);
+        assertEquals(ato.getName(),newAttribute.getName());
+        assertTrue(newAttribute.getId()!=0);
+        try {
+        daoClient.deleteAttribute(newAttribute.getId());
+        } catch (Exception e) {
+            Assert.fail("Error deleting attribute");
+        }
+        try {
+        System.out.println("Deleting group " + resultAttributeGroup.getId());
+        daoClient.deleteAttributeGroup(resultAttributeGroup.getId());
+        } catch (Exception e) {
+            Assert.fail("Error deleting attribute group");
+        }
+        passed();
     }
 }
index 71a844563a720a539118313ca293549a4c6657a8..369f5a9e35d165c78ee8adbfa49e96a7791874dd 100644 (file)
@@ -51,6 +51,14 @@ CREATE TABLE attribute (
     mandatory boolean
 );
 
+DROP TABLE IF EXISTS attribute_group_attribute CASCADE;
+CREATE TABLE attribute_group_attribute (
+    attributegroup_id bigint REFERENCES attribute_group (id),
+    attribute_id bigint REFERENCES attribute(id)
+);
+CREATE INDEX attribute_group_attribute_idx1 on attribute_group_attribute(attributegroup_id);
+CREATE INDEX attribute_group_attribute_idx2 on attribute_group_attribute(attribute_id);
+
 /* DATA */
 
 DROP TABLE IF EXISTS gde_file CASCADE;