Search in sources :

Example 6 with SchoolDataIdentifier

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

the class UserRESTService method createFlagShare.

@POST
@Path("/flags/{ID}/shares")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createFlagShare(@PathParam("ID") Long id, fi.otavanopisto.muikku.rest.model.FlagShare payload) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    Flag flag = flagController.findFlagById(id);
    if (flag == null) {
        return Response.status(Status.NOT_FOUND).entity(String.format("Flag#%d not found", id)).build();
    }
    if (flag.getArchived()) {
        return Response.status(Status.NOT_FOUND).entity(String.format("Flag#%d not found", id)).build();
    }
    if (!flagController.hasFlagPermission(flag, sessionController.getLoggedUser())) {
        return Response.status(Status.FORBIDDEN).entity(String.format("You do not have permission to flag#%d", flag.getId())).build();
    }
    SchoolDataIdentifier userIdentifier = SchoolDataIdentifier.fromId(payload.getUserIdentifier());
    if (userIdentifier == null) {
        return Response.status(Status.BAD_REQUEST).entity("userIdentifier is malformed").build();
    }
    return Response.ok(createRestModel(flagController.createFlagShare(flag, userIdentifier))).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) Flag(fi.otavanopisto.muikku.model.users.Flag) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 7 with SchoolDataIdentifier

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

the class UserRESTService method findStudent.

@GET
@Path("/students/{ID}")
@RESTPermit(handling = Handling.INLINE)
public Response findStudent(@Context Request request, @PathParam("ID") String id) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).build();
    }
    SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(id);
    if (studentIdentifier == null) {
        return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Invalid studentIdentifier %s", id)).build();
    }
    UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
    if (userEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("UserEntity not found").build();
    }
    // Bug fix #2966: REST endpoint should only return students
    EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
    if (environmentUser != null) {
        EnvironmentRoleEntity userRole = environmentUser.getRole();
        if (userRole == null || userRole.getArchetype() != EnvironmentRoleArchetype.STUDENT) {
            return Response.status(Status.NOT_FOUND).build();
        }
    }
    EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(userEntity.getVersion())));
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    // TODO: There's no permission handling, this is relying on schooldatacontroller to check for permission
    User user = userController.findUserByIdentifier(studentIdentifier);
    if (user == null) {
        return Response.status(Status.NOT_FOUND).entity("User not found").build();
    }
    String emailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, true);
    Date studyStartDate = user.getStudyStartDate() != null ? Date.from(user.getStudyStartDate().toInstant()) : null;
    Date studyEndDate = user.getStudyEndDate() != null ? Date.from(user.getStudyEndDate().toInstant()) : null;
    Date studyTimeEnd = user.getStudyTimeEnd() != null ? Date.from(user.getStudyTimeEnd().toInstant()) : null;
    Student student = new Student(studentIdentifier.toId(), user.getFirstName(), user.getLastName(), user.getNickName(), user.getStudyProgrammeName(), false, user.getNationality(), user.getLanguage(), user.getMunicipality(), user.getSchool(), emailAddress, studyStartDate, studyEndDate, studyTimeEnd, user.getCurriculumIdentifier(), userEntity.getUpdatedByStudent());
    return Response.ok(student).cacheControl(cacheControl).tag(tag).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity) User(fi.otavanopisto.muikku.schooldata.entity.User) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) FlagStudent(fi.otavanopisto.muikku.model.users.FlagStudent) Student(fi.otavanopisto.muikku.rest.model.Student) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Date(java.util.Date) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 8 with SchoolDataIdentifier

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

the class UserRESTService method listFlags.

@GET
@Path("/flags/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listFlags(@QueryParam("ownerIdentifier") String ownerId) {
    SchoolDataIdentifier ownerIdentifier = null;
    if (StringUtils.isNotBlank(ownerId)) {
        ownerIdentifier = SchoolDataIdentifier.fromId(ownerId);
        if (ownerIdentifier == null) {
            return Response.status(Status.BAD_REQUEST).entity("ownerIdentifier is malformed").build();
        }
        // TODO: Add permission to list flags owned by others
        if (!ownerIdentifier.equals(sessionController.getLoggedUser())) {
            return Response.status(Status.FORBIDDEN).build();
        }
    } else {
        return Response.status(Status.FORBIDDEN).build();
    }
    List<Flag> flags = flagController.listByOwnedAndSharedFlags(ownerIdentifier);
    return Response.ok(createRestModel(flags.toArray(new Flag[0]))).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) Flag(fi.otavanopisto.muikku.model.users.Flag) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 9 with SchoolDataIdentifier

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

the class PyramusWorkspaceSchoolDataBridge method createWorkspaceEntity.

private Workspace createWorkspaceEntity(Course course) {
    if (course == null)
        return null;
    SchoolDataIdentifier educationTypeIdentifier = null;
    SchoolDataIdentifier educationSubtypeIdentifier = null;
    if (course.getSubjectId() != null) {
        Subject subject = pyramusClient.get("/common/subjects/" + course.getSubjectId(), fi.otavanopisto.pyramus.rest.model.Subject.class);
        if (subject == null) {
            logger.severe(String.format("Subject with id %d not found", course.getSubjectId()));
        } else {
            educationTypeIdentifier = identifierMapper.getEducationTypeIdentifier(subject.getEducationTypeId());
        }
    }
    Map<String, List<String>> courseEducationTypeMap = new HashMap<String, List<String>>();
    CourseEducationType[] courseEducationTypes = pyramusClient.get(String.format("/courses/courses/%d/educationTypes", course.getId()), CourseEducationType[].class);
    if (courseEducationTypes != null) {
        for (CourseEducationType courseEducationType : courseEducationTypes) {
            // #1632: if subject didn't determine education type and course only has one education type, use that instead
            if (educationTypeIdentifier == null && courseEducationTypes.length == 1) {
                educationTypeIdentifier = identifierMapper.getEducationTypeIdentifier(courseEducationTypes[0].getEducationTypeId());
            }
            CourseEducationSubtype[] courseEducationSubtypes = pyramusClient.get(String.format("/courses/courses/%d/educationTypes/%d/educationSubtypes", course.getId(), courseEducationType.getId()), CourseEducationSubtype[].class);
            if (courseEducationSubtypes == null) {
                continue;
            }
            if (educationSubtypeIdentifier == null && courseEducationSubtypes.length == 1) {
                educationSubtypeIdentifier = identifierMapper.getEducationSubtypeIdentifier(courseEducationSubtypes[0].getEducationSubtypeId());
            }
            EducationType educationType = pyramusClient.get(String.format("/common/educationTypes/%d", courseEducationType.getEducationTypeId()), EducationType.class);
            if (educationType == null) {
                logger.severe(String.format("Could not find educationType %d", courseEducationType.getEducationTypeId()));
                continue;
            }
            String educationTypeCode = educationType.getCode();
            List<String> courseEducationSubtypeList = new ArrayList<String>();
            for (CourseEducationSubtype courseEducationSubtype : courseEducationSubtypes) {
                EducationSubtype educationSubtype = pyramusClient.get(String.format("/common/educationTypes/%d/subtypes/%d", educationType.getId(), courseEducationSubtype.getEducationSubtypeId()), EducationSubtype.class);
                if (educationSubtype != null) {
                    String educationSubtypeCode = educationSubtype.getCode();
                    courseEducationSubtypeList.add(educationSubtypeCode);
                } else {
                    logger.severe(String.format("Could not find education subtype %d from type %d", courseEducationSubtype.getEducationSubtypeId(), educationType.getId()));
                }
            }
            courseEducationTypeMap.put(educationTypeCode, courseEducationSubtypeList);
        }
    }
    return entityFactory.createEntity(course, educationTypeIdentifier, educationSubtypeIdentifier, courseEducationTypeMap);
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) CourseEducationType(fi.otavanopisto.pyramus.rest.model.CourseEducationType) EducationType(fi.otavanopisto.pyramus.rest.model.EducationType) CourseEducationType(fi.otavanopisto.pyramus.rest.model.CourseEducationType) HashMap(java.util.HashMap) CourseEducationSubtype(fi.otavanopisto.pyramus.rest.model.CourseEducationSubtype) EducationSubtype(fi.otavanopisto.pyramus.rest.model.EducationSubtype) CourseEducationSubtype(fi.otavanopisto.pyramus.rest.model.CourseEducationSubtype) ArrayList(java.util.ArrayList) Subject(fi.otavanopisto.pyramus.rest.model.Subject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with SchoolDataIdentifier

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

the class PyramusWorkspaceSchoolDataBridge method copyWorkspace.

@Override
public Workspace copyWorkspace(SchoolDataIdentifier identifier, String name, String nameExtension, String description) {
    if (!getSchoolDataSource().equals(identifier.getDataSource())) {
        logger.severe(String.format("Invalid workspace identfier for Pyramus bridge", identifier));
        return null;
    }
    Long pyramusCourseId = identifierMapper.getPyramusCourseId(identifier.getIdentifier());
    if (pyramusCourseId == null) {
        logger.severe(String.format("Workspace identifier %s is not valid", identifier));
        return null;
    }
    Course course = pyramusClient.get(String.format("/courses/courses/%d", pyramusCourseId), Course.class);
    if (course == null) {
        logger.severe(String.format("Could not find Pyramus course by id %d", pyramusCourseId));
        return null;
    }
    List<String> copiedTags = null;
    if (course.getTags() != null) {
        copiedTags = new ArrayList<String>();
        for (String tag : course.getTags()) {
            copiedTags.add(tag);
        }
    }
    Course courseCopy = new Course(// copy has no id
    null, // copy has new name
    name, course.getCreated(), course.getLastModified(), // copy has new description
    description, course.getArchived(), course.getCourseNumber(), course.getMaxParticipantCount(), course.getBeginDate(), course.getEndDate(), // copy has new name extension
    nameExtension, course.getLocalTeachingDays(), course.getTeachingHours(), course.getDistanceTeachingHours(), course.getDistanceTeachingDays(), course.getAssessingHours(), course.getPlanningHours(), course.getEnrolmentTimeEnd(), course.getCreatorId(), course.getLastModifierId(), course.getSubjectId(), course.getCurriculumIds(), course.getLength(), course.getLengthUnitId(), course.getModuleId(), course.getStateId(), course.getTypeId(), // variables are not copied
    null, // copy has its own tag list
    copiedTags);
    Course createdCourse = pyramusClient.post("/courses/courses/", courseCopy);
    if (createdCourse == null) {
        logger.severe(String.format("Failed to create new course based on course %d", pyramusCourseId));
        return null;
    }
    // #2915: Copy additional course descriptions
    CourseDescription[] courseDescriptions = pyramusClient.get(String.format("/courses/courses/%d/descriptions", pyramusCourseId), CourseDescription[].class);
    if (ArrayUtils.isNotEmpty(courseDescriptions)) {
        for (int i = 0; i < courseDescriptions.length; i++) {
            pyramusClient.post(String.format("/courses/courses/%d/descriptions", createdCourse.getId()), new CourseDescription(null, createdCourse.getId(), courseDescriptions[i].getCourseDescriptionCategoryId(), courseDescriptions[i].getDescription()));
        }
    }
    SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(identifierMapper.getWorkspaceIdentifier(createdCourse.getId()), SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE);
    workspaceDiscoveryWaiter.waitDiscovered(workspaceIdentifier);
    return createWorkspaceEntity(createdCourse);
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) CourseDescription(fi.otavanopisto.pyramus.rest.model.CourseDescription) Course(fi.otavanopisto.pyramus.rest.model.Course)

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