private CreditLogRec updateCreditMarginInVault(
      String tradingSystem, long tradeId, int version, String tokenStatus)
      throws IOException, NamingException, BadLocationException, SQLException {

    CreditLogRec clr = new CreditLogRec();
    clr.setTradeId(tradeId);
    clr.setProcessFlag("N");
    clr.setCmt("No Record Found");
    TradeRqmtRec trr = dao.getTradeConfirmId(tradingSystem, tradeId, version);
    if (trr.getTradeRqmtConfirmId() > 0) {
      String creditMarginToken = "";
      if ("Y".equalsIgnoreCase(tokenStatus)) {
        creditMarginToken = getCreditMarginToken(tradingSystem, tradeId, version);
      }
      Logger.getLogger(CreditMarginProcessor.class)
          .info("Trade Id = " + tradeId + "; Token = " + creditMarginToken);
      ContractRequest cr = new ContractRequest();
      cr.setTradeRqmtConfirmId(trr.getTradeRqmtConfirmId());
      ContractResponse response = confirmation.getContractFromVault(cr, dbUserName);
      // update the vault only if the contract in the vault
      if (response.getContract() != null & !"".equalsIgnoreCase(response.getContract())) {
        String marginInsertedContract =
            rtfInserter.insertMarginToken(
                response.getContract(), creditMarginToken, marginToken, clr);
        storeContractInVault(trr, marginInsertedContract);
        if ("CRDT".equalsIgnoreCase(trr.getCurrentStatus())) {
          dao.updateTradeRqmtStatus(trr);
          sendEMail(trr);
        }
        clr.setProcessFlag("Y");
      } else {
        clr.setCmt("No Contract Found in the Vault");
      }

    } else {
      Logger.getLogger(this.getClass())
          .info("No Rqmt confirm id found in db for the trade " + tradeId);
    }
    return clr;
  }
  private void storeContractInVault(TradeRqmtRec trr, String marginInsertedContract) {

    ContractRequest cr = new ContractRequest();
    cr.setTradeSysCode(trr.getTradesystem());
    cr.setTemplateId(trr.getTemplateId());
    cr.setSempraCptySn(trr.getSeCptySn());
    cr.setCptySn(trr.getCptySn());
    cr.setDateSent(getDateSent());
    cr.setSignedFlag(trr.getSignedFlag());
    cr.setTradeId(trr.getTradeId());
    cr.setRqmtId(trr.getTradeRqmtId());
    cr.setTradeRqmtConfirmId(trr.getTradeRqmtConfirmId());
    cr.setTradeDate(trr.getTradeDate());
    cr.setCdtyCode(trr.getCdtyCode());
    cr.setCdtyGroupCode(trr.getCdtyGroupCode());
    cr.setSettlementType(trr.getSttlType());
    cr.setContract(marginInsertedContract);
    ContractResponse response = confirmation.storeContractInVault(cr, dbUserName);

    log.info("Store Contract response status = " + response.getResponseStatus());
    log.info("Store Contract response = " + response.getResponseText());
  }