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();
}
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();
}
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();
}
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);
}
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);
}
Aggregations