Search in sources :

Example 6 with Defaults

use of fi.otavanopisto.pyramus.domainmodel.base.Defaults in project pyramus by otavanopisto.

the class MuikkuRESTService method createUser.

@Path("/users")
@POST
@RESTPermit(MuikkuPermissions.MUIKKU_CREATE_STAFF_MEMBER)
public Response createUser(@Context HttpServletRequest request, StaffMemberPayload payload) {
    // Prerequisites
    DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO();
    Defaults defaults = defaultsDAO.getDefaults();
    if (defaults.getUserDefaultContactType() == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("userDefaultContactType not set in Defaults").build();
    }
    User loggedUser = sessionController.getUser();
    if (loggedUser.getOrganization() == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Current user lacks organization").build();
    }
    if (StringUtils.isAnyBlank(payload.getFirstName(), payload.getLastName(), payload.getEmail(), payload.getRole())) {
        return Response.status(Status.BAD_REQUEST).entity("Empty fields in payload").build();
    }
    // Endpoint only supports creation of managers and teachers
    Role role = Role.valueOf(payload.getRole());
    if (role != Role.MANAGER && role != Role.TEACHER) {
        return Response.status(Status.BAD_REQUEST).entity(String.format("Unsupported role %s", payload.getRole())).build();
    }
    // Check if user exists based on email
    String address = StringUtils.trim(StringUtils.lowerCase(payload.getEmail()));
    if (!commonController.isEmailAvailable(address)) {
        return Response.status(Status.CONFLICT).entity(getMessage(request.getLocale(), "error.emailInUse")).build();
    }
    // User creation
    Person person = personController.createPerson(null, null, null, null, Boolean.FALSE);
    StaffMember staffMember = userController.createStaffMember(loggedUser.getOrganization(), payload.getFirstName(), payload.getLastName(), role, person);
    personController.updatePersonDefaultUser(person, staffMember);
    userController.addUserEmail(staffMember, defaults.getUserDefaultContactType(), address, Boolean.TRUE);
    payload.setIdentifier(staffMember.getId().toString());
    return Response.ok(payload).build();
}
Also used : Role(fi.otavanopisto.pyramus.domainmodel.users.Role) Defaults(fi.otavanopisto.pyramus.domainmodel.base.Defaults) StudentGroupUser(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser) User(fi.otavanopisto.pyramus.domainmodel.users.User) DefaultsDAO(fi.otavanopisto.pyramus.dao.base.DefaultsDAO) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) Person(fi.otavanopisto.pyramus.domainmodel.base.Person) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) POST(javax.ws.rs.POST)

Example 7 with Defaults

use of fi.otavanopisto.pyramus.domainmodel.base.Defaults in project pyramus by otavanopisto.

the class MuikkuRESTService method createStudent.

@Path("/students")
@POST
@RESTPermit(MuikkuPermissions.MUIKKU_CREATE_STUDENT)
public Response createStudent(@Context HttpServletRequest request, StudentPayload payload) {
    // Prerequisites
    DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO();
    Defaults defaults = defaultsDAO.getDefaults();
    if (defaults.getStudentDefaultContactType() == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("studentDefaultContactType not set in Defaults").build();
    }
    User loggedUser = sessionController.getUser();
    if (loggedUser.getOrganization() == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Current user lacks organization").build();
    }
    if (StringUtils.isAnyBlank(payload.getFirstName(), payload.getLastName(), payload.getEmail())) {
        return Response.status(Status.BAD_REQUEST).entity("Empty fields in payload").build();
    }
    Sex sex = null;
    if (!StringUtils.isBlank(payload.getGender())) {
        try {
            sex = Sex.valueOf(payload.getGender());
        } catch (IllegalArgumentException e) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid payload gender %s", payload.getGender())).build();
        }
    }
    if (StringUtils.isBlank(payload.getStudyProgrammeIdentifier()) || !NumberUtils.isNumber(payload.getStudyProgrammeIdentifier())) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid payload study programme").build();
    }
    StudyProgrammeDAO studyProgrammeDAO = DAOFactory.getInstance().getStudyProgrammeDAO();
    StudyProgramme studyProgramme = studyProgrammeDAO.findById(Long.valueOf(payload.getStudyProgrammeIdentifier()));
    if (!UserUtils.canAccessOrganization(sessionController.getUser(), studyProgramme.getOrganization())) {
        return Response.status(Status.BAD_REQUEST).entity("No study programme access").build();
    }
    // Birthday generation if SSN present
    Date birthday = null;
    if (!StringUtils.isBlank(payload.getSsn())) {
        try {
            birthday = new SimpleDateFormat("ddMMyy").parse(payload.getSsn().substring(0, 6));
        } catch (Exception e) {
            return Response.status(Status.BAD_REQUEST).entity("Invalid payload SSN").build();
        }
    }
    // Check if user exists based on email or (possible) SSN
    String address = StringUtils.trim(StringUtils.lowerCase(payload.getEmail()));
    if (!commonController.isEmailAvailable(address)) {
        return Response.status(Status.CONFLICT).entity(getMessage(request.getLocale(), "error.emailInUse")).build();
    }
    String ssn = null;
    if (!StringUtils.isBlank(payload.getSsn())) {
        ssn = StringUtils.upperCase(payload.getSsn());
        Person person = personController.findBySsn(ssn);
        if (person != null) {
            return Response.status(Status.CONFLICT).entity(getMessage(request.getLocale(), "error.ssnInUse")).build();
        }
    }
    // Student creation
    Person person = personController.createPerson(birthday, ssn, sex, null, Boolean.FALSE);
    Student student = studentController.createStudent(person, payload.getFirstName(), payload.getLastName(), // nickname
    null, // additionalInfo
    null, // studyTimeEnd
    null, // activityType
    null, // examinationType
    null, // educationalLevel
    null, // education
    null, // nationality
    null, // municipality
    null, // language
    null, // school
    null, studyProgramme, // curriculum
    null, // previousStudies
    null, // studyStartDate
    new Date(), // studyEndDate
    null, // studyEndReason
    null, // studyEndText
    null);
    personController.updatePersonDefaultUser(person, student);
    userController.addUserEmail(student, defaults.getStudentDefaultContactType(), address, Boolean.TRUE);
    payload.setIdentifier(student.getId().toString());
    return Response.ok(payload).build();
}
Also used : StudentGroupUser(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser) User(fi.otavanopisto.pyramus.domainmodel.users.User) StudyProgramme(fi.otavanopisto.pyramus.domainmodel.base.StudyProgramme) Sex(fi.otavanopisto.pyramus.domainmodel.students.Sex) DefaultsDAO(fi.otavanopisto.pyramus.dao.base.DefaultsDAO) CourseStudent(fi.otavanopisto.pyramus.domainmodel.courses.CourseStudent) StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) StudyProgrammeDAO(fi.otavanopisto.pyramus.dao.base.StudyProgrammeDAO) Date(java.util.Date) UserEmailInUseException(fi.otavanopisto.pyramus.framework.UserEmailInUseException) Defaults(fi.otavanopisto.pyramus.domainmodel.base.Defaults) SimpleDateFormat(java.text.SimpleDateFormat) Person(fi.otavanopisto.pyramus.domainmodel.base.Person) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) POST(javax.ws.rs.POST)

Example 8 with Defaults

use of fi.otavanopisto.pyramus.domainmodel.base.Defaults in project pyramus by otavanopisto.

the class MuikkuRESTService method updateStudent.

@Path("/students/{IDENTIFIER}")
@PUT
@RESTPermit(MuikkuPermissions.MUIKKU_UPDATE_STUDENT)
public Response updateStudent(@Context HttpServletRequest request, @PathParam("IDENTIFIER") String identifier, StudentPayload payload) {
    // Prerequisites
    DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO();
    Defaults defaults = defaultsDAO.getDefaults();
    if (defaults.getStudentDefaultContactType() == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("studentDefaultContactType not set in Defaults").build();
    }
    User loggedUser = sessionController.getUser();
    if (loggedUser.getOrganization() == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Current user lacks organization").build();
    }
    // Find student
    Long studentId = Long.valueOf(payload.getIdentifier());
    Student student = studentController.findStudentById(studentId);
    if (student == null) {
        return Response.status(Status.NOT_FOUND).entity(String.format("No student for identifier %s", identifier)).build();
    }
    if (student.getPerson().getDefaultUser() == null) {
        return Response.status(Status.BAD_REQUEST).entity(String.format("Student %s has no default user set", identifier)).build();
    }
    if (!studentId.equals(student.getPerson().getDefaultUser().getId())) {
        return Response.status(Status.BAD_REQUEST).entity(String.format("Student %s is not set as default", identifier)).build();
    }
    if (!StringUtils.equals(payload.getIdentifier(), identifier)) {
        return Response.status(Status.BAD_REQUEST).entity("Payload identifier doesn't match path identifier").build();
    }
    if (StringUtils.isAnyBlank(payload.getFirstName(), payload.getLastName(), payload.getEmail())) {
        return Response.status(Status.BAD_REQUEST).entity("Empty fields in payload").build();
    }
    Sex sex = null;
    if (!StringUtils.isBlank(payload.getGender())) {
        try {
            sex = Sex.valueOf(payload.getGender());
        } catch (IllegalArgumentException e) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid payload gender %s", payload.getGender())).build();
        }
    }
    if (!UserUtils.canAccessOrganization(sessionController.getUser(), student.getStudyProgramme().getOrganization())) {
        return Response.status(Status.BAD_REQUEST).entity("No study programme access").build();
    }
    if (StringUtils.isBlank(payload.getStudyProgrammeIdentifier()) || !NumberUtils.isNumber(payload.getStudyProgrammeIdentifier())) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid payload study programme").build();
    }
    StudyProgrammeDAO studyProgrammeDAO = DAOFactory.getInstance().getStudyProgrammeDAO();
    StudyProgramme studyProgramme = studyProgrammeDAO.findById(Long.valueOf(payload.getStudyProgrammeIdentifier()));
    if (!UserUtils.canAccessOrganization(sessionController.getUser(), studyProgramme.getOrganization())) {
        return Response.status(Status.BAD_REQUEST).entity("No study programme access").build();
    }
    // Birthday generation if SSN present
    Date birthday = null;
    if (!StringUtils.isBlank(payload.getSsn())) {
        try {
            birthday = new SimpleDateFormat("ddMMyy").parse(payload.getSsn().substring(0, 6));
        } catch (Exception e) {
            return Response.status(Status.BAD_REQUEST).entity("Invalid payload SSN").build();
        }
    }
    // Check that email and (possible) SSN belong to the student being edited
    String address = StringUtils.trim(StringUtils.lowerCase(payload.getEmail()));
    boolean emailAvailable = commonController.isEmailAvailable(address);
    if (!emailAvailable) {
        Person person = personController.findUniquePersonByEmail(address);
        if (person != null && !person.getId().equals(student.getPerson().getId())) {
            return Response.status(Status.CONFLICT).entity(getMessage(request.getLocale(), "error.emailInUse")).build();
        }
    }
    String ssn = null;
    if (!StringUtils.isBlank(payload.getSsn())) {
        ssn = StringUtils.upperCase(payload.getSsn());
        Person person = personController.findBySsn(ssn);
        if (person != null && !person.getId().equals(student.getPerson().getId())) {
            return Response.status(Status.CONFLICT).entity(getMessage(request.getLocale(), "error.ssnInUse")).build();
        }
    }
    // Update
    Person person = student.getPerson();
    person = personController.updatePerson(person, birthday, ssn, sex, person.getBasicInfo(), person.getSecureInfo());
    student = studentController.updateStudent(student, payload.getFirstName(), payload.getLastName(), student.getNickname(), student.getAdditionalInfo(), student.getStudyTimeEnd(), student.getActivityType(), student.getExaminationType(), student.getEducationalLevel(), student.getEducation(), student.getNationality(), student.getMunicipality(), student.getLanguage(), student.getSchool(), student.getCurriculum(), student.getPreviousStudies(), student.getStudyStartDate(), student.getStudyEndDate(), student.getStudyEndReason(), student.getStudyEndText());
    if (!student.getStudyProgramme().getId().equals(studyProgramme.getId())) {
        student = studentController.updateStudyProgramme(student, studyProgramme);
    }
    if (emailAvailable) {
        EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO();
        Email existingEmail = emailDAO.findByContactInfoAndDefaultAddress(student.getContactInfo(), Boolean.TRUE);
        if (existingEmail != null) {
            emailDAO.delete(existingEmail);
        }
        userController.addUserEmail(student, defaults.getStudentDefaultContactType(), address, Boolean.TRUE);
    }
    return Response.ok(payload).build();
}
Also used : StudentGroupUser(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser) User(fi.otavanopisto.pyramus.domainmodel.users.User) Email(fi.otavanopisto.pyramus.domainmodel.base.Email) StudyProgramme(fi.otavanopisto.pyramus.domainmodel.base.StudyProgramme) Sex(fi.otavanopisto.pyramus.domainmodel.students.Sex) DefaultsDAO(fi.otavanopisto.pyramus.dao.base.DefaultsDAO) CourseStudent(fi.otavanopisto.pyramus.domainmodel.courses.CourseStudent) StudentGroupStudent(fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) StudyProgrammeDAO(fi.otavanopisto.pyramus.dao.base.StudyProgrammeDAO) Date(java.util.Date) UserEmailInUseException(fi.otavanopisto.pyramus.framework.UserEmailInUseException) EmailDAO(fi.otavanopisto.pyramus.dao.base.EmailDAO) Defaults(fi.otavanopisto.pyramus.domainmodel.base.Defaults) SimpleDateFormat(java.text.SimpleDateFormat) Person(fi.otavanopisto.pyramus.domainmodel.base.Person) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) PUT(javax.ws.rs.PUT)

Example 9 with Defaults

use of fi.otavanopisto.pyramus.domainmodel.base.Defaults in project pyramus by otavanopisto.

the class DefaultsDAO method updateInitialCourseParticipationType.

public Defaults updateInitialCourseParticipationType(CourseParticipationType initialCourseParticipationType) {
    EntityManager entityManager = getEntityManager();
    Defaults defaults = getDefaults();
    if (defaults == null) {
        defaults = new Defaults();
        defaults.setId(1l);
    }
    defaults.setInitialCourseParticipationType(initialCourseParticipationType);
    entityManager.persist(defaults);
    return defaults;
}
Also used : EntityManager(javax.persistence.EntityManager) Defaults(fi.otavanopisto.pyramus.domainmodel.base.Defaults)

Example 10 with Defaults

use of fi.otavanopisto.pyramus.domainmodel.base.Defaults in project pyramus by otavanopisto.

the class DefaultsDAO method updateDefaultInitialCourseState.

public Defaults updateDefaultInitialCourseState(CourseState initialCourseState) {
    EntityManager entityManager = getEntityManager();
    Defaults defaults = getDefaults();
    if (defaults == null) {
        defaults = new Defaults();
        defaults.setId(1l);
    }
    defaults.setInitialCourseState(initialCourseState);
    entityManager.persist(defaults);
    return defaults;
}
Also used : EntityManager(javax.persistence.EntityManager) Defaults(fi.otavanopisto.pyramus.domainmodel.base.Defaults)

Aggregations

Defaults (fi.otavanopisto.pyramus.domainmodel.base.Defaults)13 DefaultsDAO (fi.otavanopisto.pyramus.dao.base.DefaultsDAO)9 CourseStudent (fi.otavanopisto.pyramus.domainmodel.courses.CourseStudent)5 Student (fi.otavanopisto.pyramus.domainmodel.students.Student)5 Person (fi.otavanopisto.pyramus.domainmodel.base.Person)4 User (fi.otavanopisto.pyramus.domainmodel.users.User)4 StudyProgrammeDAO (fi.otavanopisto.pyramus.dao.base.StudyProgrammeDAO)3 CourseDAO (fi.otavanopisto.pyramus.dao.courses.CourseDAO)3 CourseStudentDAO (fi.otavanopisto.pyramus.dao.courses.CourseStudentDAO)3 StudentDAO (fi.otavanopisto.pyramus.dao.students.StudentDAO)3 Room (fi.otavanopisto.pyramus.domainmodel.accommodation.Room)3 Course (fi.otavanopisto.pyramus.domainmodel.courses.Course)3 StudentGroupUser (fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser)3 RESTPermit (fi.otavanopisto.pyramus.rest.annotation.RESTPermit)3 BigDecimal (java.math.BigDecimal)3 Currency (java.util.Currency)3 Date (java.util.Date)3 Path (javax.ws.rs.Path)3 SmvcRuntimeException (fi.internetix.smvc.SmvcRuntimeException)2 StaffMemberDAO (fi.otavanopisto.pyramus.dao.users.StaffMemberDAO)2