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