Search in sources :

Example 1 with WorkspaceVisit

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit in project muikku by otavanopisto.

the class WorkspaceVisitDAO method lockingFindByUserEntityAndWorkspaceEntity.

public WorkspaceVisit lockingFindByUserEntityAndWorkspaceEntity(UserEntity userEntity, WorkspaceEntity workspaceEntity) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceVisit> criteria = criteriaBuilder.createQuery(WorkspaceVisit.class);
    Root<WorkspaceVisit> root = criteria.from(WorkspaceVisit.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceVisit_.userEntityId), userEntity.getId()), criteriaBuilder.equal(root.get(WorkspaceVisit_.workspaceEntityId), workspaceEntity.getId())));
    TypedQuery<WorkspaceVisit> query = entityManager.createQuery(criteria);
    query.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    return getSingleResult(query);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceVisit(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit)

Example 2 with WorkspaceVisit

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit in project muikku by otavanopisto.

the class WorkspaceVisitDAO method findByUserEntityAndWorkspaceEntity.

public WorkspaceVisit findByUserEntityAndWorkspaceEntity(UserEntity userEntity, WorkspaceEntity workspaceEntity) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceVisit> criteria = criteriaBuilder.createQuery(WorkspaceVisit.class);
    Root<WorkspaceVisit> root = criteria.from(WorkspaceVisit.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceVisit_.userEntityId), userEntity.getId()), criteriaBuilder.equal(root.get(WorkspaceVisit_.workspaceEntityId), workspaceEntity.getId())));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) WorkspaceVisit(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit)

Example 3 with WorkspaceVisit

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit in project muikku by otavanopisto.

the class WorkspaceVisitController method visit.

public void visit(WorkspaceEntity workspaceEntity) {
    UserEntity userEntity = sessionController.getLoggedUserEntity();
    if (userEntity == null) {
        return;
    } else {
        synchronized (userEntity) {
            WorkspaceVisit workspaceVisit = workspaceVisitDAO.lockingFindByUserEntityAndWorkspaceEntity(userEntity, workspaceEntity);
            if (workspaceVisit == null) {
                workspaceVisit = workspaceVisitDAO.create(userEntity, workspaceEntity, new Date());
            }
            workspaceVisitDAO.updateNumVisitsAndLastVisit(workspaceVisit, workspaceVisit.getNumVisits() + 1, new Date());
        }
    }
}
Also used : WorkspaceVisit(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Date(java.util.Date)

Example 4 with WorkspaceVisit

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit in project muikku by otavanopisto.

the class WorkspaceVisitController method listEnrolledWorkspaceEntitiesByMinVisitsOrderByLastVisit.

public List<WorkspaceEntity> listEnrolledWorkspaceEntitiesByMinVisitsOrderByLastVisit(UserEntity userEntity, Long numVisits) {
    List<WorkspaceEntity> workspaceEntities = workspaceEntityController.listActiveWorkspaceEntitiesByUserEntity(userEntity);
    Map<Long, WorkspaceEntity> workspaceEntityMap = new HashMap<>();
    for (WorkspaceEntity workspaceEntity : workspaceEntities) {
        workspaceEntityMap.put(workspaceEntity.getId(), workspaceEntity);
    }
    List<WorkspaceVisit> workspaceVisits = workspaceVisitDAO.listByWorkspaceEntityIdsAndUserEntityAndMinVisitsOrderByLastVisit(workspaceEntityMap.keySet(), userEntity, numVisits, null, null);
    List<WorkspaceEntity> result = new ArrayList<>(workspaceVisits.size());
    for (WorkspaceVisit workspaceVisit : workspaceVisits) {
        result.add(workspaceEntityMap.get(workspaceVisit.getWorkspaceEntityId()));
    }
    return result;
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) HashMap(java.util.HashMap) WorkspaceVisit(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit) ArrayList(java.util.ArrayList)

Example 5 with WorkspaceVisit

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit in project muikku by otavanopisto.

the class WorkspaceVisitController method listWorkspaceEntitiesByMinVisitsOrderByLastVisit.

public List<WorkspaceEntity> listWorkspaceEntitiesByMinVisitsOrderByLastVisit(UserEntity userEntity, Long numVisits) {
    List<WorkspaceVisit> workspaceVisits = workspaceVisitDAO.listByUserEntityAndMinVisitsOrderByLastVisit(userEntity, numVisits, null, null);
    List<WorkspaceEntity> result = new ArrayList<>(workspaceVisits.size());
    for (WorkspaceVisit workspaceVisit : workspaceVisits) {
        result.add(workspaceEntityController.findWorkspaceEntityById(workspaceVisit.getWorkspaceEntityId()));
    }
    return result;
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceVisit(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit) ArrayList(java.util.ArrayList)

Aggregations

WorkspaceVisit (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceVisit)8 EntityManager (javax.persistence.EntityManager)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)2 ArrayList (java.util.ArrayList)2 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1