Search in sources :

Example 16 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class WorkspaceRESTService method createWorkspace.

@POST
@Path("/workspaces/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createWorkspace(@QueryParam("sourceWorkspaceIdentifier") String sourceWorkspaceId, @QueryParam("sourceWorkspaceEntityId") Long sourceWorkspaceEntityId, fi.otavanopisto.muikku.plugins.workspace.rest.model.Workspace payload) {
    SchoolDataIdentifier workspaceIdentifier = null;
    if (sourceWorkspaceId != null) {
        workspaceIdentifier = SchoolDataIdentifier.fromId(sourceWorkspaceId);
        if (workspaceIdentifier == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid source workspace identifier %s", sourceWorkspaceId)).build();
        }
    }
    WorkspaceEntity sourceWorkspaceEntity = null;
    if (sourceWorkspaceEntityId != null) {
        sourceWorkspaceEntity = workspaceEntityController.findWorkspaceEntityById(sourceWorkspaceEntityId);
        if (sourceWorkspaceEntity == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid source workspace entity id %d", sourceWorkspaceEntityId)).build();
        }
        workspaceIdentifier = new SchoolDataIdentifier(sourceWorkspaceEntity.getIdentifier(), sourceWorkspaceEntity.getDataSource().getIdentifier());
    }
    if (workspaceIdentifier == null) {
        return Response.status(Status.NOT_IMPLEMENTED).entity("Creating new workspaces without sourceWorkspace is not not implemented yet").build();
    }
    if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.COPY_WORKSPACE)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    if (StringUtils.isBlank(payload.getName())) {
        return Response.status(Status.BAD_REQUEST).entity("Name is required").build();
    }
    Workspace workspace = workspaceController.copyWorkspace(workspaceIdentifier, payload.getName(), payload.getNameExtension(), payload.getDescription());
    if (workspace == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Failed to create copy of workspace %s", sourceWorkspaceId)).build();
    }
    WorkspaceEntity workspaceEntity = findCopiedWorkspaceEntity(workspace);
    if (workspaceEntity == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Failed to create local copy of workspace %s", sourceWorkspaceId)).build();
    }
    // #2599: Also copy workspace default license and producers
    if (sourceWorkspaceEntity != null) {
        workspaceEntityController.updateDefaultMaterialLicense(workspaceEntity, sourceWorkspaceEntity.getDefaultMaterialLicense());
        List<WorkspaceMaterialProducer> workspaceMaterialProducers = workspaceController.listWorkspaceMaterialProducers(sourceWorkspaceEntity);
        for (WorkspaceMaterialProducer workspaceMaterialProducer : workspaceMaterialProducers) {
            workspaceController.createWorkspaceMaterialProducer(workspaceEntity, workspaceMaterialProducer.getName());
        }
    }
    return Response.ok(createRestModel(workspaceEntity, workspace.getName(), workspace.getNameExtension(), workspace.getDescription(), convertWorkspaceCurriculumIds(workspace), workspace.getSubjectIdentifier())).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceMaterialProducer(fi.otavanopisto.muikku.model.workspace.WorkspaceMaterialProducer) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 17 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class WorkspaceForumRESTService method getWorkspaceForumStatistics.

@GET
@Path("/workspaces/{WORKSPACEENTITYID}/forumStatistics")
@RESTPermit(handling = Handling.INLINE)
public Response getWorkspaceForumStatistics(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @QueryParam("userIdentifier") String userId) {
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).entity(String.format("Workspace entity %d could not be found", workspaceEntityId)).build();
    }
    SchoolDataIdentifier userIdentifier = null;
    if (StringUtils.isNotBlank(userId)) {
        userIdentifier = SchoolDataIdentifier.fromId(userId);
    }
    if (userIdentifier == null) {
        return Response.status(Status.NOT_IMPLEMENTED).entity("Listing forum statistics for all users is not implemented yet").build();
    }
    UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(userIdentifier);
    if (userEntity == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid userIdentifier").build();
    }
    if (!sessionController.hasWorkspacePermission(ForumResourcePermissionCollection.FORUM_FINDWORKSPACE_USERSTATISTICS, workspaceEntity)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    Long messageCount = forumController.countUserEntityWorkspaceMessages(workspaceEntity, userEntity);
    ForumMessage latestMessage = forumController.findUserEntitysLatestWorkspaceMessage(workspaceEntity, userEntity);
    return Response.ok(new WorkspaceForumUserStatisticsRESTModel(messageCount, latestMessage != null ? latestMessage.getCreated() : null)).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ForumMessage(fi.otavanopisto.muikku.plugins.forum.model.ForumMessage) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 18 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class TranscriptOfRecordsController method listWorkspaceIdentifiersBySubjectIdentifierAndCourseNumber.

public List<VopsWorkspace> listWorkspaceIdentifiersBySubjectIdentifierAndCourseNumber(String schoolDataSource, String subjectIdentifier, int courseNumber) {
    List<VopsWorkspace> retval = new ArrayList<>();
    SearchProvider searchProvider = getProvider("elastic-search");
    if (searchProvider != null) {
        SearchResult sr = searchProvider.searchWorkspaces(schoolDataSource, subjectIdentifier, courseNumber);
        List<Map<String, Object>> results = sr.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];
                    String educationTypeId = (String) result.get("educationSubtypeIdentifier");
                    String name = (String) result.get("name");
                    String description = (String) result.get("description");
                    @SuppressWarnings("unchecked") ArrayList<String> curriculums = (ArrayList<String>) result.get("curriculumIdentifiers");
                    SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(identifier, dataSource);
                    SchoolDataIdentifier educationSubtypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
                    Set<SchoolDataIdentifier> curriculumIdentifiers = new HashSet<>();
                    for (String curriculum : curriculums) {
                        curriculumIdentifiers.add(SchoolDataIdentifier.fromId(curriculum));
                    }
                    retval.add(new VopsWorkspace(workspaceIdentifier, educationSubtypeIdentifier, curriculumIdentifiers, name, description));
                }
            }
        }
    }
    return retval;
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) ArrayList(java.util.ArrayList) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) SearchResult(fi.otavanopisto.muikku.search.SearchResult) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 19 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class TranscriptOfRecordsFileUploadServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (!sessionController.isLoggedIn()) {
        sendResponse(resp, "Must be logged in", HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    if (!sessionController.hasEnvironmentPermission(TranscriptofRecordsPermissions.TRANSCRIPT_OF_RECORDS_FILE_UPLOAD)) {
        sendResponse(resp, "Insufficient permissions", HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    Part userIdentifierPart = req.getPart("userIdentifier");
    if (userIdentifierPart == null) {
        sendResponse(resp, "Missing userIdentifier", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String userIdentifier = "";
    try (InputStream is = userIdentifierPart.getInputStream()) {
        userIdentifier = IOUtils.toString(is, StandardCharsets.UTF_8);
    }
    SchoolDataIdentifier schoolDataIdentifier = SchoolDataIdentifier.fromId(userIdentifier);
    if (schoolDataIdentifier == null) {
        sendResponse(resp, "Invalid userIdentifier", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(schoolDataIdentifier);
    if (userEntity == null) {
        sendResponse(resp, "User entity not found", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    Part titlePart = req.getPart("title");
    if (titlePart == null) {
        sendResponse(resp, "Missing title", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String title = "";
    try (InputStream is = titlePart.getInputStream()) {
        title = IOUtils.toString(is, StandardCharsets.UTF_8);
    }
    Part descriptionPart = req.getPart("description");
    if (descriptionPart == null) {
        sendResponse(resp, "Missing description", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String description = "";
    try (InputStream is = descriptionPart.getInputStream()) {
        description = IOUtils.toString(is, StandardCharsets.UTF_8);
    }
    Part uploadPart = req.getPart("upload");
    if (uploadPart == null) {
        sendResponse(resp, "Missing file", HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    String contentType = uploadPart.getContentType();
    long fileSizeLimit = systemSettingsController.getUploadFileSizeLimit();
    if (uploadPart.getSize() > fileSizeLimit) {
        sendResponse(resp, "File too large", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
        return;
    }
    try (InputStream is = uploadPart.getInputStream()) {
        TranscriptOfRecordsFile file = transcriptOfRecordsFileController.attachFile(userEntity, is, contentType, title, description);
        String result = (new ObjectMapper()).writeValueAsString(file);
        sendResponse(resp, result, HttpServletResponse.SC_OK);
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) Part(javax.servlet.http.Part) InputStream(java.io.InputStream) TranscriptOfRecordsFile(fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 20 with SchoolDataIdentifier

use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.

the class UserIndexer method indexUser.

public void indexUser(String dataSource, String identifier) {
    schoolDataBridgeSessionController.startSystemSession();
    try {
        User user = userController.findUserByDataSourceAndIdentifier(dataSource, identifier);
        if (user != null) {
            EnvironmentRoleArchetype archetype = null;
            UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(user.getSchoolDataSource(), user.getIdentifier());
            if (userEntity != null) {
                EnvironmentUser eu = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
                if ((eu != null) && (eu.getRole() != null))
                    archetype = eu.getRole().getArchetype();
            }
            if ((archetype != null) && (userEntity != null)) {
                SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
                boolean isDefaultIdentifier = (userEntity.getDefaultIdentifier() != null && userEntity.getDefaultSchoolDataSource() != null) ? userEntity.getDefaultIdentifier().equals(user.getIdentifier()) && userEntity.getDefaultSchoolDataSource().getIdentifier().equals(user.getSchoolDataSource()) : false;
                Map<String, Object> extra = new HashMap<>();
                extra.put("archetype", archetype);
                extra.put("userEntityId", userEntity.getId());
                extra.put("isDefaultIdentifier", isDefaultIdentifier);
                Set<Long> workspaceEntityIds = new HashSet<Long>();
                Set<Long> userGroupIds = new HashSet<Long>();
                // List workspaces in which the student is active (TODO Should we have a separate variable for all workspaces?)
                List<WorkspaceEntity> workspaces = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserIdentifier(userIdentifier);
                for (WorkspaceEntity workspace : workspaces) {
                    workspaceEntityIds.add(workspace.getId());
                }
                extra.put("workspaces", workspaceEntityIds);
                List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(userIdentifier);
                for (UserGroupEntity userGroup : userGroups) {
                    userGroupIds.add(userGroup.getId());
                }
                extra.put("groups", userGroupIds);
                if (EnvironmentRoleArchetype.TEACHER.equals(archetype) || EnvironmentRoleArchetype.STUDY_GUIDER.equals(archetype) || EnvironmentRoleArchetype.STUDY_PROGRAMME_LEADER.equals(archetype) || EnvironmentRoleArchetype.MANAGER.equals(archetype) || EnvironmentRoleArchetype.ADMINISTRATOR.equals(archetype)) {
                    String userDefaultEmailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, false);
                    extra.put("email", userDefaultEmailAddress);
                }
                indexer.index(User.class.getSimpleName(), user, extra);
            } else
                indexer.index(User.class.getSimpleName(), user);
        } else {
            logger.info(String.format("Removing user %s/%s from index", identifier, dataSource));
            removeUser(dataSource, identifier);
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Indexing of user identifier " + identifier + " failed.", ex);
    } finally {
        schoolDataBridgeSessionController.endSystemSession();
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) User(fi.otavanopisto.muikku.schooldata.entity.User) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype) HashMap(java.util.HashMap) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) HashSet(java.util.HashSet)

Aggregations

SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)130 Path (javax.ws.rs.Path)63 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)59 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)58 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)53 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)50 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)48 ArrayList (java.util.ArrayList)38 GET (javax.ws.rs.GET)36 User (fi.otavanopisto.muikku.schooldata.entity.User)30 HashMap (java.util.HashMap)19 Workspace (fi.otavanopisto.muikku.schooldata.entity.Workspace)13 SearchResult (fi.otavanopisto.muikku.search.SearchResult)13 WorkspaceUser (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser)12 Date (java.util.Date)11 POST (javax.ws.rs.POST)11 GradingScaleItem (fi.otavanopisto.muikku.schooldata.entity.GradingScaleItem)10 WorkspaceAssessment (fi.otavanopisto.muikku.schooldata.entity.WorkspaceAssessment)10 HashSet (java.util.HashSet)9 PUT (javax.ws.rs.PUT)9