Search in sources :

Example 91 with RESTPermit

use of fi.otavanopisto.security.rest.RESTPermit in project muikku by otavanopisto.

the class WorkspaceRESTService method updateWorkspaceDetails.

@PUT
@Path("/workspaces/{ID}/details")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response updateWorkspaceDetails(@PathParam("ID") Long workspaceEntityId, WorkspaceDetails payload) {
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    Workspace workspace = workspaceController.findWorkspace(workspaceEntity);
    if (workspace == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_DETAILS, workspaceEntity)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    if ((payload.getExternalViewUrl() != null) && (!StringUtils.equals(workspace.getViewLink(), payload.getExternalViewUrl()))) {
        return Response.status(Status.BAD_REQUEST).entity("externalViewUrl is read-only property").build();
    }
    SchoolDataIdentifier typeIdentifier = null;
    if (payload.getTypeId() != null) {
        typeIdentifier = SchoolDataIdentifier.fromId(payload.getTypeId());
        if (typeIdentifier == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Invlid typeId %s", payload.getTypeId())).build();
        }
    }
    if (!isEqualDateTime(workspace.getBeginDate(), payload.getBeginDate()) || !isEqualDateTime(workspace.getEndDate(), payload.getEndDate()) || !Objects.equals(typeIdentifier, workspace.getWorkspaceTypeId())) {
        workspace.setBeginDate(payload.getBeginDate());
        workspace.setEndDate(payload.getEndDate());
        workspace.setWorkspaceTypeId(typeIdentifier);
        workspaceController.updateWorkspace(workspace);
    }
    String typeId = workspace.getWorkspaceTypeId() != null ? workspace.getWorkspaceTypeId().toId() : null;
    return Response.ok(new WorkspaceDetails(typeId, workspace.getBeginDate(), workspace.getEndDate(), workspace.getViewLink())).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceDetails(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceDetails) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) PUT(javax.ws.rs.PUT)

Example 92 with RESTPermit

use of fi.otavanopisto.security.rest.RESTPermit in project muikku by otavanopisto.

the class WorkspaceRESTService method listWorkspaces.

@GET
@Path("/workspaces/")
@RESTPermit(handling = Handling.INLINE)
public Response listWorkspaces(@QueryParam("userId") Long userEntityId, @QueryParam("userIdentifier") String userId, @QueryParam("includeInactiveWorkspaces") @DefaultValue("false") Boolean includeInactiveWorkspaces, @QueryParam("search") String searchString, @QueryParam("subjects") List<String> subjects, @QueryParam("educationTypes") List<String> educationTypeIds, @QueryParam("curriculums") List<String> curriculumIds, @QueryParam("minVisits") Long minVisits, @QueryParam("includeUnpublished") @DefaultValue("false") Boolean includeUnpublished, @QueryParam("orderBy") List<String> orderBy, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("50") Integer maxResults, @Context Request request) {
    List<fi.otavanopisto.muikku.plugins.workspace.rest.model.Workspace> workspaces = new ArrayList<>();
    boolean doMinVisitFilter = minVisits != null;
    UserEntity userEntity = userEntityId != null ? userEntityController.findUserEntityById(userEntityId) : null;
    List<WorkspaceEntity> workspaceEntities = null;
    String schoolDataSourceFilter = null;
    List<String> workspaceIdentifierFilters = null;
    SchoolDataIdentifier userIdentifier = SchoolDataIdentifier.fromId(userId);
    if (userIdentifier != null) {
        if (doMinVisitFilter && userEntity == null) {
            userEntity = userEntityController.findUserEntityByUserIdentifier(userIdentifier);
        }
    }
    if (includeInactiveWorkspaces && userIdentifier == null) {
        return Response.status(Status.BAD_REQUEST).entity("includeInactiveWorkspaces works only with userIdentifier parameter").build();
    }
    if (includeInactiveWorkspaces && doMinVisitFilter) {
        return Response.status(Status.BAD_REQUEST).entity("includeInactiveWorkspaces cannot be used with doMinVisitFilter").build();
    }
    if (doMinVisitFilter) {
        if (!sessionController.isLoggedIn()) {
            return Response.status(Status.UNAUTHORIZED).entity("You need to be logged in to filter by visit count").build();
        }
        UserEntity loggedUserEntity = sessionController.getLoggedUserEntity();
        workspaceEntities = workspaceVisitController.listEnrolledWorkspaceEntitiesByMinVisitsOrderByLastVisit(loggedUserEntity, minVisits);
    } else {
        if (userIdentifier != null) {
            if (includeInactiveWorkspaces) {
                workspaceEntities = workspaceUserEntityController.listWorkspaceEntitiesByUserIdentifier(userIdentifier);
            } else {
                workspaceEntities = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserIdentifier(userIdentifier);
            }
        } else if (userEntity != null) {
            workspaceEntities = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserEntity(userEntity);
        } else {
            if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_ALL_WORKSPACES)) {
                return Response.status(Status.FORBIDDEN).build();
            }
            workspaceEntities = Boolean.TRUE.equals(includeUnpublished) ? workspaceController.listWorkspaceEntities() : workspaceController.listPublishedWorkspaceEntities();
        }
    }
    Iterator<SearchProvider> searchProviderIterator = searchProviders.iterator();
    if (searchProviderIterator.hasNext()) {
        SearchProvider searchProvider = searchProviderIterator.next();
        SearchResult searchResult = null;
        if (workspaceEntities != null) {
            workspaceIdentifierFilters = new ArrayList<>();
            for (WorkspaceEntity workspaceEntity : workspaceEntities) {
                if (schoolDataSourceFilter == null) {
                    schoolDataSourceFilter = workspaceEntity.getDataSource().getIdentifier();
                }
                workspaceIdentifierFilters.add(workspaceEntity.getIdentifier());
            }
        }
        List<Sort> sorts = null;
        if (orderBy != null && orderBy.contains("alphabet")) {
            sorts = new ArrayList<>();
            sorts.add(new Sort("name.untouched", Sort.Order.ASC));
        }
        List<SchoolDataIdentifier> educationTypes = null;
        if (educationTypeIds != null) {
            educationTypes = new ArrayList<>(educationTypeIds.size());
            for (String educationTypeId : educationTypeIds) {
                SchoolDataIdentifier educationTypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
                if (educationTypeIdentifier != null) {
                    educationTypes.add(educationTypeIdentifier);
                } else {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed education type identifier", educationTypeId)).build();
                }
            }
        }
        List<SchoolDataIdentifier> curriculums = null;
        if (curriculumIds != null) {
            curriculums = new ArrayList<>(curriculumIds.size());
            for (String curriculumId : curriculumIds) {
                SchoolDataIdentifier curriculumIdentifier = SchoolDataIdentifier.fromId(curriculumId);
                if (curriculumIdentifier != null) {
                    curriculums.add(curriculumIdentifier);
                } else {
                    return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed curriculum identifier", curriculumId)).build();
                }
            }
        }
        searchResult = searchProvider.searchWorkspaces(schoolDataSourceFilter, subjects, workspaceIdentifierFilters, educationTypes, curriculums, searchString, null, null, includeUnpublished, firstResult, maxResults, sorts);
        List<Map<String, Object>> results = searchResult.getResults();
        for (Map<String, Object> result : results) {
            String searchId = (String) result.get("id");
            if (StringUtils.isNotBlank(searchId)) {
                String[] id = searchId.split("/", 2);
                if (id.length == 2) {
                    String dataSource = id[1];
                    String identifier = id[0];
                    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(dataSource, identifier);
                    if (workspaceEntity != null) {
                        String name = (String) result.get("name");
                        String description = (String) result.get("description");
                        String nameExtension = (String) result.get("nameExtension");
                        String subjectIdentifier = (String) result.get("subjectIdentifier");
                        Object curriculumIdentifiersObject = result.get("curriculumIdentifiers");
                        Set<String> curriculumIdentifiers = new HashSet<String>();
                        if (curriculumIdentifiersObject instanceof Collection) {
                            Collection<?> curriculumIdentifierCollection = (Collection<?>) curriculumIdentifiersObject;
                            for (Object o : curriculumIdentifierCollection) {
                                if (o instanceof String)
                                    curriculumIdentifiers.add((String) o);
                                else
                                    logger.warning("curriculumIdentifier not of type String");
                            }
                        }
                        if (StringUtils.isNotBlank(name)) {
                            workspaces.add(createRestModel(workspaceEntity, name, nameExtension, description, curriculumIdentifiers, subjectIdentifier));
                        }
                    }
                }
            }
        }
    } else {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }
    if (workspaces.isEmpty()) {
        return Response.noContent().build();
    }
    if (orderBy.contains("lastVisit")) {
        Collections.sort(workspaces, new Comparator<fi.otavanopisto.muikku.plugins.workspace.rest.model.Workspace>() {

            @Override
            public int compare(fi.otavanopisto.muikku.plugins.workspace.rest.model.Workspace workspace1, fi.otavanopisto.muikku.plugins.workspace.rest.model.Workspace workspace2) {
                if (workspace1.getLastVisit() == null || workspace2.getLastVisit() == null) {
                    return 0;
                }
                if (workspace1.getLastVisit().before(workspace2.getLastVisit())) {
                    return 1;
                }
                if (workspace1.getLastVisit().after(workspace2.getLastVisit())) {
                    return -1;
                }
                return 0;
            }
        });
    }
    return Response.ok(workspaces).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) ArrayList(java.util.ArrayList) Sort(fi.otavanopisto.muikku.search.SearchProvider.Sort) HashSet(java.util.HashSet) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 93 with RESTPermit

use of fi.otavanopisto.security.rest.RESTPermit in project muikku by otavanopisto.

the class WorkspaceRESTService method deleteWorkspaceMaterialProducer.

@DELETE
@Path("/workspaces/{WORKSPACEENTITYID}/materialProducers/{ID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response deleteWorkspaceMaterialProducer(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("ID") Long workspaceMaterialProducerId) {
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIAL_PRODUCERS, workspaceEntity)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    WorkspaceMaterialProducer materialProducer = workspaceController.findWorkspaceMaterialProducer(workspaceMaterialProducerId);
    workspaceController.deleteWorkspaceMaterialProducer(materialProducer);
    return Response.noContent().build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialProducer(fi.otavanopisto.muikku.model.workspace.WorkspaceMaterialProducer) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RESTPermit(fi.otavanopisto.security.rest.RESTPermit)

Example 94 with RESTPermit

use of fi.otavanopisto.security.rest.RESTPermit in project muikku by otavanopisto.

the class DeusNexMachinaRESTService method cleanWorkspaceMaterials.

@GET
@Path("/cleanworkspacematerials/{ID}")
@RESTPermit(DeusNexMachinaPermissions.CLEAN_WORKSPACE_MATERIALS)
public Response cleanWorkspaceMaterials(@PathParam("ID") Long workspaceEntityId, @Context Request request) {
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity != null) {
        WorkspaceNode rootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(workspaceEntity);
        try {
            List<WorkspaceNode> nodes = workspaceMaterialController.listWorkspaceNodesByParent(rootFolder);
            cleanMaterials(nodes);
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Cleaning materials of workspace " + workspaceEntityId + " failed", e);
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
        }
    } else {
        return Response.status(Status.NOT_FOUND).entity("Unknown workspace " + workspaceEntityId).build();
    }
    return Response.status(Status.OK).entity("Workspace " + workspaceEntityId + " materials successfully cleaned").build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 95 with RESTPermit

use of fi.otavanopisto.security.rest.RESTPermit in project muikku by otavanopisto.

the class PyramusSystemRESTService method ping.

@GET
@Path("/cache/statistics")
@RESTPermit(handling = Handling.UNSECURED)
public Response ping() {
    Map<String, Map<String, CachePathStatistics>> result = new HashMap<>();
    for (String cacheType : entityCacheStatistics.getCacheTypes()) {
        Statistics statistics = entityCacheStatistics.getStatistics(cacheType);
        Map<String, CachePathStatistics> pathStatistics = new HashMap<>();
        for (String path : statistics.getPaths()) {
            pathStatistics.put(path, new CachePathStatistics(statistics.getHits(path), statistics.getMisses(path), statistics.getSkips(path)));
        }
        result.put(cacheType, pathStatistics);
    }
    return Response.ok(result).build();
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Statistics(fi.otavanopisto.muikku.plugins.schooldatapyramus.rest.cache.EntityCacheStatistics.Statistics) EntityCacheStatistics(fi.otavanopisto.muikku.plugins.schooldatapyramus.rest.cache.EntityCacheStatistics) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

RESTPermit (fi.otavanopisto.security.rest.RESTPermit)215 Path (javax.ws.rs.Path)214 GET (javax.ws.rs.GET)99 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)90 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)83 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)61 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)57 POST (javax.ws.rs.POST)51 DELETE (javax.ws.rs.DELETE)45 ArrayList (java.util.ArrayList)36 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)30 ForumArea (fi.otavanopisto.muikku.plugins.forum.model.ForumArea)30 PUT (javax.ws.rs.PUT)26 ForumThread (fi.otavanopisto.muikku.plugins.forum.model.ForumThread)24 WorkspaceForumArea (fi.otavanopisto.muikku.plugins.forum.model.WorkspaceForumArea)21 CommunicatorMessageId (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId)20 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)20 User (fi.otavanopisto.muikku.schooldata.entity.User)19 EnvironmentForumArea (fi.otavanopisto.muikku.plugins.forum.model.EnvironmentForumArea)18 Date (java.util.Date)16