/**
   * Set Cell on the table
   *
   * @param pRowIndex
   * @param pColumnIndex
   * @param pCell
   * @throws PuException
   * @throws IndexOutOfBoundsException
   */
  public void setCell(int pRowIndex, int pColumnIndex, TCell pCell)
      throws PuException, IndexOutOfBoundsException {

    TRow row = _mListOfRows.get(pRowIndex);

    if (!row.getCell(pColumnIndex).getValueType().equals(pCell.getValueType())) {
      throw new PuException(
          _mContext,
          "New cell value type does not match expected value type."
              + " Expected type: "
              + row.getCell(pColumnIndex).getValueType()
              + " but was: "
              + pCell.getValueType());
    }
    row.setCell(pColumnIndex, pCell);
  }
  /**
   * get cell by Name
   *
   * @param pCellName
   * @return the cell
   */
  public TCell cellByName(String pCellName) {

    TCell lresult = null;
    if (_mSqliteDataBase != null) lresult = new TCell(_mContext, _mCDTStatus);
    else lresult = new TCell();

    int lColumnSize = getColumnsCount();

    for (int i = 0; i < lColumnSize && getRowsCount() != 0; i++) {
      if (_mListOfColumns.get(i).getName().equalsIgnoreCase(pCellName)) {
        lresult = getCell(_mPosition, i);
        lresult.setCDTStatus(_mCDTStatus);
        break;
      }
    }
    return lresult;
  }
  private void deleteFromDB() {

    if (_mWhereClause != null) {

      String[] lArgs = new String[_mWhereClauseColumns.length];

      for (int i = 0; i < _mWhereClauseColumns.length; i++) {
        TCell lCell = cellByName(_mWhereClauseColumns[i]);

        lArgs[i] = lCell.asString();
        if (lCell.asString().equals(""))
          Log.e(TAG_Error, "Valeur vide pour la clé de suppression :" + _mWhereClauseColumns[i]);
      }

      _mSqliteDataBase.delete(_mTableName, _mWhereClause.toString(), lArgs);
    } else PuUtils.showMessage(_mContext, "Erreur delete", "Clause where non definit");
  }
  private void updateInDB() {

    if (_mWhereClause != null) {
      ContentValues lValues = new ContentValues();
      String[] lArgs = new String[_mWhereClauseColumns.length];
      int lSize = _mListOfColumns.size();

      for (int i = 0; i < lSize; i++) {

        String lColumnName = _mListOfColumns.get(i).getName();
        TCell lCell = cellByName(lColumnName);
        String lColumnValue = lCell.asString();
        DBAccessMode lAccessMode = lCell.getDBAccessMode();

        if (lAccessMode == DBAccessMode.UPDATE) lValues.put(lColumnName, lColumnValue);
      }

      for (int i = 0; i < _mWhereClauseColumns.length; i++) {

        if (indexOfColumn(_mWhereClauseColumns[i]) == -1) {
          PuUtils.showMessage(
              _mContext,
              "Erreur Column",
              "Column "
                  + _mWhereClauseColumns[i]
                  + " de clause Where  "
                  + "n'est pas une column de client data table :"
                  + _mCDTName);
          return;
        } else if (lValues.containsKey(_mWhereClauseColumns[i])) {
          PuUtils.showMessage(
              _mContext,
              "Erreur Column",
              "impossible de definir la colonne "
                  + _mWhereClauseColumns[i]
                  + " de clause Where comme colonne à modifier dans le CDT:"
                  + _mCDTName);
          return;
        }
        TCell lCell = cellByName(_mWhereClauseColumns[i]);
        lArgs[i] = lCell.asString();
      }
      if (lValues.size() != 0)
        _mSqliteDataBase.update(_mTableName, lValues, _mWhereClause.toString(), lArgs);
    } else PuUtils.showMessage(_mContext, "Erreur Update", "Clause where non definit");
  }
  private void insertInDB() {

    ContentValues lValues = new ContentValues();

    int lSize = _mListOfColumns.size();

    for (int i = 0; i < lSize; i++) {

      String lColumnName = _mListOfColumns.get(i).getName();
      TCell lCell = cellByName(lColumnName);
      String lColumnValue = lCell.asString();
      DBAccessMode lAccessMode = lCell.getDBAccessMode();

      if (lAccessMode == DBAccessMode.INSERT) lValues.put(lColumnName, lColumnValue);
    }
    if (lValues.size() == 0) {
      PuUtils.showMessage(_mContext, "Erreur insertion", "Acune valeur définie pour inserer");
      return;
    }
    _mSqliteDataBase.insertOrThrow(_mTableName, null, lValues);
  }
    @Override
    public int compare(TRow pRow1, TRow pRow2) {

      int lCompare = 0;
      int lIndexOfCoulmn = 0;

      // init Cells to compare
      TCell lCellRow1 = pRow1.getCell(indexOfColumn(_mListOfSortedColumnsName[lIndexOfCoulmn]));
      TCell lCellRow2 = pRow2.getCell(indexOfColumn(_mListOfSortedColumnsName[lIndexOfCoulmn]));

      for (int i = 0; i < _mListOfSortedColumnsName.length; i++) {

        // Update cells with the differents values of columns names
        lCellRow1 = pRow1.getCell(indexOfColumn(_mListOfSortedColumnsName[i]));
        lCellRow2 = pRow2.getCell(indexOfColumn(_mListOfSortedColumnsName[i]));

        switch (lCellRow1.getValueType()) {
          case INTEGER:
            // If eguals , incremente lIndexOfColumn to pass to the next
            // columns comparaison
            if (lCellRow1.asInteger() == lCellRow2.asInteger())
              if (lIndexOfCoulmn < _mListOfSortedColumnsName.length - 1) lIndexOfCoulmn++;
              else break;
            break;
          case DOUBLE:
            // If eguals , incremente lIndexOfColumn to pass to the next columns comparaison
            if (lCellRow1.asDouble() == lCellRow2.asDouble())
              if (lIndexOfCoulmn < _mListOfSortedColumnsName.length - 1) lIndexOfCoulmn++;
              else break;
            break;

          case BOOLEAN:
          case TEXT:
            if (lCellRow1.asString().equals(lCellRow2.asString()))
              if (lIndexOfCoulmn < _mListOfSortedColumnsName.length - 1) lIndexOfCoulmn++;
              else break;
            break;

          default:
            break;
        }
        // Update cells with the differents values of columns names
        lCellRow1 = pRow1.getCell(indexOfColumn(_mListOfSortedColumnsName[lIndexOfCoulmn]));
        lCellRow2 = pRow2.getCell(indexOfColumn(_mListOfSortedColumnsName[lIndexOfCoulmn]));

        switch (lCellRow1.getValueType()) {
          case INTEGER:
            lCompare = lCellRow1.asInteger() - lCellRow2.asInteger();
            break;
          case DOUBLE:
            lCompare = Double.compare(lCellRow1.asDouble(), lCellRow2.asDouble());
            break;
          case BOOLEAN:
          case TEXT:
            lCompare = lCellRow1.asString().compareTo(lCellRow2.asString());
            break;
          default:
            break;
        }
      }
      // inverse sort
      if (_mSortType == TRowSortOrder.DESC) lCompare = -lCompare;

      return lCompare;
    }