From 5c634c3908c2cc29a17d1209e30ab6c848e3c1d0 Mon Sep 17 00:00:00 2001 From: Bojnourdi Date: Fri, 14 Aug 2015 15:35:09 +0200 Subject: [PATCH] Dao refactoring --- .../java/com/edf/gde/dao/AttributeDao.java | 98 ++----- .../src/java/com/edf/gde/dao/ChunkDao.java | 59 +---- .../src/java/com/edf/gde/dao/FileDao.java | 61 +---- .../src/java/com/edf/gde/dao/ProfileDao.java | 56 +--- .../src/java/com/edf/gde/dao/StudyDao.java | 67 +---- .../src/java/com/edf/gde/dao/UserDao.java | 192 +++----------- .../edf/gde/dao/impl/AttributeDaoImpl.java | 116 +++++++++ .../com/edf/gde/dao/impl/ChunkDaoImpl.java | 65 +++++ .../com/edf/gde/dao/impl/FileDaoImpl.java | 82 ++++++ .../com/edf/gde/dao/impl/ProfileDaoImpl.java | 76 ++++++ .../com/edf/gde/dao/impl/StudyDaoImpl.java | 88 +++++++ .../com/edf/gde/dao/impl/UserDaoImpl.java | 246 ++++++++++++++++++ .../java/com/edf/gde/ejb/AttributesEJB.java | 39 ++- .../src/java/com/edf/gde/ejb/ChunkEJB.java | 21 +- .../src/java/com/edf/gde/ejb/FileEJB.java | 30 ++- .../edf/gde/ejb/PermissionsManagerEJB.java | 4 +- .../src/java/com/edf/gde/ejb/ProfileEJB.java | 70 +++++ .../src/java/com/edf/gde/ejb/StudyEJB.java | 33 ++- .../src/java/com/edf/gde/ejb/UserEJB.java | 29 ++- 19 files changed, 941 insertions(+), 491 deletions(-) create mode 100644 projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java create mode 100644 projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ChunkDaoImpl.java create mode 100644 projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/FileDaoImpl.java create mode 100644 projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java create mode 100644 projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/StudyDaoImpl.java create mode 100644 projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/UserDaoImpl.java create mode 100644 projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ProfileEJB.java 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 93aeab6..ccb4b5b 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,102 +3,42 @@ */ 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.Collection; -import javax.persistence.EntityManager; /** * * @author Kavoos */ -public class AttributeDao { +public interface AttributeDao { - private final EntityManager em; + AttributeTO createAttribute(AttributeTO ato); - public AttributeDao(EntityManager em) { - this.em = em; - } - - public AttributeTO createAttribute(AttributeTO ato) { - Attribute a = Attribute.fromAttributeTO(em,ato); - em.persist(a); - em.flush(); - 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(em,ato); - Attribute up = em.merge(a); - return up.toAttributeTO(); - } + /** + * + * @param agto + * @return + */ + AttributeGroupTO createAttributeGroup(AttributeGroupTO agto); - public AttributeTO readAttribute(long attributeId) { - Attribute found = em.find(Attribute.class, attributeId); - return found.toAttributeTO(); - } + void deleteAttribute(long attributeId); - private AttributeTO findBy(String queryName, String varName, T value) { - Attribute found = (Attribute) em.createNamedQuery(queryName) - .setParameter(varName, value) - .getSingleResult(); - return found.toAttributeTO(); - } + void deleteAttributeGroup(long id); - public AttributeTO findById(long id) { - return findBy("Attribute.findById", "id", id); - } + AttributeTO findById(long id); - public AttributeTO findByName(String name) { - return findBy("Attribute.findByName", "name", name); - } + AttributeTO findByName(String name); - public AttributeTO findByType(String type) { - return findBy("Attribute.findByType", "type", type); - } + AttributeTO findByType(String type); - public AttributeTO findByValue(String value) { - return findBy("Attribute.findByValue", "value", value); - } + AttributeTO findByValue(String value); - /** - * - * @param agto - * @return - */ - public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) { - AttributeGroup group = AttributeGroup.fromAttributeGroupTO(em,agto); - em.persist(group); - return group.toAttributeGroupTO(); - } + AttributeTO readAttribute(long attributeId); - public void deleteAttributeGroup(long id) { - AttributeGroup group = em.find(AttributeGroup.class, id); - em.remove(group); - em.flush(); - em.clear(); - } + AttributeGroupTO readAttributeGroup(long groupId); - public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) { - AttributeGroup group = AttributeGroup.fromAttributeGroupTO(em,agto); - AttributeGroup up = em.merge(group); - Collection attributeCollection = agto.getAttributeCollection(); - - return up.toAttributeGroupTO(); - } + AttributeTO updateAttribute(AttributeTO ato); - public AttributeGroupTO readAttributeGroup(long groupId) { - AttributeGroup found = (AttributeGroup) em.createNamedQuery("AttributeGroup.findById") - .setParameter("id", groupId) - .getSingleResult(); - return found.toAttributeGroupTO(); - } + AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto); + } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/ChunkDao.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/ChunkDao.java index a8625dd..a7007d1 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/ChunkDao.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/ChunkDao.java @@ -3,57 +3,24 @@ */ package com.edf.gde.dao; -import com.edf.gde.entities.Chunk; import com.edf.gde.transferables.ChunkTO; -import javax.persistence.EntityManager; -import javax.persistence.Query; /** * * @author Kavoos */ -public class ChunkDao { - - private final EntityManager em; - - public ChunkDao(EntityManager em) { - this.em = em; - } - - public ChunkTO createChunk(ChunkTO cto) { - Chunk c = Chunk.fromChunkTO(cto); - em.persist(c); - return c.toChunkTO(); - } - - public void deleteChunk(ChunkTO cto) { - Chunk c = Chunk.fromChunkTO(cto); - em.remove(c); - } - - public ChunkTO updateChunk(ChunkTO cto) { - Chunk c = Chunk.fromChunkTO(cto); - Chunk up = em.merge(c); - return up.toChunkTO(); - } - - public ChunkTO findChunk(ChunkTO cto) { - Chunk found = em.find(Chunk.class, cto.getId()); - return found.toChunkTO(); - } - - public ChunkTO findById(long id) { - Chunk found = (Chunk) em.createNamedQuery("Chunk.findById") - .setParameter("id", id) - .getSingleResult(); - return found.toChunkTO(); - } - - public ChunkTO findByFileId(long fileId) { - Chunk found = (Chunk) em.createNamedQuery("Chunk.findByFileId") - .setParameter("fileId", fileId) - .getSingleResult(); - return found.toChunkTO(); - } +public interface ChunkDao { + ChunkTO createChunk(ChunkTO cto); + + void deleteChunk(ChunkTO cto); + + ChunkTO findByFileId(long fileId); + + ChunkTO findById(long id); + + ChunkTO findChunk(ChunkTO cto); + + ChunkTO updateChunk(ChunkTO cto); + } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/FileDao.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/FileDao.java index d18eebd..f244e5a 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/FileDao.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/FileDao.java @@ -3,70 +3,31 @@ */ package com.edf.gde.dao; -import com.edf.gde.entities.GDEFile; import com.edf.gde.transferables.FileTO; import java.util.Date; -import javax.persistence.EntityManager; /** * * @author Kavoos */ -public class FileDao { +public interface FileDao { - private EntityManager em; + FileTO createFile(FileTO fto); - public FileDao(EntityManager em) { - this.em = em; - } + void deleteFile(FileTO fto); - public FileTO createFile(FileTO fto) { - GDEFile f = GDEFile.fromFileTO(fto); - em.persist(f); - return f.toFileTO(); - } + FileTO findByCreationDate(Date creationDate); - public void deleteFile(FileTO fto) { - GDEFile f = GDEFile.fromFileTO(fto); - em.remove(f); - } + FileTO findByDeletionDate(Date deletionDate); - public FileTO updateFile(FileTO fto) { - GDEFile f = GDEFile.fromFileTO(fto); - GDEFile up = em.merge(f); - return up.toFileTO(); - } + FileTO findById(long id); - public FileTO findFile(FileTO fto) { - GDEFile found = em.find(GDEFile.class, fto.getId()); - return found.toFileTO(); - } + FileTO findByName(String name); - private FileTO findBy(String queryName, String varName, T value) { - GDEFile found = (GDEFile) em.createNamedQuery(queryName) - .setParameter(varName, value) - .getSingleResult(); - return found.toFileTO(); - } + FileTO findByUpdateDate(Date updateDate); - public FileTO findById(long id) { - return findBy("File.findById", "id", id); - } - - public FileTO findByName(String name) { - return findBy("File.findByName", "name", name); - } - - public FileTO findByCreationDate(Date creationDate) { - return findBy("File.findByCreationDate", "creationDate", creationDate); - } - - public FileTO findByUpdateDate(Date updateDate) { - return findBy("File.findByUpdateDate", "updateDate", updateDate); - } - - public FileTO findByDeletionDate(Date deletionDate) { - return findBy("File.findByDeletionDate", "deletionDate", deletionDate); - } + FileTO findFile(FileTO fto); + FileTO updateFile(FileTO fto); + } 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 f6dfaf8..42253b8 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,65 +3,29 @@ */ package com.edf.gde.dao; -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; /** * * @author Kavoos */ -public class ProfileDao { +public interface ProfileDao { - private final EntityManager em; + ProfileTO createProfile(ProfileTO pto); - public ProfileDao(EntityManager em) { - this.em = em; - } + ProfileAttributeTO createProfileAttribute(ProfileAttributeTO attributeTO); - public ProfileTO createProfile(ProfileTO pto) { - Profile profile = Profile.fromProtileTO(em, pto); - em.persist(profile); - return profile.toProfileTO(); - } + void deleteProfile(long profileId); - public void deleteProfile(long profileId) { - Profile profile = em.find(Profile.class, profileId); - em.remove(profile); - } + void deleteProfileAttribute(long id); - public ProfileTO readProfile(long profileId) { - Profile profile = em.find(Profile.class, profileId); - return profile.toProfileTO(); - } + ProfileTO readProfile(long profileId); - public ProfileTO updateProfile(ProfileTO profileTO) { - Profile profile = Profile.fromProtileTO(em, profileTO); - em.merge(profile); - return profile.toProfileTO(); - } + ProfileAttributeTO readProfileAttribute(long id); - public ProfileAttributeTO createProfileAttribute(ProfileAttributeTO attributeTO) { - ProfileAttribute attribute = ProfileAttribute.fromProfileAttributeTO(em, attributeTO); - em.persist(attribute); - return attribute.toProfileAttributeTO(); - } + ProfileTO updateProfile(ProfileTO profileTO); - public void deleteProfileAttribute(long id) { - ProfileAttribute attribute = em.find(ProfileAttribute.class, id); - em.remove(attribute); - } - - public ProfileAttributeTO readProfileAttribute(long id) { - ProfileAttribute attribute = em.find(ProfileAttribute.class, id); - return attribute.toProfileAttributeTO(); - } - - public ProfileAttributeTO updateProfileAttribute(ProfileAttributeTO attributeTO) { - ProfileAttribute attribute = ProfileAttribute.fromProfileAttributeTO(em, attributeTO); - em.merge(attribute); - return attribute.toProfileAttributeTO(); - } + ProfileAttributeTO updateProfileAttribute(ProfileAttributeTO attributeTO); + } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/StudyDao.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/StudyDao.java index e070442..d51bf4c 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/StudyDao.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/StudyDao.java @@ -3,76 +3,31 @@ */ package com.edf.gde.dao; -import com.edf.gde.entities.Study; import com.edf.gde.transferables.StudyTO; import java.util.Date; -import javax.persistence.EntityManager; /** * * @author Kavoos */ -public class StudyDao { +public interface StudyDao { - private final EntityManager em; + StudyTO createStudy(StudyTO sto); - public StudyDao(EntityManager em) { - this.em = em; - } + void deleteStudy(long studyId); - public StudyTO createStudy(StudyTO sto) { - Study s = Study.fromStudyTO(sto); - em.persist(s); - return s.toStudyTO(); - } + StudyTO findByCreationDate(Date creationDate); - public void deleteStudy(long studyId) { - /* - Study s = Study.fromStudyTO(sto); - em.remove(s); - */ - /* We never remove a study */ - Study study = em.find(Study.class, studyId); - study.setDeleted(true); - study.setDeletionDate(new Date(System.currentTimeMillis())); - } + StudyTO findByDeletionDate(Date deletionDate); - public StudyTO updateStudy(StudyTO sto) { - Study s = Study.fromStudyTO(sto); - Study up = em.merge(s); - return up.toStudyTO(); - } + StudyTO findById(long id); - public StudyTO findStudy(StudyTO sto) { - Study found = em.find(Study.class, sto.getId()); - return found.toStudyTO(); - } + StudyTO findByName(String name); - private StudyTO findBy(String queryName, String varName, T value) { - Study found = (Study) em.createNamedQuery(queryName) - .setParameter(varName, value) - .getSingleResult(); - return found.toStudyTO(); - } + StudyTO findByUpdateDate(Date updateDate); - public StudyTO findById(long id) { - return findBy("Study.findById", "id", id); - } - - public StudyTO findByName(String name) { - return findBy("Study.findByName", "name", name); - } - - public StudyTO findByCreationDate(Date creationDate) { - return findBy("Study.findByCreationDate", "creationDate", creationDate); - } - - public StudyTO findByUpdateDate(Date updateDate) { - return findBy("Study.findByUpdateDate", "updateDate", updateDate); - } - - public StudyTO findByDeletionDate(Date deletionDate) { - return findBy("Study.findByDeletionDate", "deletionDate", deletionDate); - } + StudyTO findStudy(StudyTO sto); + StudyTO updateStudy(StudyTO sto); + } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/UserDao.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/UserDao.java index fbdb007..4470717 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/UserDao.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/UserDao.java @@ -5,229 +5,107 @@ package com.edf.gde.dao; import com.edf.gde.entities.Group; import com.edf.gde.entities.User; -import com.edf.gde.entities.UserGroup; import java.util.List; -import javax.persistence.EntityManager; -import javax.persistence.Query; /** * * @author Kavoos */ -public class UserDao { - private final EntityManager em; - public UserDao(EntityManager em) { - this.em = em; - } +public interface UserDao { /** - * Create a new user - * - * @param userName - * @param password - * @return the new User or null if user exists or on error - */ - public User createUser(String userName, String password) { - - if (userExists(userName)) { - throw new RuntimeException("Unable to create user " + userName); - } - User user = new User(); - user.setName(userName); - user.setPassword(password); - em.persist(user); - return user; - } - - /** - * Create a new group - * - * @param groupName - * @return the new created Group or null on error - */ - public Group createGroup(String groupName) { - if (groupExists(groupName)) { - throw new RuntimeException("Unable to create group " + groupName); - } - Group group = new Group(); - group.setName(groupName); - em.persist(group); - return group; - } - - /** - * Find group + * Add a user to a group * - * @param name - * @return null if the group does not exists + * @param groupId + * @param userId + * @return */ - public Group findGroup(String name) { - Group group = null; - Query q = em.createNamedQuery("Group.findByName"); - q.setParameter("name", name); - group = (Group) q.getSingleResult(); - return group; - } + void addToGroup(long groupId, long userId); /** + * Add users to a group in one transaction * - * @param id + * @param groupId + * @param userIds * @return */ - public Group findGroup(long id) { - Group group = null; - Query q = em.createNamedQuery("Group.findById"); - q.setParameter("id", id); - group = (Group) q.getSingleResult(); - return group; - } + void addToGroup(long groupId, List userIds); /** + * Create a new group * - * @param name - * @return + * @param groupName + * @return the new created Group or null on error */ - private boolean groupExists(String name) { - Group group = null; - Query q = em.createNamedQuery("Group.findByName"); - q.setParameter("name", name); - try { - group = (Group) q.getSingleResult(); - } catch (Exception ex) { - return false; - } - return true; - } + Group createGroup(String groupName); /** - * Add a user to a group + * Create a new user * - * @param groupId - * @param userId - * @return + * @param userName + * @param password + * @return the new User or null if user exists or on error */ - public void addToGroup(long groupId, long userId) { - if (!isInGroup(groupId, userId)) { - UserGroup userGroup = new UserGroup(); - userGroup.setGroupId(groupId); - userGroup.setUserId(userId); - em.persist(userGroup); - } else { - throw new RuntimeException("Unable to add " + userId + " to group " + groupId); - } - } + User createUser(String userName, String password); /** - * Add users to a group in one transaction * * @param groupId - * @param userIds * @return */ - public void addToGroup(long groupId, List userIds) { - for (Long id : userIds) { - addToGroup(groupId, id); - } - } + boolean deleteGroup(long groupId); /** * - * @param groupId * @param userId * @return */ - public void removeFromGroup(long groupId, long userId) { - Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId"); - q.setParameter("groupId", groupId); - q.setParameter("userId", userId); - List l = q.getResultList(); - if (l.isEmpty()) { - throw new RuntimeException("UserGroup not found"); - } - UserGroup ug = l.get(0); - em.remove(ug); - } + boolean deleteUser(long userId); /** + * Find group * - * @param groupId - * @param userId - * @return + * @param name + * @return null if the group does not exists */ - public boolean isInGroup(long groupId, long userId) { - Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId"); - q.setParameter("groupId", groupId); - q.setParameter("userId", userId); - List l = q.getResultList(); - return !l.isEmpty(); - } + Group findGroup(String name); /** - * Find user by user name (login) * - * @param userName + * @param id * @return */ - public User findUser(String userName) { - Query q = em.createNamedQuery("User.findByName"); - q.setParameter("username", userName); - User user = null; - user = (User) q.getSingleResult(); - return user; - } + Group findGroup(long id); /** + * Find user by user name (login) * * @param userName * @return */ - private boolean userExists(String userName) { - Query q = em.createNamedQuery("User.findByName"); - q.setParameter("username", userName); - User user = null; - try { - user = (User) q.getSingleResult(); - } catch (Exception ex) { - return false; - } - return true; - } + User findUser(String userName); /** * * @param id * @return */ - public User findUser(long id) { - return em.find(User.class, id); - } + User findUser(long id); /** * + * @param groupId * @param userId * @return */ - public boolean deleteUser(long userId) { - User user = findUser(userId); - if (user != null) { - em.remove(user); - return true; - } - return false; - } + boolean isInGroup(long groupId, long userId); /** * * @param groupId + * @param userId * @return */ - public boolean deleteGroup(long groupId) { - Group group = findGroup(groupId); - if (group != null) { - em.remove(group); - return true; - } - return false; - } - + void removeFromGroup(long groupId, long userId); + } 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 new file mode 100644 index 0000000..c6351b1 --- /dev/null +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/AttributeDaoImpl.java @@ -0,0 +1,116 @@ +/* + * (C) 2015 EDF + */ +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; + +/** + * + * @author Kavoos + */ +public class AttributeDaoImpl implements AttributeDao { + + private final EntityManager em; + + public AttributeDaoImpl(EntityManager em) { + this.em = em; + } + + @Override + public AttributeTO createAttribute(AttributeTO ato) { + Attribute a = Attribute.fromAttributeTO(em, ato); + em.persist(a); + em.flush(); + return a.toAttributeTO(); + } + + @Override + public void deleteAttribute(long attributeId) { + Attribute a = em.find(Attribute.class, attributeId); + em.remove(a); + } + + @Override + public AttributeTO updateAttribute(AttributeTO ato) { + Attribute a = Attribute.fromAttributeTO(em, ato); + Attribute up = em.merge(a); + return up.toAttributeTO(); + } + + @Override + public AttributeTO readAttribute(long attributeId) { + Attribute found = em.find(Attribute.class, attributeId); + return found.toAttributeTO(); + } + + private AttributeTO findBy(String queryName, String varName, T value) { + Attribute found = (Attribute) em.createNamedQuery(queryName) + .setParameter(varName, value) + .getSingleResult(); + return found.toAttributeTO(); + } + + @Override + public AttributeTO findById(long id) { + return findBy("Attribute.findById", "id", id); + } + + @Override + public AttributeTO findByName(String name) { + return findBy("Attribute.findByName", "name", name); + } + + @Override + public AttributeTO findByType(String type) { + return findBy("Attribute.findByType", "type", type); + } + + @Override + public AttributeTO findByValue(String value) { + return findBy("Attribute.findByValue", "value", value); + } + + /** + * + * @param agto + * @return + */ + @Override + public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) { + AttributeGroup group = AttributeGroup.fromAttributeGroupTO(em, agto); + em.persist(group); + return group.toAttributeGroupTO(); + } + + @Override + public void deleteAttributeGroup(long id) { + AttributeGroup group = em.find(AttributeGroup.class, id); + em.remove(group); + em.flush(); + em.clear(); + } + + @Override + public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) { + AttributeGroup group = AttributeGroup.fromAttributeGroupTO(em, agto); + AttributeGroup up = em.merge(group); + Collection attributeCollection = agto.getAttributeCollection(); + + return up.toAttributeGroupTO(); + } + + @Override + 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/impl/ChunkDaoImpl.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ChunkDaoImpl.java new file mode 100644 index 0000000..69ee589 --- /dev/null +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ChunkDaoImpl.java @@ -0,0 +1,65 @@ +/* + * (C) 2015 EDF + */ +package com.edf.gde.dao.impl; + +import com.edf.gde.dao.ChunkDao; +import com.edf.gde.entities.Chunk; +import com.edf.gde.transferables.ChunkTO; +import javax.persistence.EntityManager; + +/** + * + * @author Kavoos + */ +public class ChunkDaoImpl implements ChunkDao { + + private final EntityManager em; + + public ChunkDaoImpl(EntityManager em) { + this.em = em; + } + + @Override + public ChunkTO createChunk(ChunkTO cto) { + Chunk c = Chunk.fromChunkTO(cto); + em.persist(c); + return c.toChunkTO(); + } + + @Override + public void deleteChunk(ChunkTO cto) { + Chunk c = Chunk.fromChunkTO(cto); + em.remove(c); + } + + @Override + public ChunkTO updateChunk(ChunkTO cto) { + Chunk c = Chunk.fromChunkTO(cto); + Chunk up = em.merge(c); + return up.toChunkTO(); + } + + @Override + public ChunkTO findChunk(ChunkTO cto) { + Chunk found = em.find(Chunk.class, cto.getId()); + return found.toChunkTO(); + } + + @Override + public ChunkTO findById(long id) { + Chunk found = (Chunk) em.createNamedQuery("Chunk.findById") + .setParameter("id", id) + .getSingleResult(); + return found.toChunkTO(); + } + + @Override + public ChunkTO findByFileId(long fileId) { + Chunk found = (Chunk) em.createNamedQuery("Chunk.findByFileId") + .setParameter("fileId", fileId) + .getSingleResult(); + return found.toChunkTO(); + } + +} diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/FileDaoImpl.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/FileDaoImpl.java new file mode 100644 index 0000000..f22c86a --- /dev/null +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/FileDaoImpl.java @@ -0,0 +1,82 @@ +/* + * (C) 2015 EDF + */ +package com.edf.gde.dao.impl; + +import com.edf.gde.dao.FileDao; +import com.edf.gde.entities.GDEFile; +import com.edf.gde.transferables.FileTO; +import java.util.Date; +import javax.persistence.EntityManager; + +/** + * + * @author Kavoos + */ +public class FileDaoImpl implements FileDao { + + private final EntityManager em; + + public FileDaoImpl(EntityManager em) { + this.em = em; + } + + @Override + public FileTO createFile(FileTO fto) { + GDEFile f = GDEFile.fromFileTO(fto); + em.persist(f); + return f.toFileTO(); + } + + @Override + public void deleteFile(FileTO fto) { + GDEFile f = GDEFile.fromFileTO(fto); + em.remove(f); + } + + @Override + public FileTO updateFile(FileTO fto) { + GDEFile f = GDEFile.fromFileTO(fto); + GDEFile up = em.merge(f); + return up.toFileTO(); + } + + @Override + public FileTO findFile(FileTO fto) { + GDEFile found = em.find(GDEFile.class, fto.getId()); + return found.toFileTO(); + } + + private FileTO findBy(String queryName, String varName, T value) { + GDEFile found = (GDEFile) em.createNamedQuery(queryName) + .setParameter(varName, value) + .getSingleResult(); + return found.toFileTO(); + } + + @Override + public FileTO findById(long id) { + return findBy("File.findById", "id", id); + } + + @Override + public FileTO findByName(String name) { + return findBy("File.findByName", "name", name); + } + + @Override + public FileTO findByCreationDate(Date creationDate) { + return findBy("File.findByCreationDate", "creationDate", creationDate); + } + + @Override + public FileTO findByUpdateDate(Date updateDate) { + return findBy("File.findByUpdateDate", "updateDate", updateDate); + } + + @Override + public FileTO findByDeletionDate(Date deletionDate) { + return findBy("File.findByDeletionDate", "deletionDate", deletionDate); + } + +} 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 new file mode 100644 index 0000000..a527970 --- /dev/null +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/ProfileDaoImpl.java @@ -0,0 +1,76 @@ +/* + * (C) 2015 EDF + */ +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; + +/** + * + * @author Kavoos + */ +public class ProfileDaoImpl implements ProfileDao { + + private final EntityManager em; + + public ProfileDaoImpl(EntityManager em) { + this.em = em; + } + + @Override + public ProfileTO createProfile(ProfileTO pto) { + Profile profile = Profile.fromProtileTO(em, pto); + em.persist(profile); + return profile.toProfileTO(); + } + + @Override + public void deleteProfile(long profileId) { + Profile profile = em.find(Profile.class, profileId); + em.remove(profile); + } + + @Override + public ProfileTO readProfile(long profileId) { + Profile profile = em.find(Profile.class, profileId); + return profile.toProfileTO(); + } + + @Override + public ProfileTO updateProfile(ProfileTO profileTO) { + Profile profile = Profile.fromProtileTO(em, profileTO); + em.merge(profile); + return profile.toProfileTO(); + } + + @Override + public ProfileAttributeTO createProfileAttribute(ProfileAttributeTO attributeTO) { + ProfileAttribute attribute = ProfileAttribute.fromProfileAttributeTO(em, attributeTO); + em.persist(attribute); + return attribute.toProfileAttributeTO(); + } + + @Override + public void deleteProfileAttribute(long id) { + ProfileAttribute attribute = em.find(ProfileAttribute.class, id); + em.remove(attribute); + } + + @Override + public ProfileAttributeTO readProfileAttribute(long id) { + ProfileAttribute attribute = em.find(ProfileAttribute.class, id); + return attribute.toProfileAttributeTO(); + } + + @Override + public ProfileAttributeTO updateProfileAttribute(ProfileAttributeTO attributeTO) { + ProfileAttribute attribute = ProfileAttribute.fromProfileAttributeTO(em, attributeTO); + em.merge(attribute); + return attribute.toProfileAttributeTO(); + } +} diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/StudyDaoImpl.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/StudyDaoImpl.java new file mode 100644 index 0000000..6321042 --- /dev/null +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/StudyDaoImpl.java @@ -0,0 +1,88 @@ +/* + * (C) 2015 EDF + */ +package com.edf.gde.dao.impl; + +import com.edf.gde.dao.StudyDao; +import com.edf.gde.entities.Study; +import com.edf.gde.transferables.StudyTO; +import java.util.Date; +import javax.persistence.EntityManager; + +/** + * + * @author Kavoos + */ +public class StudyDaoImpl implements StudyDao { + + private final EntityManager em; + + public StudyDaoImpl(EntityManager em) { + this.em = em; + } + + @Override + public StudyTO createStudy(StudyTO sto) { + Study s = Study.fromStudyTO(sto); + em.persist(s); + return s.toStudyTO(); + } + + @Override + public void deleteStudy(long studyId) { + /* + Study s = Study.fromStudyTO(sto); + em.remove(s); + */ + /* We never remove a study */ + Study study = em.find(Study.class, studyId); + study.setDeleted(true); + study.setDeletionDate(new Date(System.currentTimeMillis())); + } + + @Override + public StudyTO updateStudy(StudyTO sto) { + Study s = Study.fromStudyTO(sto); + Study up = em.merge(s); + return up.toStudyTO(); + } + + @Override + public StudyTO findStudy(StudyTO sto) { + Study found = em.find(Study.class, sto.getId()); + return found.toStudyTO(); + } + + private StudyTO findBy(String queryName, String varName, T value) { + Study found = (Study) em.createNamedQuery(queryName) + .setParameter(varName, value) + .getSingleResult(); + return found.toStudyTO(); + } + + @Override + public StudyTO findById(long id) { + return findBy("Study.findById", "id", id); + } + + @Override + public StudyTO findByName(String name) { + return findBy("Study.findByName", "name", name); + } + + @Override + public StudyTO findByCreationDate(Date creationDate) { + return findBy("Study.findByCreationDate", "creationDate", creationDate); + } + + @Override + public StudyTO findByUpdateDate(Date updateDate) { + return findBy("Study.findByUpdateDate", "updateDate", updateDate); + } + + @Override + public StudyTO findByDeletionDate(Date deletionDate) { + return findBy("Study.findByDeletionDate", "deletionDate", deletionDate); + } + +} diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/UserDaoImpl.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/UserDaoImpl.java new file mode 100644 index 0000000..9b9dc0b --- /dev/null +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/impl/UserDaoImpl.java @@ -0,0 +1,246 @@ +/* + * (C) 2015 EDF + */ +package com.edf.gde.dao.impl; + +import com.edf.gde.dao.UserDao; +import com.edf.gde.entities.Group; +import com.edf.gde.entities.User; +import com.edf.gde.entities.UserGroup; +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.Query; + +/** + * + * @author Kavoos + */ +public class UserDaoImpl implements UserDao { + private final EntityManager em; + public UserDaoImpl(EntityManager em) { + this.em = em; + } + + /** + * Create a new user + * + * @param userName + * @param password + * @return the new User or null if user exists or on error + */ + @Override + public User createUser(String userName, String password) { + + if (userExists(userName)) { + throw new RuntimeException("Unable to create user " + userName); + } + User user = new User(); + user.setName(userName); + user.setPassword(password); + em.persist(user); + return user; + } + + /** + * Create a new group + * + * @param groupName + * @return the new created Group or null on error + */ + @Override + public Group createGroup(String groupName) { + if (groupExists(groupName)) { + throw new RuntimeException("Unable to create group " + groupName); + } + Group group = new Group(); + group.setName(groupName); + em.persist(group); + return group; + } + + /** + * Find group + * + * @param name + * @return null if the group does not exists + */ + @Override + public Group findGroup(String name) { + Group group = null; + Query q = em.createNamedQuery("Group.findByName"); + q.setParameter("name", name); + group = (Group) q.getSingleResult(); + return group; + } + + /** + * + * @param id + * @return + */ + @Override + public Group findGroup(long id) { + Group group = null; + Query q = em.createNamedQuery("Group.findById"); + q.setParameter("id", id); + group = (Group) q.getSingleResult(); + return group; + } + + /** + * + * @param name + * @return + */ + private boolean groupExists(String name) { + Group group = null; + Query q = em.createNamedQuery("Group.findByName"); + q.setParameter("name", name); + try { + group = (Group) q.getSingleResult(); + } catch (Exception ex) { + return false; + } + return true; + } + + /** + * Add a user to a group + * + * @param groupId + * @param userId + * @return + */ + @Override + public void addToGroup(long groupId, long userId) { + if (!isInGroup(groupId, userId)) { + UserGroup userGroup = new UserGroup(); + userGroup.setGroupId(groupId); + userGroup.setUserId(userId); + em.persist(userGroup); + } else { + throw new RuntimeException("Unable to add " + userId + " to group " + groupId); + } + } + + /** + * Add users to a group in one transaction + * + * @param groupId + * @param userIds + * @return + */ + @Override + public void addToGroup(long groupId, List userIds) { + for (Long id : userIds) { + addToGroup(groupId, id); + } + } + + /** + * + * @param groupId + * @param userId + * @return + */ + @Override + public void removeFromGroup(long groupId, long userId) { + Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId"); + q.setParameter("groupId", groupId); + q.setParameter("userId", userId); + List l = q.getResultList(); + if (l.isEmpty()) { + throw new RuntimeException("UserGroup not found"); + } + UserGroup ug = l.get(0); + em.remove(ug); + } + + /** + * + * @param groupId + * @param userId + * @return + */ + @Override + public boolean isInGroup(long groupId, long userId) { + Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId"); + q.setParameter("groupId", groupId); + q.setParameter("userId", userId); + List l = q.getResultList(); + return !l.isEmpty(); + } + + /** + * Find user by user name (login) + * + * @param userName + * @return + */ + @Override + public User findUser(String userName) { + Query q = em.createNamedQuery("User.findByName"); + q.setParameter("username", userName); + User user = null; + user = (User) q.getSingleResult(); + return user; + } + + /** + * + * @param userName + * @return + */ + private boolean userExists(String userName) { + Query q = em.createNamedQuery("User.findByName"); + q.setParameter("username", userName); + User user = null; + try { + user = (User) q.getSingleResult(); + } catch (Exception ex) { + return false; + } + return true; + } + + /** + * + * @param id + * @return + */ + @Override + public User findUser(long id) { + return em.find(User.class, id); + } + + /** + * + * @param userId + * @return + */ + @Override + public boolean deleteUser(long userId) { + User user = findUser(userId); + if (user != null) { + em.remove(user); + return true; + } + return false; + } + + /** + * + * @param groupId + * @return + */ + @Override + public boolean deleteGroup(long groupId) { + Group group = findGroup(groupId); + if (group != null) { + em.remove(group); + return true; + } + return false; + } + +} 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 181396b..91ad861 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 @@ -4,6 +4,7 @@ package com.edf.gde.ejb; import com.edf.gde.dao.AttributeDao; +import com.edf.gde.dao.impl.AttributeDaoImpl; import com.edf.gde.transferables.AttributeGroupTO; import com.edf.gde.transferables.AttributeTO; import javax.ejb.Stateless; @@ -17,70 +18,82 @@ import javax.persistence.PersistenceContext; */ @Stateless @LocalBean -public class AttributesEJB { +public class AttributesEJB implements AttributeDao { @PersistenceContext(unitName = "GDE-ejbPU") private EntityManager em; /* Attributes */ + @Override public AttributeTO createAttribute(AttributeTO ato) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.createAttribute(ato); } + @Override public void deleteAttribute(long attributeId) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); dao.deleteAttribute(attributeId); } + @Override public AttributeTO updateAttribute(AttributeTO ato) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.updateAttribute(ato); } + @Override public AttributeTO readAttribute(long attributeId) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.readAttribute(attributeId); } + @Override public AttributeTO findById(long id) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.findById(id); } + @Override public AttributeTO findByName(String name) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.findByName(name); } + @Override public AttributeTO findByType(String type) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.findByType(type); } + @Override public AttributeTO findByValue(String value) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.findByValue(value); } /* Attributes Groups */ + @Override public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.createAttributeGroup(agto); } + @Override public void deleteAttributeGroup(long id) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); dao.deleteAttributeGroup(id); } + @Override public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.updateAttributeGroup(agto); } + @Override public AttributeGroupTO readAttributeGroup(long groupId) { - AttributeDao dao = new AttributeDao(em); + AttributeDao dao = new AttributeDaoImpl(em); return dao.readAttributeGroup(groupId); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ChunkEJB.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ChunkEJB.java index b999e0a..3f899cf 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ChunkEJB.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ChunkEJB.java @@ -4,6 +4,7 @@ package com.edf.gde.ejb; import com.edf.gde.dao.ChunkDao; +import com.edf.gde.dao.impl.ChunkDaoImpl; import com.edf.gde.transferables.ChunkTO; import javax.ejb.Stateless; import javax.ejb.LocalBean; @@ -16,38 +17,44 @@ import javax.persistence.PersistenceContext; */ @Stateless @LocalBean -public class ChunkEJB { +public class ChunkEJB implements ChunkDao { @PersistenceContext(unitName = "GDE-ejbPU") private EntityManager em; + @Override public ChunkTO createChunk(ChunkTO cto) { - ChunkDao dao = new ChunkDao(em); + ChunkDaoImpl dao = new ChunkDaoImpl(em); return dao.createChunk(cto); } + @Override public void deleteChunk(ChunkTO cto) { - ChunkDao dao = new ChunkDao(em); + ChunkDaoImpl dao = new ChunkDaoImpl(em); dao.deleteChunk(cto); } + @Override public ChunkTO updateChunk(ChunkTO cto) { - ChunkDao dao = new ChunkDao(em); + ChunkDaoImpl dao = new ChunkDaoImpl(em); return dao.updateChunk(cto); } + @Override public ChunkTO findChunk(ChunkTO cto) { - ChunkDao dao = new ChunkDao(em); + ChunkDaoImpl dao = new ChunkDaoImpl(em); return dao.findChunk(cto); } + @Override public ChunkTO findById(long id) { - ChunkDao dao = new ChunkDao(em); + ChunkDaoImpl dao = new ChunkDaoImpl(em); return dao.findById(id); } + @Override public ChunkTO findByFileId(long fileId) { - ChunkDao dao = new ChunkDao(em); + ChunkDaoImpl dao = new ChunkDaoImpl(em); return dao.findByFileId(fileId); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/FileEJB.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/FileEJB.java index 7d6fd68..2a77598 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/FileEJB.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/FileEJB.java @@ -4,6 +4,7 @@ package com.edf.gde.ejb; import com.edf.gde.dao.FileDao; +import com.edf.gde.dao.impl.FileDaoImpl; import com.edf.gde.transferables.FileTO; import java.util.Date; import javax.ejb.Stateless; @@ -17,53 +18,62 @@ import javax.persistence.PersistenceContext; */ @Stateless @LocalBean -public class FileEJB { +public class FileEJB implements FileDao { @PersistenceContext(unitName = "GDE-ejbPU") private EntityManager em; + @Override public FileTO createFile(FileTO fto) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); return dao.createFile(fto); } + @Override public void deleteFile(FileTO fto) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); dao.deleteFile(fto); } + @Override public FileTO updateFile(FileTO fto) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); return dao.updateFile(fto); } + @Override public FileTO findFile(FileTO fto) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); return dao.findFile(fto); } + @Override public FileTO findById(long id) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); return dao.findById(id); } + @Override public FileTO findByName(String name) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); return dao.findByName(name); } + @Override public FileTO findByCreationDate(Date creationDate) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); return dao.findByCreationDate(creationDate); } + @Override public FileTO findByUpdateDate(Date updateDate) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); return dao.findByUpdateDate(updateDate); } + @Override public FileTO findByDeletionDate(Date deletionDate) { - FileDao dao = new FileDao(em); + FileDaoImpl dao = new FileDaoImpl(em); return dao.findByDeletionDate(deletionDate); } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/PermissionsManagerEJB.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/PermissionsManagerEJB.java index c44fa37..ca607e1 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/PermissionsManagerEJB.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/PermissionsManagerEJB.java @@ -3,7 +3,7 @@ */ package com.edf.gde.ejb; -import com.edf.gde.dao.UserDao; +import com.edf.gde.dao.impl.UserDaoImpl; import com.edf.gde.entities.GroupPermission; import com.edf.gde.entities.User; import com.edf.gde.entities.UserGroup; @@ -52,7 +52,7 @@ public class PermissionsManagerEJB { } public void checkPermission(String userLogin, String serviceName, int methodIndex) { - UserDao userDao = new UserDao(em); + UserDaoImpl userDao = new UserDaoImpl(em); User user = userDao.findUser(userLogin); Query query = em.createNamedQuery("UserGroup.findByUserId"); query.setParameter("userId", user.getId()); 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 new file mode 100644 index 0000000..a93f91d --- /dev/null +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/ProfileEJB.java @@ -0,0 +1,70 @@ +/* + * (C) 2015 EDF + */ +package com.edf.gde.ejb; + +import com.edf.gde.dao.ProfileDao; +import com.edf.gde.dao.impl.ProfileDaoImpl; +import com.edf.gde.transferables.ProfileAttributeTO; +import com.edf.gde.transferables.ProfileTO; +import javax.ejb.Stateless; +import javax.ejb.LocalBean; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +/** + * + * @author Kavoos + */ +@Stateless +@LocalBean +public class ProfileEJB implements ProfileDao { + + @PersistenceContext(unitName = "GDE-ejbPU") + private EntityManager em; + + public ProfileTO createProfile(ProfileTO pto) { + ProfileDao dao = new ProfileDaoImpl(em); + return dao.createProfile(pto); + } + + public void deleteProfile(long profileId) { + ProfileDao dao = new ProfileDaoImpl(em); + dao.deleteProfile(profileId); + } + + public ProfileTO readProfile(long profileId) { + ProfileDao dao = new ProfileDaoImpl(em); + return dao.readProfile(profileId); + } + + public ProfileTO updateProfile(ProfileTO profileTO) { + ProfileDao dao = new ProfileDaoImpl(em); + return dao.updateProfile(profileTO); + } + + @Override + public ProfileAttributeTO createProfileAttribute(ProfileAttributeTO attributeTO) { + ProfileDao dao = new ProfileDaoImpl(em); + return dao.createProfileAttribute(attributeTO); + } + + @Override + public void deleteProfileAttribute(long id) { + ProfileDao dao = new ProfileDaoImpl(em); + dao.deleteProfileAttribute(id); + } + + @Override + public ProfileAttributeTO readProfileAttribute(long id) { + ProfileDao dao = new ProfileDaoImpl(em); + return dao.readProfileAttribute(id); + } + + @Override + public ProfileAttributeTO updateProfileAttribute(ProfileAttributeTO attributeTO) { + ProfileDao dao = new ProfileDaoImpl(em); + return dao.updateProfileAttribute(attributeTO); + } + +} diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/StudyEJB.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/StudyEJB.java index 527ecd3..e9bb54e 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/StudyEJB.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/StudyEJB.java @@ -4,6 +4,7 @@ package com.edf.gde.ejb; import com.edf.gde.dao.StudyDao; +import com.edf.gde.dao.impl.StudyDaoImpl; import com.edf.gde.transferables.StudyTO; import java.util.Date; import javax.ejb.Stateless; @@ -17,57 +18,67 @@ import javax.persistence.PersistenceContext; */ @Stateless @LocalBean -public class StudyEJB { +public class StudyEJB implements StudyDao { @PersistenceContext(unitName = "GDE-ejbPU") private EntityManager em; + @Override public StudyTO createStudy(StudyTO sto) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); return dao.createStudy(sto); } + @Override public void deleteStudy(long studyId) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); dao.deleteStudy(studyId); } + @Override public StudyTO updateStudy(StudyTO sto) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); return dao.updateStudy(sto); } + @Override public StudyTO findStudy(StudyTO sto) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); return dao.findStudy(sto); } + @Override public StudyTO findById(long id) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); return dao.findById(id); } + @Override public StudyTO findByName(String name) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); return dao.findByName(name); } + @Override public StudyTO findByCreationDate(Date creationDate) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); return dao.findByCreationDate(creationDate); } + @Override public StudyTO findByUpdateDate(Date updateDate) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); return dao.findByUpdateDate(updateDate); } + @Override public StudyTO findByDeletionDate(Date deletionDate) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); return dao.findByDeletionDate(deletionDate); } + public void setStudyState(long studyId, boolean state) { - StudyDao dao = new StudyDao(em); + StudyDaoImpl dao = new StudyDaoImpl(em); dao.findById(studyId).setLocked(state); } } diff --git a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/UserEJB.java b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/UserEJB.java index 2f15f3b..2e87b39 100644 --- a/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/UserEJB.java +++ b/projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/UserEJB.java @@ -1,6 +1,7 @@ package com.edf.gde.ejb; import com.edf.gde.dao.UserDao; +import com.edf.gde.dao.impl.UserDaoImpl; import com.edf.gde.entities.Group; import com.edf.gde.entities.User; import java.util.List; @@ -15,7 +16,7 @@ import javax.persistence.PersistenceContext; */ @Stateless @LocalBean -public class UserEJB { +public class UserEJB implements UserDao { @PersistenceContext(unitName = "GDE-ejbPU") private EntityManager em; @@ -28,7 +29,7 @@ public class UserEJB { * @return the new User or null if user exists or on error */ public User createUser(String userName, String password) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.createUser(userName, password); } @@ -39,7 +40,7 @@ public class UserEJB { * @return the new created Group or null on error */ public Group createGroup(String groupName) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.createGroup(groupName); } @@ -50,7 +51,7 @@ public class UserEJB { * @return null if the group does not exists */ public Group findGroup(String name) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.findGroup(name); } @@ -60,7 +61,7 @@ public class UserEJB { * @return */ public Group findGroup(long id) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.findGroup(id); } @@ -72,7 +73,7 @@ public class UserEJB { * @return */ public void addToGroup(long groupId, long userId) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); dao.addToGroup(groupId, userId); } @@ -84,7 +85,7 @@ public class UserEJB { * @return */ public void addToGroup(long groupId, List userIds) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); dao.addToGroup(groupId, userIds); } @@ -95,7 +96,7 @@ public class UserEJB { * @return */ public void removeFromGroup(long groupId, long userId) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); dao.removeFromGroup(groupId, userId); } @@ -106,7 +107,7 @@ public class UserEJB { * @return */ public boolean isInGroup(long groupId, long userId) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.isInGroup(groupId, userId); } @@ -117,7 +118,7 @@ public class UserEJB { * @return */ public User findUser(String userName) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.findUser(userName); } @@ -127,7 +128,7 @@ public class UserEJB { * @return */ public User findUser(long id) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.findUser(id); } @@ -137,7 +138,7 @@ public class UserEJB { * @return */ public boolean deleteUser(long userId) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.deleteUser(userId); } @@ -147,12 +148,12 @@ public class UserEJB { * @return */ public boolean deleteGroup(long groupId) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); return dao.deleteGroup(groupId); } public void checkPassword(String login, String password) { - UserDao dao = new UserDao(em); + UserDaoImpl dao = new UserDaoImpl(em); User user = dao.findUser(login); if (!user.getPassword().equals(password)) { throw new RuntimeException("Invalid login / password"); -- 2.39.2