Search in sources :

Example 1 with ContactType

use of fi.otavanopisto.pyramus.rest.model.ContactType in project muikku by otavanopisto.

the class PyramusMocksRest method mockUsers.

public static void mockUsers(List<String> payloads) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new JSR310Module()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    OffsetDateTime birthday = OffsetDateTime.of(1990, 2, 2, 0, 0, 0, 0, ZoneOffset.UTC);
    Person person = mockPerson(1l, birthday, "345345-3453", Sex.MALE, 1l);
    Person staff1 = mockPerson(2l, birthday, "345345-3453", Sex.MALE, 2l);
    Person staff2 = mockPerson(3l, birthday, "345345-3453", Sex.MALE, 3l);
    Person staff3 = mockPerson(4l, birthday, "345345-3453", Sex.MALE, 4l);
    Person staff4 = mockPerson(5l, birthday, "345345-3453", Sex.MALE, 5l);
    Person studentPerson2 = mockPerson(6l, birthday, "345345-3453", Sex.MALE, 6l);
    Person[] personArray = { person, staff1, staff2, staff3, staff4, studentPerson2 };
    String personArrayJson = objectMapper.writeValueAsString(personArray);
    for (Person p : personArray) addPayload(payloads, objectMapper.writeValueAsString(new WebhookPersonCreatePayload(p.getId())));
    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)));
    Map<String, String> variables = null;
    List<String> tags = null;
    Student student1 = mockStudent(1l, 1l, "Test", "User", "testuser@example.com", tags, variables, payloads, toDate(2010, 1, 1), getNextYear());
    Student student2 = mockStudent(6l, 6l, "Hidden", "Dragon", "crouchingtiger@example.com", tags, variables, payloads, toDate(2010, 1, 1), toDate(2011, 1, 1));
    StaffMember staffMember1 = mockStaffMember(2l, 2l, "Test", "Staff1member", "teacher@example.com", UserRole.USER, tags, variables, payloads);
    StaffMember staffMember2 = mockStaffMember(3l, 3l, "Test", "Staff2member", "mana@example.com", UserRole.MANAGER, tags, variables, payloads);
    StaffMember staffMember3 = mockStaffMember(4l, 4l, "Test", "Administrator", "admin@example.com", UserRole.ADMINISTRATOR, tags, variables, payloads);
    StaffMember staffMember4 = mockStaffMember(5l, 5l, "Trusted", "System", "trusted@example.com", UserRole.TRUSTED_SYSTEM, tags, variables, payloads);
    Student[] studentArray = { student1, student2 };
    StaffMember[] staffArray = { staffMember1, staffMember2, staffMember3, staffMember4 };
    String studentArrayJson = objectMapper.writeValueAsString(studentArray);
    String staffArrayJson = objectMapper.writeValueAsString(staffArray);
    stubFor(get(urlMatching("/1/students/students?filterArchived=false&firstResult=.*&maxResults=.*")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(studentArrayJson).withStatus(200)));
    stubFor(get(urlEqualTo("/1/staff/members")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(staffArrayJson).withStatus(200)));
    mockPersonStudens(studentArray);
    mockPersonStaffMembers(staffArray);
    ContactType contactType = new ContactType((long) 1, "Koti", false, false);
    ContactType[] contactTypes = { contactType };
    String contactTypeJson = objectMapper.writeValueAsString(contactType);
    stubFor(get(urlMatching("/1/common/contactTypes/.*")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(contactTypeJson).withStatus(200)));
    String contactTypesJson = objectMapper.writeValueAsString(contactTypes);
    stubFor(get(urlEqualTo("/1/common/contactTypes")).willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(contactTypesJson).withStatus(200)));
}
Also used : WebhookPersonCreatePayload(fi.otavanopisto.pyramus.webhooks.WebhookPersonCreatePayload) ContactType(fi.otavanopisto.pyramus.rest.model.ContactType) JSR310Module(com.fasterxml.jackson.datatype.jsr310.JSR310Module) Student(fi.otavanopisto.pyramus.rest.model.Student) StudentGroupStudent(fi.otavanopisto.pyramus.rest.model.StudentGroupStudent) CourseStudent(fi.otavanopisto.pyramus.rest.model.CourseStudent) CourseStaffMember(fi.otavanopisto.pyramus.rest.model.CourseStaffMember) StaffMember(fi.otavanopisto.pyramus.rest.model.StaffMember) OffsetDateTime(java.time.OffsetDateTime) Person(fi.otavanopisto.pyramus.rest.model.Person) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ContactType

use of fi.otavanopisto.pyramus.rest.model.ContactType in project muikku by otavanopisto.

the class PyramusUserSchoolDataBridge method listUserAddresses.

@Override
public List<UserAddress> listUserAddresses(SchoolDataIdentifier userIdentifier) {
    if (!StringUtils.equals(userIdentifier.getDataSource(), getSchoolDataSource())) {
        throw new SchoolDataBridgeInternalException(String.format("Could not list email addresses for user from school data source %s", userIdentifier.getDataSource()));
    }
    Address[] addresses = null;
    Long pyramusStudentId = identifierMapper.getPyramusStudentId(userIdentifier.getIdentifier());
    if (pyramusStudentId != null) {
        addresses = pyramusClient.get(String.format("/students/students/%d/addresses", pyramusStudentId), Address[].class);
    } else {
        Long pyramusStaffId = identifierMapper.getPyramusStaffId(userIdentifier.getIdentifier());
        if (pyramusStaffId != null) {
            addresses = pyramusClient.get(String.format("/staff/members/%d/addresses", pyramusStaffId), Address[].class);
        }
    }
    if (addresses == null) {
        return Collections.emptyList();
    }
    List<UserAddress> result = new ArrayList<>(addresses.length);
    for (Address address : addresses) {
        ContactType contactType = address.getContactTypeId() != null ? pyramusClient.get(String.format("/common/contactTypes/%d", address.getContactTypeId()), ContactType.class) : null;
        result.add(entityFactory.createEntity(userIdentifier, address, contactType));
    }
    return result;
}
Also used : ContactType(fi.otavanopisto.pyramus.rest.model.ContactType) UserAddress(fi.otavanopisto.muikku.schooldata.entity.UserAddress) Address(fi.otavanopisto.pyramus.rest.model.Address) SchoolDataBridgeInternalException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException) ArrayList(java.util.ArrayList) UserAddress(fi.otavanopisto.muikku.schooldata.entity.UserAddress)

Example 3 with ContactType

use of fi.otavanopisto.pyramus.rest.model.ContactType in project muikku by otavanopisto.

the class PyramusUserSchoolDataBridge method listUserPhoneNumbers.

@Override
public List<UserPhoneNumber> listUserPhoneNumbers(SchoolDataIdentifier userIdentifier) {
    if (!StringUtils.equals(userIdentifier.getDataSource(), getSchoolDataSource())) {
        throw new SchoolDataBridgeInternalException(String.format("Could not list phone numbers for user from school data source %s", userIdentifier.getDataSource()));
    }
    PhoneNumber[] phoneNumbers = null;
    Long pyramusStudentId = identifierMapper.getPyramusStudentId(userIdentifier.getIdentifier());
    if (pyramusStudentId != null) {
        phoneNumbers = pyramusClient.get(String.format("/students/students/%d/phoneNumbers", pyramusStudentId), PhoneNumber[].class);
        if (phoneNumbers == null) {
            return Collections.emptyList();
        }
    } else {
        Long pyramusStaffId = identifierMapper.getPyramusStaffId(userIdentifier.getIdentifier());
        if (pyramusStaffId != null) {
            phoneNumbers = pyramusClient.get(String.format("/staff/members/%d/phoneNumbers", pyramusStaffId), PhoneNumber[].class);
            if (phoneNumbers == null) {
                return Collections.emptyList();
            }
        }
    }
    List<UserPhoneNumber> result = new ArrayList<>();
    for (PhoneNumber phoneNumber : phoneNumbers) {
        ContactType contactType = phoneNumber.getContactTypeId() != null ? pyramusClient.get(String.format("/common/contactTypes/%d", phoneNumber.getContactTypeId()), ContactType.class) : null;
        result.add(entityFactory.createEntity(userIdentifier, phoneNumber, contactType));
    }
    return result;
}
Also used : ContactType(fi.otavanopisto.pyramus.rest.model.ContactType) SchoolDataBridgeInternalException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException) ArrayList(java.util.ArrayList) UserPhoneNumber(fi.otavanopisto.muikku.schooldata.entity.UserPhoneNumber) PhoneNumber(fi.otavanopisto.pyramus.rest.model.PhoneNumber) UserPhoneNumber(fi.otavanopisto.muikku.schooldata.entity.UserPhoneNumber)

Example 4 with ContactType

use of fi.otavanopisto.pyramus.rest.model.ContactType 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);
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) RoleSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) Email(fi.otavanopisto.pyramus.rest.model.Email) ContactType(fi.otavanopisto.pyramus.rest.model.ContactType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Student(fi.otavanopisto.pyramus.rest.model.Student) StudentGroupStudent(fi.otavanopisto.pyramus.rest.model.StudentGroupStudent) CourseStudent(fi.otavanopisto.pyramus.rest.model.CourseStudent) CourseStaffMember(fi.otavanopisto.pyramus.rest.model.CourseStaffMember) StaffMember(fi.otavanopisto.pyramus.rest.model.StaffMember) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserGroupUserEntity(fi.otavanopisto.muikku.model.users.UserGroupUserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) UserRole(fi.otavanopisto.pyramus.rest.model.UserRole) List(java.util.List) ArrayList(java.util.ArrayList) AccessTimeout(javax.ejb.AccessTimeout) Lock(javax.ejb.Lock)

Example 5 with ContactType

use of fi.otavanopisto.pyramus.rest.model.ContactType 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();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserEmail(fi.otavanopisto.muikku.schooldata.entity.UserEmail) Email(fi.otavanopisto.pyramus.rest.model.Email) ContactType(fi.otavanopisto.pyramus.rest.model.ContactType) ArrayList(java.util.ArrayList) UserEmail(fi.otavanopisto.muikku.schooldata.entity.UserEmail)

Aggregations

ContactType (fi.otavanopisto.pyramus.rest.model.ContactType)6 ArrayList (java.util.ArrayList)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JSR310Module (com.fasterxml.jackson.datatype.jsr310.JSR310Module)2 SchoolDataBridgeInternalException (fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException)2 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)2 CourseStaffMember (fi.otavanopisto.pyramus.rest.model.CourseStaffMember)2 CourseStudent (fi.otavanopisto.pyramus.rest.model.CourseStudent)2 Email (fi.otavanopisto.pyramus.rest.model.Email)2 StaffMember (fi.otavanopisto.pyramus.rest.model.StaffMember)2 Student (fi.otavanopisto.pyramus.rest.model.Student)2 StudentGroupStudent (fi.otavanopisto.pyramus.rest.model.StudentGroupStudent)2 RoleSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.RoleSchoolDataIdentifier)1 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)1 UserGroupUserEntity (fi.otavanopisto.muikku.model.users.UserGroupUserEntity)1 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)1 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)1 UserAddress (fi.otavanopisto.muikku.schooldata.entity.UserAddress)1 UserEmail (fi.otavanopisto.muikku.schooldata.entity.UserEmail)1 UserPhoneNumber (fi.otavanopisto.muikku.schooldata.entity.UserPhoneNumber)1