<commit_before>/**
 * A tree wich displays an overview of all poiConfigs 
 */
qx.Class.define("qxex.ui.table.TableWithModelWidget", {
  extend : qx.ui.core.Widget,
  include : qx.locale.MTranslation, //for this.trc to work

  construct : function(TableConstructor,ModelConstructor){
    this.base(arguments);
    this._setLayout(new qx.ui.layout.Grow());
    this.__TableConstructor = TableConstructor;
    this.__ModelConstructor = ModelConstructor;
  },

  events : {
    "changeSelectionData" : "qx.event.type.Data",
    "cellTapData" : "qx.event.type.Data",
    "cellDbltapData" : "qx.event.type.Data",
    "cellContextmenuData" : "qx.event.type.Data"
  },

  members : {

    __primaryKeyColumnIdx : -1,

    _table : null,
    _model : null,
    _tcm : null,

    /**
     * Must be populated by deriving classes
     *
     * @type {Object}
     */
    _columns : null,

    /**
     * Will be filled with the objects in columns after a call to __setUpColumns();
     *
     * @type {Array} 
     */
    __columnsArr : null,


    /*
    ---------------------------------------------------------------------------
      PUBLIC METHODS
    ---------------------------------------------------------------------------
    */


    /**
     * Must be called after _columns contains all needed columns!
     */
    create : function(){

      this._model = new this.__ModelConstructor();
      this.__setUpColumns();
      this._table = new this.__TableConstructor(this._model);
      this._tcm = this._table.getTableColumnModel();

      // Set up column renderers
      for (key in this._columns){
        var col = this._columns[key];
        col.configure && col.configure.call(this,col.idx, this._model, this._tcm);
      }
      this._addListeners();

      //add children to widget
      this._add(this._table);
      return this;
    },

    setPrimaryKeyColumn : function(idx){
      if(this.__primaryKeyColumnIdx != -1){
        //can only be set once
      }else{
        this.__primaryKeyColumnIdx = idx;
        this._model.addIndex(idx);
      }
    },


    /*
    ---------------------------------------------------------------------------
      PUBLIC CRUD METHODS to modify state of rows. https://de.wikipedia.org/wiki/CRUD
      CRUD              SQL-92    REST
      -----------------------------------------
      Create            INSERT    PUT oder POST
      Read (Retrieve)   SELECT    GET
      Update            UPDATE    PATCH oder PUT
      Delete (Destroy)  DELETE    DELETE

      Access based on Primary Key!
    ---------------------------------------------------------------------------
    */


    insertData : function(primaryKey, data){
      var view = 0;

      var overwritingExistingData = this.deleteData(primaryKey);

      var row = this.__makeRowFromData(data);
      this._model.addRows([row],false);

      return overwritingExistingData;
    },

    readData : function(primaryKey){
      var view = 0;
      var rowIdx = this.__getRowIdxFromPrimaryKey(primaryKey,view);
      if(rowIdx != null){
        return this.__getDataFromRowIdx(rowIdx,view);
      }
      return null;
    },

    updateData : function(primaryKey, colIdx, value){
      var view = 0;
      var rowIdx = this.__getRowIdxFromPrimaryKey(primaryKey,view);
      if(rowIdx != null){
        this._model.setValue(colIdx,rowIdx,value,view);
        return true;
      }else{
        return false;
      }
    },

    deleteData : function(primaryKey){
      var view = 0;
      var rowIdx = this.__getRowIdxFromPrimaryKey(primaryKey,view);
      if(rowIdx != null){
        var retVal = this._model.removeRows(rowId,1,view);
        return true;
      }else{
        return false;
      }
    },

    //Convenience Bulk CRUD interface:
    deleteAllData : function(){
      this._model.clearAllRows();
    },

    /*<commit_msg>TableWithModelWidget now allows to further setup the model before the table constructor is called. Added Convenience bulk readAllData function<commit_after>/**
 * A tree wich displays an overview of all poiConfigs 
 */
qx.Class.define("qxex.ui.table.TableWithModelWidget", {
  extend : qx.ui.core.Widget,
  include : qx.locale.MTranslation, //for this.trc to work

  construct : function(TableConstructor,ModelConstructor){
    this.base(arguments);
    this._setLayout(new qx.ui.layout.Grow());
    this.__TableConstructor = TableConstructor;
    this.__ModelConstructor = ModelConstructor;
  },

  events : {
    "changeSelectionData" : "qx.event.type.Data",
    "cellTapData" : "qx.event.type.Data",
    "cellDbltapData" : "qx.event.type.Data",
    "cellContextmenuData" : "qx.event.type.Data"
  },

  members : {

    __primaryKeyColumnIdx : -1,

    _table : null,
    _model : null,
    _tcm : null,

    /**
     * Must be populated by deriving classes
     *
     * @type {Object}
     */
    _columns : null,

    /**
     * Will be filled with the objects in columns after a call to __setUpColumns();
     *
     * @type {Array} 
     */
    __columnsArr : null,


    /*
    ---------------------------------------------------------------------------
      PUBLIC METHODS
    ---------------------------------------------------------------------------
    */

    /**
     * if we want to further customize the model before a call to create;
     */
    createModel : function(){
      this._model = new this.__ModelConstructor();
    },


    /**
     * Must be called after _columns contains all needed columns!
     */
    create : function(){

      if(!this._model){
        this.createModel();
        this.__setUpColumns();
      }

      this._table = new this.__TableConstructor(this._model);
      this._tcm = this._table.getTableColumnModel();

      // Set up column renderers
      for (key in this._columns){
        var col = this._columns[key];
        col.configure && col.configure.call(this,col.idx, this._model, this._tcm);
      }
      this._addListeners();

      //add children to widget
      this._add(this._table);
      return this;
    },

    setPrimaryKeyColumn : function(idx){
      if(this.__primaryKeyColumnIdx != -1){
        //can only be set once
      }else{
        this.__primaryKeyColumnIdx = idx;
        this._model.addIndex(idx);
      }
    },


    /*
    ---------------------------------------------------------------------------
      PUBLIC CRUD METHODS to modify state of rows. https://de.wikipedia.org/wiki/CRUD
      CRUD              SQL-92    REST
      -----------------------------------------
      Create            INSERT    PUT oder POST
      Read (Retrieve)   SELECT    GET
      Update            UPDATE    PATCH oder PUT
      Delete (Destroy)  DELETE    DELETE

      Access based on Primary Key!
    ---------------------------------------------------------------------------
    */


    insertData : function(primaryKey, data){
      var view = 0;

      var overwritingExistingData = this.deleteData(primaryKey);

      var row = this.__makeRowFromData(data);
      this._model.addRows([row],false,false); //data changed

      return overwritingExistingData;
    },

    readData : function(primaryKey){
      var view = 0;
      var rowIdx = this.__getRowIdxFromPrimaryKey(primaryKey,view);
      if(rowIdx != null){
        return this.__getDataFromRowIdx(rowIdx,view);
      }
      return null;
    },

    updateData : function(primaryKey, colIdx, value){
      var view = 0;
      var rowIdx = this.__getRowIdxFromPrimaryKey(primaryKey,view);
      if(rowIdx != null){
        this._model.setValue(colIdx,rowIdx,value,view);
        return true;
      }else{
        return false;
      }
    },

    deleteData : function(primaryKey){
      var view = 0;
      var rowIdx = this.__getRowIdxFromPrimaryKey(primaryKey,view);
      if(rowIdx != null){
        var retVal = this._model.removeRows(rowId,1,view);
        return true;
      }else{
        return false;
      }
    },

    //Convenience Bulk CRUD interface:
    readAllData : function(){
      var array = [];
      var arrarr = this._model.getData();   //Simple.model.Default points this to __rowData, wich is the currently selected view and not all the data?? do we want this?
      for (var irow=0; irow<arrarr.length; irow++) {
        var row = arrarr[irow];
        var data = this.__extractDataFromRow(row);
        array.push(data);
      }
      return array;
    },

    deleteAllData : function(){
      this._model.clearAllRows();
    },

    /*
{'fraction_non_alphanumeric': 0.18306527909176915, 'fraction_numerical': 0.002246925260170293, 'mean_word_length': 1.728944820909971, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4147321', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2321, 'n_words': 554}