org.piotr.apollo.service.LessonService.java Source code

Java tutorial

Introduction

Here is the source code for org.piotr.apollo.service.LessonService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.piotr.apollo.service;

import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.piotr.apollo.database.mongoDB;
import org.piotr.apollo.model.Finished;
import org.piotr.apollo.model.Lesson;

/**
 *
 * @author Leniwiec
 */
public class LessonService {

    mongoDB mongoDB = new mongoDB();
    private final String lessonCollection = "lessonCollection";

    public LessonService() {
    }

    public List<Lesson> getSectionsLessonList(String sectionId) {
        return getLessonBySectionId(sectionId);
    }

    public Lesson addNewLessonToSection(Lesson lesson) {
        MongoCollection lessonCollection = mongoDB.getCollection(this.lessonCollection);

        Document obj = new Document();

        obj.put("code", lesson.getCode());
        obj.put("section_Id", new ObjectId(lesson.getSectionId()));
        obj.put("name", lesson.getName());
        obj.put("description", lesson.getDescription());
        obj.put("testCounter", 0);
        obj.put("percentageCorrect", 0.0);
        obj.put("grade", 0.0);

        lessonCollection.insertOne(obj);

        return lesson;
    }

    private List<Lesson> getLessonBySectionId(String sectionId) {

        List<Lesson> lessonList = new ArrayList<>();
        MongoCollection collection = mongoDB.getCollection(lessonCollection);

        Document doc = new Document();

        doc.append("section_Id", new ObjectId(sectionId));

        FindIterable findIterable = collection.find(doc);
        MongoCursor<Document> cursor = findIterable.iterator();

        while (cursor.hasNext()) {
            Document obj = cursor.next();
            Lesson lesson = new Lesson();
            ObjectId id = (ObjectId) obj.get("_id");

            lesson.set_Id(id);
            lesson.setId(id.toString());
            lesson.setCode(obj.getString("code"));
            lesson.setName(obj.getString("name"));
            lesson.setDescription(obj.getString("description"));
            lesson.setTestCounter(obj.getInteger("testCounter"));
            lesson.setPercentageCorrect(obj.getDouble("percentageCorrect"));
            lesson.setGrade(obj.getDouble("grade"));

            lessonList.add(lesson);
        }

        return lessonList;
    }

    public void updateLessonTest(Finished finished) {

        MongoCollection collection = mongoDB.getCollection(lessonCollection);
        Double gradeToUpdate = 0.0;

        BasicDBObject toUpdate = new BasicDBObject();
        BasicDBObject oldObject = new BasicDBObject();

        oldObject.append("_id", new ObjectId(finished.getLessonId()));

        Document document = new Document();
        document.append("_id", new ObjectId(finished.getLessonId()));

        FindIterable iterable = collection.find(document);
        MongoCursor<Document> cursor = iterable.iterator();

        while (cursor.hasNext()) {
            Document temp = cursor.next();

            Integer testCounter = temp.getInteger("testCounter");
            Double percentageCorrect = temp.getDouble("percentageCorrect");

            toUpdate.append("testCounter", testCounter + 1);

            if (percentageCorrect != 0.0) {
                toUpdate.append("percentageCorrect", (percentageCorrect + finished.getPercentage()) / 2);
                gradeToUpdate = getProperGrade((percentageCorrect + finished.getPercentage()) / 2);
            } else {
                toUpdate.append("percentageCorrect", finished.getPercentage());
                gradeToUpdate = getProperGrade(finished.getPercentage());
            }

            toUpdate.append("grade", gradeToUpdate);

            collection.updateOne(oldObject, new BasicDBObject("$set", toUpdate));

        }
    }

    private Double getProperGrade(Double percentage) {

        if (percentage < 30.0) {
            return 2.0;
        } else if (percentage < 50.0) {
            return 3.0;
        } else if (percentage < 65.0) {
            return 3.5;
        } else if (percentage < 75.0) {
            return 4.0;
        } else if (percentage < 85.0) {
            return 4.5;
        } else {
            return 4.5;
        }
    }
}