/*
 * 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.Table;
import javax.servlet.http.HttpServletRequest;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * The Domain object of the Anonymous datas
 * @author inagy
 */
@Entity
@Table(name = "debgen_anonymousdata"/*, uniqueConstraints = {@UniqueConstraint(columnNames = {"userName", "email"})}*/)
public class AnonymousData implements ToucanBean, Serializable, XmlConvertable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long anonymousId;
    private String monogram;

    /**
     * Get the value of monogram
     *
     * @return the value of monogram
     */
    public String getMonogram() {
        return monogram;
    }

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

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

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

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

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

    /**
     * Returns the walue of the id
     * @return id
     */
    public Long getId() {
        return anonymousId;
    }

    /**
     * Generates the monogram from the name
     * @param name
     */
    public void generateMonogram(String name) {
        String[] split = name.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < split.length; i++) {
            if (split[i].length() > 0) {
                sb.append(split[i].charAt(0));
            }
        }
        this.setMonogram(sb.toString().toUpperCase());
    }

    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 + "monogram", monogram));
        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.anonymousId + "\", \n");
        sb.append("\"monogram\": \"" + this.monogram + "\" \n");
        sb.append("}");
        return sb.toString();
    }
}
