Search in sources :

Example 1 with UserAddress

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;
}
Also used : Locale(java.util.Locale) SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) OffsetDateTime(java.time.OffsetDateTime) UserAddress(fi.otavanopisto.muikku.schooldata.entity.UserAddress) UserPhoneNumber(fi.otavanopisto.muikku.schooldata.entity.UserPhoneNumber) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) RequestAction(org.ocpsoft.rewrite.annotation.RequestAction) LoggedIn(fi.otavanopisto.security.LoggedIn)

Example 2 with UserAddress

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;
}
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 UserAddress

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();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserAddress(fi.otavanopisto.muikku.schooldata.entity.UserAddress) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 4 with UserAddress

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();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserAddress(fi.otavanopisto.muikku.schooldata.entity.UserAddress) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) PUT(javax.ws.rs.PUT)

Aggregations

UserAddress (fi.otavanopisto.muikku.schooldata.entity.UserAddress)4 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)3 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)3 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)2 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 Path (javax.ws.rs.Path)2 SchoolDataBridgeInternalException (fi.otavanopisto.muikku.schooldata.SchoolDataBridgeInternalException)1 User (fi.otavanopisto.muikku.schooldata.entity.User)1 UserPhoneNumber (fi.otavanopisto.muikku.schooldata.entity.UserPhoneNumber)1 Address (fi.otavanopisto.pyramus.rest.model.Address)1 ContactType (fi.otavanopisto.pyramus.rest.model.ContactType)1 LoggedIn (fi.otavanopisto.security.LoggedIn)1 OffsetDateTime (java.time.OffsetDateTime)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 RequestAction (org.ocpsoft.rewrite.annotation.RequestAction)1