--- /dev/null
+/*
+ * (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();
+ }
+}
+++ /dev/null
-/*
- * (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();
- }
-}
*/
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;
/* 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);
}
@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;
@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)
@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() {
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 + " ]";
package com.edf.gde.transferables;
import java.io.Serializable;
+import java.util.Objects;
/**
*
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;
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;
+ }
}
}
break;
case DELETEATTRIBUTEGROUP: {
- long attributeGroupId = commandTO.getLong("attributeLongId");
+ long attributeGroupId = commandTO.getLong("attributeGroupId");
mjb.deleteAttributeGroup(attributeGroupId);
}
break;
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();
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();
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;
@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();
}
}
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;