Search in sources :

Example 1 with WorkspaceJournalEntry

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

the class WorkspaceRESTService method listJournalEntries.

@GET
@Path("/workspaces/{WORKSPACEID}/journal")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listJournalEntries(@PathParam("WORKSPACEID") Long workspaceEntityId, @QueryParam("userEntityId") Long userEntityId, @QueryParam("workspaceStudentId") String workspaceStudentId, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("25") Integer maxResults) {
    List<WorkspaceJournalEntry> entries = new ArrayList<>();
    List<WorkspaceJournalEntryRESTModel> result = new ArrayList<>();
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    UserEntity userEntity = sessionController.getLoggedUserEntity();
    boolean canListAllEntries = sessionController.hasWorkspacePermission(MuikkuPermissions.LIST_ALL_JOURNAL_ENTRIES, workspaceEntity);
    if (workspaceStudentId == null && userEntityId == null && canListAllEntries) {
        List<WorkspaceUserEntity> workspaceUserEntities = workspaceUserEntityController.listActiveWorkspaceStudents(workspaceEntity);
        Set<UserEntity> userEntities = new HashSet<>();
        for (WorkspaceUserEntity workspaceUserEntity : workspaceUserEntities) {
            userEntities.add(workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity());
        }
        entries = workspaceJournalController.listEntriesForStudents(workspaceEntity, userEntities, firstResult, maxResults);
    } else {
        if (userEntityId != null) {
            // List by user entity (Muikku)
            if (!userEntityId.equals(userEntity.getId())) {
                if (canListAllEntries) {
                    userEntity = userEntityController.findUserEntityById(userEntityId);
                    if (userEntity == null) {
                        return Response.status(Status.NOT_FOUND).build();
                    }
                } else {
                    return Response.status(Status.FORBIDDEN).build();
                }
            }
        } else if (workspaceStudentId != null) {
            // List by workspace student (school data)
            SchoolDataIdentifier workspaceUserIdentifier = SchoolDataIdentifier.fromId(workspaceStudentId);
            if (workspaceUserIdentifier == null) {
                return Response.status(Status.BAD_REQUEST).entity("Invalid workspaceStudentId").build();
            }
            WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifierIncludeArchived(workspaceUserIdentifier);
            if (workspaceUserEntity == null) {
                return Response.status(Status.NOT_FOUND).build();
            }
            UserEntity userEntityFromWorkspaceUser = workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity();
            if (userEntityFromWorkspaceUser == null) {
                return Response.status(Status.NOT_FOUND).build();
            }
            if (!canListAllEntries) {
                if (!userEntity.getId().equals(userEntityFromWorkspaceUser.getId())) {
                    return Response.status(Status.FORBIDDEN).build();
                }
            } else {
                userEntity = userEntityFromWorkspaceUser;
            }
        }
        entries = workspaceJournalController.listEntriesByWorkspaceEntityAndUserEntity(workspaceEntity, userEntity, firstResult, maxResults);
    }
    for (WorkspaceJournalEntry entry : entries) {
        UserEntity entryUserEntity = userEntityController.findUserEntityById(entry.getUserEntityId());
        if (entryUserEntity != null) {
            User user = userController.findUserByUserEntityDefaults(entryUserEntity);
            if (user != null) {
                result.add(new WorkspaceJournalEntryRESTModel(entry.getId(), entry.getWorkspaceEntityId(), entry.getUserEntityId(), user.getFirstName(), user.getLastName(), entry.getHtml(), entry.getTitle(), entry.getCreated()));
            }
        }
    }
    return Response.ok(result).build();
}
Also used : WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) ArrayList(java.util.ArrayList) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceJournalEntryRESTModel(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceJournalEntryRESTModel) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 2 with WorkspaceJournalEntry

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

the class WorkspaceJournalEntryDAO method listByWorkspaceEntityAndUserEntities.

public List<WorkspaceJournalEntry> listByWorkspaceEntityAndUserEntities(WorkspaceEntity workspaceEntity, Collection<UserEntity> userEntities, int firstResult, int maxResults) {
    EntityManager entityManager = getEntityManager();
    Set<Long> userEntityIds = userEntities.stream().map(userEntity -> userEntity.getId()).collect(Collectors.toSet());
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceJournalEntry> criteria = criteriaBuilder.createQuery(WorkspaceJournalEntry.class);
    Root<WorkspaceJournalEntry> root = criteria.from(WorkspaceJournalEntry.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.workspaceEntityId), workspaceEntity.getId()), root.get(WorkspaceJournalEntry_.userEntityId).in(userEntityIds), criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.archived), Boolean.FALSE)));
    criteria.orderBy(criteriaBuilder.desc(root.get(WorkspaceJournalEntry_.created)));
    TypedQuery<WorkspaceJournalEntry> query = entityManager.createQuery(criteria);
    query.setFirstResult(firstResult);
    query.setMaxResults(maxResults);
    return query.getResultList();
}
Also used : CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Date(java.util.Date) WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) Collection(java.util.Collection) Set(java.util.Set) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) TypedQuery(javax.persistence.TypedQuery) List(java.util.List) WorkspaceJournalEntry_(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry_) CorePluginsDAO(fi.otavanopisto.muikku.plugins.CorePluginsDAO) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Root(javax.persistence.criteria.Root) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) EntityManager(javax.persistence.EntityManager)

Example 3 with WorkspaceJournalEntry

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

the class WorkspaceJournalEntryDAO method create.

public WorkspaceJournalEntry create(WorkspaceEntity workspaceEntity, UserEntity userEntity, String html, String title, Date created, Boolean archived) {
    WorkspaceJournalEntry journalEntry = new WorkspaceJournalEntry();
    journalEntry.setUserEntityId(userEntity.getId());
    journalEntry.setWorkspaceEntityId(workspaceEntity.getId());
    journalEntry.setHtml(html);
    journalEntry.setTitle(title);
    journalEntry.setCreated(created);
    journalEntry.setArchived(archived);
    return persist(journalEntry);
}
Also used : WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry)

Example 4 with WorkspaceJournalEntry

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

the class WorkspaceJournalEntryDAO method countByWorkspaceEntity.

public long countByWorkspaceEntity(WorkspaceEntity workspaceEntity) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class);
    Root<WorkspaceJournalEntry> root = criteria.from(WorkspaceJournalEntry.class);
    criteria.select(criteriaBuilder.count(root));
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.workspaceEntityId), workspaceEntity.getId()), criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.archived), Boolean.FALSE)));
    return entityManager.createQuery(criteria).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) EntityManager(javax.persistence.EntityManager)

Example 5 with WorkspaceJournalEntry

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

the class WorkspaceJournalEntryDAO method countByWorkspaceEntityIdAndUserEntityId.

public long countByWorkspaceEntityIdAndUserEntityId(Long workspaceEntityId, Long userEntityId) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = criteriaBuilder.createQuery(Long.class);
    Root<WorkspaceJournalEntry> root = criteria.from(WorkspaceJournalEntry.class);
    criteria.select(criteriaBuilder.count(root));
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.workspaceEntityId), workspaceEntityId), criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.userEntityId), userEntityId), criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.archived), Boolean.FALSE)));
    return entityManager.createQuery(criteria).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) EntityManager(javax.persistence.EntityManager)

Aggregations

WorkspaceJournalEntry (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry)11 EntityManager (javax.persistence.EntityManager)6 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)3 Path (javax.ws.rs.Path)3 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)1 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 CorePluginsDAO (fi.otavanopisto.muikku.plugins.CorePluginsDAO)1 WorkspaceJournalEntry_ (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry_)1 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)1 WorkspaceMaterialReply (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply)1 WorkspaceJournalEntryRESTModel (fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceJournalEntryRESTModel)1 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)1 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)1 User (fi.otavanopisto.muikku.schooldata.entity.User)1 WorkspaceUser (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1