use of fi.otavanopisto.muikku.schooldata.entity.UserAddress in project muikku by otavanopisto.
the class ProfileBackingBean method init.
@RequestAction
@LoggedIn
public String init() {
UserEntity userEntity = sessionController.getLoggedUserEntity();
User user = userController.findUserByDataSourceAndIdentifier(sessionController.getLoggedUserSchoolDataSource(), sessionController.getLoggedUserIdentifier());
List<UserAddress> userAddresses = userController.listUserAddresses(user);
List<UserPhoneNumber> userPhoneNumbers = userController.listUserPhoneNumbers(user);
displayName = user.getNickName() == null ? user.getDisplayName() : String.format("%s %s (%s)", user.getNickName(), user.getLastName(), user.getStudyProgrammeName());
studyStartDate = user.getStudyStartDate();
studyTimeEnd = user.getStudyTimeEnd();
studyTimeLeftStr = "";
if (studyTimeEnd != null) {
OffsetDateTime now = OffsetDateTime.now();
Locale locale = sessionController.getLocale();
if (now.isBefore(studyTimeEnd)) {
long studyTimeLeftYears = now.until(studyTimeEnd, ChronoUnit.YEARS);
now = now.plusYears(studyTimeLeftYears);
if (studyTimeLeftYears > 0) {
studyTimeLeftStr += studyTimeLeftYears + " " + localeController.getText(locale, "plugin.profile.studyTimeEndShort.y");
}
long studyTimeLeftMonths = now.until(studyTimeEnd, ChronoUnit.MONTHS);
now = now.plusMonths(studyTimeLeftMonths);
if (studyTimeLeftMonths > 0) {
if (studyTimeLeftStr.length() > 0)
studyTimeLeftStr += " ";
studyTimeLeftStr += studyTimeLeftMonths + " " + localeController.getText(locale, "plugin.profile.studyTimeEndShort.m");
}
long studyTimeLeftDays = now.until(studyTimeEnd, ChronoUnit.DAYS);
now = now.plusDays(studyTimeLeftDays);
if (studyTimeLeftDays > 0) {
if (studyTimeLeftStr.length() > 0)
studyTimeLeftStr += " ";
studyTimeLeftStr += studyTimeLeftDays + " " + localeController.getText(locale, "plugin.profile.studyTimeEndShort.d");
}
}
}
addresses = new ArrayList<>();
for (UserAddress userAddress : userAddresses) {
addresses.add(String.format("%s %s %s %s", userAddress.getStreet(), userAddress.getPostalCode(), userAddress.getCity(), userAddress.getCountry()));
}
phoneNumbers = new ArrayList<>();
for (UserPhoneNumber userPhoneNumber : userPhoneNumbers) {
phoneNumbers.add(userPhoneNumber.getNumber());
}
SchoolDataIdentifier identifier = new SchoolDataIdentifier(userEntity.getDefaultIdentifier(), userEntity.getDefaultSchoolDataSource().getIdentifier());
emails = userEmailEntityController.getUserEmailAddresses(identifier);
return null;
}
use of fi.otavanopisto.muikku.schooldata.entity.UserAddress 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;
}
use of fi.otavanopisto.muikku.schooldata.entity.UserAddress in project muikku by otavanopisto.
the class UserRESTService method listStudentAddressses.
@GET
@Path("/students/{ID}/addresses")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listStudentAddressses(@PathParam("ID") String id) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).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 studentEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
if (studentEntity == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Could not find user entity for identifier %s", id)).build();
}
if (!studentEntity.getId().equals(sessionController.getLoggedUserEntity().getId())) {
if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_STUDENT_ADDRESSES)) {
return Response.status(Status.FORBIDDEN).build();
}
}
List<UserAddress> addresses = userController.listUserAddresses(studentIdentifier);
Collections.sort(addresses, new Comparator<UserAddress>() {
@Override
public int compare(UserAddress o1, UserAddress o2) {
return o1.getDefaultAddress() ? -1 : o2.getDefaultAddress() ? 1 : 0;
}
});
return Response.ok(createRestModel(addresses.toArray(new UserAddress[0]))).build();
}
use of fi.otavanopisto.muikku.schooldata.entity.UserAddress in project muikku by otavanopisto.
the class UserRESTService method updateStudentAddress.
@PUT
@Path("/students/{ID}/addresses/{ADDRESSID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response updateStudentAddress(@PathParam("ID") String id, @PathParam("ADDRESSID") String addressId, StudentAddress studentAddress) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).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 studentEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
if (studentEntity == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Could not find user entity for identifier %s", id)).build();
}
if (!studentEntity.getId().equals(sessionController.getLoggedUserEntity().getId())) {
if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_STUDENT_ADDRESSES)) {
return Response.status(Status.FORBIDDEN).build();
}
}
List<UserAddress> addresses = userController.listUserAddresses(studentIdentifier);
for (UserAddress address : addresses) {
if (address.getIdentifier().toId().equals(studentAddress.getIdentifier())) {
userEntityController.markAsUpdatedByStudent(studentEntity);
userController.updateUserAddress(studentIdentifier, address.getIdentifier(), studentAddress.getStreet(), studentAddress.getPostalCode(), studentAddress.getCity(), studentAddress.getCountry());
return Response.ok().entity(studentAddress).build();
}
}
return Response.status(Status.NOT_FOUND).entity("address not found").build();
}
Aggregations