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