/*
 * 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.sample;

import hu.astrid.debgen.exception.ValidationFailedException;
import hu.astrid.toucan.control.bean.ToucanBean;
import hu.astrid.toucan.servlet.httprequesttoobjectparser.HttpRequestToObjectParser;
import hu.astrid.xmltools.beans.XmlConvertable;
import hu.astrid.xmltools.beantoxml.XmlToBeanConverter;
import java.io.Serializable;
import java.util.ArrayList;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.servlet.http.HttpServletRequest;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * The Domain object of the Sample types
 * @author inagy
 */
@Entity
@Table(name = "debgen_sampletype")
public class SampleType implements ToucanBean, Serializable, XmlConvertable {

    private static final long serialVersionUID = 1L;
    private static String ID_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String sampleCategoryName;
    private String oneLetterCode;

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final SampleType other = (SampleType) obj;
        if ((this.sampleCategoryName == null) ? (other.sampleCategoryName != null) : !this.sampleCategoryName.equals(other.sampleCategoryName)) {
            return false;
        }
        if ((this.oneLetterCode == null) ? (other.oneLetterCode != null) : !this.oneLetterCode.equals(other.oneLetterCode)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 31 * hash + (this.sampleCategoryName != null ? this.sampleCategoryName.hashCode() : 0);
        hash = 31 * hash + (this.oneLetterCode != null ? this.oneLetterCode.hashCode() : 0);
        return hash;
    }

    /**
     * Get the value of oneLetterCode
     *
     * @return the value of oneLetterCode
     */
    public String getOneLetterCode() {
        return oneLetterCode;
    }

    /**
     * Set the value of oneLetterCode
     *
     * @param oneLetterCode new value of oneLetterCode
     */
    public void setOneLetterCode(String oneLetterCode) {
        this.oneLetterCode = oneLetterCode;
    }


    /**
     * Get the value of sampleCategoryName
     *
     * @return the value of sampleCategoryName
     */
    public String getSampleCategoryName() {
        return sampleCategoryName;
    }

    /**
     * Set the value of sampleCategoryName
     *
     * @param sampleCategoryName new value of sampleCategoryName
     */
    public void setSampleCategoryName(String sampleCategoryName) {
        this.sampleCategoryName = sampleCategoryName;
    }


    /**
     * Get the value of id
     *
     * @return the value of id
     */
    public Long getId() {
        return id;
    }

    /**
     * Set the value of id
     *
     * @param id new value of id
     */
    public void setId(Long id) {
        this.id = id;
    }

    public void convert(HttpServletRequest request) throws Exception {
        HttpRequestToObjectParser parser = HttpRequestToObjectParser.getInstance(request);
        parser.parseRequest(SampleType.class, this);
    }

    public boolean validate() throws ValidationFailedException {
        ArrayList<String> properties = new ArrayList<String>(2);
        if (oneLetterCode == null || oneLetterCode.length() != 1 || !SampleType.ID_CHARSET.contains(oneLetterCode))  {
            properties.add("ONE_LETTER_CODE");
        }
        if (sampleCategoryName == null || sampleCategoryName.length() < 2) {
            properties.add("SAMPLE_NAME");
        }
        if (properties.isEmpty()) {
            return true;
        } else {
            throw new ValidationFailedException(properties);
        }
    }

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

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

    public Node toXml(Document dcmnt) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Node toXml(Document dcmnt, String string) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public XmlToBeanConverter getXmlConverter() throws Exception {
        return new XmlToBeanConverter();
    }



}
