use of fi.otavanopisto.pyramus.rest.model.Email in project muikku by otavanopisto.
the class PyramusMocksRest method mockStudent.
public static Student mockStudent(Long personId, Long studentId, String firstName, String lastName, String email, List<String> tags, Map<String, String> variables, List<String> payloads, OffsetDateTime studyStartDate, OffsetDateTime studyEndDate) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JSR310Module()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Student student = new Student(studentId, personId, firstName, lastName, null, null, null, null, null, null, null, null, null, null, null, (long) 1, null, null, null, false, studyStartDate, studyEndDate, null, null, variables, tags, false);
String studentJson = objectMapper.writeValueAsString(student);
stubFor(get(urlEqualTo(String.format("/1/students/students/%d", studentId))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(studentJson).withStatus(200)));
Email emailObj = new Email(studentId, (long) 2, true, email);
Email[] emails = { emailObj };
String emailJson = objectMapper.writeValueAsString(emails);
Student[] studentArray = { student };
String studentArrayJson = objectMapper.writeValueAsString(studentArray);
stubFor(get(urlEqualTo(String.format("/1/students/students/%d/emails", studentId))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(emailJson).withStatus(200)));
stubFor(get(urlEqualTo(String.format("/1/students/students?email=%s", email))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(studentArrayJson).withStatus(200)));
addPayload(payloads, objectMapper.writeValueAsString(new WebhookStudentCreatePayload(student.getId())));
return student;
}
use of fi.otavanopisto.pyramus.rest.model.Email in project muikku by otavanopisto.
the class PyramusMocks method personsPyramusMocks.
public static void personsPyramusMocks() throws Exception {
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JSR310Module()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Map<String, String> variables = null;
List<String> tags = null;
OffsetDateTime birthday = OffsetDateTime.of(1990, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
Person person = mockPerson(1l, birthday, "030545-3453", fi.otavanopisto.pyramus.rest.model.Sex.MALE, 1l);
Student student = new Student((long) 1, (long) 1, "Test", "User", null, null, null, null, null, null, null, null, null, null, null, (long) 1, null, null, null, false, null, null, null, null, variables, tags, false);
String studentJson = objectMapper.writeValueAsString(student);
stubFor(get(urlEqualTo("/1/students/students/1")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(studentJson).withStatus(200)));
Email email = new Email((long) 1, (long) 1, true, "testuser@example.com");
Email[] emails = { email };
String emailJson = objectMapper.writeValueAsString(emails);
stubFor(get(urlEqualTo("/1/students/students/1/emails")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(emailJson).withStatus(200)));
Student[] studentArray = { student };
String studentArrayJson = objectMapper.writeValueAsString(studentArray);
stubFor(get(urlEqualTo("/1/students/students?email=testuser@example.com")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(studentArrayJson).withStatus(200)));
/* Student #2 for workspace #2*/
OffsetDateTime birthday2 = OffsetDateTime.of(1992, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
Person person2 = mockPerson(5l, birthday2, "021092-2112", fi.otavanopisto.pyramus.rest.model.Sex.MALE, 5l);
Student student2 = new Student((long) 5, (long) 5, "Second", "User", null, null, null, null, null, null, null, null, null, null, null, (long) 1, null, null, null, false, null, null, null, null, variables, tags, false);
String student2Json = objectMapper.writeValueAsString(student2);
stubFor(get(urlEqualTo("/1/students/students/5")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(student2Json).withStatus(200)));
Email email2 = new Email((long) 5, (long) 1, true, "seconduser@example.com");
Email[] emails2 = { email2 };
String email2Json = objectMapper.writeValueAsString(emails2);
stubFor(get(urlEqualTo("/1/students/students/5/emails")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(email2Json).withStatus(200)));
Student[] student2Array = { student2 };
String student2ArrayJson = objectMapper.writeValueAsString(student2Array);
stubFor(get(urlEqualTo("/1/students/students?email=seconduser@example.com")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(student2ArrayJson).withStatus(200)));
/* Student #2 for workspace #2 */
Person staff1 = mockPerson(2l, birthday, "030545-3454", fi.otavanopisto.pyramus.rest.model.Sex.MALE, 2l);
Person staff2 = mockPerson(3l, birthday, "030545-3455", fi.otavanopisto.pyramus.rest.model.Sex.MALE, 3l);
Person staff3 = mockPerson(4l, birthday, "030545-3456", fi.otavanopisto.pyramus.rest.model.Sex.MALE, 4l);
Person[] personArray = { person, person2, staff1, staff2, staff3 };
String personArrayJson = objectMapper.writeValueAsString(personArray);
stubFor(get(urlMatching("/1/persons/persons?filterArchived=.*")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(personArrayJson).withStatus(200)));
stubFor(get(urlEqualTo("/1/persons/persons")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(personArrayJson).withStatus(200)));
Student[] students = { student, student2 };
student2ArrayJson = objectMapper.writeValueAsString(students);
stubFor(get(urlMatching("/1/students/students?filterArchived=false&firstResult=.*&maxResults=.*")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(studentArrayJson).withStatus(200)));
StaffMember staffMember1 = new StaffMember((long) 2, (long) 2, null, "Test", "Staff1member", null, fi.otavanopisto.pyramus.rest.model.UserRole.MANAGER, tags, variables);
String staffMemberJson = objectMapper.writeValueAsString(staffMember1);
stubFor(get(urlEqualTo("/1/staff/members/2")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberJson).withStatus(200)));
StaffMember[] staffMemberArray = { staffMember1 };
String staffMemberArrayJson = objectMapper.writeValueAsString(staffMemberArray);
stubFor(get(urlEqualTo("/1/staff/members?email=teacher@example.com")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberArrayJson).withStatus(200)));
StaffMember staffMember2 = new StaffMember((long) 3, (long) 3, null, "Test", "Staff2member", null, fi.otavanopisto.pyramus.rest.model.UserRole.MANAGER, tags, variables);
staffMemberJson = objectMapper.writeValueAsString(staffMember2);
stubFor(get(urlEqualTo("/1/staff/members/3")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberJson).withStatus(200)));
StaffMember[] staffMember2Array = { staffMember2 };
staffMemberArrayJson = objectMapper.writeValueAsString(staffMember2Array);
stubFor(get(urlEqualTo("/1/staff/members?email=mana@example.com")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberArrayJson).withStatus(200)));
StaffMember staffMember3 = new StaffMember((long) 4, (long) 4, null, "Test", "Administrator", null, fi.otavanopisto.pyramus.rest.model.UserRole.ADMINISTRATOR, tags, variables);
staffMemberJson = objectMapper.writeValueAsString(staffMember3);
stubFor(get(urlEqualTo("/1/staff/members/4")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberJson).withStatus(200)));
StaffMember[] staffMember3Array = { staffMember3 };
staffMemberArrayJson = objectMapper.writeValueAsString(staffMember3Array);
stubFor(get(urlEqualTo("/1/staff/members?email=admin@example.com")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberArrayJson).withStatus(200)));
StaffMember[] staffArray = { staffMember1, staffMember2, staffMember3 };
String staffArrayJson = objectMapper.writeValueAsString(staffArray);
stubFor(get(urlEqualTo("/1/staff/members")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffArrayJson).withStatus(200)));
stubFor(get(urlEqualTo("1/courses/courses/1/staffMembers")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffArrayJson).withStatus(200)));
staffMemberJson = objectMapper.writeValueAsString(staffMember1);
stubFor(get(urlEqualTo("1/courses/courses/1/staffMembers/2")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberJson).withStatus(200)));
staffMemberJson = objectMapper.writeValueAsString(staffMember2);
stubFor(get(urlEqualTo("1/courses/courses/1/staffMembers/3")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberJson).withStatus(200)));
staffMemberJson = objectMapper.writeValueAsString(staffMember3);
stubFor(get(urlEqualTo("1/courses/courses/1/staffMembers/4")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberJson).withStatus(200)));
Email staff1Email = new Email((long) 2, (long) 1, true, "teacher@example.com");
Email[] staff1Emails = { staff1Email };
String staff1EmailJson = objectMapper.writeValueAsString(staff1Emails);
stubFor(get(urlEqualTo("/1/staff/members/2/emails")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staff1EmailJson).withStatus(200)));
Email staff2Email = new Email((long) 3, (long) 1, true, "mana@example.com");
Email[] staff2Emails = { staff2Email };
String staff2EmailJson = objectMapper.writeValueAsString(staff2Emails);
stubFor(get(urlEqualTo("/1/staff/members/3/emails")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staff2EmailJson).withStatus(200)));
Email staff3Email = new Email((long) 4, (long) 1, true, "admin@example.com");
Email[] staff3Emails = { staff3Email };
String staff3EmailJson = objectMapper.writeValueAsString(staff3Emails);
stubFor(get(urlEqualTo("/1/staff/members/4/emails")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staff3EmailJson).withStatus(200)));
CourseStaffMemberRole teacherRole = new CourseStaffMemberRole((long) 8, "Opettaja");
CourseStaffMemberRole[] cRoleArray = { teacherRole };
String cRoleJson = objectMapper.writeValueAsString(cRoleArray);
stubFor(get(urlEqualTo("/1/courses/staffMemberRoles")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(cRoleJson).withStatus(200)));
for (CourseStaffMemberRole role : cRoleArray) {
stubFor(get(urlEqualTo(String.format("/1/courses/staffMemberRoles/%d", role.getId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(role)).withStatus(200)));
}
CourseStaffMember staffMember = new CourseStaffMember(1l, 1l, 4l, 1l);
CourseStaffMember[] staffMembers = { staffMember };
stubFor(get(urlEqualTo(String.format("/1/courses/courses/%d/staffMembers", 1l))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(staffMembers)).withStatus(200)));
stubFor(get(urlEqualTo(String.format("/1/courses/courses/%d/staffMembers/%d", 1l, staffMember.getId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(staffMember)).withStatus(200)));
stubFor(get(urlEqualTo(String.format("/1/courses/courses/%d/staffMembers", 2l))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(staffMembers)).withStatus(200)));
stubFor(get(urlEqualTo(String.format("/1/courses/courses/%d/staffMembers/%d", 2l, staffMember.getId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(staffMember)).withStatus(200)));
stubFor(get(urlMatching("/1/courses/courses/1/students?filterArchived=.*")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(studentArrayJson).withStatus(200)));
mockContactTypes();
mockStudyProgrammes();
CourseStudent courseStudent = new CourseStudent(3l, 1l, 1l, OffsetDateTime.of(2010, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC), false, null, null, null, null, null);
CourseStudent[] csArray = { courseStudent };
String coursestudentArrayJson = objectMapper.writeValueAsString(csArray);
String courseStudenJson = objectMapper.writeValueAsString(courseStudent);
stubFor(get(urlMatching(String.format("/1/courses/courses/%d/students?filterArchived=.*", courseStudent.getCourseId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(coursestudentArrayJson).withStatus(200)));
stubFor(get(urlMatching("/1/courses/courses/1/students")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(coursestudentArrayJson).withStatus(200)));
stubFor(get(urlMatching(String.format("/1/courses/courses/%d/students/%d", courseStudent.getCourseId(), courseStudent.getId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(courseStudenJson).withStatus(200)));
OffsetDateTime assessmentCreated = OffsetDateTime.of(2015, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
CourseAssessment courseAssessment = new CourseAssessment(1l, courseStudent.getId(), 1l, 1l, 4l, assessmentCreated, "Test evaluation.", Boolean.TRUE);
stubFor(post(urlMatching(String.format("/1/students/students/%d/courses/%d/assessments/", student.getId(), courseStudent.getCourseId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(courseAssessment)).withStatus(200)));
stubFor(get(urlMatching(String.format("/1/students/students/%d/courses/%d/assessments/", student.getId(), courseStudent.getCourseId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(new ArrayList<>())).withStatus(200)));
CourseStudent courseStudent2 = new CourseStudent(2l, 2l, 5l, OffsetDateTime.of(2010, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC), false, null, null, null, null, null);
CourseStudent[] csArray2 = { courseStudent2 };
String coursestudentArrayJson2 = objectMapper.writeValueAsString(csArray2);
String courseStudenJson2 = objectMapper.writeValueAsString(courseStudent2);
stubFor(get(urlMatching(String.format("/1/courses/courses/%d/students?filterArchived=.*", courseStudent2.getCourseId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(coursestudentArrayJson2).withStatus(200)));
stubFor(get(urlMatching("/1/courses/courses/2/students")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(coursestudentArrayJson2).withStatus(200)));
stubFor(get(urlMatching(String.format("/1/courses/courses/%d/students/%d", courseStudent2.getCourseId(), courseStudent2.getId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(courseStudenJson2).withStatus(200)));
CourseAssessment courseAssessment2 = new CourseAssessment(1l, courseStudent.getId(), 1l, 1l, 4l, assessmentCreated, "", Boolean.TRUE);
stubFor(get(urlMatching(String.format("/1/students/students/%d/courses/%d/assessments/", student2.getId(), courseStudent2.getCourseId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(courseAssessment2)).withStatus(200)));
CourseStaffMember courseStaffMember = new CourseStaffMember(1l, 1l, 4l, 7l);
CourseStaffMember[] coursestaffMembers = { courseStaffMember };
stubFor(get(urlEqualTo(String.format("/1/courses/courses/%d/staffMembers", 1l))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(coursestaffMembers)).withStatus(200)));
stubFor(get(urlEqualTo(String.format("/1/courses/courses/%d/staffMembers/%d", 1l, staffMember.getId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(courseStaffMember)).withStatus(200)));
mockPersonStudens(new Student[] { student, student2 });
mockPersonStaffMembers(new StaffMember[] { staffMember1, staffMember2, staffMember3 });
String payload = objectMapper.writeValueAsString(new WebhookStudentCreatePayload((long) 5));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookPersonCreatePayload((long) 5));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookStudentCreatePayload((long) 1));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookPersonCreatePayload((long) 1));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookStaffMemberCreatePayload((long) 4));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookPersonCreatePayload((long) 4));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookCourseStudentCreatePayload(courseStudent.getId(), courseStudent.getCourseId(), courseStudent.getStudentId()));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookCourseStudentCreatePayload(courseStudent2.getId(), courseStudent2.getCourseId(), courseStudent2.getStudentId()));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookCourseStaffMemberCreatePayload(1l, 1l, 4l));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
}
use of fi.otavanopisto.pyramus.rest.model.Email in project muikku by otavanopisto.
the class PyramusUpdater method updatePerson.
@Lock(LockType.WRITE)
@AccessTimeout(value = 30000)
public void updatePerson(Person person) {
Long userEntityId = null;
String defaultIdentifier = null;
Long defaultUserId = person.getDefaultUserId();
UserRole defaultUserPyramusRole = null;
List<String> identifiers = new ArrayList<>();
List<String> removedIdentifiers = new ArrayList<>();
List<String> updatedIdentifiers = new ArrayList<>();
List<String> discoveredIdentifiers = new ArrayList<>();
Map<SchoolDataIdentifier, List<String>> emails = new HashMap<>();
// List all person's students and staffMembers
Student[] students = pyramusClient.get().get(String.format("/persons/persons/%d/students", person.getId()), Student[].class);
StaffMember[] staffMembers = pyramusClient.get().get(String.format("/persons/persons/%d/staffMembers", person.getId()), StaffMember[].class);
// If person does not have a defaultUserId specified, we try to guess something
if (defaultUserId == null) {
if ((staffMembers != null) && (staffMembers.length > 0)) {
// If person has a staffMember instance, lets use that one
defaultUserId = staffMembers[0].getId();
} else {
if (students != null) {
// Otherwise just use first non archived student (if any)
for (Student student : students) {
if (!student.getArchived()) {
defaultUserId = student.getId();
break;
}
}
}
}
}
if (students != null) {
// Iterate over all student instances
for (Student student : students) {
String identifier = identifierMapper.getStudentIdentifier(student.getId());
SchoolDataIdentifier schoolDataIdentifier = toIdentifier(identifier);
List<String> identifierEmails = new ArrayList<String>();
if (!student.getArchived()) {
// If student is not archived, add it to identifiers list
identifiers.add(identifier);
// If it's the specified defaultUserId, update defaultIdentifier and role accordingly
if ((defaultIdentifier == null) && student.getId().equals(defaultUserId)) {
defaultIdentifier = identifier;
defaultUserPyramusRole = UserRole.STUDENT;
}
// List emails and add all emails that are not specified non unique (e.g. contact persons) to the emails list
Email[] studentEmails = pyramusClient.get().get("/students/students/" + student.getId() + "/emails", Email[].class);
if (studentEmails != null) {
for (Email studentEmail : studentEmails) {
if (studentEmail.getContactTypeId() != null) {
ContactType contactType = pyramusClient.get().get("/common/contactTypes/" + studentEmail.getContactTypeId(), ContactType.class);
if (!contactType.getNonUnique() && !identifierEmails.contains(studentEmail.getAddress())) {
identifierEmails.add(studentEmail.getAddress());
}
} else {
logger.log(Level.WARNING, "ContactType of email is null - email is ignored");
}
}
}
} else {
// If the student instance if archived, we add it the the removed identifiers list
removedIdentifiers.add(identifier);
}
emails.put(schoolDataIdentifier, identifierEmails);
}
}
if (staffMembers != null) {
for (StaffMember staffMember : staffMembers) {
// Add staffMember identifier into the identifier list
String identifier = identifierMapper.getStaffIdentifier(staffMember.getId());
SchoolDataIdentifier schoolDataIdentifier = toIdentifier(identifier);
List<String> identifierEmails = new ArrayList<String>();
identifiers.add(identifier);
// If it's the specified defaultUserId, update defaultIdentifier and role accordingly
if ((defaultIdentifier == null) && staffMember.getId().equals(defaultUserId)) {
defaultIdentifier = identifier;
defaultUserPyramusRole = staffMember.getRole();
}
// List emails and add all emails that are not specified non unique (e.g. contact persons) to the emails list
Email[] staffMemberEmails = pyramusClient.get().get("/staff/members/" + staffMember.getId() + "/emails", Email[].class);
if (staffMemberEmails != null) {
for (Email staffMemberEmail : staffMemberEmails) {
if (staffMemberEmail.getContactTypeId() != null) {
ContactType contactType = pyramusClient.get().get("/common/contactTypes/" + staffMemberEmail.getContactTypeId(), ContactType.class);
if (!contactType.getNonUnique() && !identifierEmails.contains(staffMemberEmail.getAddress())) {
identifierEmails.add(staffMemberEmail.getAddress());
}
} else {
logger.log(Level.WARNING, "ContactType of email is null - email is ignored");
}
}
}
emails.put(schoolDataIdentifier, identifierEmails);
}
}
// Iterate over all discovered identifiers (students and staff members)
for (String identifier : identifiers) {
UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, identifier);
if (userSchoolDataIdentifier == null) {
// If no user entity can be found by the identifier, add it the the discovered identities list
discoveredIdentifiers.add(identifier);
} else {
// user entity found with given identity, so we need to make sure they all belong to same user
UserEntity userEntity = userSchoolDataIdentifier.getUserEntity();
if (userEntityId == null) {
userEntityId = userEntity.getId();
} else if (!userEntityId.equals(userEntity.getId())) {
logger.warning(String.format("Person %d synchronization failed. Found two userEntitys bound to it (%d and %d)", person.getId(), userEntityId, userEntity.getId()));
return;
}
}
}
UserEntity userEntity = userEntityId != null ? userEntityController.findUserEntityById(userEntityId) : null;
if (userEntity != null) {
// User already exists in the system so we check which of the identifiers have been removed and which just updated
List<UserSchoolDataIdentifier> existingSchoolDataIdentifiers = userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity);
for (UserSchoolDataIdentifier existingSchoolDataIdentifier : existingSchoolDataIdentifiers) {
if (existingSchoolDataIdentifier.getDataSource().getIdentifier().equals(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE)) {
if (!identifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
if (!removedIdentifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
removedIdentifiers.add(existingSchoolDataIdentifier.getIdentifier());
}
} else if (!discoveredIdentifiers.contains(existingSchoolDataIdentifier.getIdentifier())) {
updatedIdentifiers.add(existingSchoolDataIdentifier.getIdentifier());
}
}
}
}
// Resolve the user's desired environment role
SchoolDataIdentifier environmentRoleIdentifier = null;
if (defaultUserPyramusRole != null) {
String roleIdentifier = identifierMapper.getEnvironmentRoleIdentifier(defaultUserPyramusRole);
environmentRoleIdentifier = new SchoolDataIdentifier(roleIdentifier, SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE);
}
// And finally fire the update event
fireSchoolDataUserUpdated(userEntityId, defaultIdentifier, removedIdentifiers, updatedIdentifiers, discoveredIdentifiers, emails, environmentRoleIdentifier);
}
use of fi.otavanopisto.pyramus.rest.model.Email in project muikku by otavanopisto.
the class PyramusUserSchoolDataBridge method listUserEmailsByUserIdentifier.
@Override
public List<UserEmail> listUserEmailsByUserIdentifier(String userIdentifier) {
Email[] emails = null;
Long studentId = identifierMapper.getPyramusStudentId(userIdentifier);
if (studentId != null) {
emails = pyramusClient.get(String.format("/students/students/%d/emails", studentId), Email[].class);
} else {
Long staffId = identifierMapper.getPyramusStaffId(userIdentifier);
if (staffId != null) {
emails = pyramusClient.get(String.format("/staff/members/%d/emails", staffId), Email[].class);
} else {
logger.severe(String.format("PyramusUserSchoolDataBridge.listUserEmailsByUserIdentifier malformed user identifier %s\n%s", userIdentifier, ExceptionUtils.getStackTrace(new Throwable())));
}
}
if (emails != null) {
List<UserEmail> result = new ArrayList<>(emails.length);
for (Email email : emails) {
ContactType contactType = email != null ? pyramusClient.get("/common/contactTypes/" + email.getContactTypeId(), ContactType.class) : null;
UserEmail userEmail = entityFactory.createEntity(new SchoolDataIdentifier(userIdentifier, getSchoolDataSource()), email, contactType);
if (userEmail != null) {
result.add(userEmail);
}
}
return result;
}
return Collections.emptyList();
}
use of fi.otavanopisto.pyramus.rest.model.Email in project muikku by otavanopisto.
the class PyramusMocks method adminMock.
public static void adminMock() throws Exception {
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JSR310Module()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Map<String, String> variables = null;
List<String> tags = null;
OffsetDateTime birthday = OffsetDateTime.of(1990, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
Person staff3 = mockPerson((long) 4, birthday, "345345-3453", fi.otavanopisto.pyramus.rest.model.Sex.MALE, (long) 4);
Person[] personArray = { staff3 };
String personArrayJson = objectMapper.writeValueAsString(personArray);
stubFor(get(urlMatching("/1/persons/persons?filterArchived=.*")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(personArrayJson).withStatus(200)));
stubFor(get(urlEqualTo("/1/persons/persons")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(personArrayJson).withStatus(200)));
StaffMember staffMember3 = new StaffMember((long) 4, (long) 4, null, "Test", "Administrator", null, fi.otavanopisto.pyramus.rest.model.UserRole.ADMINISTRATOR, tags, variables);
String staffMemberJson = objectMapper.writeValueAsString(staffMember3);
stubFor(get(urlEqualTo("/1/staff/members/4")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberJson).withStatus(200)));
StaffMember[] staffMember3Array = { staffMember3 };
String staffMemberArrayJson = objectMapper.writeValueAsString(staffMember3Array);
stubFor(get(urlEqualTo("/1/staff/members?email=admin@example.com")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberArrayJson).withStatus(200)));
StaffMember[] staffArray = { staffMember3 };
String staffArrayJson = objectMapper.writeValueAsString(staffArray);
stubFor(get(urlEqualTo("/1/staff/members")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffArrayJson).withStatus(200)));
staffMemberJson = objectMapper.writeValueAsString(staffMember3);
stubFor(get(urlEqualTo("1/courses/courses/1/staffMembers/4")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffMemberJson).withStatus(200)));
Email staff3Email = new Email((long) 4, (long) 1, true, "admin@example.com");
Email[] staff3Emails = { staff3Email };
String staff3EmailJson = objectMapper.writeValueAsString(staff3Emails);
stubFor(get(urlEqualTo("/1/staff/members/4/emails")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staff3EmailJson).withStatus(200)));
CourseStaffMemberRole teacherRole = new CourseStaffMemberRole((long) 8, "Opettaja");
// CourseStaffMemberRole tutorRole = new CourseStaffMemberRole((long) 2, "Tutor");
// CourseStaffMemberRole vRole = new CourseStaffMemberRole((long) 3, "Vastuuhenkilö");
// CourseStaffMemberRole studentRole = new CourseStaffMemberRole((long) 9, "Opiskelija");
CourseStaffMemberRole[] cRoleArray = { teacherRole };
String cRoleJson = objectMapper.writeValueAsString(cRoleArray);
stubFor(get(urlEqualTo("/1/courses/staffMemberRoles")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(cRoleJson).withStatus(200)));
for (CourseStaffMemberRole role : cRoleArray) {
stubFor(get(urlEqualTo(String.format("/1/courses/staffMemberRoles/%d", role.getId()))).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(objectMapper.writeValueAsString(role)).withStatus(200)));
}
mockContactTypes();
String payload = objectMapper.writeValueAsString(new WebhookStaffMemberCreatePayload((long) 4));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
payload = objectMapper.writeValueAsString(new WebhookPersonCreatePayload((long) 4));
TestUtilities.webhookCall("http://dev.muikku.fi:" + System.getProperty("it.port.http") + "/pyramus/webhook", payload);
}
Aggregations