<commit_before>// Copyright (C) 2015 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and The University
// of Manchester.
// All rights reserved.

/*
 * InsertSpecieRowsCommand.cpp
 *
 *  Created on: 27 Aug 2014
 *      Author: dada
 */

#include "CopasiDataModel/CCopasiDataModel.h"
#include "report/CCopasiRootContainer.h"
#include "model/CMetab.h"
//#include "model/CReactionInterface.h"
#include "model/CModel.h"
#include "CQSpecieDM.h"

#include "InsertSpeciesRowsCommand.h"
#include "UndoSpeciesData.h"

InsertSpecieRowsCommand::InsertSpecieRowsCommand(int position, int rows, CQSpecieDM * pSpecieDM)
  : CCopasiUndoCommand("Species", SPECIES_INSERT)
  , mpSpecieDM(pSpecieDM)
  , mRows(rows)
  , mPosition(position)
  , mIndex()
  , mpSpeciesData()
  , mValue()
{
  setText(QObject::tr(": Inserted new species"));
}

InsertSpecieRowsCommand::InsertSpecieRowsCommand(int position, int rows, CQSpecieDM *pSpecieDM, const QModelIndex& index, const QVariant& value)
  : CCopasiUndoCommand("Species", SPECIES_INSERT)
  , mpSpecieDM(pSpecieDM)
  , mRows(rows)
  , mPosition(position)
  , mIndex(index)
  , mpSpeciesData()
  , mValue(value)
{
  setText(QObject::tr(": Inserted new species"));
}

void InsertSpecieRowsCommand::redo()
{
  if (mpSpeciesData.count() == 0)
    {
      mpSpeciesData = mpSpecieDM->insertNewSpecieRow(mPosition, mRows, mIndex, mValue);
    }
  else
    {
      mpSpecieDM->insertSpecieRows(mpSpeciesData);
    }

  setUndoState(true);
  setAction("Add to list");
  setName(mpSpeciesData[0]->getName());
}

void InsertSpecieRowsCommand::undo()
{
  mpSpecieDM->deleteSpecieRows(mpSpeciesData);
  setUndoState(false);
  setAction("Remove from list");
}

InsertSpecieRowsCommand::~InsertSpecieRowsCommand()
{
  foreach(UndoSpeciesData * data, mpSpeciesData)
  {
    pdelete(data);
  }
  mpSpeciesData.clear();
}<commit_msg>- issue 2499: don't crash on failure to create an element<commit_after>// Copyright (C) 2015 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and The University
// of Manchester.
// All rights reserved.

/*
 * InsertSpecieRowsCommand.cpp
 *
 *  Created on: 27 Aug 2014
 *      Author: dada
 */

#include "CopasiDataModel/CCopasiDataModel.h"
#include "report/CCopasiRootContainer.h"
#include "model/CMetab.h"
//#include "model/CReactionInterface.h"
#include "model/CModel.h"
#include "CQSpecieDM.h"

#include "InsertSpeciesRowsCommand.h"
#include "UndoSpeciesData.h"

InsertSpecieRowsCommand::InsertSpecieRowsCommand(int position, int rows, CQSpecieDM * pSpecieDM)
  : CCopasiUndoCommand("Species", SPECIES_INSERT)
  , mpSpecieDM(pSpecieDM)
  , mRows(rows)
  , mPosition(position)
  , mIndex()
  , mpSpeciesData()
  , mValue()
{
  setText(QObject::tr(": Inserted new species"));
}

InsertSpecieRowsCommand::InsertSpecieRowsCommand(int position, int rows, CQSpecieDM *pSpecieDM, const QModelIndex& index, const QVariant& value)
  : CCopasiUndoCommand("Species", SPECIES_INSERT)
  , mpSpecieDM(pSpecieDM)
  , mRows(rows)
  , mPosition(position)
  , mIndex(index)
  , mpSpeciesData()
  , mValue(value)
{
  setText(QObject::tr(": Inserted new species"));
}

void InsertSpecieRowsCommand::redo()
{
  if (mpSpeciesData.count() == 0)
    {
      mpSpeciesData = mpSpecieDM->insertNewSpecieRow(mPosition, mRows, mIndex, mValue);
    }
  else
    {
      mpSpecieDM->insertSpecieRows(mpSpeciesData);
    }

  setUndoState(true);
  setAction("Add to list");

  if (!mpSpeciesData.empty())
    setName(mpSpeciesData[0]->getName());
}

void InsertSpecieRowsCommand::undo()
{
  mpSpecieDM->deleteSpecieRows(mpSpeciesData);
  setUndoState(false);
  setAction("Remove from list");
}

InsertSpecieRowsCommand::~InsertSpecieRowsCommand()
{
  foreach(UndoSpeciesData * data, mpSpeciesData)
  {
    pdelete(data);
  }
  mpSpeciesData.clear();
}
{'fraction_non_alphanumeric': 0.13192818110850899, 'fraction_numerical': 0.007285974499089253, 'mean_word_length': 5.140575079872204, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '6074112', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1362, 'n_words': 248}