Program: 87011
Project: 87011
Buggy source:
/** 
 * Update the environment entry in database. Old name of the environment should be passed if name has changed. If oldName is null, current name of the environment is taken and used for finding the records to update
 * @param context activity/service context
 * @param oldName The name of the environment to be modified
 * @return success code
 */
public boolean updateInDatabase(Context context,String oldName){
  if (oldName == null || oldName.isEmpty()) {
    oldName=getName();
  }
  Environment oldEnvironment=getFullEnvironment(context,oldName);
  if (oldEnvironment == null)   return false;
  ContentValues environmentValues=new ContentValues();
  environmentValues.put(EnvironmentEntry.COLUMN_NAME,getName());
  environmentValues.put(EnvironmentEntry.COLUMN_BLUETOOTH_ALL_OR_ANY,isBluetoothAllOrAny() ? 1 : 0);
  environmentValues.put(EnvironmentEntry.COLUMN_IS_ENABLED,isEnabled() ? 1 : 0);
  environmentValues.put(EnvironmentEntry.COLUMN_ENVIRONMENT_HINT,getHint());
  if (hasNoiseLevel && getNoiseLevelEnvironmentVariable() != null) {
    environmentValues.put(EnvironmentEntry.COLUMN_IS_MAX_NOISE_ENABLED,getNoiseLevelEnvironmentVariable().hasUpperLimit);
    environmentValues.put(EnvironmentEntry.COLUMN_IS_MIN_NOISE_ENABLED,getNoiseLevelEnvironmentVariable().hasLowerLimit);
    environmentValues.put(EnvironmentEntry.COLUMN_MAX_NOISE_LEVEL,getNoiseLevelEnvironmentVariable().getUpperLimit());
    environmentValues.put(EnvironmentEntry.COLUMN_MIN_NOISE_LEVEL,getNoiseLevelEnvironmentVariable().getLowerLimit());
  }
 else {
    environmentValues.put(EnvironmentEntry.COLUMN_IS_MAX_NOISE_ENABLED,false);
    environmentValues.put(EnvironmentEntry.COLUMN_IS_MIN_NOISE_ENABLED,false);
  }
  context.getContentResolver().update(EnvironmentEntry.buildEnvironmentUriWithId(oldEnvironment.id),environmentValues,null,null);
  if (oldEnvironment.hasLocation) {
    if (hasLocation && !oldEnvironment.locationEnvironmentVariable.equals(locationEnvironmentVariable)) {
      int updatedEntries=context.getContentResolver().update(EnvironmentEntry.buildEnvironmentUriWithIdAndLocation(oldEnvironment.id),locationEnvironmentVariable.getContentValues(),null,null);
      if (updatedEntries > 1) {
        Intent intentToRemoveOldGeofenceMonitor=BaseService.getServiceIntent(context,null,BaseService.ACTION_REMOVE_GEOFENCES);
        ArrayList<String> geofencesToRemove=new ArrayList<String>();
        geofencesToRemove.add(String.valueOf(oldEnvironment.getLocationEnvironmentVariable().getId()));
        intentToRemoveOldGeofenceMonitor.putExtra(BaseService.EXTRA_GEOFENCE_IDS_TO_REMOVE,geofencesToRemove);
        context.startService(intentToRemoveOldGeofenceMonitor);
      }
    }
 else     if (!hasLocation) {
      context.getContentResolver().delete(EnvironmentEntry.buildEnvironmentUriWithIdAndLocation(oldEnvironment.id),null,null);
    }
  }
 else   if (hasLocation) {
    context.getContentResolver().insert(EnvironmentEntry.buildEnvironmentUriWithIdAndLocation(oldEnvironment.id),locationEnvironmentVariable.getContentValues());
  }
  if (oldEnvironment.hasWiFiNetwork) {
    if (hasWiFiNetwork && !oldEnvironment.wiFiEnvironmentVariable.equals(wiFiEnvironmentVariable)) {
      context.getContentResolver().update(EnvironmentEntry.buildEnvironmentUriWithIdAndWifi(oldEnvironment.id),wiFiEnvironmentVariable.getContentValues(),null,null);
    }
 else     if (!hasWiFiNetwork) {
      context.getContentResolver().delete(EnvironmentEntry.buildEnvironmentUriWithIdAndWifi(oldEnvironment.id),null,null);
    }
  }
 else   if (hasWiFiNetwork) {
    context.getContentResolver().insert(EnvironmentEntry.buildEnvironmentUriWithIdAndWifi(oldEnvironment.id),wiFiEnvironmentVariable.getContentValues());
  }
  context.getContentResolver().delete(EnvironmentEntry.buildEnvironmentUriWithIdAndBluetooth(oldEnvironment.id),null,null);
  if (hasBluetoothDevices && getBluetoothEnvironmentVariables() != null && !getBluetoothEnvironmentVariables().isEmpty()) {
    Vector<BluetoothEnvironmentVariable> bluetoothEnvironmentVariables=getBluetoothEnvironmentVariables();
    Uri insertUri=EnvironmentEntry.buildEnvironmentUriWithIdAndBluetooth(oldEnvironment.id);
    for (    BluetoothEnvironmentVariable variable : bluetoothEnvironmentVariables) {
      ContentValues bluetoothValues=variable.getContentValues();
      context.getContentResolver().insert(insertUri,bluetoothValues);
    }
  }
  return true;
}

Fixed version (ground truth):
/** 
 * Update the environment entry in database. Old name of the environment should be passed if name has changed. If oldName is null, current name of the environment is taken and used for finding the records to update
 * @param context activity/service context
 * @param oldName The name of the environment to be modified
 * @return success code
 */
public boolean updateInDatabase(Context context,String oldName){
  if (oldName == null || oldName.isEmpty()) {
    oldName=getName();
  }
  Environment oldEnvironment=getFullEnvironment(context,oldName);
  if (oldEnvironment == null)   return false;
  ContentValues environmentValues=new ContentValues();
  environmentValues.put(EnvironmentEntry.COLUMN_NAME,getName());
  environmentValues.put(EnvironmentEntry.COLUMN_BLUETOOTH_ALL_OR_ANY,isBluetoothAllOrAny() ? 1 : 0);
  environmentValues.put(EnvironmentEntry.COLUMN_IS_ENABLED,isEnabled() ? 1 : 0);
  environmentValues.put(EnvironmentEntry.COLUMN_ENVIRONMENT_HINT,getHint());
  if (hasNoiseLevel && getNoiseLevelEnvironmentVariable() != null) {
    environmentValues.put(EnvironmentEntry.COLUMN_IS_MAX_NOISE_ENABLED,getNoiseLevelEnvironmentVariable().hasUpperLimit);
    environmentValues.put(EnvironmentEntry.COLUMN_IS_MIN_NOISE_ENABLED,getNoiseLevelEnvironmentVariable().hasLowerLimit);
    environmentValues.put(EnvironmentEntry.COLUMN_MAX_NOISE_LEVEL,getNoiseLevelEnvironmentVariable().getUpperLimit());
    environmentValues.put(EnvironmentEntry.COLUMN_MIN_NOISE_LEVEL,getNoiseLevelEnvironmentVariable().getLowerLimit());
  }
 else {
    environmentValues.put(EnvironmentEntry.COLUMN_IS_MAX_NOISE_ENABLED,false);
    environmentValues.put(EnvironmentEntry.COLUMN_IS_MIN_NOISE_ENABLED,false);
  }
  context.getContentResolver().update(EnvironmentEntry.buildEnvironmentUriWithId(oldEnvironment.id),environmentValues,null,null);
  if (oldEnvironment.hasLocation) {
    if (hasLocation && !oldEnvironment.locationEnvironmentVariable.equals(locationEnvironmentVariable)) {
      int updatedEntries=context.getContentResolver().update(EnvironmentEntry.buildEnvironmentUriWithIdAndLocation(oldEnvironment.id),locationEnvironmentVariable.getContentValues(),null,null);
      if (updatedEntries > 1) {
        removeFromCurrentGeofences(oldEnvironment.getLocationEnvironmentVariable(),context);
      }
    }
 else     if (!hasLocation) {
      int deletedEntries=context.getContentResolver().delete(EnvironmentEntry.buildEnvironmentUriWithIdAndLocation(oldEnvironment.id),null,null);
      if (deletedEntries > 1) {
        removeFromCurrentGeofences(oldEnvironment.getLocationEnvironmentVariable(),context);
      }
    }
  }
 else   if (hasLocation) {
    context.getContentResolver().insert(EnvironmentEntry.buildEnvironmentUriWithIdAndLocation(oldEnvironment.id),locationEnvironmentVariable.getContentValues());
  }
  if (oldEnvironment.hasWiFiNetwork) {
    if (hasWiFiNetwork && !oldEnvironment.wiFiEnvironmentVariable.equals(wiFiEnvironmentVariable)) {
      context.getContentResolver().update(EnvironmentEntry.buildEnvironmentUriWithIdAndWifi(oldEnvironment.id),wiFiEnvironmentVariable.getContentValues(),null,null);
    }
 else     if (!hasWiFiNetwork) {
      context.getContentResolver().delete(EnvironmentEntry.buildEnvironmentUriWithIdAndWifi(oldEnvironment.id),null,null);
    }
  }
 else   if (hasWiFiNetwork) {
    context.getContentResolver().insert(EnvironmentEntry.buildEnvironmentUriWithIdAndWifi(oldEnvironment.id),wiFiEnvironmentVariable.getContentValues());
  }
  context.getContentResolver().delete(EnvironmentEntry.buildEnvironmentUriWithIdAndBluetooth(oldEnvironment.id),null,null);
  if (hasBluetoothDevices && getBluetoothEnvironmentVariables() != null && !getBluetoothEnvironmentVariables().isEmpty()) {
    Vector<BluetoothEnvironmentVariable> bluetoothEnvironmentVariables=getBluetoothEnvironmentVariables();
    Uri insertUri=EnvironmentEntry.buildEnvironmentUriWithIdAndBluetooth(oldEnvironment.id);
    for (    BluetoothEnvironmentVariable variable : bluetoothEnvironmentVariables) {
      ContentValues bluetoothValues=variable.getContentValues();
      context.getContentResolver().insert(insertUri,bluetoothValues);
    }
  }
  return true;
}