From e9ee5e7d9e2d3498fea0fabbdde88507751a8d2b Mon Sep 17 00:00:00 2001 From: Kavoos Bojnourdi Date: Tue, 18 Aug 2015 23:50:47 +0200 Subject: [PATCH] Cleanup DAO The DAO should not see the transferables... Updated AttributeDao and ProfileDao + EJBs --- .../java/com/edf/gde/dao/AttributeDao.java | 28 ++++----- .../src/java/com/edf/gde/dao/ProfileDao.java | 16 ++--- .../edf/gde/dao/impl/AttributeDaoImpl.java | 59 ++++++++----------- .../com/edf/gde/dao/impl/ProfileDaoImpl.java | 40 ++++++------- .../java/com/edf/gde/ejb/AttributesEJB.java | 23 ++++---- .../src/java/com/edf/gde/ejb/ProfileEJB.java | 14 +++-- .../java/com/edf/gde/entities/Profile.java | 2 +- .../edf/gde/entities/ProfileAttribute.java | 4 +- 8 files changed, 84 insertions(+), 102 deletions(-) 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 index ccb4b5b..780bbc0 100644 --- 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 @@ -3,7 +3,8 @@ */ package com.edf.gde.dao; -import com.edf.gde.transferables.AttributeGroupTO; +import com.edf.gde.entities.Attribute; +import com.edf.gde.entities.AttributeGroup; import com.edf.gde.transferables.AttributeTO; /** @@ -12,33 +13,28 @@ import com.edf.gde.transferables.AttributeTO; */ public interface AttributeDao { - AttributeTO createAttribute(AttributeTO ato); + Attribute createAttribute(Attribute attribute); - /** - * - * @param agto - * @return - */ - AttributeGroupTO createAttributeGroup(AttributeGroupTO agto); + AttributeGroup createAttributeGroup(AttributeGroup agto); void deleteAttribute(long attributeId); void deleteAttributeGroup(long id); - AttributeTO findById(long id); + Attribute findById(long id); - AttributeTO findByName(String name); + Attribute findByName(String name); - AttributeTO findByType(String type); + Attribute findByType(String type); - AttributeTO findByValue(String value); + Attribute findByValue(String value); - AttributeTO readAttribute(long attributeId); + Attribute readAttribute(long attributeId); - AttributeGroupTO readAttributeGroup(long groupId); + AttributeGroup readAttributeGroup(long groupId); - AttributeTO updateAttribute(AttributeTO ato); + Attribute updateAttribute(Attribute ato); - AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto); + AttributeGroup updateAttributeGroup(AttributeGroup agto); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/ProfileDao.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/ProfileDao.java index 42253b8..0e9e589 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/ProfileDao.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/ProfileDao.java @@ -3,8 +3,8 @@ */ package com.edf.gde.dao; -import com.edf.gde.transferables.ProfileAttributeTO; -import com.edf.gde.transferables.ProfileTO; +import com.edf.gde.entities.Profile; +import com.edf.gde.entities.ProfileAttribute; /** * @@ -12,20 +12,20 @@ import com.edf.gde.transferables.ProfileTO; */ public interface ProfileDao { - ProfileTO createProfile(ProfileTO pto); + Profile createProfile(Profile pto); - ProfileAttributeTO createProfileAttribute(ProfileAttributeTO attributeTO); + ProfileAttribute createProfileAttribute(ProfileAttribute attribute); void deleteProfile(long profileId); void deleteProfileAttribute(long id); - ProfileTO readProfile(long profileId); + Profile readProfile(long profileId); - ProfileAttributeTO readProfileAttribute(long id); + ProfileAttribute readProfileAttribute(long id); - ProfileTO updateProfile(ProfileTO profileTO); + Profile updateProfile(Profile profile); - ProfileAttributeTO updateProfileAttribute(ProfileAttributeTO attributeTO); + ProfileAttribute updateProfileAttribute(ProfileAttribute attribute); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java index 7fcb879..51daa1a 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java @@ -6,9 +6,6 @@ package com.edf.gde.dao.impl; import com.edf.gde.dao.AttributeDao; 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.Collection; import javax.persistence.EntityManager; /** @@ -24,11 +21,10 @@ public class AttributeDaoImpl implements AttributeDao { } @Override - public AttributeTO createAttribute(AttributeTO ato) { - Attribute a = Attribute.fromAttributeTO(em, ato); - a.getAttributeGroup().getAttributes().add(a); + public Attribute createAttribute(Attribute attribute) { + attribute.getAttributeGroup().getAttributes().add(attribute); em.flush(); - return a.toAttributeTO(); + return attribute; } @Override @@ -38,60 +34,53 @@ public class AttributeDaoImpl implements AttributeDao { } @Override - public AttributeTO updateAttribute(AttributeTO ato) { - Attribute a = Attribute.fromAttributeTO(em, ato); - Attribute up = em.merge(a); - return up.toAttributeTO(); + public Attribute updateAttribute(Attribute attribute) { + Attribute up = em.merge(attribute); + return up; } @Override - public AttributeTO readAttribute(long attributeId) { + public Attribute readAttribute(long attributeId) { Attribute found = em.find(Attribute.class, attributeId); - return found.toAttributeTO(); + return found; } - private AttributeTO findBy(String queryName, String varName, T value) { + private Attribute findBy(String queryName, String varName, T value) { Attribute found = (Attribute) em.createNamedQuery(queryName) .setParameter(varName, value) .getSingleResult(); - return found.toAttributeTO(); + return found; } @Override - public AttributeTO findById(long id) { + public Attribute findById(long id) { return findBy("Attribute.findById", "id", id); } @Override - public AttributeTO findByName(String name) { + public Attribute findByName(String name) { return findBy("Attribute.findByName", "name", name); } @Override - public AttributeTO findByType(String type) { + public Attribute findByType(String type) { return findBy("Attribute.findByType", "type", type); } @Override - public AttributeTO findByValue(String value) { + public Attribute findByValue(String value) { return findBy("Attribute.findByValue", "value", value); } - /** - * - * @param agto - * @return - */ @Override - public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) { - AttributeGroup group = AttributeGroup.fromAttributeGroupTO(em, agto); + public AttributeGroup createAttributeGroup(AttributeGroup group) { em.persist(group); if (group.getAttributes() != null) { for (Attribute a : group.getAttributes()) { a.setAttributeGroup(group); } } - return group.toAttributeGroupTO(); + return group; } @Override @@ -103,23 +92,21 @@ public class AttributeDaoImpl implements AttributeDao { } @Override - public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) { - if (agto.getAttributeCollection() != null) { - for (AttributeTO attributeTO : agto.getAttributeCollection()) { - attributeTO.setGroupId(agto.getId()); + public AttributeGroup updateAttributeGroup(AttributeGroup group) { + if (group.getAttributes() != null) { + for (Attribute attribute : group.getAttributes()) { + attribute.setAttributeGroup(group); } } - AttributeGroup group = AttributeGroup.fromAttributeGroupTO(em, agto); AttributeGroup up = em.merge(group); - Collection attributeCollection = agto.getAttributeCollection(); - return up.toAttributeGroupTO(); + return up; } @Override - public AttributeGroupTO readAttributeGroup(long groupId) { + public AttributeGroup readAttributeGroup(long groupId) { AttributeGroup found = (AttributeGroup) em.createNamedQuery("AttributeGroup.findById") .setParameter("id", groupId) .getSingleResult(); - return found.toAttributeGroupTO(); + return found; } } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java index 7fdf553..d011174 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java @@ -6,8 +6,6 @@ package com.edf.gde.dao.impl; import com.edf.gde.dao.ProfileDao; import com.edf.gde.entities.Profile; import com.edf.gde.entities.ProfileAttribute; -import com.edf.gde.transferables.ProfileAttributeTO; -import com.edf.gde.transferables.ProfileTO; import javax.persistence.EntityManager; /** @@ -23,15 +21,14 @@ public class ProfileDaoImpl implements ProfileDao { } @Override - public ProfileTO createProfile(ProfileTO pto) { - Profile profile = Profile.fromProfileTO(em, pto); + public Profile createProfile(Profile profile) { em.persist(profile); - if (profile.getProfileAttributeCollection() != null) { - for (ProfileAttribute a : profile.getProfileAttributeCollection()) { + if (profile.getAttributes() != null) { + for (ProfileAttribute a : profile.getAttributes()) { a.setProfile(profile); } } - return profile.toProfileTO(); + return profile; } @Override @@ -41,30 +38,28 @@ public class ProfileDaoImpl implements ProfileDao { } @Override - public ProfileTO readProfile(long profileId) { + public Profile readProfile(long profileId) { Profile profile = em.find(Profile.class, profileId); - return profile.toProfileTO(); + return profile; } @Override - public ProfileTO updateProfile(ProfileTO profileTO) { - if (profileTO.getAttributes() != null) { - for (ProfileAttributeTO attributeTO : profileTO.getAttributes()) { - attributeTO.setProfileId(profileTO.getId()); + public Profile updateProfile(Profile profile) { + if (profile.getAttributes() != null) { + for (ProfileAttribute attribute : profile.getAttributes()) { + attribute.setProfile(profile); } } - Profile profile = Profile.fromProfileTO(em, profileTO); em.merge(profile); - return profile.toProfileTO(); + return profile; } @Override - public ProfileAttributeTO createProfileAttribute(ProfileAttributeTO attributeTO) { - ProfileAttribute attribute = ProfileAttribute.fromProfileAttributeTO(em, attributeTO); + public ProfileAttribute createProfileAttribute(ProfileAttribute attribute) { Profile profile = attribute.getProfile(); profile.addProfileAttribute(attribute); em.flush(); - return attribute.toProfileAttributeTO(); + return attribute; } @Override @@ -74,15 +69,14 @@ public class ProfileDaoImpl implements ProfileDao { } @Override - public ProfileAttributeTO readProfileAttribute(long id) { + public ProfileAttribute readProfileAttribute(long id) { ProfileAttribute attribute = em.find(ProfileAttribute.class, id); - return attribute.toProfileAttributeTO(); + return attribute; } @Override - public ProfileAttributeTO updateProfileAttribute(ProfileAttributeTO attributeTO) { - ProfileAttribute attribute = ProfileAttribute.fromProfileAttributeTO(em, attributeTO); + public ProfileAttribute updateProfileAttribute(ProfileAttribute attribute) { em.merge(attribute); - return attribute.toProfileAttributeTO(); + return attribute; } } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/AttributesEJB.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/AttributesEJB.java index 5a4e007..18708ed 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/AttributesEJB.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/AttributesEJB.java @@ -5,6 +5,8 @@ package com.edf.gde.ejb; import com.edf.gde.dao.AttributeDao; import com.edf.gde.dao.impl.AttributeDaoImpl; +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.ejb.Stateless; @@ -26,7 +28,8 @@ public class AttributesEJB { /* Attributes */ public AttributeTO createAttribute(AttributeTO ato) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.createAttribute(ato); + Attribute attribute = Attribute.fromAttributeTO(em, ato); + return dao.createAttribute(attribute).toAttributeTO(); } public void deleteAttribute(long attributeId) { @@ -36,38 +39,38 @@ public class AttributesEJB { public AttributeTO updateAttribute(AttributeTO ato) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.updateAttribute(ato); + return dao.updateAttribute(Attribute.fromAttributeTO(em, ato)).toAttributeTO(); } public AttributeTO readAttribute(long attributeId) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.readAttribute(attributeId); + return dao.readAttribute(attributeId).toAttributeTO(); } public AttributeTO findById(long id) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.findById(id); + return dao.findById(id).toAttributeTO(); } public AttributeTO findByName(String name) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.findByName(name); + return dao.findByName(name).toAttributeTO(); } public AttributeTO findByType(String type) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.findByType(type); + return dao.findByType(type).toAttributeTO(); } public AttributeTO findByValue(String value) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.findByValue(value); + return dao.findByValue(value).toAttributeTO(); } /* Attributes Groups */ public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.createAttributeGroup(agto); + return dao.createAttributeGroup(AttributeGroup.fromAttributeGroupTO(em, agto)).toAttributeGroupTO(); } public void deleteAttributeGroup(long id) { @@ -77,12 +80,12 @@ public class AttributesEJB { public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.updateAttributeGroup(agto); + return dao.updateAttributeGroup(AttributeGroup.fromAttributeGroupTO(em, agto)).toAttributeGroupTO(); } public AttributeGroupTO readAttributeGroup(long groupId) { AttributeDao dao = new AttributeDaoImpl(em); - return dao.readAttributeGroup(groupId); + return dao.readAttributeGroup(groupId).toAttributeGroupTO(); } } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ProfileEJB.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ProfileEJB.java index d8d720b..9575c26 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ProfileEJB.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ProfileEJB.java @@ -5,6 +5,8 @@ package com.edf.gde.ejb; import com.edf.gde.dao.ProfileDao; import com.edf.gde.dao.impl.ProfileDaoImpl; +import com.edf.gde.entities.Profile; +import com.edf.gde.entities.ProfileAttribute; import com.edf.gde.transferables.ProfileAttributeTO; import com.edf.gde.transferables.ProfileTO; import javax.ejb.Stateless; @@ -25,7 +27,7 @@ public class ProfileEJB { public ProfileTO createProfile(ProfileTO pto) { ProfileDao dao = new ProfileDaoImpl(em); - return dao.createProfile(pto); + return dao.createProfile(Profile.fromProfileTO(em, pto)).toProfileTO(); } public void deleteProfile(long profileId) { @@ -35,17 +37,17 @@ public class ProfileEJB { public ProfileTO readProfile(long profileId) { ProfileDao dao = new ProfileDaoImpl(em); - return dao.readProfile(profileId); + return dao.readProfile(profileId).toProfileTO(); } public ProfileTO updateProfile(ProfileTO profileTO) { ProfileDao dao = new ProfileDaoImpl(em); - return dao.updateProfile(profileTO); + return dao.updateProfile(Profile.fromProfileTO(em, profileTO)).toProfileTO(); } public ProfileAttributeTO createProfileAttribute(ProfileAttributeTO attributeTO) { ProfileDao dao = new ProfileDaoImpl(em); - return dao.createProfileAttribute(attributeTO); + return dao.createProfileAttribute(ProfileAttribute.fromProfileAttributeTO(em, attributeTO)).toProfileAttributeTO(); } public void deleteProfileAttribute(long id) { @@ -55,12 +57,12 @@ public class ProfileEJB { public ProfileAttributeTO readProfileAttribute(long id) { ProfileDao dao = new ProfileDaoImpl(em); - return dao.readProfileAttribute(id); + return dao.readProfileAttribute(id).toProfileAttributeTO(); } public ProfileAttributeTO updateProfileAttribute(ProfileAttributeTO attributeTO) { ProfileDao dao = new ProfileDaoImpl(em); - return dao.updateProfileAttribute(attributeTO); + return dao.updateProfileAttribute(ProfileAttribute.fromProfileAttributeTO(em, attributeTO)).toProfileAttributeTO(); } } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Profile.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Profile.java index 163a34e..dc5730c 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Profile.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/Profile.java @@ -79,7 +79,7 @@ public class Profile implements Serializable { this.name = name; } - public Collection getProfileAttributeCollection() { + public Collection getAttributes() { return profileAttributeCollection; } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/ProfileAttribute.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/ProfileAttribute.java index 2ff80f9..247714f 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/ProfileAttribute.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/entities/ProfileAttribute.java @@ -100,8 +100,8 @@ public class ProfileAttribute implements Serializable { return profile; } - public void setProfile(Profile profileId) { - this.profile = profileId; + public void setProfile(Profile profile) { + this.profile = profile; } public static ProfileAttribute fromProfileAttributeTO (EntityManager em, ProfileAttributeTO attributeTO) { -- 2.39.2