package com.k_int.webapp3;

import com.k_int.bank.service.dto.TermInfoDTO;
import com.k_int.bank.service.dto.VocabInfoDTO;
import com.k_int.commons.util.XMLUtil;
import com.k_int.webapp3.util.DTOToDoc;
import com.k_int.zthes.service.impl.ZThesServiceImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;

import javax.xml.parsers.ParserConfigurationException;

import java.util.List;


public class GetVocabAction extends AbstractXmlOrHtmlAction {
  private static final long serialVersionUID = 1L;
  private final Log log = LogFactory.getLog(GetVocabAction.class);
  private String auth_id;
  private String vocab_id;
  private VocabInfoDTO vocab;
  private List<TermInfoDTO> top_terms;
    
  public void setAuthId(String auth_id)                     { this.auth_id = auth_id;   }
  public String getAuthId()                                 { return auth_id;           }
   
  public void setVocabId(String vocab_id)                   { this.vocab_id = vocab_id; }
  public String getVocabId()                                { return this.vocab_id;     }
  
  public List<TermInfoDTO> getTopTerms()                    { return top_terms;         }
  public VocabInfoDTO getVocab()                            { return vocab;             }
  

  public String responseSpecificExecute() throws Exception {
    log.debug("Getting zthes service, fetching vocab and top terms");
    ZThesServiceImpl svc = (ZThesServiceImpl) ctx.getBean("ZThesService");
    vocab                = svc.lookupVocab(vocab_id, auth_id);

    if (vocab!=null) {
      if (response_type.equals("xml")) { 
        try {
          doc = XMLUtil.getEmptyDocument();
          Element zthes = DTOToDoc.getZThesElement(doc);
          zthes.appendChild(DTOToDoc.getVocabThes(doc, vocab));
          doc.appendChild(zthes);
        } catch (ParserConfigurationException pce) {
          pce.printStackTrace();
        } 
      }

      return SUCCESS+"-"+response_type;
    }
    
    return ERROR+"-"+response_type;
  }
}