algorithm now uses same method for counting as GUI
This commit is contained in:
parent
5e763d8860
commit
7e56b81482
src/main/java/se/su/dsv/scipro/match/dao
@ -25,6 +25,8 @@ public interface SupervisorDao extends QueryableDao<Employee, SupervisorDaoParam
|
||||
Employee getOrCreate(User user);
|
||||
|
||||
List<Availability> getAvailability();
|
||||
|
||||
// List<Availability> getAvailability2();
|
||||
|
||||
Availability getAvailability(Employee supervisor, ProjectClass projectClass);
|
||||
|
||||
|
@ -36,208 +36,208 @@ import se.su.dsv.scipro.match.dataobject.Match;
|
||||
|
||||
@Repository("supervisorDao")
|
||||
public class SupervisorDaoJPAImp extends
|
||||
QueryableDaoJPAImp<Employee, SupervisorDaoParams> implements
|
||||
SupervisorDao {
|
||||
|
||||
@Autowired
|
||||
private ProjectIdeaDao projectIdeaDao;
|
||||
@Autowired
|
||||
private ProjectClassDao projectClassDao;
|
||||
|
||||
public SupervisorDaoJPAImp() {
|
||||
super(Employee.class);
|
||||
}
|
||||
QueryableDaoJPAImp<Employee, SupervisorDaoParams> implements
|
||||
SupervisorDao {
|
||||
|
||||
@Override
|
||||
public Employee getOrCreate(final User user) {
|
||||
Employee supervisor = getFrom(user);
|
||||
if (supervisor == null) {
|
||||
supervisor = new Employee();
|
||||
supervisor.setUser(user);
|
||||
supervisor = save(supervisor);
|
||||
}
|
||||
return supervisor;
|
||||
}
|
||||
@Autowired
|
||||
private ProjectIdeaDao projectIdeaDao;
|
||||
@Autowired
|
||||
private ProjectClassDao projectClassDao;
|
||||
|
||||
@Override
|
||||
public Employee getFrom(final User user) {
|
||||
return getJpaTemplate().execute(new JpaCallback<Employee>() {
|
||||
public SupervisorDaoJPAImp() {
|
||||
super(Employee.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee doInJpa(EntityManager em)
|
||||
throws PersistenceException {
|
||||
TypedQuery<Employee> query =
|
||||
em.createQuery(
|
||||
"select e FROM Employee e WHERE e.user = :user",
|
||||
Employee.class);
|
||||
query.setParameter("user", user);
|
||||
query.setHint(QueryHints.HINT_CACHEABLE, "true");
|
||||
try {
|
||||
return query.getSingleResult();
|
||||
} catch (NoResultException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
@Override
|
||||
public Employee getOrCreate(final User user) {
|
||||
Employee supervisor = getFrom(user);
|
||||
if (supervisor == null) {
|
||||
supervisor = new Employee();
|
||||
supervisor.setUser(user);
|
||||
supervisor = save(supervisor);
|
||||
}
|
||||
return supervisor;
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public Employee getFrom(final User user) {
|
||||
return getJpaTemplate().execute(new JpaCallback<Employee>() {
|
||||
|
||||
@Override
|
||||
public List<Employee> getCapableSupervisors(
|
||||
final ProjectClass projectClass) {
|
||||
return getJpaTemplate().execute(
|
||||
new QuerySet().projectClass(projectClass).fetchCallback());
|
||||
}
|
||||
@Override
|
||||
public Employee doInJpa(EntityManager em)
|
||||
throws PersistenceException {
|
||||
TypedQuery<Employee> query =
|
||||
em.createQuery(
|
||||
"select e FROM Employee e WHERE e.user = :user",
|
||||
Employee.class);
|
||||
query.setParameter("user", user);
|
||||
query.setHint(QueryHints.HINT_CACHEABLE, "true");
|
||||
try {
|
||||
return query.getSingleResult();
|
||||
} catch (NoResultException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public List<Employee> getCapableSupervisors(
|
||||
final ProjectClass projectClass, final Set<Language> languages) {
|
||||
return getJpaTemplate().execute(
|
||||
new QuerySet().projectClass(projectClass).languages(languages)
|
||||
.fetchCallback());
|
||||
}
|
||||
}
|
||||
|
||||
public List<Employee> getAutoCompleteCapableSupervisors(
|
||||
final ProjectClass projectClass, final Set<Language> languages,
|
||||
final String searchString, final int limit) {
|
||||
return getJpaTemplate().execute(
|
||||
new QuerySet().projectClass(projectClass).languages(languages)
|
||||
.nameLike(searchString).orderByUser().limit(limit)
|
||||
.fetchCallback());
|
||||
}
|
||||
@Override
|
||||
public List<Employee> getCapableSupervisors(
|
||||
final ProjectClass projectClass) {
|
||||
return getJpaTemplate().execute(
|
||||
new QuerySet().projectClass(projectClass).fetchCallback());
|
||||
}
|
||||
|
||||
protected AbstractQuerySet<Employee> createQuerySet(
|
||||
SupervisorDaoParams params) {
|
||||
return new QuerySet().languages(params.getLanguages())
|
||||
.projectClass(params.getProjectClass())
|
||||
.keywords(params.getKeywords())
|
||||
.nameLike(params.getNameLike());
|
||||
}
|
||||
public List<Employee> getCapableSupervisors(
|
||||
final ProjectClass projectClass, final Set<Language> languages) {
|
||||
return getJpaTemplate().execute(
|
||||
new QuerySet().projectClass(projectClass).languages(languages)
|
||||
.fetchCallback());
|
||||
}
|
||||
|
||||
private static class QuerySet extends AbstractSortableQuerySet<Employee> {
|
||||
public QuerySet() {
|
||||
super(Employee.class);
|
||||
}
|
||||
public List<Employee> getAutoCompleteCapableSupervisors(
|
||||
final ProjectClass projectClass, final Set<Language> languages,
|
||||
final String searchString, final int limit) {
|
||||
return getJpaTemplate().execute(
|
||||
new QuerySet().projectClass(projectClass).languages(languages)
|
||||
.nameLike(searchString).orderByUser().limit(limit)
|
||||
.fetchCallback());
|
||||
}
|
||||
|
||||
public QuerySet keywords(Collection<Keyword> keywords) {
|
||||
if (keywords != null) {
|
||||
getQuery().combine(
|
||||
new Query().join("_.keywords.keywords k").where("k in (:keywords)").parameter(
|
||||
"keywords", keywords)).distinct();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
protected AbstractQuerySet<Employee> createQuerySet(
|
||||
SupervisorDaoParams params) {
|
||||
return new QuerySet().languages(params.getLanguages())
|
||||
.projectClass(params.getProjectClass())
|
||||
.keywords(params.getKeywords())
|
||||
.nameLike(params.getNameLike());
|
||||
}
|
||||
|
||||
public QuerySet orderByUser() {
|
||||
getQuery().combine(RoleDaoJPAImp.orderByUserQuery());
|
||||
return this;
|
||||
}
|
||||
private static class QuerySet extends AbstractSortableQuerySet<Employee> {
|
||||
public QuerySet() {
|
||||
super(Employee.class);
|
||||
}
|
||||
|
||||
public QuerySet nameLike(String name) {
|
||||
if (name != null) {
|
||||
getQuery().combine(
|
||||
new Query().combine(RoleDaoJPAImp
|
||||
.autoCompleteUserNameQuery(name)));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public QuerySet keywords(Collection<Keyword> keywords) {
|
||||
if (keywords != null) {
|
||||
getQuery().combine(
|
||||
new Query().join("_.keywords.keywords k").where("k in (:keywords)").parameter(
|
||||
"keywords", keywords)).distinct();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public QuerySet projectClass(ProjectClass projectClass) {
|
||||
if (projectClass != null) {
|
||||
getQuery().combine(
|
||||
new Query().join("_.capabilities.projectLimits pl").where(
|
||||
"index(pl) = :projectClass AND pl > 0").parameter(
|
||||
"projectClass", projectClass));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public QuerySet orderByUser() {
|
||||
getQuery().combine(RoleDaoJPAImp.orderByUserQuery());
|
||||
return this;
|
||||
}
|
||||
|
||||
public QuerySet languages(Collection<Language> languages) {
|
||||
if (languages != null) {
|
||||
getQuery().combine(
|
||||
new Query().join("_.capabilities.languages l").where(
|
||||
"l IN (:languages)").parameter("languages", languages)).distinct();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public QuerySet nameLike(String name) {
|
||||
if (name != null) {
|
||||
getQuery().combine(
|
||||
new Query().combine(RoleDaoJPAImp
|
||||
.autoCompleteUserNameQuery(name)));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query initQuery() {
|
||||
return new Query().select("e").from("Employee e");
|
||||
}
|
||||
public QuerySet projectClass(ProjectClass projectClass) {
|
||||
if (projectClass != null) {
|
||||
getQuery().combine(
|
||||
new Query().join("_.capabilities.projectLimits pl").where(
|
||||
"index(pl) = :projectClass AND pl > 0").parameter(
|
||||
"projectClass", projectClass));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
public QuerySet languages(Collection<Language> languages) {
|
||||
if (languages != null) {
|
||||
getQuery().combine(
|
||||
new Query().join("_.capabilities.languages l").where(
|
||||
"l IN (:languages)").parameter("languages", languages)).distinct();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Availability> getAvailability() {
|
||||
return getJpaTemplate().execute(new JpaCallback<List<Availability>>() {
|
||||
@Override
|
||||
public Query initQuery() {
|
||||
return new Query().select("e").from("Employee e");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Availability> doInJpa(EntityManager em)
|
||||
throws PersistenceException {
|
||||
List<Availability> availabilities = matchedProjectIdeas(em);
|
||||
availabilities.addAll(unmatchedProjectIdeas(em));
|
||||
return availabilities;
|
||||
}
|
||||
}
|
||||
|
||||
private List<Availability> matchedProjectIdeas(EntityManager em) {
|
||||
javax.persistence.Query query =
|
||||
em.createQuery("SELECT NEW se.su.dsv.scipro.match.dataobject.Availability"
|
||||
+ "(s, COUNT(m), pl, pi.projectClass) "
|
||||
+ "FROM Match m JOIN m.supervisor s JOIN m.projectIdea pi JOIN s.capabilities.projectLimits pl "
|
||||
+ "WHERE pi.match = m AND pi.projectClass = index(pl) AND m.status IN (:statuses) "
|
||||
+ "GROUP BY s, pi.projectClass, pl, index(pl)");
|
||||
// @Override
|
||||
// public List<Availability> getAvailability() {
|
||||
// return getJpaTemplate().execute(new JpaCallback<List<Availability>>() {
|
||||
//
|
||||
// @Override
|
||||
// public List<Availability> doInJpa(EntityManager em)
|
||||
// throws PersistenceException {
|
||||
// List<Availability> availabilities = matchedProjectIdeas(em);
|
||||
// availabilities.addAll(unmatchedProjectIdeas(em));
|
||||
// return availabilities;
|
||||
// }
|
||||
//
|
||||
// private List<Availability> matchedProjectIdeas(EntityManager em) {
|
||||
// javax.persistence.Query query =
|
||||
// em.createQuery("SELECT NEW se.su.dsv.scipro.match.dataobject.Availability"
|
||||
// + "(s, COUNT(m), pl, pi.projectClass) "
|
||||
// + "FROM Match m JOIN m.supervisor s JOIN m.projectIdea pi JOIN s.capabilities.projectLimits pl "
|
||||
// + "WHERE pi.match = m AND pi.projectClass = index(pl) AND m.status IN (:statuses) "
|
||||
// + "GROUP BY s, pi.projectClass, pl, index(pl)");
|
||||
//
|
||||
// query.setParameter("statuses", occupiedStatuses());
|
||||
// query.setHint(QueryHints.HINT_CACHEABLE, "false");
|
||||
// try {
|
||||
// return query.getResultList();
|
||||
// } catch (NoResultException e) {
|
||||
// return new ArrayList<Availability>();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private List<Match.Status> occupiedStatuses() {
|
||||
// List<Match.Status> statuses = new ArrayList<Match.Status>();
|
||||
// statuses.add(Match.Status.CONFIRMED);
|
||||
// statuses.add(Match.Status.PENDING);
|
||||
// statuses.add(Match.Status.PUBLISHED);
|
||||
// return statuses;
|
||||
// }
|
||||
//
|
||||
// private List<Availability> unmatchedProjectIdeas(EntityManager em) {
|
||||
// javax.persistence.Query query =
|
||||
// em.createQuery("SELECT NEW se.su.dsv.scipro.match.dataobject.Availability"
|
||||
// + "(s, 0L, pl, index(pl)) "
|
||||
// + "FROM Match m RIGHT JOIN m.supervisor s JOIN s.capabilities.projectLimits pl LEFT JOIN m.projectIdea pi "
|
||||
// + "WHERE m = NULL OR pi.projectClass != index(pl) OR (pi.projectClass = index(pl) AND m.status NOT IN (:statuses))");
|
||||
//
|
||||
// query.setParameter("statuses", occupiedStatuses());
|
||||
// query.setHint(QueryHints.HINT_CACHEABLE, "false");
|
||||
// try {
|
||||
// return query.getResultList();
|
||||
// } catch (NoResultException e) {
|
||||
// return new ArrayList<Availability>();
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// }
|
||||
|
||||
query.setParameter("statuses", occupiedStatuses());
|
||||
query.setHint(QueryHints.HINT_CACHEABLE, "false");
|
||||
try {
|
||||
return query.getResultList();
|
||||
} catch (NoResultException e) {
|
||||
return new ArrayList<Availability>();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Availability getAvailability(Employee supervisor, ProjectClass projectClass) {
|
||||
Integer projectLimit = supervisor.getCapabilities().getProjectLimit(projectClass);
|
||||
long activeProjectIdeas = projectIdeaDao.countActiveProjectIdeas(supervisor, projectClass);
|
||||
return new Availability(supervisor, activeProjectIdeas, projectLimit, projectClass);
|
||||
}
|
||||
|
||||
private List<Match.Status> occupiedStatuses() {
|
||||
List<Match.Status> statuses = new ArrayList<Match.Status>();
|
||||
statuses.add(Match.Status.CONFIRMED);
|
||||
statuses.add(Match.Status.PENDING);
|
||||
statuses.add(Match.Status.PUBLISHED);
|
||||
return statuses;
|
||||
}
|
||||
|
||||
private List<Availability> unmatchedProjectIdeas(EntityManager em) {
|
||||
javax.persistence.Query query =
|
||||
em.createQuery("SELECT NEW se.su.dsv.scipro.match.dataobject.Availability"
|
||||
+ "(s, 0L, pl, index(pl)) "
|
||||
+ "FROM Match m RIGHT JOIN m.supervisor s JOIN s.capabilities.projectLimits pl LEFT JOIN m.projectIdea pi "
|
||||
+ "WHERE m = NULL OR pi.projectClass != index(pl) OR (pi.projectClass = index(pl) AND m.status NOT IN (:statuses))");
|
||||
|
||||
query.setParameter("statuses", occupiedStatuses());
|
||||
query.setHint(QueryHints.HINT_CACHEABLE, "false");
|
||||
try {
|
||||
return query.getResultList();
|
||||
} catch (NoResultException e) {
|
||||
return new ArrayList<Availability>();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Availability getAvailability(Employee supervisor, ProjectClass projectClass) {
|
||||
Integer projectLimit = supervisor.getCapabilities().getProjectLimit(projectClass);
|
||||
long activeProjectIdeas = projectIdeaDao.countActiveProjectIdeas(supervisor, projectClass);
|
||||
return new Availability(supervisor, activeProjectIdeas, projectLimit, projectClass);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly=false)
|
||||
public void setAvailability(Availability availability) {
|
||||
availability.getSupervisor().getCapabilities().getProjectLimits().put(
|
||||
availability.getProjectClass(), availability.getNumCapable());
|
||||
save(availability.getSupervisor());
|
||||
}
|
||||
@Override
|
||||
@Transactional(readOnly=false)
|
||||
public void setAvailability(Availability availability) {
|
||||
availability.getSupervisor().getCapabilities().getProjectLimits().put(
|
||||
availability.getProjectClass(), availability.getNumCapable());
|
||||
save(availability.getSupervisor());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly=false)
|
||||
@ -247,4 +247,16 @@ public class SupervisorDaoJPAImp extends
|
||||
e.setCountFromDate(projectClass, new Date());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Availability> getAvailability() {
|
||||
List<Availability> list = new ArrayList<Availability>();
|
||||
for (ProjectClass pc : projectClassDao.findAll()){
|
||||
for (Employee e : getCapableSupervisors(pc)){
|
||||
list.add(getAvailability(e, pc));
|
||||
System.out.println(e.getNameAsString() + " numcap " + getAvailability(e, pc).getNumCapable() + " nummatched" + getAvailability(e, pc).getNumMatched());
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user