diff --git a/src/main/java/se/su/dsv/scipro/project/panels/AcceptIdeaDialogPanel.java b/src/main/java/se/su/dsv/scipro/project/panels/AcceptIdeaDialogPanel.java index 6a437374b5..b1893eef95 100644 --- a/src/main/java/se/su/dsv/scipro/project/panels/AcceptIdeaDialogPanel.java +++ b/src/main/java/se/su/dsv/scipro/project/panels/AcceptIdeaDialogPanel.java @@ -9,7 +9,6 @@ import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.panel.Panel; import org.apache.wicket.model.IModel; -import org.apache.wicket.model.Model; import org.apache.wicket.spring.injection.annot.SpringBean; import org.apache.wicket.util.time.Duration; @@ -17,6 +16,7 @@ import se.su.dsv.scipro.data.dataobjects.ProjectClass; import se.su.dsv.scipro.data.dataobjects.Student; import se.su.dsv.scipro.data.dataobjects.User; import se.su.dsv.scipro.match.dataobject.SupervisorIdea; +import se.su.dsv.scipro.peer.data.dao.controllers.Pair; import se.su.dsv.scipro.reusable.AddRemoveStudentsPanel; import se.su.dsv.scipro.springdata.services.SupervisorIdeaService; import se.su.dsv.scipro.util.JavascriptEventConfirmation; @@ -66,8 +66,14 @@ public class AcceptIdeaDialogPanel extends Panel { @Override public void onSubmit(){ - ideaService.partnerAcceptIdea(model, loggedInUser); - getSession().info("You have accepted to be a part of the project: "+ model.getObject().getTitle()); + Pair<Boolean, String> acceptance = ideaService.validatePartnerAcceptance(loggedInUser); + if(acceptance.head){ + ideaService.partnerAcceptIdea(model.getObject(), loggedInUser); + getSession().info("You have accepted to be a part of the project: "+ model.getObject().getTitle()); + } else { + getSession().error(acceptance.tail); + } + } }; EnclosureContainer cont = new EnclosureContainer("unconfirmedContainer", acceptButton); @@ -78,7 +84,7 @@ public class AcceptIdeaDialogPanel extends Panel { @Override public void onSubmit(){ - ideaService.declineIdea(model); + ideaService.declineIdea(model.getObject()); getSession().info("You have declined to be a part of the project: "+ model.getObject().getTitle()); } }; @@ -112,10 +118,12 @@ public class AcceptIdeaDialogPanel extends Panel { @Override public void onSubmit() { - if(ideaService.acceptIdea(model, loggedInUser, partnerPanel.getStudentSet())){ + Pair<Boolean, String> acceptance = ideaService.validateIdeaAcceptance(model.getObject(), loggedInUser, partnerPanel.getStudentSet()); + if(acceptance.head){ + ideaService.acceptIdea(model.getObject(), loggedInUser, partnerPanel.getStudentSet()); getSession().info("You have successfully accepted this project idea"); } else { - getSession().error("Something went wrong while trying to accept the project idea"); + getSession().error(acceptance.tail); } diff --git a/src/main/java/se/su/dsv/scipro/project/panels/CompleteIdeaDialogPanel.java b/src/main/java/se/su/dsv/scipro/project/panels/CompleteIdeaDialogPanel.java index eaaaa7b97e..908cb3e4fd 100644 --- a/src/main/java/se/su/dsv/scipro/project/panels/CompleteIdeaDialogPanel.java +++ b/src/main/java/se/su/dsv/scipro/project/panels/CompleteIdeaDialogPanel.java @@ -41,7 +41,7 @@ public class CompleteIdeaDialogPanel extends Panel { @Override public void onSubmit() { - ideaService.updateIdea(model); + ideaService.updateIdea(model.getObject()); getSession().info("Project idea updated"); } }; @@ -56,7 +56,7 @@ public class CompleteIdeaDialogPanel extends Panel { @Override public void onSubmit() { - ideaService.declineIdea(model); + ideaService.declineIdea(model.getObject()); getSession().info("Project idea participation cancelled"); } }; diff --git a/src/main/java/se/su/dsv/scipro/springdata/repos/SupervisorIdeaRepo.java b/src/main/java/se/su/dsv/scipro/springdata/repos/SupervisorIdeaRepo.java index 7ea9bc7d14..3f01cb3ac0 100644 --- a/src/main/java/se/su/dsv/scipro/springdata/repos/SupervisorIdeaRepo.java +++ b/src/main/java/se/su/dsv/scipro/springdata/repos/SupervisorIdeaRepo.java @@ -22,6 +22,9 @@ public interface SupervisorIdeaRepo extends JpaRepository<SupervisorIdea, Long>, @Query("select idea from SupervisorIdea idea, Student author join idea.ideaParticipations ideaPart join author.ideaParticipations authorPart where ideaPart = authorPart and author = ? and idea.ideaStatus = ?") List<SupervisorIdea> findIdeasByAuthorAndStatus(Student author, IdeaStatus status); + @Query("select count(idea) from SupervisorIdea idea, Student author join idea.ideaParticipations ideaPart join author.ideaParticipations authorPart where ideaPart = authorPart and author = ? and idea.ideaStatus = ?") + Long countIdeasByAuthorAndStatus(Student author, IdeaStatus status); + @Query("select idea from SupervisorIdea idea, Student author join idea.ideaParticipations ideaPart join author.ideaParticipations authorPart where ideaPart = authorPart and author = ? and idea.ideaStatus = ? and ideaPart.confirmed = ?") List<SupervisorIdea> findIdeas(Student author, IdeaStatus status, boolean confirmed); } diff --git a/src/main/java/se/su/dsv/scipro/springdata/serviceimpls/SupervisorIdeaServiceImpl.java b/src/main/java/se/su/dsv/scipro/springdata/serviceimpls/SupervisorIdeaServiceImpl.java index ff29cfbf6d..0b63974cd6 100644 --- a/src/main/java/se/su/dsv/scipro/springdata/serviceimpls/SupervisorIdeaServiceImpl.java +++ b/src/main/java/se/su/dsv/scipro/springdata/serviceimpls/SupervisorIdeaServiceImpl.java @@ -7,7 +7,7 @@ import java.util.SortedSet; import javax.annotation.Resource; -import org.apache.wicket.model.IModel; +import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.domain.Page; @@ -24,6 +24,7 @@ import se.su.dsv.scipro.match.dataobject.IdeaParticipation; import se.su.dsv.scipro.match.dataobject.QSupervisorIdea; import se.su.dsv.scipro.match.dataobject.SupervisorIdea; import se.su.dsv.scipro.match.dataobject.Watson; +import se.su.dsv.scipro.peer.data.dao.controllers.Pair; import se.su.dsv.scipro.springdata.repos.SupervisorIdeaRepo; import se.su.dsv.scipro.springdata.services.ApplicationPeriodService; import se.su.dsv.scipro.springdata.services.StudentService; @@ -46,7 +47,7 @@ public class SupervisorIdeaServiceImpl extends AbstractQueryService<SupervisorId private SupervisorService supervisorService; @Resource private ApplicationPeriodService applicationPeriodService; - + private transient Logger logger = Logger.getLogger(SupervisorIdeaService.class); private int MAX_PARTNERS = 1; @Autowired @@ -59,9 +60,7 @@ public class SupervisorIdeaServiceImpl extends AbstractQueryService<SupervisorId @Override @Transactional ( readOnly = false ) - public void saveSupervisorCreatedIdea(IModel<SupervisorIdea> model, Employee creator, SortedSet<Student> students) { - - SupervisorIdea idea = model.getObject(); + public void saveSupervisorCreatedIdea(SupervisorIdea idea, Employee creator, SortedSet<Student> students) { if(!students.isEmpty()) { for(Student s : students) { @@ -84,91 +83,133 @@ public class SupervisorIdeaServiceImpl extends AbstractQueryService<SupervisorId @Override @Transactional ( readOnly = false ) - public void deleteWaitingIdea(IModel<SupervisorIdea> model) { - SupervisorIdea idea = supervisorIdeaRepo.findOne(model.getObject().getId()); - if(!idea.getIdeaStatus().equals(IdeaStatus.WAITING)) //Should not be deleted if IdeaStatus is anything else than waiting. + public void deleteWaitingIdea(SupervisorIdea idea) { + SupervisorIdea reloadedIdea = supervisorIdeaRepo.findOne(idea.getId()); + if(!reloadedIdea.getIdeaStatus().equals(IdeaStatus.WAITING)) //Should not be deleted if IdeaStatus is anything else than waiting. return; else - supervisorIdeaRepo.delete(idea); + supervisorIdeaRepo.delete(reloadedIdea); } @Override @Transactional ( readOnly = false ) - public boolean acceptIdea(IModel<SupervisorIdea> model, User mainAuthor, + public void acceptIdea(SupervisorIdea idea, User mainAuthor, SortedSet<Student> studentSet) { - if(studentSet.size()> MAX_PARTNERS){ - return false; //You may not add more than a specified number of partners. - } else { - SupervisorIdea idea = supervisorIdeaRepo.findOne(model.getObject().getId()); - if(idea.getProjectClass().getCode().equals(ProjectClass.BACHELOR) && studentSet.isEmpty()){ - return false; //You need to select a partner if the idea is on bachelor level. - } + + SupervisorIdea reloadedIdea = supervisorIdeaRepo.findOne(idea.getId()); Student author = studentService.findByUser(mainAuthor); - if(studentSet.contains(author)) { - return false; //You may not select yourself as partner. - } - IdeaParticipation mainParticipation = new IdeaParticipation(); - mainParticipation.setStudent(author); - mainParticipation.setSupervisorIdea(idea); - mainParticipation.setConfirmed(true); - author.addIdeaParticipation(mainParticipation); + for(Student s : studentSet) { s = studentService.findOne(s.getId()); //Needed for lazy loading to work. IdeaParticipation ip = new IdeaParticipation(); ip.setStudent(s); - ip.setSupervisorIdea(idea); + ip.setSupervisorIdea(reloadedIdea); ip.setConfirmed(false); s.addIdeaParticipation(ip); } - idea.setIdeaStatus(IdeaStatus.TAKEN); - idea.setApplicationPeriod(applicationPeriodService.getCurrentPeriod(new Date(), idea.getProjectClass())); - return true; - } + + IdeaParticipation mainParticipation = new IdeaParticipation(); + mainParticipation.setStudent(author); + mainParticipation.setSupervisorIdea(reloadedIdea); + mainParticipation.setConfirmed(true); + author.addIdeaParticipation(mainParticipation); + + reloadedIdea.setIdeaStatus(IdeaStatus.TAKEN); + reloadedIdea.setApplicationPeriod(applicationPeriodService.getCurrentPeriod(new Date(), reloadedIdea.getProjectClass())); } + @Override + public Pair<Boolean, String> validateIdeaAcceptance(SupervisorIdea idea, + User mainAuthor, SortedSet<Student> students) { + if(students.size()> MAX_PARTNERS){ + logger.error("Accepting user: "+mainAuthor.getEmailAddress()+ " Error: More than "+MAX_PARTNERS+" partner(s) added to project idea."); + return new Pair<Boolean, String>(false, "You may not add more than "+MAX_PARTNERS+" partner to the project idea"); //You may not add more than a specified number of partners. + } + + SupervisorIdea reloadedIdea = supervisorIdeaRepo.findOne(idea.getId()); + + if(reloadedIdea.getProjectClass().getCode().equals(ProjectClass.BACHELOR) && students.isEmpty()){ + logger.error("Accepting user: " + mainAuthor.getEmailAddress() + " Error: No partner selected on bachelor level thesis."); + return new Pair<Boolean, String>(false, "You need to select a partner when the idea is on bachelor level"); //You need to select a partner if the idea is on bachelor level. + } + + Student author = studentService.findByUser(mainAuthor); + + if(students.contains(author)) { + logger.error("Accepting user: " + mainAuthor.getEmailAddress() + " Error: Accepting user selected as his/her own partner."); + return new Pair<Boolean, String>(false, "You may not select yourself as partner"); //You may not select yourself as partner. + } + + if(alreadyParticipatingOnIdea(author)) { + logger.error("Accepting user: " + mainAuthor.getEmailAddress() + " Error: Author already attached to a taken project idea."); + return new Pair<Boolean, String>(false, "You may only be part of one taken idea at once"); //You may only be part of one taken idea at once. + } + + for(Student s : students) { + if(alreadyParticipatingOnIdea(s)) { + logger.error("Accepting user: " + mainAuthor.getEmailAddress() + " Error: Co-author already attached to a taken project idea."); + return new Pair<Boolean, String>(false, "Your project partner is already part of another project idea"); + } + } + + return new Pair<Boolean, String>(true, "Validation successful"); + + } + + @Override + public Pair<Boolean, String> validatePartnerAcceptance(User user) { + Student author = studentService.findByUser(user); + + if(partnerAlreadyParticipatingOnIdea(author)){ + logger.error("Accepting user: " + user.getEmailAddress() + " Error: May not accept to be a partner if already participating in another idea."); + return new Pair<Boolean, String>(false, "You may not accept to be a partner if already participating in another idea"); + } + return new Pair<Boolean, String>(true,"Validation passed"); + } + @Override @Transactional (readOnly = false) - public void partnerAcceptIdea(IModel<SupervisorIdea> model, User loggedInUser) { - SupervisorIdea idea = supervisorIdeaRepo.findOne(model.getObject().getId()); + public void partnerAcceptIdea(SupervisorIdea idea, User loggedInUser) { + SupervisorIdea reloadedIdea = supervisorIdeaRepo.findOne(idea.getId()); Student author = studentService.findByUser(loggedInUser); - for (IdeaParticipation ip : idea.getIdeaParticipations()) + for (IdeaParticipation ip : reloadedIdea.getIdeaParticipations()) { if(ip.getStudent().equals(author)) ip.setConfirmed(true); - if(idea.getApplicationPeriod()==null) { - idea.setApplicationPeriod(applicationPeriodService.getCurrentPeriod(new Date(), idea.getProjectClass())); + } + if(reloadedIdea.getApplicationPeriod()==null) { + reloadedIdea.setApplicationPeriod(applicationPeriodService.getCurrentPeriod(new Date(), reloadedIdea.getProjectClass())); } } @Override @Transactional ( readOnly = false ) - public void declineIdea(IModel<SupervisorIdea> model) { - SupervisorIdea idea = supervisorIdeaRepo.findOne(model.getObject().getId()); + public void declineIdea(SupervisorIdea idea) { + SupervisorIdea reloadedIdea = supervisorIdeaRepo.findOne(idea.getId()); //Remove participations - for (IdeaParticipation ip : idea.getIdeaParticipations()) { + for (IdeaParticipation ip : reloadedIdea.getIdeaParticipations()) { Student s = ip.getStudent(); s.removeIdeaParticipation(ip); } //Erase watson boxes - idea.setWatson(new Watson()); + reloadedIdea.setWatson(new Watson()); //Remove application period association - idea.setApplicationPeriod(null); + reloadedIdea.setApplicationPeriod(null); //Change status back to waiting - idea.setIdeaStatus(IdeaStatus.WAITING); - + reloadedIdea.setIdeaStatus(IdeaStatus.WAITING); } @Override @Transactional ( readOnly = false ) - public void updateIdea(IModel<SupervisorIdea> model) { - SupervisorIdea idea = supervisorIdeaRepo.findOne(model.getObject().getId()); - if(model.getObject().getWatson().getWhat()!=null) - idea.getWatson().setWhat(model.getObject().getWatson().getWhat()); - if(model.getObject().getWatson().getWhy()!=null) - idea.getWatson().setWhy(model.getObject().getWatson().getWhy()); - if(model.getObject().getWatson().getTheoryHow()!=null) - idea.getWatson().setTheoryHow(model.getObject().getWatson().getTheoryHow()); - if(model.getObject().getWatson().getPracticalHow()!=null) - idea.getWatson().setPracticalHow(model.getObject().getWatson().getPracticalHow()); + public void updateIdea(SupervisorIdea idea) { + SupervisorIdea reloadedIdea = supervisorIdeaRepo.findOne(idea.getId()); + if(idea.getWatson().getWhat()!=null) + reloadedIdea.getWatson().setWhat(idea.getWatson().getWhat()); + if(idea.getWatson().getWhy()!=null) + reloadedIdea.getWatson().setWhy(idea.getWatson().getWhy()); + if(idea.getWatson().getTheoryHow()!=null) + reloadedIdea.getWatson().setTheoryHow(idea.getWatson().getTheoryHow()); + if(idea.getWatson().getPracticalHow()!=null) + reloadedIdea.getWatson().setPracticalHow(idea.getWatson().getPracticalHow()); } @Override @@ -187,8 +228,8 @@ public class SupervisorIdeaServiceImpl extends AbstractQueryService<SupervisorId // Only editable if idea status is waiting and the current user is the creator @Override - public boolean isIdeaEditable(IModel<SupervisorIdea> model, User currentUser) { - return model.getObject().getIdeaStatus().equals(IdeaStatus.WAITING)&&model.getObject().getCreator().getUser().equals(currentUser); + public boolean isIdeaEditable(SupervisorIdea idea, User currentUser) { + return idea.getIdeaStatus().equals(IdeaStatus.WAITING)&&idea.getCreator().getUser().equals(currentUser); } @@ -228,6 +269,21 @@ public class SupervisorIdeaServiceImpl extends AbstractQueryService<SupervisorId return ideas; } + @Override + public Long countByStatus(IdeaStatus status) { + return supervisorIdeaRepo.count(byStatus(status)); + } + + @Override + public Long countIdeas(IdeaStatus status, Employee supervisor, ProjectClass pc) { + return supervisorIdeaRepo.count(byStatus(status).and(bySupervisor(supervisor)).and(byLevel(pc))); + } + + @Override + public Long countByAuthorAndStatus(Student author, IdeaStatus status) { + return supervisorIdeaRepo.countIdeasByAuthorAndStatus(author, status); + } + private Predicate predicateFromParams(FilterParams params) { return levelFilter(params.getLevels()).and(bySupervisor(params.getSupervisor())); } @@ -286,14 +342,19 @@ public class SupervisorIdeaServiceImpl extends AbstractQueryService<SupervisorId return QSupervisorIdea.supervisorIdea.ideaStatus.eq(status); } - @Override - public Long countByStatus(IdeaStatus status) { - return supervisorIdeaRepo.count(byStatus(status)); + private boolean alreadyParticipatingOnIdea(Student author) { + Long ideas = countByAuthorAndStatus(author, IdeaStatus.TAKEN); + if(ideas==0) + return false; + else + return true; } - - @Override - public Long countIdeas(IdeaStatus status, Employee supervisor, ProjectClass pc) { - return supervisorIdeaRepo.count(byStatus(status).and(bySupervisor(supervisor)).and(byLevel(pc))); + + private boolean partnerAlreadyParticipatingOnIdea(Student author) { + Long ideas = countByAuthorAndStatus(author, IdeaStatus.TAKEN); + if(ideas==1) + return false; + else + return true; } - } diff --git a/src/main/java/se/su/dsv/scipro/springdata/services/SupervisorIdeaService.java b/src/main/java/se/su/dsv/scipro/springdata/services/SupervisorIdeaService.java index 49e352ae3f..93221f3ea9 100644 --- a/src/main/java/se/su/dsv/scipro/springdata/services/SupervisorIdeaService.java +++ b/src/main/java/se/su/dsv/scipro/springdata/services/SupervisorIdeaService.java @@ -5,7 +5,6 @@ import java.util.Collection; import java.util.List; import java.util.SortedSet; -import org.apache.wicket.model.IModel; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -15,6 +14,7 @@ import se.su.dsv.scipro.data.dataobjects.Student; import se.su.dsv.scipro.data.dataobjects.User; import se.su.dsv.scipro.match.dataobject.Idea.IdeaStatus; import se.su.dsv.scipro.match.dataobject.SupervisorIdea; +import se.su.dsv.scipro.peer.data.dao.controllers.Pair; public interface SupervisorIdeaService extends GenericService<SupervisorIdea, Long>, QueryService<SupervisorIdea, Long> { @@ -25,19 +25,23 @@ public interface SupervisorIdeaService extends GenericService<SupervisorIdea, Lo List<SupervisorIdea> findByStatusAndAuthor(IdeaStatus status, Student author); List<SupervisorIdea> findIdeas(IdeaStatus status, Student author, boolean confirmed); + Long countByAuthorAndStatus(Student author, IdeaStatus status); Long countByStatus(IdeaStatus status); Long countIdeas(IdeaStatus status, Employee supervisor, ProjectClass pc); - void saveSupervisorCreatedIdea(IModel<SupervisorIdea> model, Employee creator, SortedSet<Student> students); - void deleteWaitingIdea(IModel<SupervisorIdea> model); - void partnerAcceptIdea(IModel<SupervisorIdea> model, User loggedInUser); - void declineIdea(IModel<SupervisorIdea> model); - void updateIdea(IModel<SupervisorIdea> model); + void saveSupervisorCreatedIdea(SupervisorIdea idea, Employee creator, SortedSet<Student> students); + void acceptIdea(SupervisorIdea idea, User mainAuthor, SortedSet<Student> sortedSet); + void deleteWaitingIdea(SupervisorIdea idea); + void partnerAcceptIdea(SupervisorIdea idea, User loggedInUser); + void declineIdea(SupervisorIdea idea); + void updateIdea(SupervisorIdea idea); - boolean acceptIdea(IModel<SupervisorIdea> model, User mainAuthor, SortedSet<Student> sortedSet); boolean hasTakenIdeas(User authorUser, boolean confirmed); - boolean isIdeaEditable(IModel<SupervisorIdea> model, User currentUser); + boolean isIdeaEditable(SupervisorIdea idea, User currentUser); + Pair<Boolean, String> validateIdeaAcceptance(SupervisorIdea idea, User loggedInUser, SortedSet<Student> students); + Pair<Boolean, String> validatePartnerAcceptance(User loggedInUser); + public static class FilterParams implements Serializable { private static final long serialVersionUID = 4981420721152104292L; @@ -61,4 +65,6 @@ public interface SupervisorIdeaService extends GenericService<SupervisorIdea, Lo } } + + } diff --git a/src/main/java/se/su/dsv/scipro/supervisor/panels/SupervisorEditIdeaPanel.java b/src/main/java/se/su/dsv/scipro/supervisor/panels/SupervisorEditIdeaPanel.java index 385c9eb55c..0f1924f815 100644 --- a/src/main/java/se/su/dsv/scipro/supervisor/panels/SupervisorEditIdeaPanel.java +++ b/src/main/java/se/su/dsv/scipro/supervisor/panels/SupervisorEditIdeaPanel.java @@ -155,7 +155,7 @@ public class SupervisorEditIdeaPanel extends Panel { target.addComponent(feedbackPanel); try { - ideaService.saveSupervisorCreatedIdea(model, ideaCreator, studentsPanel.getStudentSet()); + ideaService.saveSupervisorCreatedIdea(model.getObject(), ideaCreator, studentsPanel.getStudentSet()); setResponsePage(SupervisorProjectIdeaStartPage.class); getSession().info("Idea successfully submitted"); } catch (Exception e) { diff --git a/src/main/java/se/su/dsv/scipro/supervisor/panels/SupervisorProjectIdeaOverviewPanel.java b/src/main/java/se/su/dsv/scipro/supervisor/panels/SupervisorProjectIdeaOverviewPanel.java index ab0e088667..9626edc6b2 100644 --- a/src/main/java/se/su/dsv/scipro/supervisor/panels/SupervisorProjectIdeaOverviewPanel.java +++ b/src/main/java/se/su/dsv/scipro/supervisor/panels/SupervisorProjectIdeaOverviewPanel.java @@ -111,7 +111,7 @@ public class SupervisorProjectIdeaOverviewPanel extends Panel { @Override public boolean shouldBeVisible(IModel<SupervisorIdea> ideaModel) { - return ideaService.isIdeaEditable(ideaModel, supervisor); + return ideaService.isIdeaEditable(ideaModel.getObject(), supervisor); } }; columns[6] = new ClickableIconColumn<SupervisorIdea>(Model.of("Delete"), null, ImageIcon.ICON_DELETE) { @@ -119,12 +119,12 @@ public class SupervisorProjectIdeaOverviewPanel extends Panel { @Override protected void onClick(IModel<SupervisorIdea> ideaModel, AjaxRequestTarget target) { - ideaService.deleteWaitingIdea(ideaModel); + ideaService.deleteWaitingIdea(ideaModel.getObject()); target.addComponent(getWMC()); } @Override public boolean shouldBeVisible(IModel<SupervisorIdea> ideaModel) { - return ideaService.isIdeaEditable(ideaModel, supervisor); + return ideaService.isIdeaEditable(ideaModel.getObject(), supervisor); } }; diff --git a/src/test/java/se/su/dsv/scipro/springdata/TestSupervisorIdea.java b/src/test/java/se/su/dsv/scipro/springdata/TestSupervisorIdea.java index 7da25883db..19e9fdc558 100644 --- a/src/test/java/se/su/dsv/scipro/springdata/TestSupervisorIdea.java +++ b/src/test/java/se/su/dsv/scipro/springdata/TestSupervisorIdea.java @@ -6,12 +6,11 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.SortedSet; import java.util.TreeSet; import junit.framework.Assert; -import org.apache.wicket.model.IModel; -import org.apache.wicket.model.Model; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,6 +35,7 @@ import se.su.dsv.scipro.match.dataobject.Keyword; import se.su.dsv.scipro.match.dataobject.KeywordType; import se.su.dsv.scipro.match.dataobject.SupervisorIdea; import se.su.dsv.scipro.match.facade.ApplicationPeriodFacade; +import se.su.dsv.scipro.peer.data.dao.controllers.Pair; import se.su.dsv.scipro.springdata.services.ProjectClassService; import se.su.dsv.scipro.springdata.services.RoleService; import se.su.dsv.scipro.springdata.services.SupervisorIdeaService; @@ -64,8 +64,8 @@ public class TestSupervisorIdea { private KeywordDao keywordDao; private ProjectClass bachelor, master; - private User authorUser1, authorUser2, supervisorUser, supervisorUser2, unconfirmedUser; - private Student author1, author2, unconfirmedAuthor; + private User authorUser1, authorUser2, authorUser3, supervisorUser, supervisorUser2; + private Student author1, author2, author3; private Employee supervisor, supervisor2; private SupervisorIdea waitingBachelorIdea, waitingMasterIdea, takenBachelorIdea, completedMasterIdea, waitingBachelor2, waitingMaster2, waitingBachelor3; private ApplicationPeriod bachelorPeriod, masterPeriod; @@ -88,13 +88,13 @@ public class TestSupervisorIdea { supervisorUser2 = newUser(); authorUser1 = newUser(); authorUser2 = newUser(); - unconfirmedUser = newUser(); + authorUser3 = newUser(); supervisor = newEmployee(supervisorUser); supervisor2 = newEmployee(supervisorUser2); author1 = newStudent(authorUser1); author2 = newStudent(authorUser2); - unconfirmedAuthor = newStudent(unconfirmedUser); + author3 = newStudent(authorUser3); KeywordType type = new KeywordType("test type"); type = keywordTypeDao.save(type); @@ -189,8 +189,12 @@ public class TestSupervisorIdea { @Rollback public void testSaveSupervisorCreatedIdea() { Assert.assertEquals(new Long(5), ideaService.countByStatus(IdeaStatus.WAITING)); - IModel<SupervisorIdea> model = newIdeaModel(bachelor); - ideaService.saveSupervisorCreatedIdea(model, supervisor, new TreeSet<Student>()); + SupervisorIdea idea = new SupervisorIdea(); + idea.setProjectClass(bachelor); + idea.setTitle("Title"); + idea.setDescription("Description"); + idea.setRequirements("Reqs"); + ideaService.saveSupervisorCreatedIdea(idea, supervisor, new TreeSet<Student>()); Assert.assertEquals(new Long(6), ideaService.countByStatus(IdeaStatus.WAITING)); } @@ -200,14 +204,14 @@ public class TestSupervisorIdea { @Transactional @Rollback public void testIfIdeaIsEditable() { - boolean shouldBeTrue = ideaService.isIdeaEditable(Model.of(waitingBachelorIdea), supervisorUser); + boolean shouldBeTrue = ideaService.isIdeaEditable(waitingBachelorIdea, supervisorUser); Assert.assertTrue("Not true", shouldBeTrue); - boolean shouldBeFalse = ideaService.isIdeaEditable(Model.of(takenBachelorIdea), supervisorUser); + boolean shouldBeFalse = ideaService.isIdeaEditable(takenBachelorIdea, supervisorUser); Assert.assertFalse("Not false", shouldBeFalse); waitingBachelorIdea.setCreator(supervisor2); - boolean shouldAlsoBeFalse = ideaService.isIdeaEditable(Model.of(waitingBachelorIdea), supervisorUser); + boolean shouldAlsoBeFalse = ideaService.isIdeaEditable(waitingBachelorIdea, supervisorUser); Assert.assertFalse("Not false", shouldAlsoBeFalse); } @@ -219,12 +223,12 @@ public class TestSupervisorIdea { List<SupervisorIdea> ideaList = constructList(ideas); Assert.assertEquals(7, ideaList.size()); - ideaService.deleteWaitingIdea(Model.of(takenBachelorIdea)); //Should not be deleted. + ideaService.deleteWaitingIdea(takenBachelorIdea); //Should not be deleted. ideas = ideaService.findAll(); ideaList = constructList(ideas); Assert.assertEquals(7, ideaList.size()); - ideaService.deleteWaitingIdea(Model.of(waitingBachelorIdea)); //Should be deleted. + ideaService.deleteWaitingIdea(waitingBachelorIdea); //Should be deleted. ideas = ideaService.findAll(); ideaList = constructList(ideas); Assert.assertEquals(6, ideaList.size()); @@ -319,6 +323,93 @@ public class TestSupervisorIdea { Assert.assertEquals(Arrays.asList(new SupervisorIdea[]{waitingBachelorIdea, waitingMasterIdea}), ideas); } + @Test + @Transactional + @Rollback + public void testCountIdeasByAuthorAndStatus() { + Long numberOfWaitingIdeas = ideaService.countByAuthorAndStatus(author1, IdeaStatus.TAKEN); + Assert.assertTrue(numberOfWaitingIdeas==0); + + IdeaParticipation participation = new IdeaParticipation(); + participation.setStudent(author1); + participation.setSupervisorIdea(takenBachelorIdea); + participation.setConfirmed(true); + author1.addIdeaParticipation(participation); + + numberOfWaitingIdeas = ideaService.countByAuthorAndStatus(author1, IdeaStatus.TAKEN); + Assert.assertTrue(numberOfWaitingIdeas==1); + } + + @Test + @Transactional + @Rollback + public void testIdeaAcceptance() { + + Pair<Boolean, String> acceptance = ideaService.validateIdeaAcceptance(waitingBachelorIdea, authorUser1, new TreeSet<Student>()); + Assert.assertFalse("Not false", acceptance.head); //Should not be able to accept a bachelor idea without partner. + + SortedSet<Student> partnerSet = new TreeSet<Student>(); + partnerSet.add(author2); + partnerSet.add(author3); + + acceptance = ideaService.validateIdeaAcceptance(waitingBachelorIdea, authorUser1, partnerSet); + Assert.assertFalse("Not false", acceptance.head); //Should not be able to accept a bachelor idea with more than one partner. + + partnerSet.add(author1); + partnerSet.remove(author2); + partnerSet.remove(author3); + + acceptance = ideaService.validateIdeaAcceptance(waitingBachelorIdea, authorUser1, partnerSet); + Assert.assertFalse("Not false", acceptance.head); //Should not be able to accept a bachelor idea with himself as partner. + + partnerSet.remove(author1); + partnerSet.add(author2); + + acceptance = ideaService.validateIdeaAcceptance(waitingBachelorIdea, authorUser1, partnerSet); + Assert.assertTrue("Not true", acceptance.head); //Everything ok, should be able to accept a bachelor with one partner. + if(acceptance.head) + ideaService.acceptIdea(waitingBachelorIdea, authorUser1, partnerSet); + + partnerSet.remove(author2); + partnerSet.add(author3); + + acceptance = ideaService.validateIdeaAcceptance(waitingBachelor2, authorUser1, partnerSet); + Assert.assertFalse("Not false", acceptance.head); //Should not be able to accept more than one idea. + + partnerSet.remove(author3); + partnerSet.add(author2); + + acceptance = ideaService.validateIdeaAcceptance(waitingBachelor2, authorUser3, partnerSet); + Assert.assertFalse("Not false", acceptance.head); //Should not be able to accept when the partner have a taken idea. + } + + @Test + @Transactional + @Rollback + public void testPartnerAcceptIdea() { + SortedSet<Student> studentSet = new TreeSet<Student>(); + studentSet.add(author1); + studentSet.add(author2); + + SupervisorIdea idea = new SupervisorIdea(); + idea.setProjectClass(bachelor); + idea.setTitle("Title"); + idea.setDescription("Description"); + idea.setRequirements("Reqs"); + ideaService.saveSupervisorCreatedIdea(idea, supervisor, studentSet); + + Assert.assertNull(idea.getApplicationPeriod()); + + for(IdeaParticipation ip : author1.getIdeaParticipations()) + Assert.assertFalse(ip.isConfirmed()); + for(IdeaParticipation ip : author2.getIdeaParticipations()) + Assert.assertFalse(ip.isConfirmed()); + + ideaService.partnerAcceptIdea(idea, authorUser1); + + Assert.assertNotNull(idea.getApplicationPeriod()); + } + // HELPER METHODS private SupervisorIdea newIdea(ProjectClass pc, ApplicationPeriod ap, Employee supervisor, IdeaStatus ideaStatus) { SupervisorIdea idea = new SupervisorIdea(); @@ -332,15 +423,6 @@ public class TestSupervisorIdea { return ideaService.save(idea); } - private IModel<SupervisorIdea> newIdeaModel(ProjectClass pc) { - IModel<SupervisorIdea> model = new Model<SupervisorIdea>(new SupervisorIdea()); - model.getObject().setDescription("Model description"); - model.getObject().setRequirements("Model reqs"); - model.getObject().setTitle("Model title"); - model.getObject().setProjectClass(pc); - model.getObject().setKeywords(newKeywordSet()); - return model; - } private Set<Keyword> newKeywordSet() { Set<Keyword> set = new HashSet<Keyword>();