+/*
+ * (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();
+ }
+}