From adf30437afa6253a4bb1e7556daa121f17a24b98 Mon Sep 17 00:00:00 2001 From: Bojnourdi Date: Thu, 13 Aug 2015 15:04:27 +0200 Subject: [PATCH] Attribute DAO --- .../{MetadataDao.java => AttributeDao.java} | 38 +++++++++++++++---- .../src/java/com/edf/gde/ejb/MetadataEJB.java | 26 ++++++------- .../java/com/edf/gde/entities/Attribute.java | 3 +- .../com/edf/gde/entities/AttributeGroup.java | 29 ++++++++------ .../edf/gde/transferables/AttributeTO.java | 36 ++++++++++++++++-- .../edf/gde/services/AttributesService.java | 2 +- .../GDE-war/test/com/edf/gde/dao/BaseDao.java | 4 -- .../edf/gde/test/dao/AttributeDaoTest.java | 36 +++++++++++++++++- projects/GDE_App/src/GDE_DB_Init.sql | 8 ++++ 9 files changed, 139 insertions(+), 43 deletions(-) rename projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/{MetadataDao.java => AttributeDao.java} (70%) 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/AttributeDao.java similarity index 70% rename from projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/MetadataDao.java rename to projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/AttributeDao.java index bdfc239..eaf8e69 100644 --- 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/AttributeDao.java @@ -7,17 +7,20 @@ 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 MetadataDao { +public class AttributeDao { private EntityManager em; - public MetadataDao(EntityManager em) { + public AttributeDao(EntityManager em) { this.em = em; } @@ -27,8 +30,8 @@ public class MetadataDao { return a.toAttributeTO(); } - public void deleteAttribute(long attributeId ) { - Attribute a = new Attribute(attributeId); + public void deleteAttribute(long attributeId) { + Attribute a = em.find(Attribute.class, attributeId); em.remove(a); } @@ -66,21 +69,42 @@ public class MetadataDao { return findBy("Attribute.findByValue", "value", value); } - /* Attributes Groups */ + /** + * + * @param agto + * @return + */ public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) { AttributeGroup group = AttributeGroup.fromAttributeGroupTO(agto); + AttributeGroupTO ret = new AttributeGroupTO(); em.persist(group); - return group.toAttributeGroupTO(); + ret.setId(group.getId()); + + Collection l = agto.getAttributeCollection(); + List 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 = new AttributeGroup(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 attributeCollection = agto.getAttributeCollection(); + return up.toAttributeGroupTO(); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/MetadataEJB.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/MetadataEJB.java index fb94d72..3fad94b 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/MetadataEJB.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/MetadataEJB.java @@ -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); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java index ae6d036..2935df6 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Attribute.java @@ -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; diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java index 3a068ef..5fe68e6 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/AttributeGroup.java @@ -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 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 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 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 + " ]"; diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/transferables/AttributeTO.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/transferables/AttributeTO.java index 5c5cd68..4cfd511 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/transferables/AttributeTO.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/transferables/AttributeTO.java @@ -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; + } } diff --git a/projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java b/projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java index 4a05885..cd10869 100644 --- a/projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java +++ b/projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java @@ -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; diff --git a/projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java b/projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java index 7fb87e0..244bff5 100644 --- a/projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java +++ b/projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java @@ -86,8 +86,6 @@ public abstract class BaseDao extends SimpleRestApi { protected T postCommand(CommandTO commandTO, Object object, Class 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(); diff --git a/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java b/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java index 91fde67..48d825e 100644 --- a/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java +++ b/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java @@ -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(); } } diff --git a/projects/GDE_App/src/GDE_DB_Init.sql b/projects/GDE_App/src/GDE_DB_Init.sql index 71a8445..369f5a9 100644 --- a/projects/GDE_App/src/GDE_DB_Init.sql +++ b/projects/GDE_App/src/GDE_DB_Init.sql @@ -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; -- 2.30.2