/*
 * This file (AbstractLabelPrinter.java) is part of DebGen-LabelPrintLib  developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 *  Copyright ASTRID Research, Hungary, 2010.
 *
 *  ASTRID Research
 *  Headquaters: Csapó utca 42. Debrecen H-4029 HUNGARY
 *  Phone: +36-52-533-173        Fax: +36-52-533-174
 *  Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DEBGEN-Biobank-Project.
 * File last modified at 2010.01.07 14:44
 */
package hu.astrid.debgen.labelprint.printer;

import hu.astrid.debgen.labelprint.domain.Label;
import hu.astrid.debgen.labelprint.exception.LabelPrintException;
import hu.astrid.debgen.labelprint.util.ReadFromXML;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.print.PrintService;
import java.util.ArrayList;
import java.util.List;

/**
 * @author inagy
 */
public abstract class AbstractLabelPrinter implements LabelPrinter {

    /**
     * The printService.
     */
    protected PrintService printService;

    public List<Label> createLabelListFromDocument(Document doc) throws LabelPrintException {
        //new ValidateXML().validateXML(doc);
        List<Node> sampleCodes = ReadFromXML.getNodes(doc, "debgenbiobank:analysis", "biobankData","labels","sampleCode");
        List<Label> labels = new ArrayList<Label>();
        Label l = Label.getInstance("PATIENT", ReadFromXML.getAttributeValue(doc, "copies", "debgenbiobank:analysis", "patient", "commonData", "patientCode"));
        l.setCodeValue(ReadFromXML.getNodeValue(doc, "debgenbiobank:analysis", "patient", "commonData", "patientCode"));
        labels.add(l);
        for (Node n : sampleCodes) {
            String codeValue = ReadFromXML.getNodeValue(n, "codevalue");
            Label label = Label.getInstance("SAMPLE", ReadFromXML.getAttributeValue(n, "copies"));
            label.setCodeValue(codeValue);
            labels.add(label);
        }
        return labels;
    }

    /**
     * Return the printService.
     */
    public PrintService getPrintService() {
        return printService;
    }

    /**
     * Sets the printService.
     * @param printService The printService to set
     */
    public void setPrintService(PrintService printService) {
        this.printService = printService;
    }

    /**
     * Print labels from XML.
     * @param doc XML document to read values from
     */
    public void printLabel(Document doc) throws LabelPrintException {
        try {
            List<Label> labels = createLabelListFromDocument(doc);
            printLabel(labels);
        } catch (LabelPrintException ex) {
            throw ex;
        } catch (Exception e) {
            throw new LabelPrintException(e);
        }
    }

    /**
     * Prints label.
     * @param label Label to print.
     */
    public void printLabel(Label label) throws LabelPrintException {
        if (label == null) {
            throw new IllegalArgumentException("Parameter label must be set");
        }
        List<Label> labels = new ArrayList<Label>();
        labels.add(label);
        printLabel(labels);
    }

    /**
     * Abstract method printLabel.
     */
    public abstract void printLabel(List<Label> labels) throws LabelPrintException;
}
