Search in sources :

Example 11 with SchoolDataBridgeInternalException

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

the class PyramusUserSchoolDataBridge method updateUserAddress.

@Override
public void updateUserAddress(SchoolDataIdentifier studentIdentifier, SchoolDataIdentifier identifier, String street, String postalCode, String city, String country) {
    Long addressId = identifierMapper.getPyramusAddressId(identifier.getIdentifier());
    Long studentId = identifierMapper.getPyramusStudentId(studentIdentifier.getIdentifier());
    if (addressId == null) {
        throw new SchoolDataBridgeInternalException(String.format("Malformed identifier %s", identifier));
    }
    if (studentId == null) {
        throw new SchoolDataBridgeInternalException(String.format("Malformed identifier %s", studentIdentifier));
    }
    try {
        Address address = pyramusClient.get(String.format("/students/students/%d/addresses/%d", studentId, addressId), Address.class);
        if (address == null) {
            throw new SchoolDataBridgeInternalException(String.format("Address %d of student %d not found", addressId, studentId));
        }
        address.setStreetAddress(street);
        address.setPostalCode(postalCode);
        address.setCity(city);
        address.setCountry(country);
        pyramusClient.put(String.format("/students/students/%d/addresses/%s", studentId, addressId), address);
    } catch (PyramusRestClientUnauthorizedException purr) {
        throw new SchoolDataBridgeUnauthorizedException(purr.getMessage());
    }
}
Also used : UserAddress(fi.otavanopisto.muikku.schooldata.entity.UserAddress) Address(fi.otavanopisto.pyramus.rest.model.Address) SchoolDataBridgeInternalException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException) SchoolDataBridgeUnauthorizedException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeUnauthorizedException) PyramusRestClientUnauthorizedException(fi.otavanopisto.muikku.plugins.schooldatapyramus.rest.PyramusRestClientUnauthorizedException)

Example 12 with SchoolDataBridgeInternalException

use of fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException 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 13 with SchoolDataBridgeInternalException

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

the class PyramusCourseMetaSchoolDataBridge method listCourseIdentifiersBySubject.

@Override
public List<CourseIdentifier> listCourseIdentifiersBySubject(String subjectIdentifier) {
    if (!StringUtils.isNumeric(subjectIdentifier)) {
        throw new SchoolDataBridgeInternalException("Identifier has to be numeric");
    }
    // TODO Fix workaround
    fi.otavanopisto.pyramus.rest.model.Subject subject = pyramusClient.get("/common/subjects/" + subjectIdentifier, fi.otavanopisto.pyramus.rest.model.Subject.class);
    List<CourseIdentifier> result = new ArrayList<>();
    List<String> courseNumbers = new ArrayList<String>();
    String identifier = subject.getId().toString();
    Course[] courses = pyramusClient.get("/common/subjects/" + identifier + "/courses", fi.otavanopisto.pyramus.rest.model.Course[].class);
    for (Course course : courses) {
        String courseNumber = course.getCourseNumber() != null ? course.getCourseNumber().toString() : "null";
        if (!courseNumbers.contains(courseNumber))
            courseNumbers.add(courseNumber);
    }
    for (String cn : courseNumbers) {
        result.add(new PyramusCourseIdentifier(subject.getId().toString() + "/" + cn, subject.getCode(), subject.getId().toString()));
    }
    return result;
}
Also used : PyramusCourseIdentifier(fi.otavanopisto.muikku.plugins.schooldatapyramus.entities.PyramusCourseIdentifier) ArrayList(java.util.ArrayList) PyramusCourseIdentifier(fi.otavanopisto.muikku.plugins.schooldatapyramus.entities.PyramusCourseIdentifier) CourseIdentifier(fi.otavanopisto.muikku.schooldata.entity.CourseIdentifier) SchoolDataBridgeInternalException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException) Course(fi.otavanopisto.pyramus.rest.model.Course)

Example 14 with SchoolDataBridgeInternalException

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

the class PyramusUserSchoolDataBridge method findActiveUser.

@Override
public User findActiveUser(String identifier) {
    Long studentId = identifierMapper.getPyramusStudentId(identifier);
    if (studentId != null) {
        Student student = findPyramusStudent(studentId);
        Person person = findPyramusPerson(student.getPersonId());
        if (!student.getId().equals(person.getDefaultUserId())) {
            return findUserByPyramusUser(person.getDefaultUserId());
        }
        return createStudentEntity(student);
    }
    Long staffId = identifierMapper.getPyramusStaffId(identifier);
    if (staffId != null) {
        StaffMember staffMember = findPyramusStaffMember(staffId);
        if (staffMember == null) {
            return null;
        }
        Person person = findPyramusPerson(staffMember.getPersonId());
        if (!staffMember.getId().equals(person.getDefaultUserId())) {
            return findUserByPyramusUser(person.getDefaultUserId());
        }
        return entityFactory.createEntity(staffMember);
    }
    logger.warning(String.format("PyramusUserSchoolDataBridge.findActiveUser malformed user identifier %s\n%s", identifier, ExceptionUtils.getStackTrace(new Throwable())));
    throw new SchoolDataBridgeInternalException(String.format("Malformed user identifier %s", identifier));
}
Also used : SchoolDataBridgeInternalException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException) Student(fi.otavanopisto.pyramus.rest.model.Student) StudentGroupStudent(fi.otavanopisto.pyramus.rest.model.StudentGroupStudent) StaffMember(fi.otavanopisto.pyramus.rest.model.StaffMember) Person(fi.otavanopisto.pyramus.rest.model.Person)

Example 15 with SchoolDataBridgeInternalException

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

the class PyramusUserSchoolDataBridge method updateUser.

@Override
public User updateUser(User user) {
    Long studentId = identifierMapper.getPyramusStudentId(user.getIdentifier());
    if (studentId == null) {
        throw new SchoolDataBridgeInternalException("User is not a Pyramus student");
    }
    Student student = pyramusClient.get(String.format("/students/students/%d", studentId), Student.class);
    Municipality[] municipalities = pyramusClient.get("/students/municipalities", Municipality[].class);
    // There's no search endpoint in Pyramus REST, and the municipality field is text, so we need to do this
    boolean municipalityFound = false;
    for (Municipality municipality : Arrays.asList(municipalities)) {
        if (StringUtils.equalsIgnoreCase(municipality.getName(), user.getMunicipality())) {
            student.setMunicipalityId(municipality.getId());
            municipalityFound = true;
            break;
        }
    }
    if (!municipalityFound) {
        throw new SchoolDataBridgeInternalException("Municipality not found");
    }
    pyramusClient.put(String.format("/students/students/%d", studentId), student);
    return user;
}
Also used : Municipality(fi.otavanopisto.pyramus.rest.model.Municipality) SchoolDataBridgeInternalException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException) Student(fi.otavanopisto.pyramus.rest.model.Student) StudentGroupStudent(fi.otavanopisto.pyramus.rest.model.StudentGroupStudent)

Aggregations

SchoolDataBridgeInternalException (fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException)21 Student (fi.otavanopisto.pyramus.rest.model.Student)7 StudentGroupStudent (fi.otavanopisto.pyramus.rest.model.StudentGroupStudent)7 ArrayList (java.util.ArrayList)7 PyramusUserProperty (fi.otavanopisto.muikku.plugins.schooldatapyramus.entities.PyramusUserProperty)3 StaffMember (fi.otavanopisto.pyramus.rest.model.StaffMember)3 LocalUser (fi.otavanopisto.muikku.plugins.schooldatalocal.model.LocalUser)2 LocalUserEmail (fi.otavanopisto.muikku.plugins.schooldatalocal.model.LocalUserEmail)2 LocalUserImage (fi.otavanopisto.muikku.plugins.schooldatalocal.model.LocalUserImage)2 PyramusCourseIdentifier (fi.otavanopisto.muikku.plugins.schooldatapyramus.entities.PyramusCourseIdentifier)2 PyramusRestClientUnauthorizedException (fi.otavanopisto.muikku.plugins.schooldatapyramus.rest.PyramusRestClientUnauthorizedException)2 SchoolDataBridgeUnauthorizedException (fi.otavanopisto.muikku.schooldata.SchoolDataBridgeUnauthorizedException)2 UserAddress (fi.otavanopisto.muikku.schooldata.entity.UserAddress)2 Address (fi.otavanopisto.pyramus.rest.model.Address)2 ContactType (fi.otavanopisto.pyramus.rest.model.ContactType)2 Course (fi.otavanopisto.pyramus.rest.model.Course)2 EnvironmentRoleEntity (fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity)1 RoleEntity (fi.otavanopisto.muikku.model.users.RoleEntity)1 WorkspaceRoleEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity)1 CourseIdentifier (fi.otavanopisto.muikku.schooldata.entity.CourseIdentifier)1