/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2011.
 *
 * ASTRID Research
 * Headquaters: Csapo 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 Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.patientmanagement.domain.modules.requests;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import javax.persistence.Entity;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Class of Common Analisys Requests
 * @author inagy
 */
@Entity
public class AnalysisRequestCommon extends AnalysisRequest {

    private static final long serialVersionUID = 1L;

    @Override
    public String getType() {
        return "common";
    }

    // <editor-fold defaultstate="collapsed" desc="XML conversion">
    @Override
    public void buildFromXml(Node node) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void buildFromXml(Node node, String string) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Node toXml(Document doc) throws Exception {
        return toXml(doc, "");
    }

    @Override
    public Node toXml(Document doc, String nameSpace) throws Exception {
        try {
            String prefix = nameSpace == null || nameSpace.isEmpty() ? "" : nameSpace + ":";
            Element analysis = doc.createElement(prefix + "analysis");
            analysis.setAttribute("objectType", this.empty ? "empty" : this.objectType);
            this.setXmlCommonValues(doc, analysis);
            analysis.appendChild(patient.toXml(doc));
            Element biobankData = doc.createElement("biobankData");
            analysis.appendChild(biobankData);
            biobankData.appendChild(this.getLabels().toXml(doc));
            setXmlMetaData(doc, biobankData);
            return analysis;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
    }// </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Toucan request processing">
    @Override
    public void convert(HttpServletRequest request) throws Exception {
        super.convert(request);
        requestParser.parseRequest(AnalysisRequestCommon.class, this);
        if (this.createdOn == null) {
            this.createdOn = new Date();
        }
    }// </editor-fold>

    public int compareTo(Object o) {
        if (this.createdOn == null) {
            return -1;
        }
        AnalysisRequest other = (AnalysisRequest) o;
        int res = 0 - this.createdOn.compareTo(other.createdOn);
        if (res == 0) {
            return this.id.compareTo(other.id);
        } else {
            return res;
        }

    }
}
