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