package wenrgise.ejb.help.business;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.ejb.common.helper.DBObject;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.helper.QueryValue;
import wenrgise.ejb.common.utility.DBUtilitiesBean;
import wenrgise.help.bean.HelpChapterBean;
import wenrgise.help.bean.HelpComponentBean;
import wenrgise.help.bean.HelpContentBean;
import wenrgise.help.bean.HelpPartBean;

public class HelpBO {
  public static Logger log = Logger.getLogger("wenrgise.ejb.common.business.HelpBO");
  
  public ArrayList getComponentCodes(String sModuleName) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList oList = null;
    Iterator oIt = null;
    ArrayList oComponents = null;
    String sComp = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sModuleName));
    oParameters.add(new DBObject(2, 2, -10));
    oParameters.add(new DBObject(3, 2, 12));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HLPCONTENTDETAIL.proc_GetComponents(?,?,?,?,?)");
    oList = (ArrayList)((DBObject)oOutArray.get(0)).getObject();
    if (oList.size() == 0)
      oList = new ArrayList(); 
    count = 0;
    oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oComponents = new ArrayList(); 
      count++;
      oRow = oIt.next();
      sComp = new String();
      sComp = oRow.get("component_id").getString();
      oComponents.add(sComp);
    } 
    return oComponents;
  }
  
  public ArrayList getChapter(String sCompId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    HelpChapterBean oHelpChapterBean = null;
    int count = 0;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList oList = null;
    Iterator oIt = null;
    ArrayList oChapter = null;
    String sChap = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sCompId));
    oParameters.add(new DBObject(2, 2, -10));
    oParameters.add(new DBObject(3, 2, 12));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HLPCONTENTDETAIL.proc_GetChapter(?,?,?,?,?)");
    oList = (ArrayList)((DBObject)oOutArray.get(0)).getObject();
    if (oList.size() == 0) {
      oList = new ArrayList();
      oChapter = new ArrayList();
    } 
    count = 0;
    oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oChapter = new ArrayList(); 
      count++;
      oRow = oIt.next();
      oHelpChapterBean = new HelpChapterBean();
      oHelpChapterBean.setTxtChapId(oRow.get("id").getString());
      oHelpChapterBean.setTxtHeading(oRow.get("heading").getString());
      oHelpChapterBean.setTxtKeyword(oRow.get("keyword").getString());
      oChapter.add(oHelpChapterBean);
    } 
    return oChapter;
  }
  
  public ArrayList getPart(String sChapId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList oList = null;
    Iterator oIt = null;
    ArrayList oParts = null;
    String sPart = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sChapId));
    oParameters.add(new DBObject(2, 2, -10));
    oParameters.add(new DBObject(3, 2, 12));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HLPCONTENTDETAIL.proc_GetPart(?,?,?,?,?)");
    oList = (ArrayList)((DBObject)oOutArray.get(0)).getObject();
    if (oList.size() == 0) {
      oList = new ArrayList();
      oParts = new ArrayList();
    } 
    count = 0;
    oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oParts = new ArrayList(); 
      count++;
      oRow = oIt.next();
      sPart = new String();
      sPart = oRow.get("id").getString();
      oParts.add(sPart);
    } 
    return oParts;
  }
  
  public ArrayList getModule(String sModName) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList oList = null;
    Iterator oIt = null;
    ArrayList oCompList = null;
    ArrayList oModule = null;
    ArrayList oChapList = null;
    ArrayList oPartList = null;
    ArrayList oCompListDtl = null;
    ArrayList oModuleDtl = null;
    ArrayList oChapListDtl = null;
    ArrayList oPartListDtl = null;
    ArrayList oSubPartListDtl = null;
    HelpChapterBean oHelpChapterBean = new HelpChapterBean();
    HelpChapterBean oChapterBean = new HelpChapterBean();
    HelpComponentBean oHelpComponentBean = new HelpComponentBean();
    HelpPartBean oHelpPartBean = new HelpPartBean();
    String sCompId = "";
    String sChapId = "";
    String sPartId = "";
    String sPart = "";
    String sPartHeading = "";
    oCompList = getComponentCodes(sModName);
    oCompListDtl = new ArrayList();
    for (int i = 0; i < oCompList.size(); i++) {
      sCompId = oCompList.get(i);
      oHelpComponentBean = new HelpComponentBean();
      oHelpComponentBean.setTxtComponentCode(sCompId);
      oChapList = getChapter(sCompId);
      oChapListDtl = new ArrayList();
      for (int j = 0; j < oChapList.size(); j++) {
        oChapterBean = oChapList.get(j);
        sChapId = oChapterBean.getTxtChapId();
        oHelpChapterBean = new HelpChapterBean();
        oHelpChapterBean.setTxtHeading(oChapterBean.getTxtHeading());
        oHelpChapterBean.setTxtChapId(oChapterBean.getTxtChapId());
        oPartList = getPart(sChapId);
        oPartListDtl = new ArrayList();
        for (int k = 0; k < oPartList.size(); k++) {
          sPartId = oPartList.get(k);
          oHelpPartBean = new HelpPartBean();
          oParameters = new ArrayList();
          oParameters.add(new DBObject(1, 1, 12, sPartId));
          oParameters.add(new DBObject(2, 2, -10));
          oParameters.add(new DBObject(3, 2, 12));
          oParameters.add(new DBObject(4, 2, 12));
          oParameters.add(new DBObject(5, 2, 4));
          ArrayList oOutArray = oBean.callProc(oParameters, "HLPCONTENTDETAIL.proc_GetPartDtl(?,?,?,?,?)");
          oList = (ArrayList)((DBObject)oOutArray.get(0)).getObject();
          if (oList.size() == 0)
            oSubPartListDtl = new ArrayList(); 
          count = 0;
          oIt = oList.iterator();
          while (oIt.hasNext()) {
            if (count == 0)
              oSubPartListDtl = new ArrayList(); 
            count++;
            oRow = oIt.next();
            sPart = new String();
            sPartHeading = oRow.get("HEADING").getString();
            sPart = oRow.get("CONTENT").getString();
            oSubPartListDtl.add(sPart);
          } 
          oHelpPartBean.setTxtHeading(sPartHeading);
          oHelpPartBean.setArylstContent(oSubPartListDtl);
          oPartListDtl.add(oHelpPartBean);
        } 
        oHelpChapterBean.setArylstPart(oPartListDtl);
        oChapListDtl.add(oHelpChapterBean);
      } 
      oHelpComponentBean.setArylstChapter(oChapListDtl);
      oCompListDtl.add(oHelpComponentBean);
    } 
    return oCompListDtl;
  }
  
  public ArrayList getOneChapter(String sChapId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList oList = null;
    Iterator oIt = null;
    ArrayList oPartList = null;
    ArrayList oPartListDtl = null;
    ArrayList oSubPartListDtl = null;
    HelpPartBean oHelpPartBean = new HelpPartBean();
    HelpContentBean oHelpContentBean = new HelpContentBean();
    String sPartId = "";
    String sPart = "";
    String sPartHeading = "";
    oPartList = getPart(sChapId);
    oPartListDtl = new ArrayList();
    for (int k = 0; k < oPartList.size(); k++) {
      sPartId = oPartList.get(k);
      oHelpPartBean = new HelpPartBean();
      oParameters = new ArrayList();
      oParameters.add(new DBObject(1, 1, 12, sPartId));
      oParameters.add(new DBObject(2, 1, 12, sChapId));
      oParameters.add(new DBObject(3, 2, -10));
      oParameters.add(new DBObject(4, 2, 12));
      oParameters.add(new DBObject(5, 2, 12));
      oParameters.add(new DBObject(6, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "HLPCONTENTDETAIL.proc_GetPartDtl(?,?,?,?,?,?)");
      oList = (ArrayList)((DBObject)oOutArray.get(0)).getObject();
      if (oList.size() == 0) {
        oSubPartListDtl = new ArrayList();
      } else {
        count = 0;
        oIt = oList.iterator();
        while (oIt.hasNext()) {
          if (count == 0)
            oSubPartListDtl = new ArrayList(); 
          count++;
          oRow = oIt.next();
          oHelpContentBean = new HelpContentBean();
          sPartHeading = oRow.get("HEADING").getString();
          oHelpContentBean.setTxtContents(oRow.get("CONTENT").getString());
          oHelpContentBean.setTxtSecHeading(oRow.get("SECTION_NAME").getString());
          oSubPartListDtl.add(oHelpContentBean);
        } 
        oHelpPartBean.setTxtHeading(sPartHeading);
        oHelpPartBean.setArylstContent(oSubPartListDtl);
        oPartListDtl.add(oHelpPartBean);
      } 
    } 
    return oPartListDtl;
  }
}
