diff --git a/src/main/java/fr/lirmm/aren/model/User.java b/src/main/java/fr/lirmm/aren/model/User.java index 9a7c960..e0312a7 100644 --- a/src/main/java/fr/lirmm/aren/model/User.java +++ b/src/main/java/fr/lirmm/aren/model/User.java @@ -25,6 +25,8 @@ import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import javax.persistence.JoinTable; + +import fr.lirmm.aren.model.vm.VMTeam; import org.hibernate.annotations.Filter; import org.hibernate.annotations.Filters; import org.hibernate.annotations.SortNatural; @@ -135,6 +137,15 @@ public class User extends AbstractEntEntity implements Serializable { @SortNatural private SortedSet teams = new TreeSet<>(); + @JoinTable(name = "vm_teams_users", + joinColumns = { + @JoinColumn(name = "user_id", referencedColumnName = "id")}, + inverseJoinColumns = { + @JoinColumn(name = "team_id", referencedColumnName = "id")}) + @ManyToMany + @SortNatural + private SortedSet vmTeams = new TreeSet<>(); + @OneToMany(mappedBy = "owner") @SortNatural private SortedSet notifications = new TreeSet<>(); @@ -372,6 +383,40 @@ public class User extends AbstractEntEntity implements Serializable { team.getUsers().remove(this); } + /** + * + * @return + */ + public SortedSet getVmTeams() { + return vmTeams; + } + + /** + * + * @param vmTeams + */ + public void setVmTeams(SortedSet vmTeams) { + this.vmTeams = vmTeams; + } + + /** + * + * @param team + */ + public void addMember(VMTeam team) { + vmTeams.add(team); + team.getMembers().add(this); + } + + /** + * + * @param team + */ + public void removeMember(VMTeam team) { + vmTeams.remove(team); + team.getMembers().remove(this); + } + /** * * @return diff --git a/src/main/java/fr/lirmm/aren/model/vm/VMTeam.java b/src/main/java/fr/lirmm/aren/model/vm/VMTeam.java new file mode 100644 index 0000000..4a58ace --- /dev/null +++ b/src/main/java/fr/lirmm/aren/model/vm/VMTeam.java @@ -0,0 +1,81 @@ +package fr.lirmm.aren.model.vm; + +import com.fasterxml.jackson.annotation.JsonIdentityInfo; +import com.fasterxml.jackson.annotation.ObjectIdGenerators; +import fr.lirmm.aren.model.AbstractEntity; +import fr.lirmm.aren.model.User; +import org.hibernate.annotations.SortNatural; +import org.hibernate.annotations.Where; + +import javax.persistence.*; +import javax.validation.constraints.Size; +import java.io.Serializable; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * @author ANDRIAMBOLAHARIMIHANTA Havana on 11/07/2021 + * @project aren-1 + */ +@Entity +@Table(name = "vm_teams") +@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = VMTheme.class) +public class VMTeam extends AbstractEntity implements Serializable { + @Size(max = 255) + @Column(name = "name") + private String name; + + @ManyToMany(mappedBy = "vmTeams") + @SortNatural + private SortedSet members = new TreeSet<>(); + + /** + * + * @return + */ + public String getName() { + return name; + } + + /** + * + * @param name + */ + public void setName(String name) { + this.name = name; + } + + /** + * + * @return + */ + public SortedSet getMembers() { + return members; + } + + /** + * + * @param members + */ + public void setMembers(SortedSet members) { + this.members = members; + } + + /** + * + * @param user + */ + public void addUser(User user) { + this.members.add(user); + user.getVmTeams().add(this); + } + + /** + * + * @param user + */ + public void removeUser(User user) { + this.members.remove(user); + user.getVmTeams().remove(this); + } +} \ No newline at end of file diff --git a/src/main/java/fr/lirmm/aren/model/vm/VMTheme.java b/src/main/java/fr/lirmm/aren/model/vm/VMTheme.java index 7befdfa..bf0dfc6 100644 --- a/src/main/java/fr/lirmm/aren/model/vm/VMTheme.java +++ b/src/main/java/fr/lirmm/aren/model/vm/VMTheme.java @@ -29,6 +29,9 @@ public class VMTheme extends AbstractEntity implements Serializable { @ManyToOne private User author; + @JoinColumn(name = "team", referencedColumnName = "id") + @ManyToOne + private VMTeam team; @Size(max = 255) @Column(name = "title") @@ -64,6 +67,22 @@ public class VMTheme extends AbstractEntity implements Serializable { this.author = author; } + /** + * + * @return + */ + public VMTeam getTeam() { + return team; + } + + /** + * + * @param team + */ + public void setTeam(VMTeam team) { + this.team = team; + } + /** * * @return diff --git a/src/main/java/fr/lirmm/aren/service/vm/VMTeamService.java b/src/main/java/fr/lirmm/aren/service/vm/VMTeamService.java new file mode 100644 index 0000000..fe6f6b8 --- /dev/null +++ b/src/main/java/fr/lirmm/aren/service/vm/VMTeamService.java @@ -0,0 +1,94 @@ +package fr.lirmm.aren.service.vm; + +import fr.lirmm.aren.model.Debate; +import fr.lirmm.aren.model.Team; +import fr.lirmm.aren.model.vm.VMTeam; +import fr.lirmm.aren.service.AbstractService; + +import javax.enterprise.context.ApplicationScoped; +import javax.persistence.TypedQuery; +import javax.ws.rs.NotFoundException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @author ANDRIAMBOLAHARIMIHANTA Havana on 11/07/2021 + * @project aren-1 + */ +@ApplicationScoped +public class VMTeamService extends AbstractService { + /** + * + */ + public VMTeamService(){ + super(VMTeam.class) ; + } + + /** + * + * @param teamId + * @return + */ + private TypedQuery generateQuery(Long teamId){ + TypedQuery query = getEntityManager().createQuery("SELECT vmt " + +"FROM VMTeam vmt " + +"LEFT JOIN FETCH vmt.members m " + + (teamId != null + ? "WHERE vmt.id = :teamId " + : "WHERE vmt.id IS NOT NULL ") + , VMTeam.class + ) ; + if (teamId != null) { + query.setParameter("teamId", teamId); + } + return query ; + } + + /** + * + * @return + */ + public Set findAll() { + return new HashSet(generateQuery(null).getResultList()); + } + + /** + * + * @param team + */ + @Override + public void edit(VMTeam team) { + super.edit(team); + } + + @Override + public VMTeam insert(VMTeam team) { + return super.insert(team) ; + } + + /** + * + * @param teamId + * @return + */ + public VMTeam find(Long teamId) { + List teams = generateQuery(teamId).getResultList(); + if (teams.isEmpty()) { + throw new NotFoundException(); + } + return teams.get(0); + } + + public void updateExternaleTables(VMTeam team) { + super.transactionBegin(); + team.getMembers().forEach(member->{ + System.out.println(team.getId()+" --- "+member.getId()); + getEntityManager().createNativeQuery("INSERT INTO vm_teams_users(vmteam_id,members_id) VALUES(?,?) ") + .setParameter(1, team.getId()) + .setParameter(2, member.getId()) + .executeUpdate(); + super.commit(); + }); + } +} diff --git a/src/main/java/fr/lirmm/aren/ws/rest/VMTeamRESTFacade.java b/src/main/java/fr/lirmm/aren/ws/rest/VMTeamRESTFacade.java new file mode 100644 index 0000000..04af0ad --- /dev/null +++ b/src/main/java/fr/lirmm/aren/ws/rest/VMTeamRESTFacade.java @@ -0,0 +1,90 @@ +package fr.lirmm.aren.ws.rest; + +import fr.lirmm.aren.model.Team; +import fr.lirmm.aren.model.User; +import fr.lirmm.aren.model.vm.VMTeam; +import fr.lirmm.aren.service.UserService; +import fr.lirmm.aren.service.vm.VMTeamService; + +import javax.annotation.security.PermitAll; +import javax.annotation.security.RolesAllowed; +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import java.util.Set; +import java.util.SortedSet; + +/** + * @author ANDRIAMBOLAHARIMIHANTA Havana on 11/07/2021 + * @project aren-1 + */ +@RequestScoped +@Path("vm/teams") +public class VMTeamRESTFacade extends AbstractRESTFacade{ + @Inject + VMTeamService teamService ; + + @Inject + UserService userService ; + + /** + * + * @return + */ + @Override + protected VMTeamService getService(){ + return teamService ; + } + + /** + * + * @param team + * @return + */ + @Override + @PermitAll + public VMTeam create(VMTeam team){ + SortedSet members=team.getMembers() ; + VMTeam teamRes= teamService.insert(team) ; + members.forEach(member->{ + teamRes.addUser(userService.getReference(member.getId())); + teamService.edit(teamRes); + }); + return teamRes ; + } + + /** + * + * @param id of the Entity to update + * @param team + * @return + */ + @Override + public VMTeam edit(Long id, VMTeam team){ + return super.edit(id,team) ; + } + + /** + * + * @return + */ + @Override + @PermitAll + public Set findAll() { + Set teams= teamService.findAll(); + return teams ; + } + + /** + * + * @param id of the Entity to fetch + * @return + */ + @Override + @PermitAll + public VMTeam find(Long id) { + return teamService.find(id); + } +}