Program: 73638
Project: 73638
Buggy source:
/** 
 * Save the model to the database, together with the references changes  to its parent models. 
 * @param isNew Whether this is a new model. 
 * @param id The id of this model. 
 * @param parentsMaintainOldVersion The list of parents names that do not want to reflect the changes to the submodel they have.  
 * @param newVersionName The new name of this saving model, to have those parent models maintaining the old reference.
 * @throws Exception Thrown if errors occur during the saving. 
 */
private void _commitSave(boolean isNew,String id,ArrayList<String> parentsMaintainOldVersion,String newVersionName) throws Exception {
  String newName=_attributesListPanel.getModelName();
  _modelToSave.setName(newName);
  _updateDisplayedModel();
  if (isNew || id == null) {
    if (_modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR) != null) {
      _modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR).setContainer(null);
      try {
        MoMLChangeRequest change=new MoMLChangeRequest(this,null,_modelToSave.exportMoML());
        change.setUndoable(true);
        _modelToSave.requestChange(change);
      }
 catch (      Exception e) {
        _resetValues();
        throw e;
      }
    }
  }
  if (_xmlModel == null) {
    _xmlModel=new XMLDBModel(_modelToSave.getName());
  }
  _xmlModel.setModelName(_modelToSave.getName());
  _xmlModel.setModel(_modelToSave.exportMoML());
  _xmlModel.setIsNew(isNew);
  _xmlModel.setModelId(id);
  XMLDBModelWithReferenceChanges xmlDBModelWithReferenceChanges=new XMLDBModelWithReferenceChanges(_xmlModel,parentsMaintainOldVersion,newVersionName);
  try {
    String modelId=_saveModelManager.saveWithParents(xmlDBModelWithReferenceChanges);
    if (modelId != null) {
      JOptionPane.showMessageDialog(this,"String_Node_Str","String_Node_Str",JOptionPane.INFORMATION_MESSAGE,null);
      if (_modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR) == null) {
        StringConstantParameter dbModelParam=new StringConstantParameter(_modelToSave,XMLDBModel.DB_MODEL_ID_ATTR);
        dbModelParam.setExpression(modelId);
        dbModelParam.setContainer(_modelToSave);
      }
 else       if (!((StringParameter)_modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR)).getExpression().equals(modelId)) {
        ((StringParameter)_modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR)).setExpression(modelId);
      }
      try {
        MoMLChangeRequest change=new MoMLChangeRequest(this,null,_modelToSave.exportMoML());
        change.setUndoable(true);
        _modelToSave.requestChange(change);
        _source.setTitle(_xmlModel.getModelName());
        try {
          _source.updateDBModelHistory(_xmlModel.getModelName(),false);
        }
 catch (        IOException e) {
        }
      }
 catch (      Exception e) {
        throw e;
      }
      if (parentsMaintainOldVersion != null && parentsMaintainOldVersion.size() > 0) {
        XMLDBModel newVersionModel=DBModelFetcher.load(newVersionName);
        for (        String parentName : parentsMaintainOldVersion) {
          if (_source.getConfiguration().getDirectory().getEntity(parentName) != null) {
            PtolemyEffigy parentModelEffigy=(PtolemyEffigy)_source.getConfiguration().getDirectory().getEffigy(parentName);
            for (            Object entity : parentModelEffigy.entityList()) {
              ComponentEntity componentEntity=(ComponentEntity)entity;
              if (Utilities.getIdFromModel(componentEntity) != null && Utilities.getIdFromModel(componentEntity).equals(modelId)) {
                StringParameter modelIdAttribute=(StringParameter)componentEntity.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR);
                modelIdAttribute.setToken(newVersionModel.getModelId());
                componentEntity.setName(parentModelEffigy.uniqueName(newVersionModel.getModelName()));
              }
            }
            MoMLChangeRequest change=new MoMLChangeRequest(null,parentModelEffigy.getModel(),parentModelEffigy.getModel().exportMoML());
            change.setUndoable(true);
            parentModelEffigy.getModel().requestChange(change);
          }
        }
      }
      if (_parentValidateFrame != null) {
        ArrayList<String> parentsModelsMaintainReferences=_parentValidateFrame._getParentsMaintainReferences();
        if (parentsModelsMaintainReferences != null && parentsModelsMaintainReferences.size() > 0) {
          XMLDBModel savedModel=DBModelFetcher.loadUsingId(modelId);
          for (          String parentName : parentsModelsMaintainReferences) {
            if (_source.getConfiguration().getDirectory().getEntity(parentName) != null) {
              PtolemyEffigy parentModelEffigy=(PtolemyEffigy)_source.getConfiguration().getDirectory().getEffigy(parentName);
              for (              Object entity : parentModelEffigy.entityList()) {
                ComponentEntity componentEntity=(ComponentEntity)entity;
                if (Utilities.getIdFromModel(componentEntity) != null && Utilities.getIdFromModel(componentEntity).equals(modelId)) {
                  MoMLChangeRequest change=new MoMLChangeRequest(null,componentEntity,savedModel.getModel());
                  change.setUndoable(true);
                  componentEntity.requestChange(change);
                }
              }
              MoMLChangeRequest change=new MoMLChangeRequest(null,parentModelEffigy.getModel(),parentModelEffigy.getModel().exportMoML());
              change.setUndoable(true);
              parentModelEffigy.getModel().requestChange(change);
            }
          }
        }
      }
      _source.setModified(false);
      if (_parentValidateFrame != null) {
        _parentValidateFrame.dispose();
      }
      dispose();
    }
 else {
      JOptionPane.showMessageDialog(this,"String_Node_Str","String_Node_Str",JOptionPane.INFORMATION_MESSAGE,null);
    }
  }
 catch (  DBConnectionException exception) {
    _resetValues();
    throw exception;
  }
catch (  DBExecutionException exception) {
    _resetValues();
    throw exception;
  }
catch (  IllegalArgumentException exception) {
    _resetValues();
    throw exception;
  }
catch (  ModelAlreadyExistException exception) {
    _resetValues();
    JOptionPane.showMessageDialog(this,"String_Node_Str" + "String_Node_Str" + "String_Node_Str");
    _rollbackModel();
  }
}

Fixed version (ground truth):
/** 
 * Save the model to the database, together with the references changes  to its parent models. 
 * @param isNew Whether this is a new model. 
 * @param id The id of this model. 
 * @param parentsMaintainOldVersion The list of parents names that do not want to reflect the changes to the submodel they have.  
 * @param newVersionName The new name of this saving model, to have those parent models maintaining the old reference.
 * @throws Exception Thrown if errors occur during the saving. 
 */
private void _commitSave(boolean isNew,String id,ArrayList<String> parentsMaintainOldVersion,String newVersionName) throws Exception {
  String newName=_attributesListPanel.getModelName();
  _modelToSave.setName(newName);
  _updateDisplayedModel();
  if (isNew || id == null) {
    if (_modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR) != null) {
      _modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR).setContainer(null);
      try {
        MoMLChangeRequest change=new MoMLChangeRequest(this,null,_modelToSave.exportMoML());
        change.setUndoable(true);
        _modelToSave.requestChange(change);
      }
 catch (      Exception e) {
        _resetValues();
        throw e;
      }
    }
  }
  if (_xmlModel == null) {
    _xmlModel=new XMLDBModel(_modelToSave.getName());
  }
  _xmlModel.setModelName(_modelToSave.getName());
  _xmlModel.setModel(_modelToSave.exportMoML());
  _xmlModel.setIsNew(isNew);
  _xmlModel.setModelId(id);
  XMLDBModelWithReferenceChanges xmlDBModelWithReferenceChanges=new XMLDBModelWithReferenceChanges(_xmlModel,parentsMaintainOldVersion,newVersionName);
  try {
    String modelId=_saveModelManager.saveWithParents(xmlDBModelWithReferenceChanges);
    if (modelId != null) {
      JOptionPane.showMessageDialog(this,"String_Node_Str","String_Node_Str",JOptionPane.INFORMATION_MESSAGE,null);
      if (_modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR) == null) {
        StringConstantParameter dbModelParam=new StringConstantParameter(_modelToSave,XMLDBModel.DB_MODEL_ID_ATTR);
        dbModelParam.setExpression(modelId);
        dbModelParam.setContainer(_modelToSave);
      }
 else       if (!((StringParameter)_modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR)).getExpression().equals(modelId)) {
        ((StringParameter)_modelToSave.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR)).setExpression(modelId);
      }
      try {
        MoMLChangeRequest change=new MoMLChangeRequest(this,null,_modelToSave.exportMoML());
        change.setUndoable(true);
        _modelToSave.requestChange(change);
        _source.setTitle(_xmlModel.getModelName());
        try {
          _source.updateDBModelHistory(_xmlModel.getModelName(),false);
        }
 catch (        IOException e) {
        }
      }
 catch (      Exception e) {
        throw e;
      }
      if (parentsMaintainOldVersion != null && parentsMaintainOldVersion.size() > 0) {
        XMLDBModel newVersionModel=DBModelFetcher.load(newVersionName);
        for (        String parentName : parentsMaintainOldVersion) {
          if (_source.getConfiguration().getDirectory().getEntity(parentName) != null) {
            PtolemyEffigy parentModelEffigy=(PtolemyEffigy)_source.getConfiguration().getDirectory().getEffigy(parentName);
            boolean modifiedFlag=parentModelEffigy.isModified();
            for (            Object entity : ((CompositeEntity)parentModelEffigy.getModel()).entityList()) {
              ComponentEntity componentEntity=(ComponentEntity)entity;
              if (Utilities.getIdFromModel(componentEntity) != null && Utilities.getIdFromModel(componentEntity).equals(modelId)) {
                StringParameter modelIdAttribute=(StringParameter)componentEntity.getAttribute(XMLDBModel.DB_MODEL_ID_ATTR);
                modelIdAttribute.setExpression(newVersionModel.getModelId());
                componentEntity.setName(parentModelEffigy.getModel().uniqueName(newVersionModel.getModelName()));
                String momlString=componentEntity.exportMoML();
                if (componentEntity instanceof CompositeEntity) {
                  CompositeEntity compositeEntity=(CompositeEntity)componentEntity;
                  compositeEntity.removeAllEntities();
                }
                MoMLChangeRequest change=new MoMLChangeRequest(null,parentModelEffigy.getModel(),momlString);
                change.setUndoable(true);
                parentModelEffigy.getModel().requestChange(change);
              }
            }
            if (!modifiedFlag) {
              parentModelEffigy.setModified(false);
            }
          }
        }
      }
      if (_parentValidateFrame != null) {
        ArrayList<String> parentsModelsMaintainReferences=_parentValidateFrame._getParentsMaintainReferences();
        if (parentsModelsMaintainReferences != null && parentsModelsMaintainReferences.size() > 0) {
          XMLDBModel savedModel=DBModelFetcher.loadUsingId(modelId);
          MoMLParser parser=new MoMLParser();
          parser.resetAll();
          Entity savedSubModel=(Entity)parser.parse(savedModel.getModel());
          StringParameter referenceAttribute=null;
          if (savedSubModel.getAttribute(XMLDBModel.DB_REFERENCE_ATTR) != null) {
            referenceAttribute=(StringParameter)savedSubModel.getAttribute(XMLDBModel.DB_REFERENCE_ATTR);
          }
 else {
            referenceAttribute=new StringParameter(savedSubModel,XMLDBModel.DB_REFERENCE_ATTR);
          }
          referenceAttribute.setExpression("String_Node_Str");
          for (          String parentName : parentsModelsMaintainReferences) {
            if (_source.getConfiguration().getDirectory().getEntity(parentName) != null) {
              PtolemyEffigy parentModelEffigy=(PtolemyEffigy)_source.getConfiguration().getDirectory().getEffigy(parentName);
              boolean modifiedFlag=parentModelEffigy.isModified();
              for (              Object entity : ((CompositeEntity)parentModelEffigy.getModel()).entityList()) {
                ComponentEntity componentEntity=(ComponentEntity)entity;
                if (Utilities.getIdFromModel(componentEntity) != null && Utilities.getIdFromModel(componentEntity).equals(modelId)) {
                  savedSubModel.setName(componentEntity.getName());
                  Location location=(Location)savedSubModel.getAttribute("String_Node_Str");
                  if (location == null) {
                    location=new Location(savedSubModel,"String_Node_Str");
                  }
                  location.setExpression(((Location)componentEntity.getAttribute("String_Node_Str")).getExpression());
                  String newMoml=savedSubModel.exportMoML();
                  if (componentEntity instanceof CompositeEntity) {
                    CompositeEntity compositeEntity=(CompositeEntity)componentEntity;
                    compositeEntity.removeAllEntities();
                  }
                  MoMLChangeRequest change=new MoMLChangeRequest(null,parentModelEffigy.getModel(),newMoml);
                  change.setUndoable(true);
                  parentModelEffigy.getModel().requestChange(change);
                }
              }
              if (!modifiedFlag) {
                parentModelEffigy.setModified(false);
              }
            }
          }
        }
      }
      _source.setModified(false);
      if (_parentValidateFrame != null) {
        _parentValidateFrame.dispose();
      }
      dispose();
    }
 else {
      JOptionPane.showMessageDialog(this,"String_Node_Str","String_Node_Str",JOptionPane.INFORMATION_MESSAGE,null);
    }
  }
 catch (  DBConnectionException exception) {
    _resetValues();
    throw exception;
  }
catch (  DBExecutionException exception) {
    _resetValues();
    throw exception;
  }
catch (  IllegalArgumentException exception) {
    _resetValues();
    throw exception;
  }
catch (  ModelAlreadyExistException exception) {
    _resetValues();
    JOptionPane.showMessageDialog(this,"String_Node_Str" + "String_Node_Str" + "String_Node_Str");
    _rollbackModel();
  }
}