gt:     Long organizationId = entity.getOrganizationId();
context: package fi.otavanopisto.pyramus.rest;
  
  import java.time.OffsetDateTime;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.Date;
  import java.util.HashSet;
  import java.util.List;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  import javax.ejb.Stateful;
  import javax.enterprise.context.RequestScoped;
  import javax.inject.Inject;
  import javax.ws.rs.Consumes;
  import javax.ws.rs.DELETE;
  import javax.ws.rs.DefaultValue;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.PUT;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  import javax.ws.rs.QueryParam;
  import javax.ws.rs.core.CacheControl;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.EntityTag;
  import javax.ws.rs.core.Request;
  import javax.ws.rs.core.Response;
  import javax.ws.rs.core.Response.ResponseBuilder;
  import javax.ws.rs.core.Response.Status;
  
  import org.apache.commons.codec.digest.DigestUtils;
  import org.apache.commons.collections.CollectionUtils;
  import org.apache.commons.lang3.StringUtils;
  import org.apache.commons.lang3.time.DateUtils;
  
  import fi.otavanopisto.pyramus.dao.base.OrganizationDAO;
  import fi.otavanopisto.pyramus.domainmodel.Archived;
  import fi.otavanopisto.pyramus.domainmodel.base.ContactURL;
  import fi.otavanopisto.pyramus.domainmodel.base.ContactURLType;
  import fi.otavanopisto.pyramus.domainmodel.base.Curriculum;
  import fi.otavanopisto.pyramus.domainmodel.base.EducationSubtype;
  import fi.otavanopisto.pyramus.domainmodel.base.EducationType;
  import fi.otavanopisto.pyramus.domainmodel.base.Email;
  import fi.otavanopisto.pyramus.domainmodel.base.Language;
  import fi.otavanopisto.pyramus.domainmodel.base.Municipality;
  import fi.otavanopisto.pyramus.domainmodel.base.Nationality;
  import fi.otavanopisto.pyramus.domainmodel.base.Organization;
  import fi.otavanopisto.pyramus.domainmodel.base.Person;
  import fi.otavanopisto.pyramus.domainmodel.base.School;
  import fi.otavanopisto.pyramus.domainmodel.base.StudyProgramme;
  import fi.otavanopisto.pyramus.domainmodel.base.StudyProgrammeCategory;
  import fi.otavanopisto.pyramus.domainmodel.base.VariableType;
  import fi.otavanopisto.pyramus.domainmodel.courses.Course;
  import fi.otavanopisto.pyramus.domainmodel.courses.CourseStudent;
  import fi.otavanopisto.pyramus.domainmodel.grading.CourseAssessment;
  import fi.otavanopisto.pyramus.domainmodel.grading.CourseAssessmentRequest;
  import fi.otavanopisto.pyramus.domainmodel.grading.CreditLink;
  import fi.otavanopisto.pyramus.domainmodel.grading.Grade;
  import fi.otavanopisto.pyramus.domainmodel.grading.TransferCredit;
  import fi.otavanopisto.pyramus.domainmodel.students.Student;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentActivityType;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentContactLogEntry;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentContactLogEntryType;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentEducationalLevel;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentExaminationType;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentGroup;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentGroupStudent;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentGroupUser;
  import fi.otavanopisto.pyramus.domainmodel.students.StudentStudyEndReason;
  import fi.otavanopisto.pyramus.domainmodel.users.StaffMember;
  import fi.otavanopisto.pyramus.domainmodel.users.User;
  import fi.otavanopisto.pyramus.domainmodel.users.UserVariable;
  import fi.otavanopisto.pyramus.domainmodel.users.UserVariableKey;
  import fi.otavanopisto.pyramus.domainmodel.worklist.WorklistItemTemplate;
  import fi.otavanopisto.pyramus.framework.UserUtils;
  import fi.otavanopisto.pyramus.rest.annotation.RESTPermit;
  import fi.otavanopisto.pyramus.rest.annotation.RESTPermit.Handling;
  import fi.otavanopisto.pyramus.rest.annotation.RESTPermit.Style;
  import fi.otavanopisto.pyramus.rest.controller.AssessmentController;
  import fi.otavanopisto.pyramus.rest.controller.CommonController;
  import fi.otavanopisto.pyramus.rest.controller.CourseController;
  import fi.otavanopisto.pyramus.rest.controller.CurriculumController;
  import fi.otavanopisto.pyramus.rest.controller.LanguageController;
  import fi.otavanopisto.pyramus.rest.controller.MatriculationEligibilityController;
  import fi.otavanopisto.pyramus.rest.controller.MunicipalityController;
  import fi.otavanopisto.pyramus.rest.controller.NationalityController;
  import fi.otavanopisto.pyramus.rest.controller.PersonController;
  import fi.otavanopisto.pyramus.rest.controller.SchoolController;
  import fi.otavanopisto.pyramus.rest.controller.StudentActivityTypeController;
  import fi.otavanopisto.pyramus.rest.controller.StudentContactLogEntryController;
  import fi.otavanopisto.pyramus.rest.controller.StudentController;
  import fi.otavanopisto.pyramus.rest.controller.StudentEducationalLevelController;
  import fi.otavanopisto.pyramus.rest.controller.StudentExaminationTypeController;
  import fi.otavanopisto.pyramus.rest.controller.StudentGroupController;
  import fi.otavanopisto.pyramus.rest.controller.StudentMatriculationEligibilityResult;
  import fi.otavanopisto.pyramus.rest.controller.StudentStudyEndReasonController;
  import fi.otavanopisto.pyramus.rest.controller.StudyProgrammeCategoryController;
  import fi.otavanopisto.pyramus.rest.controller.StudyProgrammeController;
  import fi.otavanopisto.pyramus.rest.controller.UserController;
  import fi.otavanopisto.pyramus.rest.controller.WorklistController;
  import fi.otavanopisto.pyramus.rest.controller.permissions.CourseAssessmentPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.LanguagePermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.MunicipalityPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.NationalityPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.PersonPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudentActivityTypePermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudentContactLogEntryPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudentEducationalLevelPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudentExaminationTypePermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudentGroupPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudentPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudentStudyEndReasonPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudyProgrammeCategoryPermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.StudyProgrammePermissions;
  import fi.otavanopisto.pyramus.rest.controller.permissions.UserPermissions;
  import fi.otavanopisto.pyramus.rest.model.CourseActivity;
  import fi.otavanopisto.pyramus.rest.model.StudentCourseStats;
  import fi.otavanopisto.pyramus.rest.model.StudentMatriculationEligibility;
  import fi.otavanopisto.pyramus.rest.model.worklist.CourseBillingRestModel;
  import fi.otavanopisto.pyramus.rest.security.RESTSecurity;
  import fi.otavanopisto.pyramus.rest.util.ISO8601Timestamp;
  import fi.otavanopisto.pyramus.rest.util.PyramusConsts;
  import fi.otavanopisto.pyramus.security.impl.SessionController;
  import fi.otavanopisto.pyramus.security.impl.permissions.OrganizationPermissions;
  import fi.otavanopisto.pyramus.tor.StudentTOR;
  import fi.otavanopisto.pyramus.tor.StudentTORController;
  import fi.otavanopisto.pyramus.tor.TORCourseLengthUnit;
  import fi.otavanopisto.security.LoggedIn;
  
  @Path("/students")
  @Produces("application/json")
  @Consumes("application/json")
  @Stateful
  @RequestScoped
  public class StudentRESTService extends AbstractRESTService {
  
    @Inject
    private Logger logger;
    
    @Inject
    private RESTSecurity restSecurity;
  
    @Inject
    private UserController userController;
  
    @Inject
    private WorklistController worklistController;
  
    @Inject
    private CommonController commonController;
  
    @Inject
    private StudentController studentController;
  
    @Inject
    private LanguageController languageController;
  
    @Inject
    private MunicipalityController municipalityController;
  
    @Inject
    private NationalityController nationalityController;
  
    @Inject
    private StudentActivityTypeController studentActivityTypeController;
  
    @Inject
    private StudentEducationalLevelController studentEducationalLevelController;
  
    @Inject
    private StudentExaminationTypeController studentExaminationTypeController;
  
    @Inject
    private StudyProgrammeCategoryController studyProgrammeCategoryController;
  
    @Inject
    private StudyProgrammeController studyProgrammeController;
  
    @Inject
    private StudentGroupController studentGroupController;
  
    @Inject
    private PersonController personController;
  
    @Inject
    private StudentStudyEndReasonController studentStudyEndReasonController;
  
    @Inject
    private StudentContactLogEntryController studentContactLogEntryController;
  
    @Inject
    private SchoolController schoolController;
  
    @Inject
    private CourseController courseController;
    
    @Inject
    private CurriculumController curriculumController;
  
    @Inject
    private SessionController sessionController;
  
    @Inject
    private MatriculationEligibilityController matriculationEligibilityController;
    
    @Inject
    private ObjectFactory objectFactory;
    
    @Inject
    private AssessmentController assessmentController;
  
    @Inject
    private OrganizationDAO organizationDAO;
    
    @Path("/languages")
    @POST
    @RESTPermit(LanguagePermissions.CREATE_LANGUAGE)
    public Response createLanguage(fi.otavanopisto.pyramus.rest.model.Language entity) {
      String name = entity.getName();
      String code = entity.getCode();
  
      if (StringUtils.isBlank(name) || StringUtils.isBlank(code)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok(objectFactory.createModel(languageController.createLanguage(name, code))).build();
    }
  
    @Path("/languages")
    @GET
    @RESTPermit(LanguagePermissions.LIST_LANGUAGES)
    public Response listLanguages(@DefaultValue("false") @QueryParam("filterArchived") boolean filterArchived) {
      List<Language> languages;
      if (filterArchived) {
        languages = languageController.listUnarchivedLanguages();
      } else {
        languages = languageController.listLanguages();
      }
  
      if (languages.isEmpty()) {
        return Response.noContent().build();
      }
  
      return Response.ok(objectFactory.createModel(languages)).build();
    }
  
    @Path("/languages/{ID:[0-9]*}")
    @GET
    @RESTPermit(LanguagePermissions.FIND_LANGUAGE)
    public Response findLanguageById(@PathParam("ID") Long id) {
      Language language = languageController.findLanguageById(id);
      if (language == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (language.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      return Response.ok(objectFactory.createModel(language)).build();
    }
  
    @Path("/languages/{ID:[0-9]*}")
    @PUT
    @RESTPermit(LanguagePermissions.UPDATE_LANGUAGE)
    public Response updateLanguage(@PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.Language entity) {
      Language language = languageController.findLanguageById(id);
      if (language == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (language.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      String name = entity.getName();
      String code = entity.getCode();
  
      if (StringUtils.isBlank(name) || StringUtils.isBlank(code)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok().entity(objectFactory.createModel(languageController.updateLanguage(language, name, code))).build();
    }
  
    @Path("/languages/{ID:[0-9]*}")
    @DELETE
    @RESTPermit(LanguagePermissions.DELETE_LANGUAGE)
    public Response deleteLanguage(@PathParam("ID") Long id, @DefaultValue("false") @QueryParam("permanent") Boolean permanent) {
      Language language = languageController.findLanguageById(id);
      if (language == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (permanent) {
        languageController.deleteLanguage(language);
      } else {
        languageController.archiveLanguage(language, sessionController.getUser());
      }
  
      return Response.noContent().build();
    }
  
    @Path("/municipalities")
    @POST
    @RESTPermit(MunicipalityPermissions.CREATE_MUNICIPALITY)
    public Response createMunicipality(fi.otavanopisto.pyramus.rest.model.Municipality entity) {
      String name = entity.getName();
      String code = entity.getCode();
  
      if (StringUtils.isBlank(name) || StringUtils.isBlank(code)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok(objectFactory.createModel(municipalityController.createMunicipality(name, code))).build();
    }
  
    @Path("/municipalities")
    @GET
    @RESTPermit(MunicipalityPermissions.LIST_MUNICIPALITIES)
    public Response listMunicipalities(@DefaultValue("false") @QueryParam("filterArchived") boolean filterArchived) {
      List<Municipality> municipalities;
      if (filterArchived) {
        municipalities = municipalityController.listUnarchivedMunicipalities();
      } else {
        municipalities = municipalityController.listMunicipalities();
      }
  
      if (municipalities.isEmpty()) {
        return Response.noContent().build();
      }
  
      return Response.ok(objectFactory.createModel(municipalities)).build();
    }
  
    @Path("/municipalities/{ID:[0-9]*}")
    @GET
    @RESTPermit(MunicipalityPermissions.FIND_MUNICIPALITY)
    public Response findMunicipalityById(@PathParam("ID") Long id) {
      Municipality municipality = municipalityController.findMunicipalityById(id);
      if (municipality == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (municipality.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      return Response.ok(objectFactory.createModel(municipality)).build();
    }
  
    @Path("/municipalities/{ID:[0-9]*}")
    @PUT
    @RESTPermit(MunicipalityPermissions.UPDATE_MUNICIPALITY)
    public Response updateMunicipality(@PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.Municipality entity) {
      Municipality municipality = municipalityController.findMunicipalityById(id);
      if (municipality == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (municipality.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      String name = entity.getName();
      String code = entity.getCode();
  
      if (StringUtils.isBlank(name) || StringUtils.isBlank(code)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok().entity(objectFactory.createModel(municipalityController.updateMunicipality(municipality, name, code))).build();
    }
  
    @Path("/municipalities/{ID:[0-9]*}")
    @DELETE
    @RESTPermit(MunicipalityPermissions.DELETE_MUNICIPALITY)
    public Response deleteMunicipality(@PathParam("ID") Long id, @DefaultValue("false") @QueryParam("permanent") Boolean permanent) {
      Municipality municipality = municipalityController.findMunicipalityById(id);
      if (municipality == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (permanent) {
        municipalityController.deleteMunicipality(municipality);
      } else {
        municipalityController.archiveMunicipality(municipality, sessionController.getUser());
      }
  
      return Response.noContent().build();
    }
  
    @Path("/nationalities")
    @POST
    @RESTPermit(NationalityPermissions.CREATE_NATIONALITY)
    public Response createNationality(fi.otavanopisto.pyramus.rest.model.Nationality entity) {
      String name = entity.getName();
      String code = entity.getCode();
  
      if (StringUtils.isBlank(name) || StringUtils.isBlank(code)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok(objectFactory.createModel(nationalityController.createNationality(name, code))).build();
    }
  
    @Path("/nationalities")
    @GET
    @RESTPermit(NationalityPermissions.LIST_NATIONALITIES)
    public Response listNationalities(@DefaultValue("false") @QueryParam("filterArchived") boolean filterArchived) {
      List<Nationality> nationalities;
      if (filterArchived) {
        nationalities = nationalityController.listUnarchivedNationalities();
      } else {
        nationalities = nationalityController.listNationalities();
      }
  
      if (nationalities.isEmpty()) {
        return Response.noContent().build();
      }
  
      return Response.ok(objectFactory.createModel(nationalities)).build();
    }
  
    @Path("/nationalities/{ID:[0-9]*}")
    @GET
    @RESTPermit(NationalityPermissions.FIND_NATIONALITY)
    public Response findNationalityById(@PathParam("ID") Long id) {
      Nationality nationality = nationalityController.findNationalityById(id);
      if (nationality == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (nationality.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      return Response.ok(objectFactory.createModel(nationality)).build();
    }
  
    @Path("/nationalities/{ID:[0-9]*}")
    @PUT
    @RESTPermit(NationalityPermissions.UPDATE_NATIONALITY)
    public Response updateNationality(@PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.Nationality entity) {
      Nationality nationality = nationalityController.findNationalityById(id);
      if (nationality == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (nationality.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      String name = entity.getName();
      String code = entity.getCode();
  
      if (StringUtils.isBlank(name) || StringUtils.isBlank(code)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok().entity(objectFactory.createModel(nationalityController.updateNationality(nationality, name, code))).build();
    }
  
    @Path("/nationalities/{ID:[0-9]*}")
    @DELETE
    @RESTPermit(NationalityPermissions.DELETE_NATIONALITY)
    public Response deleteNationality(@PathParam("ID") Long id, @DefaultValue("false") @QueryParam("permanent") Boolean permanent) {
      Nationality nationality = nationalityController.findNationalityById(id);
      if (nationality == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (permanent) {
        nationalityController.deleteNationality(nationality);
      } else {
        nationalityController.archiveNationality(nationality, sessionController.getUser());
      }
  
      return Response.noContent().build();
    }
  
    @Path("/activityTypes")
    @POST
    @RESTPermit(StudentActivityTypePermissions.CREATE_STUDENTACTIVITYTYPE)
    public Response createStudentActivityType(fi.otavanopisto.pyramus.rest.model.StudentActivityType entity) {
      String name = entity.getName();
  
      if (StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok(objectFactory.createModel(studentActivityTypeController.createStudentActivityType(name))).build();
    }
  
    @Path("/activityTypes")
    @GET
    @RESTPermit(StudentActivityTypePermissions.LIST_STUDENTACTIVITYTYPES)
    public Response listStudentActivityTypes(@DefaultValue("false") @QueryParam("filterArchived") boolean filterArchived) {
      List<StudentActivityType> studentActivityTypes;
      if (filterArchived) {
        studentActivityTypes = studentActivityTypeController.listUnarchivedStudentActivityTypes();
      } else {
        studentActivityTypes = studentActivityTypeController.listStudentActivityTypes();
      }
  
      if (studentActivityTypes.isEmpty()) {
        return Response.noContent().build();
      }
  
      return Response.ok(objectFactory.createModel(studentActivityTypes)).build();
    }
  
    @Path("/activityTypes/{ID:[0-9]*}")
    @GET
    @RESTPermit(StudentActivityTypePermissions.FIND_STUDENTACTIVITYTYPE)
    public Response findStudentActivityTypeById(@PathParam("ID") Long id) {
      StudentActivityType studentActivityType = studentActivityTypeController.findStudentActivityTypeById(id);
      if (studentActivityType == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (studentActivityType.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      return Response.ok(objectFactory.createModel(studentActivityType)).build();
    }
  
    @Path("/activityTypes/{ID:[0-9]*}")
    @PUT
    @RESTPermit(StudentActivityTypePermissions.UPDATE_STUDENTACTIVITYTYPE)
    public Response updateStudentActivityType(@PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.StudentActivityType entity) {
      StudentActivityType studentActivityType = studentActivityTypeController.findStudentActivityTypeById(id);
      if (studentActivityType == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (studentActivityType.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      String name = entity.getName();
  
      if (StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok().entity(objectFactory.createModel(studentActivityTypeController.updateStudentActivityType(studentActivityType, name))).build();
    }
  
    @Path("/activityTypes/{ID:[0-9]*}")
    @DELETE
    @RESTPermit(StudentActivityTypePermissions.DELETE_STUDENTACTIVITYTYPE)
    public Response deleteStudentActivityType(@PathParam("ID") Long id, @DefaultValue("false") @QueryParam("permanent") Boolean permanent) {
      StudentActivityType studentActivityType = studentActivityTypeController.findStudentActivityTypeById(id);
      if (studentActivityType == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (permanent) {
        studentActivityTypeController.deleteStudentActivityType(studentActivityType);
      } else {
        studentActivityTypeController.archiveStudentActivityType(studentActivityType, sessionController.getUser());
      }
  
      return Response.noContent().build();
    }
  
    @Path("/educationalLevels")
    @POST
    @RESTPermit(StudentEducationalLevelPermissions.CREATE_STUDENTEDUCATIONALLEVEL)
    public Response createStudentEducationalLevel(fi.otavanopisto.pyramus.rest.model.StudentEducationalLevel entity) {
      String name = entity.getName();
  
      if (StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok(objectFactory.createModel(studentEducationalLevelController.createStudentEducationalLevel(name))).build();
    }
  
    @Path("/educationalLevels")
    @GET
    @RESTPermit(StudentEducationalLevelPermissions.LIST_STUDENTEDUCATIONALLEVELS)
    public Response listStudentEducationalLevels(@DefaultValue("false") @QueryParam("filterArchived") boolean filterArchived) {
      List<StudentEducationalLevel> studentEducationalLevels;
      if (filterArchived) {
        studentEducationalLevels = studentEducationalLevelController.listUnarchivedStudentEducationalLevels();
      } else {
        studentEducationalLevels = studentEducationalLevelController.listStudentEducationalLevels();
      }
  
      if (studentEducationalLevels.isEmpty()) {
        return Response.noContent().build();
      }
  
      return Response.ok(objectFactory.createModel(studentEducationalLevels)).build();
    }
  
    @Path("/educationalLevels/{ID:[0-9]*}")
    @GET
    @RESTPermit(StudentEducationalLevelPermissions.FIND_STUDENTEDUCATIONALLEVEL)
    public Response findStudentEducationalLevelById(@PathParam("ID") Long id) {
      StudentEducationalLevel studentEducationalLevel = studentEducationalLevelController.findStudentEducationalLevelById(id);
      if (studentEducationalLevel == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (studentEducationalLevel.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      return Response.ok(objectFactory.createModel(studentEducationalLevel)).build();
    }
  
    @Path("/educationalLevels/{ID:[0-9]*}")
    @PUT
    @RESTPermit(StudentEducationalLevelPermissions.UPDATE_STUDENTEDUCATIONALLEVEL)
    public Response updateStudentEducationalLevel(@PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.StudentEducationalLevel entity) {
      StudentEducationalLevel studentEducationalLevel = studentEducationalLevelController.findStudentEducationalLevelById(id);
      if (studentEducationalLevel == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (studentEducationalLevel.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      String name = entity.getName();
  
      if (StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok().entity(objectFactory.createModel(studentEducationalLevelController.updateStudentEducationalLevel(studentEducationalLevel, name)))
          .build();
    }
  
    @Path("/educationalLevels/{ID:[0-9]*}")
    @DELETE
    @RESTPermit(StudentEducationalLevelPermissions.DELETE_STUDENTEDUCATIONALLEVEL)
    public Response deleteStudentEducationalLevel(@PathParam("ID") Long id, @DefaultValue("false") @QueryParam("permanent") Boolean permanent) {
      StudentEducationalLevel studentEducationalLevel = studentEducationalLevelController.findStudentEducationalLevelById(id);
      if (studentEducationalLevel == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (permanent) {
        studentEducationalLevelController.deleteStudentEducationalLevel(studentEducationalLevel);
      } else {
        studentEducationalLevelController.archiveStudentEducationalLevel(studentEducationalLevel, sessionController.getUser());
      }
  
      return Response.noContent().build();
    }
  
    @Path("/examinationTypes")
    @POST
    @RESTPermit(StudentExaminationTypePermissions.CREATE_STUDENTEXAMINATIONTYPE)
    public Response createStudentExaminationType(fi.otavanopisto.pyramus.rest.model.StudentExaminationType entity) {
      String name = entity.getName();
  
      if (StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok(objectFactory.createModel(studentExaminationTypeController.createStudentExaminationType(name))).build();
    }
  
    @Path("/examinationTypes")
    @GET
    @RESTPermit(StudentExaminationTypePermissions.LIST_STUDENTEXAMINATIONTYPES)
    public Response listStudentExaminationTypes(@DefaultValue("false") @QueryParam("filterArchived") boolean filterArchived) {
      List<StudentExaminationType> studentExaminationTypes;
      if (filterArchived) {
        studentExaminationTypes = studentExaminationTypeController.listUnarchivedStudentExaminationTypes();
      } else {
        studentExaminationTypes = studentExaminationTypeController.listStudentExaminationTypes();
      }
  
      if (studentExaminationTypes.isEmpty()) {
        return Response.noContent().build();
      }
  
      return Response.ok(objectFactory.createModel(studentExaminationTypes)).build();
    }
  
    @Path("/examinationTypes/{ID:[0-9]*}")
    @GET
    @RESTPermit(StudentExaminationTypePermissions.FIND_STUDENTEXAMINATIONTYPE)
    public Response findStudentExaminationTypeById(@PathParam("ID") Long id) {
      StudentExaminationType studentExaminationType = studentExaminationTypeController.findStudentExaminationTypeById(id);
      if (studentExaminationType == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (studentExaminationType.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      return Response.ok(objectFactory.createModel(studentExaminationType)).build();
    }
  
    @Path("/examinationTypes/{ID:[0-9]*}")
    @PUT
    @RESTPermit(StudentExaminationTypePermissions.UPDATE_STUDENTEXAMINATIONTYPE)
    public Response updateStudentExaminationType(@PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.StudentExaminationType entity) {
      StudentExaminationType studentExaminationType = studentExaminationTypeController.findStudentExaminationTypeById(id);
      if (studentExaminationType == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (studentExaminationType.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      String name = entity.getName();
  
      if (StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok().entity(objectFactory.createModel(studentExaminationTypeController.updateStudentExaminationType(studentExaminationType, name))).build();
    }
  
    @Path("/examinationTypes/{ID:[0-9]*}")
    @DELETE
    @RESTPermit(StudentExaminationTypePermissions.DELETE_STUDENTEXAMINATIONTYPE)
    public Response deleteStudentExaminationType(@PathParam("ID") Long id, @DefaultValue("false") @QueryParam("permanent") Boolean permanent) {
      StudentExaminationType studentExaminationType = studentExaminationTypeController.findStudentExaminationTypeById(id);
      if (studentExaminationType == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (permanent) {
        studentExaminationTypeController.deleteStudentExaminationType(studentExaminationType);
      } else {
        studentExaminationTypeController.archiveStudentExaminationType(studentExaminationType, sessionController.getUser());
      }
  
      return Response.noContent().build();
    }
  
    @Path("/studyProgrammeCategories")
    @POST
    @RESTPermit(StudyProgrammeCategoryPermissions.CREATE_STUDYPROGRAMMECATEGORY)
    public Response createStudyProgrammeCategory(fi.otavanopisto.pyramus.rest.model.StudyProgrammeCategory entity) {
      String name = entity.getName();
  
      if (StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      if (entity.getEducationTypeId() == null) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      EducationType educationType = commonController.findEducationTypeById(entity.getEducationTypeId());
      if (educationType == null) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok(objectFactory.createModel(studyProgrammeCategoryController.createStudyProgrammeCategory(name, educationType))).build();
    }
  
    @Path("/studyProgrammeCategories")
    @GET
    @RESTPermit(StudyProgrammeCategoryPermissions.LIST_STUDYPROGRAMMECATEGORIES)
    public Response listStudyProgrammeCategories(@DefaultValue("false") @QueryParam("filterArchived") boolean filterArchived) {
      List<StudyProgrammeCategory> studyProgrammeCategories;
      if (filterArchived) {
        studyProgrammeCategories = studyProgrammeCategoryController.listUnarchivedStudyProgrammeCategories();
      } else {
        studyProgrammeCategories = studyProgrammeCategoryController.listStudyProgrammeCategories();
      }
  
      if (studyProgrammeCategories.isEmpty()) {
        return Response.noContent().build();
      }
  
      return Response.ok(objectFactory.createModel(studyProgrammeCategories)).build();
    }
  
    @Path("/studyProgrammeCategories/{ID:[0-9]*}")
    @GET
    @RESTPermit(StudyProgrammeCategoryPermissions.FIND_STUDYPROGRAMMECATEGORY)
    public Response findStudyProgrammeCategoryById(@PathParam("ID") Long id) {
      StudyProgrammeCategory studyProgrammeCategory = studyProgrammeCategoryController.findStudyProgrammeCategoryById(id);
      if (studyProgrammeCategory == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (studyProgrammeCategory.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      return Response.ok(objectFactory.createModel(studyProgrammeCategory)).build();
    }
  
    @Path("/studyProgrammeCategories/{ID:[0-9]*}")
    @PUT
    @RESTPermit(StudyProgrammeCategoryPermissions.UPDATE_STUDYPROGRAMMECATEGORY)
    public Response updateStudyProgrammeCategory(@PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.StudyProgrammeCategory entity) {
      StudyProgrammeCategory studyProgrammeCategory = studyProgrammeCategoryController.findStudyProgrammeCategoryById(id);
      if (studyProgrammeCategory == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (studyProgrammeCategory.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      String name = entity.getName();
  
      if (StringUtils.isBlank(name)) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      if (entity.getEducationTypeId() == null) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      EducationType educationType = commonController.findEducationTypeById(entity.getEducationTypeId());
      if (educationType == null) {
        return Response.status(Status.BAD_REQUEST).build();
      }
  
      return Response.ok()
          .entity(objectFactory.createModel(studyProgrammeCategoryController.updateStudyProgrammeCategory(studyProgrammeCategory, name, educationType))).build();
    }
  
    @Path("/studyProgrammeCategories/{ID:[0-9]*}")
    @DELETE
    @RESTPermit(StudyProgrammeCategoryPermissions.DELETE_STUDYPROGRAMMECATEGORY)
    public Response deleteStudyProgrammeCategory(@PathParam("ID") Long id, @DefaultValue("false") @QueryParam("permanent") Boolean permanent) {
      StudyProgrammeCategory studyProgrammeCategory = studyProgrammeCategoryController.findStudyProgrammeCategoryById(id);
      if (studyProgrammeCategory == null) {
        return Response.status(Status.NOT_FOUND).build();
      }
  
      if (permanent) {
        studyProgrammeCategoryController.deleteStudyProgrammeCategory(studyProgrammeCategory);
      } else {
        studyProgrammeCategoryController.archiveStudyProgrammeCategory(studyProgrammeCategory, sessionController.getUser());
      }
  
      return Response.noContent().build();
    }
  
    @Path("/studyProgrammes")
    @POST
    @RESTPermit(StudyProgrammePermissions.CREATE_STUDYPROGRAMME)
    public Response createStudyProgramme(fi.otavanopisto.pyramus.rest.model.StudyProgramme entity) {
      String name = entity.getName();
      String code = entity.getCode();
      Long categoryId = entity.getCategoryId();
  