added text class for supervisor ideas and some new service methods

This commit is contained in:
Emil Siverhall 2012-07-18 14:07:22 +02:00
parent 2ad1722d5c
commit eaeba71038
4 changed files with 388 additions and 15 deletions
src
main/java/se/su/dsv/scipro/springdata
test/java/se/su/dsv/scipro/springdata

@ -9,6 +9,4 @@ import se.su.dsv.scipro.match.dataobject.SupervisorIdea;
@Transactional(readOnly = true)
public interface SupervisorIdeaRepo extends JpaRepository<SupervisorIdea, Long>, QueryDslPredicateExecutor<SupervisorIdea> {
//nothing here yet
}

@ -1,23 +1,34 @@
package se.su.dsv.scipro.springdata.serviceimpls;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.wicket.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import se.su.dsv.scipro.data.dataobjects.Employee;
import se.su.dsv.scipro.data.dataobjects.Student;
import se.su.dsv.scipro.data.dataobjects.User;
import se.su.dsv.scipro.match.dao.interfaces.ApplicationPeriodDao;
import se.su.dsv.scipro.match.dataobject.Idea.IdeaStatus;
import se.su.dsv.scipro.match.dataobject.Keyword;
import se.su.dsv.scipro.match.dataobject.QSupervisorIdea;
import se.su.dsv.scipro.match.dataobject.SupervisorIdea;
import se.su.dsv.scipro.springdata.repos.SupervisorIdeaRepo;
import se.su.dsv.scipro.springdata.services.StudentService;
import se.su.dsv.scipro.springdata.services.SupervisorIdeaService;
import se.su.dsv.scipro.springdata.services.SupervisorService;
import com.mysema.query.types.expr.BooleanExpression;
@Service ( "supervisorIdeaService" )
@Transactional ( readOnly = true )
@ -26,10 +37,10 @@ public class SupervisorIdeaServiceImpl extends AbstractQueryService<SupervisorId
@Resource
private SupervisorIdeaRepo supervisorIdeaRepo;
@Resource
private SupervisorService supervisorService;
@Resource
private ApplicationPeriodDao periodDao;
private StudentService studentService;
@PersistenceContext
private EntityManager em;
@Autowired
public SupervisorIdeaServiceImpl(
@Qualifier("supervisorIdeaRepo")
@ -40,15 +51,96 @@ public class SupervisorIdeaServiceImpl extends AbstractQueryService<SupervisorId
@Override
@Transactional ( readOnly = false )
public void saveIdeaFromForm(IModel<SupervisorIdea> model, User creator, SortedSet<Student> students) {
public void saveSupervisorCreatedIdea(IModel<SupervisorIdea> model, Employee creator, SortedSet<Student> students) {
SupervisorIdea idea = model.getObject();
Employee supervisor = supervisorService.findByUser(creator);
idea.setCreator(supervisor);
idea.setApplicationPeriod(periodDao.load(2L));
idea.setIdeaStatus(IdeaStatus.WAITING);
if(!students.isEmpty())
idea.setAuthors(students);
supervisorIdeaRepo.save(idea);
idea.setCreator(creator);
idea = em.merge(idea);
}
@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.
return;
else
supervisorIdeaRepo.delete(idea);
}
@Override
@Transactional ( readOnly = false )
public boolean acceptIdea(IModel<SupervisorIdea> model, User creator,
SortedSet<Student> studentSet) {
if(studentSet.size()> 1){
return false;
} else {
SupervisorIdea idea = supervisorIdeaRepo.findOne(model.getObject().getId());
Student author = studentService.findByUser(creator);
idea.addAuthor(studentSet.first());
idea.addAuthor(author);
idea.setIdeaStatus(IdeaStatus.TAKEN);
return true;
}
}
@Override
@Transactional
public boolean hasUnconfirmedIdea(User authorUser) {
Student author = studentService.findByUser(authorUser);
// find ideas by status and user
List<SupervisorIdea> authorConnectedIdeas = findByStatusAndAuthor(IdeaStatus.TAKEN, author);
// check to see if author is confirmed. how?
if(!authorConnectedIdeas.isEmpty())
return true;
else
return false;
}
// 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);
}
@Override
public Page<SupervisorIdea> findByStatus(IdeaStatus status, Pageable pageable) {
return supervisorIdeaRepo.findAll(byStatus(status), pageable);
}
@Override
public List<SupervisorIdea> findByStatusAndAuthor(IdeaStatus status, Student author) {
Iterable<SupervisorIdea> ideas = supervisorIdeaRepo.findAll(byStatus(status).and(byAuthor(author)));
return constructList(ideas);
}
private BooleanExpression byStatus(IdeaStatus status){
return QSupervisorIdea.supervisorIdea.ideaStatus.eq(status);
}
private BooleanExpression byAuthor(Student author){
return QSupervisorIdea.supervisorIdea.authors.contains(author);
}
@Override
public Long countByStatus(IdeaStatus status) {
return supervisorIdeaRepo.count(byStatus(status));
}
private List<SupervisorIdea> constructList(Iterable<SupervisorIdea> ideas) {
List<SupervisorIdea> list = new ArrayList<SupervisorIdea>();
for (SupervisorIdea idea : ideas) {
list.add(idea);
}
return list;
}
}

@ -1,14 +1,31 @@
package se.su.dsv.scipro.springdata.services;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import org.apache.wicket.model.IModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import se.su.dsv.scipro.data.dataobjects.Employee;
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.Keyword;
import se.su.dsv.scipro.match.dataobject.SupervisorIdea;
public interface SupervisorIdeaService extends GenericService<SupervisorIdea, Long>, QueryService<SupervisorIdea, Long> {
void saveIdeaFromForm(IModel<SupervisorIdea> model, User creator, SortedSet<Student> students);
Page<SupervisorIdea> findByStatus(IdeaStatus status, Pageable pageable);
List<SupervisorIdea> findByStatusAndAuthor(IdeaStatus status, Student author);
Long countByStatus(IdeaStatus status);
void saveSupervisorCreatedIdea(IModel<SupervisorIdea> model, Employee creator, SortedSet<Student> students);
void deleteWaitingIdea(IModel<SupervisorIdea> model);
boolean acceptIdea(IModel<SupervisorIdea> model, User creator, SortedSet<Student> sortedSet);
boolean hasUnconfirmedIdea(User authorUser);
boolean isIdeaEditable(IModel<SupervisorIdea> model, User currentUser);
}

@ -0,0 +1,266 @@
package se.su.dsv.scipro.springdata;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import se.su.dsv.scipro.data.dataobjects.Employee;
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.dao.interfaces.KeywordDao;
import se.su.dsv.scipro.match.dao.interfaces.KeywordTypeDao;
import se.su.dsv.scipro.match.dataobject.ApplicationPeriod;
import se.su.dsv.scipro.match.dataobject.Idea.IdeaStatus;
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.springdata.services.ProjectClassService;
import se.su.dsv.scipro.springdata.services.RoleService;
import se.su.dsv.scipro.springdata.services.SupervisorIdeaService;
import se.su.dsv.scipro.springdata.services.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(inheritLocations = false, locations = {
"classpath:test-applicationContext.xml"
})
public class TestSupervisorIdea {
@Autowired
private RoleService roleService;
@Autowired
private UserService userService;
@Autowired
private SupervisorIdeaService ideaService;
@Autowired
private ProjectClassService projectClassService;
@Autowired
private ApplicationPeriodFacade periodFacade;
@Autowired
private KeywordTypeDao keywordTypeDao;
@Autowired
private KeywordDao keywordDao;
private ProjectClass bachelor, master;
private User authorUser1, authorUser2, supervisorUser, supervisorUser2, unconfirmedUser;
private Student author1, author2, unconfirmedAuthor;
private Employee supervisor, supervisor2;
private SupervisorIdea waitingBachelorIdea, waitingMasterIdea, takenBachelorIdea, completedMasterIdea;
private ApplicationPeriod bachelorPeriod, masterPeriod;
private Keyword keyword1, keyword2;
@Before
public void startTransaction() throws Exception {
bachelor = new ProjectClass(ProjectClass.BACHELOR, "Bachelor", "Bachelor degree thesis project");
bachelor = projectClassService.save(bachelor);
master = new ProjectClass(ProjectClass.MASTER, "Master", "Master degree thesis project");
master = projectClassService.save(master);
Set<ProjectClass> bachelorSet = new HashSet<ProjectClass>();
bachelorSet.add(bachelor);
Set<ProjectClass> masterSet = new HashSet<ProjectClass>();
masterSet.add(master);
bachelorPeriod = periodFacade.createApplicationPeriod(new HashSet<ProjectClass>(bachelorSet), "Bachelor period");
masterPeriod = periodFacade.createApplicationPeriod(masterSet, "Master period");
supervisorUser = newUser();
supervisorUser2 = newUser();
authorUser1 = newUser();
authorUser2 = newUser();
unconfirmedUser = newUser();
supervisor = newEmployee(supervisorUser);
supervisor2 = newEmployee(supervisorUser2);
author1 = newStudent(authorUser1);
author2 = newStudent(authorUser2);
unconfirmedAuthor = newStudent(unconfirmedUser);
KeywordType type = new KeywordType("test type");
type = keywordTypeDao.save(type);
keyword1 = new Keyword("keyword 1", type);
keyword2 = new Keyword("keyword 2", type);
keyword1 = keywordDao.save(keyword1);
keyword2 = keywordDao.save(keyword2);
waitingBachelorIdea = newIdea(bachelor, bachelorPeriod, supervisor, IdeaStatus.WAITING);
waitingMasterIdea = newIdea(master, masterPeriod, supervisor, IdeaStatus.WAITING);
takenBachelorIdea = newIdea(bachelor, bachelorPeriod, supervisor, IdeaStatus.TAKEN);
completedMasterIdea = newIdea(master, masterPeriod, supervisor, IdeaStatus.COMPLETED);
}
@Test
@Transactional
@Rollback
public void testFindIdeasByStatus() {
Page<SupervisorIdea> waitingIdeaPage = ideaService.findByStatus(IdeaStatus.WAITING, new PageRequest(0, 10));
List<SupervisorIdea> waitingIdeasList = waitingIdeaPage.getContent();
Assert.assertEquals(Arrays.asList(new SupervisorIdea[]{waitingBachelorIdea, waitingMasterIdea}), waitingIdeasList);
Page<SupervisorIdea> takenIdeaPage = ideaService.findByStatus(IdeaStatus.TAKEN, new PageRequest(0, 10));
List<SupervisorIdea> takenIdeasList = takenIdeaPage.getContent();
Assert.assertEquals(Arrays.asList(new SupervisorIdea[]{takenBachelorIdea}), takenIdeasList);
Page<SupervisorIdea> completedIdeaPage = ideaService.findByStatus(IdeaStatus.COMPLETED, new PageRequest(0, 10));
List<SupervisorIdea> completedIdeasList = completedIdeaPage.getContent();
Assert.assertEquals(Arrays.asList(new SupervisorIdea[]{completedMasterIdea}), completedIdeasList);
}
@Test
@Transactional
@Rollback
public void testFindIdeasByStatusAndAuthor() {
List<SupervisorIdea> ideas = ideaService.findByStatusAndAuthor(IdeaStatus.COMPLETED, author1);
Assert.assertTrue("List is not empty", ideas.isEmpty());
ideas = ideaService.findByStatusAndAuthor(IdeaStatus.WAITING, author1);
Assert.assertTrue("List is not empty", ideas.isEmpty());
waitingBachelorIdea.addAuthor(author1);
ideas = ideaService.findByStatusAndAuthor(IdeaStatus.WAITING, author1);
Assert.assertTrue("Number of ideas is not 1", ideas.size()==1);
ideas = ideaService.findByStatusAndAuthor(IdeaStatus.COMPLETED, author1);
Assert.assertTrue("List is not empty", ideas.isEmpty());
}
@Test
@Transactional
@Rollback
public void testSaveSupervisorCreatedIdea() {
Assert.assertEquals(new Long(2), ideaService.countByStatus(IdeaStatus.WAITING));
IModel<SupervisorIdea> model = newIdeaModel(bachelor);
ideaService.saveSupervisorCreatedIdea(model, supervisor, new TreeSet<Student>());
Assert.assertEquals(new Long(3), ideaService.countByStatus(IdeaStatus.WAITING));
}
@Test
@Transactional
@Rollback
public void testIfIdeaIsEditable() {
boolean shouldBeTrue = ideaService.isIdeaEditable(Model.of(waitingBachelorIdea), supervisorUser);
Assert.assertTrue("Not true", shouldBeTrue);
boolean shouldBeFalse = ideaService.isIdeaEditable(Model.of(takenBachelorIdea), supervisorUser);
Assert.assertFalse("Not false", shouldBeFalse);
waitingBachelorIdea.setCreator(supervisor2);
boolean shouldAlsoBeFalse = ideaService.isIdeaEditable(Model.of(waitingBachelorIdea), supervisorUser);
Assert.assertFalse("Not false", shouldAlsoBeFalse);
}
@Test
@Transactional
@Rollback
public void testDeleteWaitingIdea() {
Iterable<SupervisorIdea> ideas = ideaService.findAll();
List<SupervisorIdea> ideaList = constructList(ideas);
Assert.assertEquals(4, ideaList.size());
ideaService.deleteWaitingIdea(Model.of(takenBachelorIdea)); //Should not be deleted.
ideas = ideaService.findAll();
ideaList = constructList(ideas);
Assert.assertEquals(4, ideaList.size());
ideaService.deleteWaitingIdea(Model.of(waitingBachelorIdea)); //Should be deleted.
ideas = ideaService.findAll();
ideaList = constructList(ideas);
Assert.assertEquals(3, ideaList.size());
}
/**
* Test that should check if an author has not confirmed his/her participation on a project idea.
*/
@Test
@Transactional
@Rollback
public void testIfAuthorHaveUnconfirmedParticipationOnIdea() {
boolean shouldBeFalse = ideaService.hasUnconfirmedIdea(authorUser1);
Assert.assertFalse("Not false",shouldBeFalse);
//Do something
takenBachelorIdea.addAuthor(author2);
boolean shouldBeTrue = ideaService.hasUnconfirmedIdea(authorUser2);
Assert.assertTrue("Not true",shouldBeTrue);
}
// HELPER METHODS
private SupervisorIdea newIdea(ProjectClass pc, ApplicationPeriod ap, Employee supervisor, IdeaStatus ideaStatus) {
SupervisorIdea idea = new SupervisorIdea();
idea.setProjectClass(pc);
idea.setApplicationPeriod(ap);
idea.setTitle("Title");
idea.setDescription("Description");
idea.setRequirements("Requisites");
idea.setCreator(supervisor);
idea.setIdeaStatus(ideaStatus);
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>();
set.add(keyword1);
set.add(keyword2);
return set;
}
private Student newStudent(final User user) {
Student student = new Student();
student.setUser(user);
return (Student) roleService.save(student);
}
private User newUser() {
User user = new User();
return userService.save(user);
}
private Employee newEmployee(final User user) {
Employee emp = new Employee();
emp.setUser(user);
return (Employee) roleService.save(emp);
}
private List<SupervisorIdea> constructList(Iterable<SupervisorIdea> ideas) {
List<SupervisorIdea> list = new ArrayList<SupervisorIdea>();
for (SupervisorIdea idea : ideas) {
list.add(idea);
}
return list;
}
}