Search in sources :

Example 1 with CourseDescription

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

the class EditModuleJSONRequestController method process.

public void process(JSONRequestContext requestContext) {
    StaffMemberDAO userDAO = DAOFactory.getInstance().getStaffMemberDAO();
    ModuleDAO moduleDAO = DAOFactory.getInstance().getModuleDAO();
    CourseDescriptionDAO courseDescriptionDAO = DAOFactory.getInstance().getCourseDescriptionDAO();
    CourseDescriptionCategoryDAO descriptionCategoryDAO = DAOFactory.getInstance().getCourseDescriptionCategoryDAO();
    CourseEducationTypeDAO courseEducationTypeDAO = DAOFactory.getInstance().getCourseEducationTypeDAO();
    CourseEducationSubtypeDAO courseEducationSubtypeDAO = DAOFactory.getInstance().getCourseEducationSubtypeDAO();
    ModuleComponentDAO moduleComponentDAO = DAOFactory.getInstance().getModuleComponentDAO();
    EducationalTimeUnitDAO educationalTimeUnitDAO = DAOFactory.getInstance().getEducationalTimeUnitDAO();
    EducationTypeDAO educationTypeDAO = DAOFactory.getInstance().getEducationTypeDAO();
    EducationSubtypeDAO educationSubtypeDAO = DAOFactory.getInstance().getEducationSubtypeDAO();
    SubjectDAO subjectDAO = DAOFactory.getInstance().getSubjectDAO();
    TagDAO tagDAO = DAOFactory.getInstance().getTagDAO();
    DefaultsDAO defaultsDAO = DAOFactory.getInstance().getDefaultsDAO();
    CurriculumDAO curriculumDAO = DAOFactory.getInstance().getCurriculumDAO();
    Long moduleId = requestContext.getLong("moduleId");
    Module module = moduleDAO.findById(moduleId);
    Long version = requestContext.getLong("version");
    if (!module.getVersion().equals(version))
        throw new StaleObjectStateException(Module.class.getName(), module.getId());
    // Education types and subtypes submitted from the web page
    Map<Long, Vector<Long>> chosenEducationTypes = new HashMap<>();
    Enumeration<String> parameterNames = requestContext.getRequest().getParameterNames();
    while (parameterNames.hasMoreElements()) {
        String name = (String) parameterNames.nextElement();
        if (name.startsWith("educationType.")) {
            String[] nameElements = name.split("\\.");
            Long educationTypeId = new Long(nameElements[1]);
            Long educationSubtypeId = new Long(nameElements[2]);
            Vector<Long> v = chosenEducationTypes.containsKey(educationTypeId) ? chosenEducationTypes.get(educationTypeId) : new Vector<Long>();
            v.add(educationSubtypeId);
            if (!chosenEducationTypes.containsKey(educationTypeId)) {
                chosenEducationTypes.put(educationTypeId, v);
            }
        }
    }
    // Course Descriptions
    List<CourseDescriptionCategory> descriptionCategories = descriptionCategoryDAO.listUnarchived();
    Set<CourseDescription> nonExistingDescriptions = new HashSet<>();
    for (CourseDescriptionCategory cat : descriptionCategories) {
        String varName = "courseDescription." + cat.getId().toString();
        Long descriptionCatId = requestContext.getLong(varName + ".catId");
        String descriptionText = requestContext.getString(varName + ".text");
        CourseDescription oldDesc = courseDescriptionDAO.findByCourseAndCategory(module, cat);
        if (descriptionCatId != null && descriptionCatId.intValue() != -1) {
            // Description has been submitted from form
            if (oldDesc != null)
                courseDescriptionDAO.update(oldDesc, module, cat, descriptionText);
            else
                courseDescriptionDAO.create(module, cat, descriptionText);
        } else {
            // Description wasn't submitted from form, if it exists, it's marked for deletion
            if (oldDesc != null)
                nonExistingDescriptions.add(oldDesc);
        }
    }
    // Delete non existing descriptions
    for (CourseDescription desc : nonExistingDescriptions) {
        courseDescriptionDAO.delete(desc);
    }
    // Remove education types and subtypes
    List<CourseEducationType> courseEducationTypes = module.getCourseEducationTypes();
    for (int i = courseEducationTypes.size() - 1; i >= 0; i--) {
        CourseEducationType courseEducationType = courseEducationTypes.get(i);
        if (!chosenEducationTypes.containsKey(courseEducationType.getEducationType().getId())) {
            courseEducationTypeDAO.delete(courseEducationType);
        } else {
            Vector<Long> v = chosenEducationTypes.get(courseEducationType.getEducationType().getId());
            List<CourseEducationSubtype> courseEducationSubtypes = courseEducationType.getCourseEducationSubtypes();
            for (int j = courseEducationSubtypes.size() - 1; j >= 0; j--) {
                CourseEducationSubtype moduleEducationSubtype = courseEducationSubtypes.get(j);
                if (!v.contains(moduleEducationSubtype.getEducationSubtype().getId())) {
                    courseEducationType.removeSubtype(moduleEducationSubtype);
                }
            }
        }
    }
    for (Map.Entry<Long, Vector<Long>> entry : chosenEducationTypes.entrySet()) {
        EducationType educationType = educationTypeDAO.findById(entry.getKey());
        CourseEducationType courseEducationType;
        if (!module.contains(educationType)) {
            courseEducationType = courseEducationTypeDAO.create(module, educationType);
        } else {
            courseEducationType = module.getCourseEducationTypeByEducationTypeId(entry.getKey());
        }
        for (Long educationSubtypeId : entry.getValue()) {
            EducationSubtype educationSubtype = educationSubtypeDAO.findById(educationSubtypeId);
            if (!courseEducationType.contains(educationSubtype)) {
                courseEducationSubtypeDAO.create(courseEducationType, educationSubtype);
            }
        }
    }
    // Module components
    int rowCount = requestContext.getInteger("componentsTable.rowCount");
    for (int i = 0; i < rowCount; i++) {
        String colPrefix = "componentsTable." + i;
        String componentName = requestContext.getString(colPrefix + ".name");
        Double componentLength = requestContext.getDouble(colPrefix + ".length");
        String componentDescription = requestContext.getString(colPrefix + ".description");
        Long componentId = requestContext.getLong(colPrefix + ".componentId");
        // TODO Component length; should be just hours but it currently depends on the default time unit - ok?
        EducationalTimeUnit componentTimeUnit = defaultsDAO.getDefaults().getBaseTimeUnit();
        if (componentId == -1) {
            componentId = moduleComponentDAO.create(module, componentLength, componentTimeUnit, componentName, componentDescription).getId();
        } else {
            moduleComponentDAO.update(moduleComponentDAO.findById(componentId), componentLength, componentTimeUnit, componentName, componentDescription);
        }
    }
    // Module basic information
    Long subjectId = requestContext.getLong("subject");
    Subject subject = subjectDAO.findById(subjectId);
    Integer courseNumber = requestContext.getInteger("courseNumber");
    String name = requestContext.getString("name");
    String description = requestContext.getString("description");
    User loggedUser = userDAO.findById(requestContext.getLoggedUserId());
    Double moduleLength = requestContext.getDouble("moduleLength");
    Long moduleLengthTimeUnitId = requestContext.getLong("moduleLengthTimeUnit");
    Long maxParticipantCount = requestContext.getLong("maxParticipantCount");
    String tagsText = requestContext.getString("tags");
    List<Curriculum> allCurriculums = curriculumDAO.listUnarchived();
    Set<Curriculum> curriculums = new HashSet<>();
    for (Curriculum curriculum : allCurriculums) {
        if ("1".equals(requestContext.getString("curriculum." + curriculum.getId()))) {
            curriculums.add(curriculum);
        }
    }
    Set<Tag> tagEntities = new HashSet<>();
    if (!StringUtils.isBlank(tagsText)) {
        List<String> tags = Arrays.asList(tagsText.split("[\\ ,]"));
        for (String tag : tags) {
            if (!StringUtils.isBlank(tag)) {
                Tag tagEntity = tagDAO.findByText(tag.trim());
                if (tagEntity == null)
                    tagEntity = tagDAO.create(tag);
                tagEntities.add(tagEntity);
            }
        }
    }
    EducationalTimeUnit moduleLengthTimeUnit = educationalTimeUnitDAO.findById(moduleLengthTimeUnitId);
    moduleDAO.update(module, name, subject, courseNumber, moduleLength, moduleLengthTimeUnit, description, maxParticipantCount, loggedUser);
    moduleDAO.updateCurriculums(module, curriculums);
    // Tags
    moduleDAO.updateTags(module, tagEntities);
    requestContext.setRedirectURL(requestContext.getReferer(true));
}
Also used : HashMap(java.util.HashMap) CourseDescriptionDAO(fi.otavanopisto.pyramus.dao.courses.CourseDescriptionDAO) ModuleDAO(fi.otavanopisto.pyramus.dao.modules.ModuleDAO) CourseDescription(fi.otavanopisto.pyramus.domainmodel.courses.CourseDescription) ModuleComponentDAO(fi.otavanopisto.pyramus.dao.modules.ModuleComponentDAO) Vector(java.util.Vector) HashSet(java.util.HashSet) EducationalTimeUnit(fi.otavanopisto.pyramus.domainmodel.base.EducationalTimeUnit) CourseEducationType(fi.otavanopisto.pyramus.domainmodel.base.CourseEducationType) EducationalTimeUnitDAO(fi.otavanopisto.pyramus.dao.base.EducationalTimeUnitDAO) EducationSubtype(fi.otavanopisto.pyramus.domainmodel.base.EducationSubtype) CourseEducationSubtype(fi.otavanopisto.pyramus.domainmodel.base.CourseEducationSubtype) CourseEducationSubtype(fi.otavanopisto.pyramus.domainmodel.base.CourseEducationSubtype) CourseEducationTypeDAO(fi.otavanopisto.pyramus.dao.base.CourseEducationTypeDAO) DefaultsDAO(fi.otavanopisto.pyramus.dao.base.DefaultsDAO) CourseEducationTypeDAO(fi.otavanopisto.pyramus.dao.base.CourseEducationTypeDAO) EducationTypeDAO(fi.otavanopisto.pyramus.dao.base.EducationTypeDAO) Module(fi.otavanopisto.pyramus.domainmodel.modules.Module) StaleObjectStateException(org.hibernate.StaleObjectStateException) HashMap(java.util.HashMap) Map(java.util.Map) User(fi.otavanopisto.pyramus.domainmodel.users.User) CourseEducationType(fi.otavanopisto.pyramus.domainmodel.base.CourseEducationType) EducationType(fi.otavanopisto.pyramus.domainmodel.base.EducationType) SubjectDAO(fi.otavanopisto.pyramus.dao.base.SubjectDAO) CourseDescriptionCategoryDAO(fi.otavanopisto.pyramus.dao.courses.CourseDescriptionCategoryDAO) StaffMemberDAO(fi.otavanopisto.pyramus.dao.users.StaffMemberDAO) CourseDescriptionCategory(fi.otavanopisto.pyramus.domainmodel.courses.CourseDescriptionCategory) CurriculumDAO(fi.otavanopisto.pyramus.dao.base.CurriculumDAO) TagDAO(fi.otavanopisto.pyramus.dao.base.TagDAO) CourseEducationSubtypeDAO(fi.otavanopisto.pyramus.dao.base.CourseEducationSubtypeDAO) Subject(fi.otavanopisto.pyramus.domainmodel.base.Subject) EducationSubtypeDAO(fi.otavanopisto.pyramus.dao.base.EducationSubtypeDAO) CourseEducationSubtypeDAO(fi.otavanopisto.pyramus.dao.base.CourseEducationSubtypeDAO) Curriculum(fi.otavanopisto.pyramus.domainmodel.base.Curriculum) Tag(fi.otavanopisto.pyramus.domainmodel.base.Tag)

Example 2 with CourseDescription

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

the class CourseDescriptionDAO method findByCourseAndCategory.

public CourseDescription findByCourseAndCategory(CourseBase courseBase, CourseDescriptionCategory category) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CourseDescription> criteria = criteriaBuilder.createQuery(CourseDescription.class);
    Root<CourseDescription> root = criteria.from(CourseDescription.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CourseDescription_.courseBase), courseBase), criteriaBuilder.equal(root.get(CourseDescription_.category), category)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CourseDescription(fi.otavanopisto.pyramus.domainmodel.courses.CourseDescription) EntityManager(javax.persistence.EntityManager)

Example 3 with CourseDescription

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

the class CourseDescriptionDAO method listByCourseBase.

public List<CourseDescription> listByCourseBase(CourseBase courseBase) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CourseDescription> criteria = criteriaBuilder.createQuery(CourseDescription.class);
    Root<CourseDescription> root = criteria.from(CourseDescription.class);
    Join<CourseDescription, CourseDescriptionCategory> category = root.join(CourseDescription_.category);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CourseDescription_.courseBase), courseBase), criteriaBuilder.equal(category.get(CourseDescriptionCategory_.archived), Boolean.FALSE)));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CourseDescription(fi.otavanopisto.pyramus.domainmodel.courses.CourseDescription) EntityManager(javax.persistence.EntityManager) CourseDescriptionCategory(fi.otavanopisto.pyramus.domainmodel.courses.CourseDescriptionCategory)

Example 4 with CourseDescription

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

the class CourseDescriptionDAO method create.

public CourseDescription create(CourseBase courseBase, CourseDescriptionCategory category, String description) {
    EntityManager entityManager = getEntityManager();
    CourseDescription courseDescription = new CourseDescription();
    courseDescription.setCourseBase(courseBase);
    courseDescription.setCategory(category);
    courseDescription.setDescription(description);
    entityManager.persist(courseDescription);
    return courseDescription;
}
Also used : CourseDescription(fi.otavanopisto.pyramus.domainmodel.courses.CourseDescription) EntityManager(javax.persistence.EntityManager)

Example 5 with CourseDescription

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

the class CourseRESTService method listCourseDescriptions.

@Path("/courses/{COURSEID:[0-9]*}/descriptions")
@GET
@RESTPermit(CoursePermissions.LIST_COURSEDESCRIPTIONS)
public Response listCourseDescriptions(@PathParam("COURSEID") Long courseId) {
    Course course = courseController.findCourseById(courseId);
    if (course == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    List<CourseDescription> courseDescriptions = courseController.listCourseDescriptionsByCourseBase(course);
    if (courseDescriptions.isEmpty()) {
        return Response.status(Status.NO_CONTENT).build();
    }
    return Response.status(Status.OK).entity(objectFactory.createModel(courseDescriptions)).build();
}
Also used : CourseDescription(fi.otavanopisto.pyramus.domainmodel.courses.CourseDescription) Course(fi.otavanopisto.pyramus.domainmodel.courses.Course) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

CourseDescription (fi.otavanopisto.pyramus.domainmodel.courses.CourseDescription)9 CourseDescriptionCategory (fi.otavanopisto.pyramus.domainmodel.courses.CourseDescriptionCategory)5 Course (fi.otavanopisto.pyramus.domainmodel.courses.Course)4 CourseEducationSubtype (fi.otavanopisto.pyramus.domainmodel.base.CourseEducationSubtype)3 CourseEducationType (fi.otavanopisto.pyramus.domainmodel.base.CourseEducationType)3 EducationSubtype (fi.otavanopisto.pyramus.domainmodel.base.EducationSubtype)3 EducationType (fi.otavanopisto.pyramus.domainmodel.base.EducationType)3 EducationalTimeUnit (fi.otavanopisto.pyramus.domainmodel.base.EducationalTimeUnit)3 Subject (fi.otavanopisto.pyramus.domainmodel.base.Subject)3 Tag (fi.otavanopisto.pyramus.domainmodel.base.Tag)3 Module (fi.otavanopisto.pyramus.domainmodel.modules.Module)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 EntityManager (javax.persistence.EntityManager)3 CourseEducationSubtypeDAO (fi.otavanopisto.pyramus.dao.base.CourseEducationSubtypeDAO)2 CourseEducationTypeDAO (fi.otavanopisto.pyramus.dao.base.CourseEducationTypeDAO)2 CurriculumDAO (fi.otavanopisto.pyramus.dao.base.CurriculumDAO)2 DefaultsDAO (fi.otavanopisto.pyramus.dao.base.DefaultsDAO)2 EducationSubtypeDAO (fi.otavanopisto.pyramus.dao.base.EducationSubtypeDAO)2