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

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 javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.servlet.http.HttpServletRequest;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * The PersonalIdentity class
 * @author inagy
 */
@Entity
@Table(name = "debgen_personalidentity"/*, uniqueConstraints = {@UniqueConstraint(columnNames = {"userName", "email"})}*/)
public class PersonalIdentity implements ToucanBean, Serializable, XmlConvertable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long personalId;
    private String patientName;   //Patient
    private String maidenName;    //Patient
    private int zipCode;          //Patient
    private String city;          //Patient
    private String address;       //Patient
    private String mothersName;   //Patient
    private String tajNumber;     //Patient
//    private String logNumber;
    @OneToOne(mappedBy = "identity")
    private Patient identityInverse;

    /**
     * Get the value of identityInverse
     *
     * @return the value of identityInverse
     */
    public Patient getIdentityInverse() {
        return identityInverse;
    }

    /**
     * Set the value of identityInverse
     *
     * @param identityInverse new value of identityInverse
     */
    public void setIdentityInverse(Patient identityInverse) {
        this.identityInverse = identityInverse;
    }

    public Long getId() {
        return personalId;
    }

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

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

//    /**
//     * Get the value of logNumber
//     *
//     * @return the value of logNumber
//     */
//    public String getLogNumber() {
//        return logNumber;
//    }
//
//    /**
//     * Set the value of logNumber
//     *
//     * @param logNumber new value of logNumber
//     */
//    public void setLogNumber(String logNumber) {
//        this.logNumber = logNumber;
//    }

    /**
     * Get the value of tajNumber
     *
     * @return the value of tajNumber
     */
    public String getTajNumber() {
        return tajNumber;
    }

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

    /**
     * Get the value of mothersName
     *
     * @return the value of mothersName
     */
    public String getMothersName() {
        return mothersName;
    }

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

    /**
     * Get the value of address
     *
     * @return the value of address
     */
    public String getAddress() {
        return address;
    }

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

    /**
     * Get the value of zipCode
     *
     * @return the value of zipCode
     */
    public int getZipCode() {
        return zipCode;
    }

    /**
     * Set the value of zipCode
     *
     * @param zipCode new value of zipCode
     */
    public void setZipCode(int zipCode) {
        this.zipCode = zipCode;
    }

    /**
     * Get the value of city
     *
     * @return the value of city
     */
    public String getCity() {
        return city;
    }

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

    /**
     * Get the value of maidenName
     *
     * @return the value of maidenName
     */
    public String getMaidenName() {
        return maidenName;
    }

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

    /**
     * Get the value of patientName
     *
     * @return the value of patientName
     */
    public String getPatientName() {
        return patientName;
    }

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

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

    @Override
    public String toString() {
        return "[Personal data: DBRowID:" + personalId + "]";
    }

    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 doc) throws Exception {
        return toXml(doc, "");
    }

    public Node toXml(Document doc, String nameSpace) throws Exception {
        String prefix = nameSpace == null || nameSpace.isEmpty() ? "" : nameSpace + ":";
        Node n = doc.createElement(prefix + "personalIdentity");
        n.appendChild(XmlToBeanConverter.getNodeWithValue(doc, prefix + "patientName", patientName));
        n.appendChild(XmlToBeanConverter.getNodeWithValue(doc, prefix + "maidenName", maidenName));
        Element addr = doc.createElement(prefix + "address");
        addr.appendChild(XmlToBeanConverter.getNodeWithValue(doc, prefix + "zipCode", zipCode + ""));
        addr.appendChild(XmlToBeanConverter.getNodeWithValue(doc, prefix + "city", city));
        addr.appendChild(XmlToBeanConverter.getNodeWithValue(doc, prefix + "address", address));
        n.appendChild(addr);
        n.appendChild(XmlToBeanConverter.getNodeWithValue(doc, prefix + "mothersName", mothersName));
        n.appendChild(XmlToBeanConverter.getNodeWithValue(doc, prefix + "medicalId", tajNumber));
//        n.appendChild(XmlToBeanConverter.getNodeWithValue(doc, prefix + "logNumber", logNumber));
        return n;
    }

    public XmlToBeanConverter getXmlConverter() throws Exception {
        return new XmlToBeanConverter();
    }
    /**
     * Returns the JSON representation of this object
     * @return The JSON representation of this object
     */
    public String toJSON() {
        StringBuilder sb = new StringBuilder();
        sb.append("{ ");
        sb.append("\"id\": \"" + this.personalId + "\", \n");
        sb.append("\"patientName\": \"" + this.patientName + "\", \n");
        sb.append("\"maidenName\": \"" + this.maidenName + "\", \n");
        sb.append("\"zipCode\": \"" + this.zipCode + "\", \n");
        sb.append("\"city\": \"" + this.city + "\", \n");
        sb.append("\"address\": \"" + this.address + "\", \n");
        sb.append("\"mothersName\": \"" + this.mothersName + "\", \n");
        sb.append("\"medicatId\": \"" + this.tajNumber + "\" \n");
//        sb.append("\"logNumber\": \"" + this.logNumber + "\" \n");
        sb.append("}");
        return sb.toString();
    }
}
