Search in sources :

Example 1 with CourseType

use of fi.otavanopisto.pyramus.domainmodel.courses.CourseType in project pyramus by otavanopisto.

the class CoursesService method createCourse.

public CourseEntity createCourse(@WebParam(name = "moduleId") Long moduleId, @WebParam(name = "name") String name, @WebParam(name = "nameExtension") String nameExtension, @WebParam(name = "subjectId") Long subjectId, @WebParam(name = "courseNumber") Integer courseNumber, @WebParam(name = "beginDate") Date beginDate, @WebParam(name = "endDate") Date endDate, @WebParam(name = "courseLength") Double courseLength, @WebParam(name = "courseLengthTimeUnitId") Long courseLengthTimeUnitId, @WebParam(name = "description") String description, @WebParam(name = "creatingUserId") Long creatingUserId) {
    StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
    CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
    ModuleDAO moduleDAO = DAOFactory.getInstance().getModuleDAO();
    CourseComponentDAO componentDAO = DAOFactory.getInstance().getCourseComponentDAO();
    CourseDescriptionDAO descriptionDAO = DAOFactory.getInstance().getCourseDescriptionDAO();
    CourseEducationTypeDAO educationTypeDAO = DAOFactory.getInstance().getCourseEducationTypeDAO();
    CourseEducationSubtypeDAO educationSubtypeDAO = DAOFactory.getInstance().getCourseEducationSubtypeDAO();
    EducationalTimeUnitDAO educationalTimeUnitDAO = DAOFactory.getInstance().getEducationalTimeUnitDAO();
    SubjectDAO subjectDAO = DAOFactory.getInstance().getSubjectDAO();
    DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO();
    Module module = moduleId == null ? null : moduleDAO.findById(moduleId);
    Subject subject = subjectId == null ? null : subjectDAO.findById(subjectId);
    EducationalTimeUnit courseLengthTimeUnit = courseLengthTimeUnitId == null ? null : educationalTimeUnitDAO.findById(courseLengthTimeUnitId);
    User creatingUser = userDAO.findById(creatingUserId);
    if (module != null) {
        name = name == null ? module.getName() : name;
        subject = subject == null ? module.getSubject() : subject;
        courseNumber = courseNumber == null ? module.getCourseNumber() : courseNumber;
        if (courseLength == null && module.getCourseLength() != null) {
            courseLength = module.getCourseLength().getUnits();
            courseLengthTimeUnit = module.getCourseLength().getUnit();
        }
        description = description == null ? module.getDescription() : description;
    }
    CourseState state = defaultsDAO.getDefaults().getInitialCourseState();
    CourseType type = null;
    // Course creation
    Course course = courseDAO.create(module, name, nameExtension, state, type, subject, courseNumber, beginDate, endDate, courseLength, courseLengthTimeUnit, null, null, null, null, null, null, description, null, null, null, null, creatingUser);
    validateEntity(course);
    if (module != null) {
        // Course Description copying from module to course
        descriptionDAO.copy(module, course);
        // Curriculums
        courseDAO.updateCurriculums(course, new HashSet<Curriculum>(module.getCurriculums()));
        // Components
        List<ModuleComponent> moduleComponents = module.getModuleComponents();
        if (moduleComponents != null) {
            for (ModuleComponent moduleComponent : moduleComponents) {
                EducationalLength educationalLength = moduleComponent.getLength();
                CourseComponent courseComponent = componentDAO.create(course, educationalLength == null ? null : educationalLength.getUnits(), educationalLength == null ? null : educationalLength.getUnit(), moduleComponent.getName(), moduleComponent.getDescription());
                validateEntity(courseComponent);
            }
        }
        // Education types
        List<CourseEducationType> typesInModule = module.getCourseEducationTypes();
        if (typesInModule != null) {
            for (CourseEducationType typeInModule : typesInModule) {
                CourseEducationType typeInCourse = educationTypeDAO.create(course, typeInModule.getEducationType());
                validateEntity(typeInCourse);
                // Education subtypes
                List<CourseEducationSubtype> subTypesInModule = typeInModule.getCourseEducationSubtypes();
                if (subTypesInModule != null) {
                    for (CourseEducationSubtype subtypeInModule : subTypesInModule) {
                        CourseEducationSubtype courseEducationSubtype = educationSubtypeDAO.create(typeInCourse, subtypeInModule.getEducationSubtype());
                        validateEntity(courseEducationSubtype);
                    }
                }
            }
        }
    }
    return EntityFactoryVault.buildFromDomainObject(course);
}
Also used : User(fi.otavanopisto.pyramus.domainmodel.users.User) SubjectDAO(fi.otavanopisto.pyramus.dao.base.SubjectDAO) CourseDAO(fi.otavanopisto.pyramus.dao.courses.CourseDAO) CourseDescriptionDAO(fi.otavanopisto.pyramus.dao.courses.CourseDescriptionDAO) ModuleDAO(fi.otavanopisto.pyramus.dao.modules.ModuleDAO) CourseComponent(fi.otavanopisto.pyramus.domainmodel.courses.CourseComponent) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) CourseStaffMemberDAO(fi.otavanopisto.pyramus.dao.courses.CourseStaffMemberDAO) CourseState(fi.otavanopisto.pyramus.domainmodel.courses.CourseState) CourseType(fi.otavanopisto.pyramus.domainmodel.courses.CourseType) Course(fi.otavanopisto.pyramus.domainmodel.courses.Course) EducationalLength(fi.otavanopisto.pyramus.domainmodel.base.EducationalLength) EducationalTimeUnit(fi.otavanopisto.pyramus.domainmodel.base.EducationalTimeUnit) CourseEducationType(fi.otavanopisto.pyramus.domainmodel.base.CourseEducationType) CourseComponentDAO(fi.otavanopisto.pyramus.dao.courses.CourseComponentDAO) EducationalTimeUnitDAO(fi.otavanopisto.pyramus.dao.base.EducationalTimeUnitDAO) CourseEducationSubtype(fi.otavanopisto.pyramus.domainmodel.base.CourseEducationSubtype) CourseEducationTypeDAO(fi.otavanopisto.pyramus.dao.base.CourseEducationTypeDAO) DefaultsDAO(fi.otavanopisto.pyramus.dao.base.DefaultsDAO) CourseEducationSubtypeDAO(fi.otavanopisto.pyramus.dao.base.CourseEducationSubtypeDAO) Subject(fi.otavanopisto.pyramus.domainmodel.base.Subject) ModuleComponent(fi.otavanopisto.pyramus.domainmodel.modules.ModuleComponent) Curriculum(fi.otavanopisto.pyramus.domainmodel.base.Curriculum) Module(fi.otavanopisto.pyramus.domainmodel.modules.Module)

Example 2 with CourseType

use of fi.otavanopisto.pyramus.domainmodel.courses.CourseType in project pyramus by otavanopisto.

the class CourseRESTService method updateCourse.

@Path("/courses/{ID:[0-9]*}")
@PUT
@RESTPermit(CoursePermissions.UPDATE_COURSE)
public Response updateCourse(@PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.Course courseEntity) {
    Course course = courseController.findCourseById(id);
    if (course == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (course.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (!course.getId().equals(courseEntity.getId())) {
        return Response.status(Status.BAD_REQUEST).entity("Cannot change entity id in update request").build();
    }
    if (courseEntity.getOrganizationId() == null) {
        return Response.status(Status.BAD_REQUEST).entity("organizationId is required").build();
    }
    String name = courseEntity.getName();
    String nameExtension = courseEntity.getNameExtension();
    CourseState state = courseController.findCourseStateById(courseEntity.getStateId());
    CourseType type = courseEntity.getTypeId() != null ? courseController.findCourseTypeById(courseEntity.getTypeId()) : null;
    Subject subject = null;
    if (courseEntity.getSubjectId() != null) {
        subject = commonController.findSubjectById(courseEntity.getSubjectId());
        if (subject == null) {
            return Response.status(Status.NOT_FOUND).entity("specified subject does not exist").build();
        }
    }
    Organization organization = organizationController.findById(courseEntity.getOrganizationId());
    if (organization == null) {
        return Response.status(Status.NOT_FOUND).entity(String.format("Organization with id %d not found", courseEntity.getOrganizationId())).build();
    }
    User user = sessionController.getUser();
    if ((course.getOrganization() != null) && !UserUtils.canAccessOrganization(user, course.getOrganization())) {
        logger.warning(String.format("User %d has no access to organization %d", user.getId(), course.getOrganization().getId()));
        return Response.status(Status.FORBIDDEN).build();
    } else if ((course.getOrganization() == null) && !UserUtils.canAccessAllOrganizations(user)) {
        logger.warning(String.format("User %d has cannot access course %d because it has no organization.", user.getId(), course.getId()));
        return Response.status(Status.FORBIDDEN).build();
    }
    if (!UserUtils.canAccessOrganization(user, organization)) {
        logger.warning(String.format("User %d has no access to organization %d", user.getId(), organization.getId()));
        return Response.status(Status.FORBIDDEN).build();
    }
    Integer courseNumber = courseEntity.getCourseNumber();
    OffsetDateTime beginDate = courseEntity.getBeginDate();
    OffsetDateTime endDate = courseEntity.getEndDate();
    Double courseLength = courseEntity.getLength();
    EducationalTimeUnit courseLengthTimeUnit = null;
    if (courseLength != null) {
        if (courseEntity.getLengthUnitId() == null) {
            return Response.status(Status.BAD_REQUEST).entity("length unit is missing").build();
        }
        courseLengthTimeUnit = commonController.findEducationalTimeUnitById(courseEntity.getLengthUnitId());
        if (courseLengthTimeUnit == null) {
            return Response.status(Status.BAD_REQUEST).entity("length unit is invalid").build();
        }
    }
    Double distanceTeachingDays = courseEntity.getDistanceTeachingDays();
    Double localTeachingDays = courseEntity.getLocalTeachingDays();
    Double teachingHours = courseEntity.getTeachingHours();
    Double distanceTeachingHours = courseEntity.getDistanceTeachingHours();
    Double planningHours = courseEntity.getPlanningHours();
    Double assessingHours = courseEntity.getAssessingHours();
    String description = courseEntity.getDescription();
    Long maxParticipantCount = courseEntity.getMaxParticipantCount();
    Date enrolmentTimeEnd = toDate(courseEntity.getEnrolmentTimeEnd());
    User loggedUser = sessionController.getUser();
    Course updatedCourse = courseController.updateCourse(course, organization, name, nameExtension, state, type, subject, courseNumber, toDate(beginDate), toDate(endDate), courseLength, courseLengthTimeUnit, distanceTeachingDays, localTeachingDays, teachingHours, distanceTeachingHours, planningHours, assessingHours, description, maxParticipantCount, enrolmentTimeEnd, loggedUser);
    Set<Curriculum> curriculums = new HashSet<Curriculum>();
    if (CollectionUtils.isNotEmpty(courseEntity.getCurriculumIds())) {
        for (Long curriculumId : courseEntity.getCurriculumIds()) {
            Curriculum curriculum = curriculumId != null ? curriculumController.findCurriculumById(curriculumId) : null;
            if (curriculum != null)
                curriculums.add(curriculum);
        }
    }
    updatedCourse = courseController.updateCourseCurriculums(updatedCourse, curriculums);
    updatedCourse = courseController.updateCourseTags(updatedCourse, courseEntity.getTags() == null ? new ArrayList<String>() : courseEntity.getTags());
    updatedCourse = courseController.updateCourseVariables(updatedCourse, courseEntity.getVariables() == null ? new HashMap<String, String>() : courseEntity.getVariables());
    return Response.ok().entity(objectFactory.createModel(updatedCourse)).build();
}
Also used : Organization(fi.otavanopisto.pyramus.domainmodel.base.Organization) User(fi.otavanopisto.pyramus.domainmodel.users.User) Subject(fi.otavanopisto.pyramus.domainmodel.base.Subject) Date(java.util.Date) OffsetDateTime(java.time.OffsetDateTime) Curriculum(fi.otavanopisto.pyramus.domainmodel.base.Curriculum) CourseState(fi.otavanopisto.pyramus.domainmodel.courses.CourseState) Course(fi.otavanopisto.pyramus.domainmodel.courses.Course) CourseType(fi.otavanopisto.pyramus.domainmodel.courses.CourseType) EducationalTimeUnit(fi.otavanopisto.pyramus.domainmodel.base.EducationalTimeUnit) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) PUT(javax.ws.rs.PUT)

Example 3 with CourseType

use of fi.otavanopisto.pyramus.domainmodel.courses.CourseType in project pyramus by otavanopisto.

the class CourseTypeDAO method listByName.

public List<CourseType> listByName(String name) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CourseType> criteria = criteriaBuilder.createQuery(CourseType.class);
    Root<CourseType> root = criteria.from(CourseType.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(CourseType_.name), name));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CourseType(fi.otavanopisto.pyramus.domainmodel.courses.CourseType)

Example 4 with CourseType

use of fi.otavanopisto.pyramus.domainmodel.courses.CourseType in project pyramus by otavanopisto.

the class CourseRESTService method findCourseTypeById.

@Path("/courseTypes/{ID:[0-9]*}")
@GET
@RESTPermit(CoursePermissions.FIND_COURSETYPE)
public Response findCourseTypeById(@PathParam("ID") Long id, @Context Request request) {
    CourseType courseType = courseController.findCourseTypeById(id);
    if (courseType == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (courseType.getArchived()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(courseType.getVersion())));
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    return Response.ok().cacheControl(cacheControl).tag(tag).entity(objectFactory.createModel(courseType)).build();
}
Also used : EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) CourseType(fi.otavanopisto.pyramus.domainmodel.courses.CourseType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) GET(javax.ws.rs.GET)

Example 5 with CourseType

use of fi.otavanopisto.pyramus.domainmodel.courses.CourseType in project pyramus by otavanopisto.

the class CourseAPI method create.

public Long create(Long organizationId, Long moduleId, Long typeId, String name, String nameExtension, String description, String subjectCode) throws InvalidScriptException {
    ModuleDAO moduleDAO = DAOFactory.getInstance().getModuleDAO();
    CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
    SubjectDAO subjectDAO = DAOFactory.getInstance().getSubjectDAO();
    StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
    DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO();
    CourseTypeDAO courseTypeDAO = DAOFactory.getInstance().getCourseTypeDAO();
    OrganizationDAO organizationDAO = DAOFactory.getInstance().getOrganizationDAO();
    Module module = moduleDAO.findById(moduleId);
    if (module == null) {
        throw new InvalidScriptException("Module #" + moduleId + " not found.");
    }
    User loggedUser = userDAO.findById(loggedUserId);
    if (loggedUser == null) {
        throw new InvalidScriptException("Logged user not found.");
    }
    Subject subject = subjectDAO.findByCode(subjectCode);
    if (subject == null) {
        throw new InvalidScriptException("Subject by code '" + subjectCode + "' not found.");
    }
    Organization organization = organizationDAO.findById(organizationId);
    if (organization == null) {
        throw new InvalidScriptException("Default organization not found.");
    }
    CourseType type = typeId != null ? courseTypeDAO.findById(typeId) : null;
    // TODO: Add support for these:
    CourseState courseState = defaultsDAO.getDefaults().getInitialCourseState();
    Date beginDate = null;
    Date endDate = null;
    Double distanceTeachingDays = null;
    Double localTeachingDays = null;
    Double teachingHours = null;
    Double distanceTeachingHours = null;
    Double planningHours = null;
    Double assessingHours = null;
    Date enrolmentTimeEnd = null;
    BigDecimal courseFee = null;
    Currency courseFeeCurrency = null;
    return courseDAO.create(module, organization, name, nameExtension, courseState, type, subject, module.getCourseNumber(), beginDate, endDate, module.getCourseLength().getUnits(), module.getCourseLength().getUnit(), distanceTeachingDays, localTeachingDays, teachingHours, distanceTeachingHours, planningHours, assessingHours, description, module.getMaxParticipantCount(), courseFee, courseFeeCurrency, enrolmentTimeEnd, loggedUser).getId();
}
Also used : CourseTypeDAO(fi.otavanopisto.pyramus.dao.courses.CourseTypeDAO) User(fi.otavanopisto.pyramus.domainmodel.users.User) Organization(fi.otavanopisto.pyramus.domainmodel.base.Organization) SubjectDAO(fi.otavanopisto.pyramus.dao.base.SubjectDAO) InvalidScriptException(fi.otavanopisto.pyramus.util.dataimport.scripting.InvalidScriptException) CourseDAO(fi.otavanopisto.pyramus.dao.courses.CourseDAO) ModuleDAO(fi.otavanopisto.pyramus.dao.modules.ModuleDAO) DefaultsDAO(fi.otavanopisto.pyramus.dao.base.DefaultsDAO) Subject(fi.otavanopisto.pyramus.domainmodel.base.Subject) Date(java.util.Date) BigDecimal(java.math.BigDecimal) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) Currency(java.util.Currency) CourseState(fi.otavanopisto.pyramus.domainmodel.courses.CourseState) Module(fi.otavanopisto.pyramus.domainmodel.modules.Module) CourseType(fi.otavanopisto.pyramus.domainmodel.courses.CourseType) OrganizationDAO(fi.otavanopisto.pyramus.dao.base.OrganizationDAO)

Aggregations

CourseType (fi.otavanopisto.pyramus.domainmodel.courses.CourseType)13 Subject (fi.otavanopisto.pyramus.domainmodel.base.Subject)7 CourseState (fi.otavanopisto.pyramus.domainmodel.courses.CourseState)7 CourseTypeDAO (fi.otavanopisto.pyramus.dao.courses.CourseTypeDAO)6 EducationalTimeUnit (fi.otavanopisto.pyramus.domainmodel.base.EducationalTimeUnit)6 Organization (fi.otavanopisto.pyramus.domainmodel.base.Organization)6 Course (fi.otavanopisto.pyramus.domainmodel.courses.Course)6 Module (fi.otavanopisto.pyramus.domainmodel.modules.Module)6 Date (java.util.Date)6 Curriculum (fi.otavanopisto.pyramus.domainmodel.base.Curriculum)5 HashSet (java.util.HashSet)5 DefaultsDAO (fi.otavanopisto.pyramus.dao.base.DefaultsDAO)4 SubjectDAO (fi.otavanopisto.pyramus.dao.base.SubjectDAO)4 CourseEducationType (fi.otavanopisto.pyramus.domainmodel.base.CourseEducationType)4 CourseComponent (fi.otavanopisto.pyramus.domainmodel.courses.CourseComponent)4 BigDecimal (java.math.BigDecimal)4 Currency (java.util.Currency)4 CourseEducationSubtypeDAO (fi.otavanopisto.pyramus.dao.base.CourseEducationSubtypeDAO)3 CourseEducationTypeDAO (fi.otavanopisto.pyramus.dao.base.CourseEducationTypeDAO)3 EducationalTimeUnitDAO (fi.otavanopisto.pyramus.dao.base.EducationalTimeUnitDAO)3