at.reisisoft.jku.ce.adaptivelearning.vaadin.ui.topic.accounting.AccountingQuestionManager.java Source code

Java tutorial

Introduction

Here is the source code for at.reisisoft.jku.ce.adaptivelearning.vaadin.ui.topic.accounting.AccountingQuestionManager.java

Source

package at.reisisoft.jku.ce.adaptivelearning.vaadin.ui.topic.accounting;

/*This file is part of the project "Reisisoft Adaptive Testing",
 * which is licenced under LGPL v3+. You may find a copy in the source,
 * or obtain one at http://www.gnu.org/licenses/lgpl-3.0-standalone.html */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;

import at.reisisoft.jku.ce.adaptivelearning.core.LogHelper;
import at.reisisoft.jku.ce.adaptivelearning.topic.accounting.AccountingDataProvider;
import at.reisisoft.jku.ce.adaptivelearning.topic.accounting.AccountingDataStorage;
import at.reisisoft.jku.ce.adaptivelearning.topic.accounting.AccountingQuestion;
import at.reisisoft.jku.ce.adaptivelearning.topic.accounting.ProfitDataStorage;
import at.reisisoft.jku.ce.adaptivelearning.topic.accounting.ProfitQuestion;
import at.reisisoft.jku.ce.adaptivelearning.vaadin.ui.QuestionManager;
import at.reisisoft.jku.ce.adaptivelearning.vaadin.ui.core.VaadinUI;
import at.reisisoft.jku.ce.adaptivelearning.xml.topic.accounting.AccountingXmlHelper;
import at.reisisoft.jku.ce.adaptivelearning.xml.topic.accounting.XmlAccountingQuestion;
import at.reisisoft.jku.ce.adaptivelearning.xml.topic.accounting.XmlProfitQuestion;

import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class AccountingQuestionManager extends QuestionManager {

    private static final long serialVersionUID = -4764723794449575244L;

    public AccountingQuestionManager(String quizName) {
        super(quizName);
        Button openKontenplan = new Button("Open Kontenplan");
        openKontenplan.addClickListener(e -> {
            openKontenplan.setEnabled(false);
            // Create Window with layout
            Window window = new Window("Kontenplan");
            GridLayout layout = new GridLayout(1, 1);
            layout.addComponent(AccountingDataProvider.getInstance().toHtmlTable());
            layout.setSizeFull();
            window.setContent(layout);
            window.center();
            window.setWidth("60%");
            window.setHeight("80%");
            window.setResizable(false);
            window.addCloseListener(e1 -> openKontenplan.setEnabled(true));
            getUI().addWindow(window);

        });
        addHelpButton(openKontenplan);
    }

    @Override
    public void startQuiz() {
        // Remove everything from the layout, save it for displaying after
        // clicking OK
        final Component[] components = new Component[getComponentCount()];
        for (int i = 0; i < components.length; i++) {
            components[i] = getComponent(i);
        }
        removeAllComponents();
        // Create first page
        VerticalLayout layout = new VerticalLayout();

        addComponent(layout);
        Label label = new Label("Answer all the questions like you were working for \"Unternehmen XY\"",
                ContentMode.HTML);
        Button cont = new Button("Continue", e -> {
            removeAllComponents();
            for (Component c : components) {
                addComponent(c);
            }
            super.startQuiz();
        });
        layout.addComponent(components[0]);// Title of the quiz
        layout.addComponent(label);
        layout.addComponent(cont);
        layout.setComponentAlignment(components[0], Alignment.MIDDLE_CENTER);

    }

    public int loadQuestions(File containingFolder) throws JAXBException, IOException {
        assert containingFolder.exists() && containingFolder.isDirectory();
        JAXBContext accountingJAXB = JAXBContext.newInstance(XmlAccountingQuestion.class,
                AccountingDataStorage.class);
        JAXBContext profitJAXB = JAXBContext.newInstance(XmlProfitQuestion.class, ProfitDataStorage.class);

        Unmarshaller accountingUnmarshaller = accountingJAXB.createUnmarshaller();
        Unmarshaller profitUnmarshaller = profitJAXB.createUnmarshaller();

        final List<AccountingQuestion> accountingList = new ArrayList<>();
        final List<ProfitQuestion> profitList = new ArrayList<>();

        String accountingRootElement = XmlAccountingQuestion.class.getAnnotation(XmlRootElement.class).name();
        String profitRootElement = XmlProfitQuestion.class.getAnnotation(XmlRootElement.class).name();

        File[] questions = containingFolder.listFiles((FileFilter) f -> f.isFile()
                && (f.canRead() || f.setReadable(true)) && f.getName().endsWith(".xml"));

        // read all questions
        for (File f : questions) {
            LogHelper.logInfo("Loading question with filename: " + f.getName());
            BufferedReader reader = null;
            StringBuilder sb = new StringBuilder();
            try {
                reader = new BufferedReader(new InputStreamReader(
                        new BOMInputStream(new FileInputStream(f), ByteOrderMark.UTF_8), "UTF8"));

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } finally {
                if (reader != null) {
                    reader.close();
                }
            }
            String fileAsString = sb.toString().replaceAll("& ", "&amp; ");
            if (fileAsString.contains(profitRootElement)) {
                LogHelper.logInfo("Question detected as " + ProfitQuestion.class.getName());
                // Profit Question
                XmlProfitQuestion question = (XmlProfitQuestion) profitUnmarshaller
                        .unmarshal(new StringReader(fileAsString));
                profitList.add(AccountingXmlHelper.fromXml(question));
            } else if (fileAsString.contains(accountingRootElement)) {
                LogHelper.logInfo("Question detected as " + AccountingQuestion.class.getName());
                // Accounting Question
                XmlAccountingQuestion question = (XmlAccountingQuestion) accountingUnmarshaller
                        .unmarshal(new StringReader(fileAsString));
                accountingList.add(AccountingXmlHelper.fromXml(question));
            } else {
                throw new IllegalArgumentException("Question type not supported. File: " + f);
            }
            LogHelper.logInfo("Loaded question with filename:" + f.getName());
        }
        // Add question to the question manager
        accountingList.forEach(q -> addQuestion(q));
        profitList.forEach(q -> addQuestion(q));
        return questions.length;
    }

    @Override
    public void loadQuestions() {
        try {
            loadQuestions(new File(VaadinUI.Servlet.getQuestionFolderName()));
        } catch (JAXBException | IOException e1) {
            Notification.show("Questions could not be loaded - FATAL error", e1.getMessage() + e1.getMessage(),
                    Type.ERROR_MESSAGE);
            LogHelper.logThrowable(e1);
        }

    }
}