// $Id$
/*
 * Copyright 2010 Institute for Systems Biology
 *                Seattle, Washington, USA.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package biotextEngine.xmlparsers.medline;

import java.sql.Types;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import biotextEngine.xmlparsers.NodeHandler;

/**
 * @author Gaurav Bhalotia
 * @author Ariel Schwartz
 */
public class MeshHeading extends NodeHandler{

    protected String pmid = null;

    static final String[] columnNameDef = {
        "pmid",
        "descriptor_name",
        "descriptor_name_major_yn"
    };

    static final String[] xmlElementNameDef = {
        "PMID",
        "MeshHeading.DescriptorName",
        "MeshHeading.DescriptorName.MajorTopicYN"
    };

    static final int[] columnTypeDef = {
        Types.INTEGER,
        Types.VARCHAR,
        Types.VARCHAR
    };

    public MeshHeading(String pmid) throws Exception{
	
        tableName = "medline_mesh_heading";

        columnName = columnNameDef;
        xmlElementName = xmlElementNameDef;
        columnType = columnTypeDef;

        initialize();

        /* Add any data that does not come through XML to the hashtable */
        putColumnValue("PMID", pmid);

        this.pmid = pmid;

    }

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {

        NodeHandler handler;
        String descriptor = null;

        try{
            if (currentElement != null) {
                if (descriptor == null)
                    descriptor = getColumnValue("MeshHeading.DescriptorName");

                if (qName.equals("QualifierName")){
                    handler = new MeshHeadingQualifier(pmid, descriptor);
                    setContentHandler(handler, namespaceURI, localName, qName, atts);
                }
                else {
                    super.startElement(namespaceURI, localName, qName, atts);
                }
            } else {
                super.startElement(namespaceURI, localName, qName, atts);
            }
        }catch (Exception e) {
            e.printStackTrace();
            throw new SAXException("Problem creating qualifier for Mesh descriptor");
        }
    }
}
