/* ----------------------------------------------------------------
 // Function Name : main
 // Date Written : 03/04/2008
 // Author : yogvinder
 // Input Parameters :
 // Output Parameters : None
 // Return Values : void
 // Description :
 // ---------------------------------------------------------------- */
 public static void main(String[] args) throws Exception {
   ServiceConfiguration.initLog();
   ServiceConfiguration.initServiceProperties(true);
   AttachAnnotationByFEAPForTransfer ap =
       new AttachAnnotationByFEAPForTransfer(
           ServiceConfiguration.serviceLog,
           "supervisor",
           "supervisor351",
           "division_060",
           "192.168.5.45",
           "3333");
   File f = new File("D:\\abc1.txt");
   FileReader fr = new FileReader(f);
   String contents = "";
   int x = 0;
   while ((x = fr.read()) != -1) {
     contents = contents + (char) x;
   }
   AnnotationBuffer abr = generateAnnotationBuffer(contents);
   System.out.println(abr);
   abr.removeAnnotation("1");
   System.out.println(abr);
 }
  // ---------------------------------------------------------------------------------------
  // Function Name : IntraDivisionProcessing
  // Date Written : 26/03/2009
  // Author : Utsav Turray
  // Input Parameters : CmdRowData commandRowData,String sourceCabinet
  // Output Parameters : None
  // Return Values : None
  // Description : To process Intra Division Cases
  //
  // ---------------------------------------------------------------------------------------
  public static void IntraDivisionProcessing(CmdRowData commandRowData, String sourceCabinet) {
    String strServerType = ServiceConfiguration.findServerType(sourceCabinet);
    String strStatusFlag = commandRowData.getStatusFlag();
    String strDestination = commandRowData.getDestination();
    try {
      ServiceConfiguration.serviceLog.info("Intra Division Processing Request Identidied.");
      if (strServerType.equalsIgnoreCase("branch")) {

        if (strStatusFlag.equalsIgnoreCase("01")) {

          if (sourceCabinet.equalsIgnoreCase(strDestination)) {

            String strCommand = commandRowData.getCommand();
            ServiceConfiguration.serviceLog.info("Processing to start for command : " + strCommand);
            /*---------------------------------------------------------------------------------------------
            Changed By : Ankur Bedi
            Reason / Cause (Bug No if Any): EDMS3.0-ERC-0040
            Date : 20/02/2012
               Change Description : selfacknowledge if success in processing
               -----------------------------------------------------------------------------------------------*/
            if (ProcessCmdData.processSynchCommand(commandRowData)) {
              selfAcknowledgeProcessedCall(commandRowData, SynchConstants.PROCESSED_STATE);
            }

          } else {
            ForwardCmdData.forwardToDivisionFromBranch(
                commandRowData,
                SynchConstants.INITIAL_STATE,
                SynchConstants.REQUEST_FORWARDED_STATE,
                SynchConstants.REQUEST_FORWARDED_STATE_MESSAGE_TO_DO);
          }
        } else if (strStatusFlag.equalsIgnoreCase("03")) {
          if (sourceCabinet.equalsIgnoreCase(strDestination))
            /*----------------------------------------------------------------------------
            Changed By : Ankur Bedi
            Reason / Cause (Bug No if Any): EDMS3.0-ERC-00033
            Date : 31/Oct/2011
               Change Description :Synch ack removal
               ----------------------------------------------------------------------------*/

            selfAcknowledgeProcessedCall(commandRowData, strStatusFlag);
          // forwardAcknowledgeComplete(commandRowData,sourceCabinet, SynchConstants.DIVISION_+
          // commandRowData.getDestinationDO().trim());
        } else if (strStatusFlag.equalsIgnoreCase("09"))
          processAcknowledgeCompletedLocally(
              commandRowData,
              sourceCabinet,
              SynchConstants.REQUEST_FORWARDED_STATE,
              SynchConstants.REQUEST_FORWARDED_STATE_COMPLETE,
              SynchConstants.REQUEST_FORWARDED_STATE_MESSAGE_TO_DO);

      } else if (strServerType.equalsIgnoreCase("division")) {

        if (strStatusFlag.equalsIgnoreCase("01")) {

          if (sourceCabinet.equalsIgnoreCase(strDestination))
            UpdateStatusLocally(
                commandRowData,
                SynchConstants.INITIAL_STATE,
                SynchConstants.PROCESSED_STATE,
                SynchConstants
                    .PROCESSED_STATE_MESSAGE); // To keep in synchronization with time based
                                               // synching
          else {
            String strCommand = commandRowData.getCommand();
            ServiceConfiguration.serviceLog.info("Processing to start for command : " + strCommand);
            ProcessCmdData.processSynchCommand(commandRowData);
          }

        } else if (strStatusFlag.equalsIgnoreCase("03")) {

          if (ForwardCmdData.forwardToBranchFromDivision(
              commandRowData,
              SynchConstants.PROCESSED_STATE,
              SynchConstants.FORWARDED_STATE,
              SynchConstants.FORWARDED_STATE_MESSAGE))
            selfAcknowledgeProcessedCall(commandRowData, SynchConstants.FORWARDED_STATE);
        } else if (strStatusFlag.equalsIgnoreCase("04")) {
          /*----------------------------------------------------------------------------
          Changed By : Ankur Bedi
          Reason / Cause (Bug No if Any):EDMS3.0-ERC-00033
          Date : 31/Oct/2011
             Change Description :Synch ack removal
             ----------------------------------------------------------------------------*/
          selfAcknowledgeProcessedCall(commandRowData, strStatusFlag);
        }
      }
    } catch (IOException ex) {
      ServiceConfiguration.serviceLog.info(
          "Error Occured for!! " + commandRowData.getUID() + " Printing Stack Trace in Error Log");
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", ex);
    } catch (NGException ex) {
      ServiceConfiguration.serviceLog.info(
          "Error Occured for !! " + commandRowData.getUID() + " Printing Stack Trace in Error Log");
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", ex);
    }
  }
  /* ----------------------------------------------------------------
  // Function Name    : CheckBODO
  // Date Written     : 29/03/2009
  // Author           : Utsav
  // Input Parameters :
  // Return Values    : boolean
  // Description      : CheckBODO Implemented
  // ---------------------------------------------------------------- */
  private static boolean CheckBODO(CmdRowData tempData) {
    boolean bCheckBODO = true;
    String strErrorMsg = "";
    if (tempData.getSourceBO().trim().equalsIgnoreCase("") || tempData.getSourceBO() == null) {
      bCheckBODO = false;
      strErrorMsg = strErrorMsg + " Source BO not found in command data.";
    }
    if (tempData.getSourceDO().trim().equalsIgnoreCase("") || tempData.getSourceDO() == null) {
      bCheckBODO = false;
      strErrorMsg = strErrorMsg + " Source DO not found in command data.";
    }
    if (tempData.getDestinationBO().trim().equalsIgnoreCase("")
        || tempData.getDestinationBO() == null) {
      bCheckBODO = false;
      strErrorMsg = strErrorMsg + " Destination BO not found in command data.";
    }
    if (tempData.getDestinationDO().trim().equalsIgnoreCase("")
        || tempData.getDestinationDO() == null) {
      bCheckBODO = false;
      strErrorMsg = strErrorMsg + " Destination DO not found in command data.";
    } else {
      if (tempData.getDestinationDO().trim().length() == 1) {
        tempData.setDestinatonDO("00" + tempData.getDestinationDO().trim());
      } else if (tempData.getDestinationDO().trim().length() == 2) {
        tempData.setDestinatonDO("0" + tempData.getDestinationDO().trim());
      }
    }
    if (bCheckBODO == false) {
      ServiceConfiguration.serviceLog.info("Error Occured for !! " + strErrorMsg);
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! " + strErrorMsg);
      strErrorMsg = "Error occured : " + strErrorMsg;

      try {
        UnderProcessFile.getReference()
            .setProperty(tempData.getUID(), SynchConstants.INITIAL_STATE);
        String inputXML =
            ServiceConfiguration.getUpdateSynchDataXML(
                tempData.getUID(), SynchConstants.INITIAL_STATE, strErrorMsg);
        String outputXML = ServiceConfiguration.executeLocalXML(inputXML);
        DMSXmlResponse xmlResponse = null;
        xmlResponse.setXmlString(outputXML);
        String statusValue = xmlResponse.getVal("Status");
        if (statusValue.equalsIgnoreCase(
            "0")) { // if local update is successful then  clear the Propessing File
          UnderProcessFile.getReference().remove(tempData.getUID());
        } else {
          // WE KEEP THE VALUE IN THE PROCESS FILE TO BE SAME STATE
          // SO THAT IT CAN BE UPDATED LATER..
        }
      } catch (IOException ex) {
        ServiceConfiguration.serviceLog.info(
            "Error Occured for !! " + tempData.getUID() + " Printing Stack Trace in Error Log");
        ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", ex);
      } catch (NGException ex) {
        ServiceConfiguration.serviceLog.info(
            "Error Occured for !! " + tempData.getUID() + " Printing Stack Trace in Error Log");
        ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", ex);
      }
    }
    return bCheckBODO;
  }
  /* ----------------------------------------------------------------
  // Function Name    : UpdateStatusLocally
  // Date Written     : 29/03/2009
  // Author           : Utsav
  // Input Parameters :
  // Return Values    : void
  // Description      : UpdateStatusLocally Implemented
  // ---------------------------------------------------------------- */
  public static void UpdateStatusLocally(
      CmdRowData tempData,
      String strIntitialState,
      String strSuccessState,
      String strSucessStateMessage) {
    ServiceConfiguration.serviceLog.info("############################");
    ServiceConfiguration.serviceLog.info("#Local Update Started# For UID :" + tempData.getUID());
    DMSXmlResponse xmlResponse = new DMSXmlResponse();
    String inputXML = "";
    String outputXML = "";
    String statusValue = "";
    // Error Handling if source is not in that format
    try {
      UnderProcessFile.getReference().setProperty(tempData.getUID(), strIntitialState);
      inputXML =
          ServiceConfiguration.getUpdateSynchDataXML(
              tempData.getUID(), strSuccessState, strSucessStateMessage);
      outputXML = ServiceConfiguration.executeLocalXML(inputXML);
      ServiceConfiguration.serviceXMLLog.info(inputXML);
      ServiceConfiguration.serviceXMLLog.info("OutputXML " + outputXML);
      xmlResponse.setXmlString(outputXML);
      statusValue = xmlResponse.getVal("Status");
      if (statusValue.equalsIgnoreCase("0")) {
        ServiceConfiguration.serviceLog.info(
            "#Local Update Completed Sucessfully# For UID :" + tempData.getUID());
        UnderProcessFile.getReference().remove(tempData.getUID());
      } else {
        ServiceConfiguration.serviceLog.info("#Local Update Process Failed#");
        ServiceConfiguration.serviceLog.info(
            "#Local Update Process Failed for :- " + tempData.getUID());
        String error = xmlResponse.getVal("Error");
        try {
          inputXML =
              ServiceConfiguration.getUpdateSynchDataXML(
                  tempData.getUID(), strIntitialState, error);
          outputXML = ServiceConfiguration.executeLocalXML(inputXML);
          xmlResponse.setXmlString(outputXML);
          statusValue = xmlResponse.getVal("Status");
        } catch (IOException e) {
          ServiceConfiguration.serviceLog.info(
              "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
          ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
          // Error Handling
        } catch (NGException e) {
          ServiceConfiguration.serviceLog.info(
              "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
          ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
        }
      }

    } catch (NGException e) {
      ServiceConfiguration.serviceLog.info(
          "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);

    } catch (IOException e) {
      ServiceConfiguration.serviceLog.info(
          "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
    }
  }
  //	 ---------------------------------------------------------------------------------------
  // Function Name : forwardAcknowledgeComplete
  // Date Written : 29/03/2009
  // Author : Utsav Turray
  // Input Parameters : none
  // Output Parameters : None
  // Return Values : None
  // Description : To acknowledge the forwarded data
  //
  // ---------------------------------------------------------------------------------------
  public static void forwardAcknowledgeComplete(
      CmdRowData tempData, String strLocalCabinet, String strDestinationCabinet) {
    ServiceConfiguration.serviceLog.info("############################");
    ServiceConfiguration.serviceLog.info(
        "#Forward and Acknowledge Process Started for UID "
            + tempData.getUID()
            + "  Source : "
            + tempData.getSource());
    DMSXmlResponse xmlResponse = new DMSXmlResponse();
    String inputXML = "";
    String outputXML = "";
    String statusValue = "";
    String destination = strDestinationCabinet.toLowerCase();

    try {

      inputXML =
          ServiceConfiguration.getForwardAckXML(
              tempData.getUID(),
              SynchConstants.B_ACK_STATE_SENT,
              SynchConstants.B_ACK_STATE_SENT_MESSAGE,
              destination);
      ServiceConfiguration.serviceXMLLog.info(inputXML);
      outputXML =
          ServiceConfiguration.executeRemoteXML(tempData.getStatusFlag(), destination, inputXML);
      ServiceConfiguration.serviceXMLLog.info("OutputXML " + outputXML);
      xmlResponse.setXmlString(outputXML);
      statusValue = xmlResponse.getVal("Status");
      if (statusValue.equalsIgnoreCase("0")) {
        UnderProcessFile.getReference()
            .setProperty(tempData.getUID(), SynchConstants.PROCESSED_ACK_SENT);
        inputXML =
            ServiceConfiguration.getForwardAckXML(
                tempData.getUID(),
                SynchConstants.PROCESSED_ACK_SENT,
                SynchConstants.B_ACK_STATE_RCVD_MESSAGE,
                strLocalCabinet);
        outputXML = ServiceConfiguration.executeLocalXML(inputXML);
        xmlResponse.setXmlString(outputXML);
        statusValue = xmlResponse.getVal("Status");
        if (statusValue.equalsIgnoreCase("0")) {
          ServiceConfiguration.serviceLog.info(
              "#Process and Acknowlged Completed Sucessfully# for UID "
                  + tempData.getUID()
                  + "  Source : "
                  + tempData.getSource());
        } else {
          ServiceConfiguration.serviceLog.info(
              "#Process and Acknowledge Process failed for UID "
                  + tempData.getUID()
                  + "  Source : "
                  + tempData.getSource());
        }
        if (statusValue.equalsIgnoreCase("0")) { // if local
          // update is
          // successful
          // then clear
          // the
          // Processing
          // File
          UnderProcessFile.getReference().remove(tempData.getUID());
        } else {
          // WE KEEP THE VALUE IN THE PROCESS FILE TO BE SAME
          // STATE SO THAT IT CAN BE UPDATED LATER..
        }
      } else {
        ServiceConfiguration.serviceLog.info(
            "#Unable to Acknowledge the Synchronization Request for :- "
                + tempData.getUID()
                + "  Source : "
                + tempData.getSource());
        String error = xmlResponse.getVal("Error");
        try {
          inputXML =
              ServiceConfiguration.getUpdateSynchDataXML(
                  tempData.getUID(),
                  SynchConstants.PROCESSED_STATE,
                  "Unable to Acknowledge the Synchronization Request");
          outputXML = ServiceConfiguration.executeLocalXML(inputXML);
          xmlResponse.setXmlString(outputXML);
          statusValue = xmlResponse.getVal("Status");
        } catch (IOException e) {
          ServiceConfiguration.serviceLog.info(
              "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
          ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
          // Error Handling
        } catch (NGException e) {
          ServiceConfiguration.serviceLog.info(
              "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
          ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
        }
      }

    } catch (NGException e) {
      ServiceConfiguration.serviceLog.info(
          "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);

    } catch (IOException e) {
      ServiceConfiguration.serviceLog.info(
          "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
    }
  }
  // ---------------------------------------------------------------------------------------
  // Function Name : selfAcknowledge
  // Date Written : 31/10/2011
  // Author : Ankur Bedi
  // Input Parameters : none
  // Output Parameters : None
  // Return Values : None
  // Description : To self acknowledge after forwarding to destination or after processing of
  // forwarded request from source
  // EDMS3.0-ERC-00033
  // ---------------------------------------------------------------------------------------
  private static void selfAcknowledgeProcessedCall(CmdRowData tempData, String STATE) {

    ServiceConfiguration.serviceLog.info("############################");
    ServiceConfiguration.serviceLog.info(
        "#Self Acknowledge Process Started for UID " + tempData.getUID());

    DMSXmlResponse xmlResponse = new DMSXmlResponse();
    String inputXML = "";
    String outputXML = "";
    String statusValue = "";
    String ACK_STATE = "";
    String ACK_MESSAGE = "";
    String localCabinet = ServiceConfiguration.getProperty("localcabinetname");
    try {

      if (STATE.equalsIgnoreCase(SynchConstants.FORWARDED_STATE)) {

        ACK_STATE = SynchConstants.PROCESSED_ACK;
        ACK_MESSAGE = SynchConstants.PROCESSED_ACK_MESSAGE;
      } else if (STATE.equalsIgnoreCase(SynchConstants.PROCESSED_STATE)) {
        ACK_STATE = SynchConstants.PROCESSED_SYNCHED_ACK;
        ACK_MESSAGE = SynchConstants.PROCESSED_SYNCHED_ACK_MESSAGE;
      } else {
        return;
      }

      inputXML =
          ServiceConfiguration.getForwardAckXML(
              tempData.getUID(), ACK_STATE, ACK_MESSAGE, localCabinet);
      ServiceConfiguration.serviceXMLLog.info(inputXML);
      outputXML = ServiceConfiguration.executeLocalXML(inputXML);
      ServiceConfiguration.serviceXMLLog.info("OutputXML " + outputXML);
      xmlResponse.setXmlString(outputXML);
      statusValue = xmlResponse.getVal("Status");
      if (statusValue.equalsIgnoreCase("0")) {
        UnderProcessFile.getReference().remove(tempData.getUID());
        ServiceConfiguration.serviceLog.info(
            "#Local Acknowlgment Completed Sucessfully# for UID " + tempData.getUID());
      } else {

        ServiceConfiguration.serviceLog.info(
            "#Unable to Local Acknowledge the Synchronization Request for :- " + tempData.getUID());
        String error = xmlResponse.getVal("Error");
        if (error == null) {
          error = "Status:" + statusValue;
        }
        try {

          inputXML = ServiceConfiguration.getUpdateSynchDataXML(tempData.getUID(), STATE, error);
          outputXML = ServiceConfiguration.executeLocalXML(inputXML);
          xmlResponse.setXmlString(outputXML);
          statusValue = xmlResponse.getVal("Status");
        } catch (IOException e) {
          ServiceConfiguration.serviceLog.info(
              "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
          ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
          // Error Handling
        } catch (NGException e) {
          ServiceConfiguration.serviceLog.info(
              "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
          ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
        }
      }

    } catch (NGException e) {
      ServiceConfiguration.serviceLog.info(
          "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);

    } catch (IOException e) {
      ServiceConfiguration.serviceLog.info(
          "Error Occured!!" + tempData.getUID() + "Printing Stack Trace in Error Log");
      ServiceConfiguration.serviceErrorLog.info("Error Occured!! ", e);
    }
  }