mirror of
https://github.com/ArenMg/aren.git
synced 2024-11-21 16:10:52 +00:00
Vote majoritaire
This commit is contained in:
parent
5deba39910
commit
2d1eac7073
11 changed files with 1166 additions and 2 deletions
311
src/main/java/fr/lirmm/aren/model/vm/VMChoice.java
Normal file
311
src/main/java/fr/lirmm/aren/model/vm/VMChoice.java
Normal file
|
@ -0,0 +1,311 @@
|
|||
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 org.hibernate.annotations.SortNatural;
|
||||
import org.hibernate.annotations.Type;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* @author ANDRIAMBOLAHARIMIHANTA Havana on 24/06/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "vm_choices")
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = VMChoice.class)
|
||||
public class VMChoice extends AbstractEntity implements Serializable {
|
||||
|
||||
@JoinColumn(name = "themeId", referencedColumnName = "id")
|
||||
@ManyToOne(optional = false)
|
||||
private VMTheme themeId;
|
||||
|
||||
@Size(max = 255)
|
||||
@Column(name = "title")
|
||||
private String title;
|
||||
|
||||
@Lob
|
||||
@Type(type = "org.hibernate.type.TextType")
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
|
||||
@Size(max = 1024)
|
||||
@Column(name = "url")
|
||||
private String url;
|
||||
|
||||
@Lob
|
||||
@Type(type = "org.hibernate.type.TextType")
|
||||
@Column(name = "img")
|
||||
private String img;
|
||||
|
||||
@Column(name = "rejected")
|
||||
private int rejected=0;
|
||||
|
||||
@Column(name = "insufficient")
|
||||
private int insufficient=0;
|
||||
|
||||
@Column(name = "pass")
|
||||
private int pass=0;
|
||||
|
||||
@Column(name = "acceptable")
|
||||
private int acceptable=0;
|
||||
|
||||
@Column(name = "good")
|
||||
private int good=0;
|
||||
|
||||
@Column(name = "very_good")
|
||||
private int veryGood=0;
|
||||
|
||||
@Column(name = "excellent")
|
||||
private int excellent=0;
|
||||
|
||||
@Column(name = "createdAt")
|
||||
private ZonedDateTime createdAt=ZonedDateTime.now();
|
||||
|
||||
|
||||
@OneToMany(mappedBy = "subThemeId",fetch = FetchType.LAZY)
|
||||
@SortNatural
|
||||
private SortedSet<VMVote> votes = new TreeSet<>();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public VMTheme getThemeId() {
|
||||
return themeId;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param themeId
|
||||
*/
|
||||
public void setThemeId(VMTheme themeId) {
|
||||
this.themeId = themeId;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param title
|
||||
*/
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param description
|
||||
*/
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getImg() {
|
||||
return img;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param img
|
||||
*/
|
||||
public void setImg(String img) {
|
||||
this.img = img;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getRejected() {
|
||||
return rejected;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param rejected
|
||||
*/
|
||||
public void setRejected(int rejected) {
|
||||
this.rejected = rejected;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getInsufficient() {
|
||||
return insufficient;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param insufficient
|
||||
*/
|
||||
public void setInsufficient(int insufficient) {
|
||||
this.insufficient = insufficient;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getPass() {
|
||||
return pass;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param pass
|
||||
*/
|
||||
public void setPass(int pass) {
|
||||
this.pass = pass;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getAcceptable() {
|
||||
return acceptable;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param acceptable
|
||||
*/
|
||||
public void setAcceptable(int acceptable) {
|
||||
this.acceptable = acceptable;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getGood() {
|
||||
return good;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param good
|
||||
*/
|
||||
public void setGood(int good) {
|
||||
this.good = good;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getVeryGood() {
|
||||
return veryGood;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param veryGood
|
||||
*/
|
||||
public void setVeryGood(int veryGood) {
|
||||
this.veryGood = veryGood;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getExcellent() {
|
||||
return excellent;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param excellent
|
||||
*/
|
||||
public void setExcellent(int excellent) {
|
||||
this.excellent = excellent;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ZonedDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param createdAt
|
||||
*/
|
||||
public void setCreatedAt(ZonedDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public SortedSet<VMVote> getVotes() {
|
||||
return votes;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param votes
|
||||
*/
|
||||
public void setVotes(SortedSet<VMVote> votes) {
|
||||
this.votes = votes;
|
||||
}
|
||||
|
||||
public boolean isVoted(){
|
||||
if(this.getRejected()!=0 ||
|
||||
this.getInsufficient()!=0 ||
|
||||
this.getPass()!=0 ||
|
||||
this.getAcceptable()!=0 ||
|
||||
this.getGood()!=0 ||
|
||||
this.getVeryGood()!=0 ||
|
||||
this.getExcellent()!=0
|
||||
) return true ;
|
||||
return false ;
|
||||
}
|
||||
}
|
153
src/main/java/fr/lirmm/aren/model/vm/VMTheme.java
Normal file
153
src/main/java/fr/lirmm/aren/model/vm/VMTheme.java
Normal file
|
@ -0,0 +1,153 @@
|
|||
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.Type;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.io.Serializable;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author ANDRIAMBOLAHARIMIHANTA Havana on 24/06/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "vm_themes")
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = VMTheme.class)
|
||||
public class VMTheme extends AbstractEntity implements Serializable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@JoinColumn(name = "author", referencedColumnName = "id")
|
||||
@ManyToOne
|
||||
private User author;
|
||||
|
||||
|
||||
@Size(max = 255)
|
||||
@Column(name = "title")
|
||||
private String title;
|
||||
|
||||
@Lob
|
||||
@Type(type = "org.hibernate.type.TextType")
|
||||
@Column(name = "description")
|
||||
private String description;
|
||||
|
||||
@Column(name = "createdAt")
|
||||
private ZonedDateTime createdAt=ZonedDateTime.now();
|
||||
|
||||
@Column(name = "expiracyDate")
|
||||
private ZonedDateTime expiracyDate;
|
||||
|
||||
@OneToMany(mappedBy = "themeId")
|
||||
private Set<VMChoice> choices = new LinkedHashSet<>();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public User getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param author
|
||||
*/
|
||||
public void setAuthor(User author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param title
|
||||
*/
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param description
|
||||
*/
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ZonedDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param createdAt
|
||||
*/
|
||||
public void setCreatedAt(ZonedDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ZonedDateTime getExpiracyDate() {
|
||||
return expiracyDate;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param expiracyDate
|
||||
*/
|
||||
public void setExpiracyDate(ZonedDateTime expiracyDate) {
|
||||
this.expiracyDate = expiracyDate;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Set<VMChoice> getChoices() {
|
||||
return choices;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param choices
|
||||
*/
|
||||
public void setChoices(Set<VMChoice> choices) {
|
||||
this.choices = choices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "title : "+this.title+"\n"
|
||||
+"Description : "+this.description+"\n"
|
||||
+"Expiration : "+this.expiracyDate+"\n";
|
||||
}
|
||||
}
|
108
src/main/java/fr/lirmm/aren/model/vm/VMVote.java
Normal file
108
src/main/java/fr/lirmm/aren/model/vm/VMVote.java
Normal file
|
@ -0,0 +1,108 @@
|
|||
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 javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
* @author ANDRIAMBOLAHARIMIHANTA Havana on 25/06/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "vm_votes")
|
||||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = VMVote.class)
|
||||
public class VMVote extends AbstractEntity implements Serializable {
|
||||
public enum Opinion {
|
||||
REJECTED,
|
||||
INSUFFICIENT,
|
||||
PASS,
|
||||
ACCEPTABLE,
|
||||
GOOD,
|
||||
VERY_GOOD,
|
||||
EXCELLENT
|
||||
}
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "subThemeId")
|
||||
private VMChoice subThemeId;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "authorId")
|
||||
private User authorId ;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "opinion", nullable = false)
|
||||
private Opinion opinion=Opinion.REJECTED;
|
||||
|
||||
@Column(name = "createdAt")
|
||||
private ZonedDateTime createdAt=ZonedDateTime.now();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public VMChoice getSubThemeId() {
|
||||
return subThemeId;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param subThemeId
|
||||
*/
|
||||
public void setSubThemeId(VMChoice subThemeId) {
|
||||
this.subThemeId = subThemeId;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public User getAuthorId() {
|
||||
return authorId;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param authorId
|
||||
*/
|
||||
public void setAuthorId(User authorId) {
|
||||
this.authorId = authorId;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Opinion getOpinion() {
|
||||
return opinion;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param opinion
|
||||
*/
|
||||
public void setOpinion(Opinion opinion) {
|
||||
this.opinion = opinion;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ZonedDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param createdAt
|
||||
*/
|
||||
public void setCreatedAt(ZonedDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
}
|
|
@ -152,6 +152,42 @@ public abstract class AbstractService<T extends AbstractEntity> {
|
|||
}
|
||||
}
|
||||
|
||||
public T insert(T entity) {
|
||||
// If ever the entity is attached, it needs to be detached
|
||||
getEntityManager().detach(entity);
|
||||
// If ever the id is set, it needs to be removed
|
||||
entity.setId(null);
|
||||
|
||||
try {
|
||||
this.transactionBegin();
|
||||
getEntityManager().persist(entity);
|
||||
getEntityManager().refresh(entity);
|
||||
this.afterCreate(entity);
|
||||
this.commit();
|
||||
} catch (PersistenceException e) {
|
||||
if (e.getCause() instanceof PropertyValueException) {
|
||||
PropertyValueException cause = (PropertyValueException) e.getCause();
|
||||
throw InsertEntityException.MANDATORY_PROPERTY(cause.getPropertyName());
|
||||
} else if (e.getCause() instanceof ConstraintViolationException) {
|
||||
// Parse the error to a client readable one
|
||||
Throwable cause = e.getCause().getCause();
|
||||
String details = cause.getMessage();
|
||||
Pattern pattern = Pattern.compile("\\((.*)\\)=\\((.*)\\)");
|
||||
Matcher matcher = pattern.matcher(details);
|
||||
if (matcher.find()) {
|
||||
String keyName = matcher.group(1);
|
||||
String keyValue = matcher.group(2);
|
||||
throw InsertEntityException.DUPLICATE_KEY(keyName, keyValue);
|
||||
} else {
|
||||
throw InsertEntityException.OTHER(details);
|
||||
}
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
return entity ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entity
|
||||
|
|
|
@ -40,8 +40,8 @@ public class FDThemeService extends AbstractService<FDTheme> {
|
|||
super.edit(fdTheme);
|
||||
}
|
||||
|
||||
public FDTheme find(Long fdPollId, boolean withChoices, boolean withVotes) {
|
||||
List<FDTheme> results = generateQuery(fdPollId, withChoices,withVotes).getResultList();
|
||||
public FDTheme find(Long themeId, boolean withChoices, boolean withVotes) {
|
||||
List<FDTheme> results = generateQuery(themeId, withChoices,withVotes).getResultList();
|
||||
if (results.isEmpty()) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
|
67
src/main/java/fr/lirmm/aren/service/vm/VMChoiceService.java
Normal file
67
src/main/java/fr/lirmm/aren/service/vm/VMChoiceService.java
Normal file
|
@ -0,0 +1,67 @@
|
|||
package fr.lirmm.aren.service.vm;
|
||||
|
||||
import fr.lirmm.aren.model.vm.VMChoice;
|
||||
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 08/07/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class VMChoiceService extends AbstractService<VMChoice> {
|
||||
public VMChoiceService(){super(VMChoice.class) ;}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param choiceId
|
||||
* @param withVotes
|
||||
* @return
|
||||
*/
|
||||
private TypedQuery<VMChoice> generateQuery(Long choiceId, boolean withVotes){
|
||||
TypedQuery<VMChoice> query = getEntityManager().createQuery("SELECT vmc "
|
||||
+"FROM VMChoice vmc "
|
||||
+(withVotes? "LEFT JOIN FETCH vmc.votes vo " : "")
|
||||
+ (choiceId != null
|
||||
? "WHERE vmc.id = :choiceId "
|
||||
: "WHERE vmc.id IS NOT NULL ")
|
||||
,VMChoice.class
|
||||
) ;
|
||||
if (choiceId != null) {
|
||||
query.setParameter("choiceId", choiceId);
|
||||
}
|
||||
return query ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param withVotes
|
||||
* @return
|
||||
*/
|
||||
public Set<VMChoice> findAll(boolean withVotes){
|
||||
return new HashSet<VMChoice>(generateQuery(null, withVotes).getResultList()) ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param choice
|
||||
*/
|
||||
@Override
|
||||
public void edit(VMChoice choice){
|
||||
super.edit(choice);
|
||||
}
|
||||
|
||||
public VMChoice find(Long choiceId, boolean withVotes){
|
||||
List<VMChoice> choices=generateQuery(choiceId,withVotes).getResultList() ;
|
||||
if (choices.isEmpty()) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return choices.get(0) ;
|
||||
}
|
||||
}
|
100
src/main/java/fr/lirmm/aren/service/vm/VMThemeService.java
Normal file
100
src/main/java/fr/lirmm/aren/service/vm/VMThemeService.java
Normal file
|
@ -0,0 +1,100 @@
|
|||
package fr.lirmm.aren.service.vm;
|
||||
|
||||
import fr.lirmm.aren.model.vm.VMChoice;
|
||||
import fr.lirmm.aren.model.vm.VMTheme;
|
||||
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 08/07/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class VMThemeService extends AbstractService<VMTheme> {
|
||||
public VMThemeService(){
|
||||
super(VMTheme.class) ;
|
||||
}
|
||||
private TypedQuery<VMTheme> generateQuery(Long themeId, boolean withChoices, boolean withVotes){
|
||||
TypedQuery<VMTheme> query = getEntityManager().createQuery("SELECT vmt "
|
||||
+"FROM VMTheme vmt "
|
||||
+(withChoices? "LEFT JOIN FETCH vmt.choices co " : "")
|
||||
+(withVotes? "LEFT JOIN FETCH co.votes vo " : "")
|
||||
+ (themeId != null
|
||||
? "WHERE vmt.id = :themeId "
|
||||
: "WHERE vmt.id IS NOT NULL ")
|
||||
, VMTheme.class
|
||||
) ;
|
||||
if (themeId != null) {
|
||||
query.setParameter("themeId", themeId);
|
||||
}
|
||||
return query ;
|
||||
}
|
||||
public Set<VMTheme> findAll(boolean withChoices, boolean withVotes) {
|
||||
return new HashSet<VMTheme>(generateQuery(null, withChoices,withVotes).getResultList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void edit(VMTheme theme) {
|
||||
super.edit(theme);
|
||||
}
|
||||
|
||||
public VMTheme find(Long themeId, boolean withChoices, boolean withVotes) {
|
||||
List<VMTheme> themes = generateQuery(themeId, withChoices,withVotes).getResultList();
|
||||
if (themes.isEmpty()) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
return themes.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param themeId
|
||||
*/
|
||||
public void delete(Long themeId) {
|
||||
this.transactionBegin();
|
||||
List<VMChoice> choices = getEntityManager().createQuery("SELECT c "
|
||||
+ "FROM VMChoice c "
|
||||
+ "WHERE c.themeId.id = :themeId ", VMChoice.class)
|
||||
.setParameter("themeId", themeId)
|
||||
.getResultList();
|
||||
if (!choices.isEmpty()) {
|
||||
for(VMChoice choice : choices){
|
||||
this.deleteVotes(choice.getId());
|
||||
}
|
||||
}
|
||||
try{
|
||||
getEntityManager().createQuery("DELETE FROM VMChoice choice "
|
||||
+ "WHERE choice.themeId.id = :themeId")
|
||||
.setParameter("themeId", themeId)
|
||||
.executeUpdate();
|
||||
|
||||
getEntityManager().createQuery("DELETE FROM VMTheme theme "
|
||||
+ "WHERE theme.id = :themeId")
|
||||
.setParameter("themeId", themeId)
|
||||
.executeUpdate();
|
||||
}catch(Exception ex){
|
||||
System.err.println("Erreur : "+ex.getMessage());
|
||||
}
|
||||
|
||||
this.commit();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param choiceId
|
||||
*/
|
||||
public void deleteVotes(Long choiceId) {
|
||||
this.transactionBegin();
|
||||
getEntityManager().createQuery("DELETE FROM VMVote vote "
|
||||
+ "WHERE vote.subThemeId.id = :choiceId")
|
||||
.setParameter("choiceId", choiceId)
|
||||
.executeUpdate();
|
||||
this.commit();
|
||||
}
|
||||
}
|
86
src/main/java/fr/lirmm/aren/service/vm/VMVoteService.java
Normal file
86
src/main/java/fr/lirmm/aren/service/vm/VMVoteService.java
Normal file
|
@ -0,0 +1,86 @@
|
|||
package fr.lirmm.aren.service.vm;
|
||||
|
||||
import fr.lirmm.aren.model.vm.VMChoice;
|
||||
import fr.lirmm.aren.model.vm.VMVote;
|
||||
import fr.lirmm.aren.service.AbstractService;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.ws.rs.NotFoundException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ANDRIAMBOLAHARIMIHANTA Havana on 25/06/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class VMVoteService extends AbstractService<VMVote> {
|
||||
public VMVoteService(){
|
||||
super(VMVote.class) ;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterCreate(VMVote entity){
|
||||
this.updateExternaleTables(entity);
|
||||
}
|
||||
|
||||
public void updateExternaleTables(VMVote vmVote){
|
||||
super.transactionBegin();
|
||||
|
||||
List<VMChoice> results = getEntityManager().createQuery("SELECT c "
|
||||
+ "FROM VMChoice c "
|
||||
+ "WHERE c.id = :id", VMChoice.class)
|
||||
.setParameter("id", vmVote.getSubThemeId().getId())
|
||||
.getResultList();
|
||||
|
||||
if (results.isEmpty()) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
VMChoice choice=results.get(0) ;
|
||||
switch(vmVote.getOpinion()){
|
||||
case REJECTED:
|
||||
choice.setRejected(choice.getRejected()+1);
|
||||
break ;
|
||||
case INSUFFICIENT:
|
||||
choice.setInsufficient(choice.getInsufficient()+1);
|
||||
break;
|
||||
case PASS:
|
||||
choice.setPass(choice.getPass()+1);
|
||||
break;
|
||||
case ACCEPTABLE:
|
||||
choice.setAcceptable(choice.getAcceptable()+1);
|
||||
break ;
|
||||
case GOOD:
|
||||
choice.setGood(choice.getGood()+1);
|
||||
break ;
|
||||
case VERY_GOOD:
|
||||
choice.setVeryGood(choice.getVeryGood()+1);
|
||||
break ;
|
||||
case EXCELLENT:
|
||||
choice.setExcellent(choice.getExcellent()+1);
|
||||
break ;
|
||||
}
|
||||
|
||||
getEntityManager().createQuery("UPDATE VMChoice c SET "
|
||||
+ "c.rejected = :rejected, "
|
||||
+ "c.insufficient = :insufficient, "
|
||||
+ "c.pass = :pass, "
|
||||
+ "c.acceptable = :acceptable, "
|
||||
+ "c.good = :good, "
|
||||
+ "c.veryGood = :veryGood, "
|
||||
+ "c.excellent = :excellent "
|
||||
+"WHERE c.id=:id"
|
||||
)
|
||||
.setParameter("rejected",choice.getRejected())
|
||||
.setParameter("insufficient", choice.getInsufficient())
|
||||
.setParameter("pass",choice.getPass())
|
||||
.setParameter("acceptable",choice.getAcceptable())
|
||||
.setParameter("good", choice.getGood())
|
||||
.setParameter("veryGood",choice.getVeryGood())
|
||||
.setParameter("excellent",choice.getExcellent())
|
||||
.setParameter("id", vmVote.getSubThemeId().getId())
|
||||
.executeUpdate() ;
|
||||
super.commit();
|
||||
}
|
||||
|
||||
}
|
88
src/main/java/fr/lirmm/aren/ws/rest/VMChoiceRESTFacade.java
Normal file
88
src/main/java/fr/lirmm/aren/ws/rest/VMChoiceRESTFacade.java
Normal file
|
@ -0,0 +1,88 @@
|
|||
package fr.lirmm.aren.ws.rest;
|
||||
|
||||
import fr.lirmm.aren.model.vm.VMChoice;
|
||||
import fr.lirmm.aren.service.vm.VMChoiceService;
|
||||
import fr.lirmm.aren.service.vm.VMVoteService;
|
||||
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.Path;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author ANDRIAMBOLAHARIMIHANTA Havana on 08/07/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@RequestScoped
|
||||
@Path("vm/choices")
|
||||
public class VMChoiceRESTFacade extends AbstractRESTFacade<VMChoice>{
|
||||
@Inject
|
||||
VMChoiceService choiceService ;
|
||||
|
||||
@Inject
|
||||
VMVoteService voteService ;
|
||||
@Override
|
||||
protected VMChoiceService getService() {
|
||||
return choiceService;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param choice
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
//@RolesAllowed({"ADMIN"})
|
||||
@PermitAll
|
||||
public VMChoice create(VMChoice choice){
|
||||
return super.create(choice) ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param id of the Entity to update
|
||||
* @param choice
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public VMChoice edit(Long id,VMChoice choice){
|
||||
return super.edit(id,choice) ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param id of the Entity to remove
|
||||
*/
|
||||
@Override
|
||||
@RolesAllowed({"ADMIN"})
|
||||
public void remove(Long id) {
|
||||
super.remove(id);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@PermitAll
|
||||
public Set<VMChoice> findAll() {
|
||||
boolean withVotes = this.overview == null;
|
||||
Set<VMChoice> choices= choiceService.findAll(withVotes);
|
||||
return choices ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param id of the Entity to fetch
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@PermitAll
|
||||
public VMChoice find(Long id) {
|
||||
boolean withVotes = this.overview == null;
|
||||
return choiceService.find(id,withVotes);
|
||||
}
|
||||
}
|
136
src/main/java/fr/lirmm/aren/ws/rest/VMThemeRESTFacade.java
Normal file
136
src/main/java/fr/lirmm/aren/ws/rest/VMThemeRESTFacade.java
Normal file
|
@ -0,0 +1,136 @@
|
|||
package fr.lirmm.aren.ws.rest;
|
||||
|
||||
import fr.lirmm.aren.model.vm.VMChoice;
|
||||
import fr.lirmm.aren.model.vm.VMTheme;
|
||||
import fr.lirmm.aren.service.vm.VMChoiceService;
|
||||
import fr.lirmm.aren.service.vm.VMThemeService;
|
||||
import fr.lirmm.aren.service.vm.VMVoteService;
|
||||
import fr.mieuxvoter.mj.*;
|
||||
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author ANDRIAMBOLAHARIMIHANTA Havana on 08/07/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@RequestScoped
|
||||
@Path("vm/themes")
|
||||
public class VMThemeRESTFacade extends AbstractRESTFacade<VMTheme>{
|
||||
@Inject
|
||||
VMThemeService themeService ;
|
||||
|
||||
@Inject
|
||||
VMChoiceService choiceService ;
|
||||
|
||||
@Inject
|
||||
VMVoteService voteService ;
|
||||
|
||||
@Override
|
||||
protected VMThemeService getService() {
|
||||
return themeService;
|
||||
}
|
||||
|
||||
@Override
|
||||
//@RolesAllowed({"ADMIN"})
|
||||
@PermitAll
|
||||
public VMTheme create(VMTheme theme){
|
||||
System.out.println(theme.toString());
|
||||
return super.create(theme);
|
||||
}
|
||||
|
||||
@Override
|
||||
//@RolesAllowed({"ADMIN"})
|
||||
public VMTheme edit(Long id, VMTheme theme){return super.edit(id, theme);}
|
||||
|
||||
@Override
|
||||
@PermitAll
|
||||
//@RolesAllowed({"MODO"})
|
||||
public Set<VMTheme> findAll() {
|
||||
boolean withChoices = this.overview == null;
|
||||
Set<VMTheme> themes= themeService.findAll(withChoices, true);
|
||||
/**
|
||||
* Sort by voting rank
|
||||
*/
|
||||
|
||||
Set<VMTheme> newThemes=new HashSet<>() ;
|
||||
|
||||
|
||||
themes.forEach(theme -> {
|
||||
Object []choices=theme.getChoices().toArray() ;
|
||||
List<VMChoice> choicesNotVoted=new ArrayList<>() ;
|
||||
List<ProposalTallyInterface> proposalTallyInterfaces=new ArrayList<>() ;
|
||||
for(int i=0 ; i<choices.length ; i++){
|
||||
VMChoice choice=(VMChoice) choices[i] ;
|
||||
if(choice.isVoted()){
|
||||
proposalTallyInterfaces.add(new ProposalTally(
|
||||
new Integer[]{choice.getRejected(),
|
||||
choice.getInsufficient(),
|
||||
choice.getPass(),
|
||||
choice.getAcceptable(),
|
||||
choice.getGood(),
|
||||
choice.getVeryGood(),
|
||||
choice.getExcellent()})) ;
|
||||
}else{
|
||||
choicesNotVoted.add(choice) ;
|
||||
}
|
||||
}
|
||||
VMChoice newChoices[]=new VMChoice[choices.length] ;
|
||||
if(!proposalTallyInterfaces.isEmpty()){
|
||||
ProposalTallyInterface []proposalTallyInterfacesArray=new ProposalTallyInterface[proposalTallyInterfaces.size()] ;
|
||||
for(int i=0 ; i<proposalTallyInterfaces.size() ; i++){
|
||||
proposalTallyInterfacesArray[i]=proposalTallyInterfaces.get(i) ;
|
||||
}
|
||||
TallyInterface tally = new NormalizedTally(proposalTallyInterfacesArray) ;
|
||||
DeliberatorInterface mj = new MajorityJudgmentDeliberator();
|
||||
ResultInterface result = mj.deliberate(tally);
|
||||
|
||||
|
||||
int index=0 ;
|
||||
for(ProposalResultInterface item : result.getProposalResults()){
|
||||
newChoices[item.getRank()-1]=(VMChoice) choices[index] ;
|
||||
System.out.println(item.getRank()+" - "+newChoices[item.getRank()-1].getTitle());
|
||||
index++ ;
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0 ; i<choicesNotVoted.size() ; i++){
|
||||
newChoices[newChoices.length-(i+1)]= choicesNotVoted.get(i) ;
|
||||
}
|
||||
LinkedHashSet<VMChoice> setChoices = new LinkedHashSet<>();
|
||||
System.out.println("Rang : ") ;
|
||||
for(int i=0 ; i<newChoices.length ; i++){
|
||||
System.out.println(i+" - "+newChoices[i].getTitle());
|
||||
setChoices.add(newChoices[i]) ;
|
||||
}
|
||||
|
||||
theme.setChoices(setChoices);
|
||||
newThemes.add(theme) ;
|
||||
|
||||
});
|
||||
|
||||
return newThemes ;
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("delete/{id}")
|
||||
@RolesAllowed({"ADMIN"})
|
||||
public void delete(@PathParam("id") Long id) {
|
||||
themeService.delete(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
//@RolesAllowed({"MODO"})
|
||||
@PermitAll
|
||||
public VMTheme find(Long id) {
|
||||
boolean withChoices = this.overview == null;
|
||||
VMTheme theme = themeService.find(id,withChoices,true);
|
||||
return theme;
|
||||
}
|
||||
}
|
79
src/main/java/fr/lirmm/aren/ws/rest/VMVoteRESTFacade.java
Normal file
79
src/main/java/fr/lirmm/aren/ws/rest/VMVoteRESTFacade.java
Normal file
|
@ -0,0 +1,79 @@
|
|||
package fr.lirmm.aren.ws.rest;
|
||||
|
||||
|
||||
import fr.lirmm.aren.model.vm.VMChoice;
|
||||
import fr.lirmm.aren.model.vm.VMVote;
|
||||
import fr.lirmm.aren.service.AbstractService;
|
||||
import fr.lirmm.aren.service.vm.VMVoteService;
|
||||
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.Path;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author ANDRIAMBOLAHARIMIHANTA Havana on 08/07/2021
|
||||
* @project aren-1
|
||||
*/
|
||||
@RequestScoped
|
||||
@Path("vm/votes")
|
||||
public class VMVoteRESTFacade extends AbstractRESTFacade<VMVote>{
|
||||
@Inject
|
||||
VMVoteService vmVoteService ;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected VMVoteService getService() {
|
||||
return this.vmVoteService;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param vote
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
//@RolesAllowed({"USER"})
|
||||
@PermitAll
|
||||
public VMVote create(VMVote vote){
|
||||
return super.create(vote) ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param voteId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
//@RolesAllowed({"USER"})
|
||||
@PermitAll
|
||||
public VMVote find(Long voteId){
|
||||
return super.find(voteId) ;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@Override
|
||||
@RolesAllowed({"ADMIN"})
|
||||
public void remove(Long id) {
|
||||
super.remove(id);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
//@RolesAllowed({"USER"})
|
||||
@PermitAll
|
||||
public Set<VMVote> findAll(){
|
||||
return super.findAll() ;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue