Program: 35500
Project: 35500
Buggy source:
/** 
 * Update the project identified by <code>projectId</code> with the given values.
 * @param values Values to update.
 * @param projectId Identifier of the project to update.
 * @param context User context.
 * @param comment Update comment.
 * @throws CommandException If an error occurs during update.
 */
@Transactional(rollbackOn=CommandException.class) protected void updateProject(final List<ValueEventWrapper> values,final Integer projectId,UserExecutionContext context,String comment) throws CommandException {
  final Date historyDate=new Date();
  final User user=context.getUser();
  final Project project=em().find(Project.class,projectId);
  if (project != null) {
    if (!Handlers.isProjectEditable(project,user)) {
      throw new IllegalStateException();
    }
  }
 else {
    OrgUnit orgUnit=em().find(OrgUnit.class,projectId);
    if (!Handlers.isOrgUnitVisible(orgUnit,user)) {
      throw new IllegalStateException();
    }
  }
  final List<String> conflicts=searchForConflicts(project,values,context);
  boolean coreVersionHasBeenModified=false;
  for (  final ValueEventWrapper valueEvent : values) {
    final FlexibleElementDTO source=valueEvent.getSourceElement();
    final FlexibleElement element=em().find(FlexibleElement.class,source.getId());
    final TripletValueDTO updateListValue=valueEvent.getTripletValue();
    final String updateSingleValue=valueEvent.getSingleValue();
    final Set<Integer> multivaluedIdsValue=valueEvent.getMultivaluedIdsValue();
    final boolean isProjectCountryChanged=valueEvent.isProjectCountryChanged();
    final Integer iterationId=valueEvent.getIterationId();
    LOG.debug("String_Node_Str",source.getId(),source.getEntityName());
    LOG.debug("String_Node_Str",valueEvent.getChangeType(),updateSingleValue,updateListValue,iterationId);
    coreVersionHasBeenModified=coreVersionHasBeenModified || element != null && element.isAmendable();
    if (source instanceof DefaultFlexibleElementDTO && !((DefaultFlexibleElementType.BUDGET.equals(((DefaultFlexibleElementDTO)source).getType())))) {
      final DefaultFlexibleElementDTO defaultElement=(DefaultFlexibleElementDTO)source;
      LOG.debug("String_Node_Str",defaultElement.getType());
      final String oldValue=saveDefaultElement(projectId,defaultElement.getType(),updateSingleValue,isProjectCountryChanged);
      List<HistoryToken> results=null;
      if (element != null) {
        final TypedQuery<HistoryToken> query=em().createQuery("String_Node_Str",HistoryToken.class);
        query.setParameter("String_Node_Str",element.getId());
        query.setParameter("String_Node_Str",projectId);
        results=query.getResultList();
      }
      if (results == null || results.isEmpty()) {
        final Date oldDate;
        final User oldOwner;
        if (project != null) {
          oldDate=project.getLastSchemaUpdate();
          oldOwner=project.getOwner();
        }
 else {
          oldDate=new Date(historyDate.getTime() - 1);
          oldOwner=null;
        }
        if (oldValue != null) {
          historize(oldDate,element,projectId,null,oldOwner,ValueEventChangeType.ADD,oldValue,null,null);
        }
      }
      historize(historyDate,element,projectId,null,user,ValueEventChangeType.EDIT,updateSingleValue,null,comment);
      continue;
    }
    final Value currentValue=retrieveOrCreateValue(projectId,source.getId(),iterationId,user);
    if (updateSingleValue != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("String_Node_Str");
      }
      currentValue.setValue(updateSingleValue);
      historize(historyDate,element,projectId,iterationId,user,ValueEventChangeType.EDIT,updateSingleValue,null,comment);
    }
 else     if (multivaluedIdsValue != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("String_Node_Str");
      }
      Set<Integer> currentIds;
      if (currentValue != null && currentValue.getValue() != null && !currentValue.getValue().isEmpty()) {
        currentIds=new HashSet<>(ValueResultUtils.splitValuesAsInteger(currentValue.getValue()));
      }
 else {
        currentIds=Collections.emptySet();
      }
switch (valueEvent.getChangeType()) {
case ADD:
        currentIds.addAll(multivaluedIdsValue);
      break;
case REMOVE:
    currentIds.removeAll(multivaluedIdsValue);
  break;
case EDIT:
currentIds=multivaluedIdsValue;
break;
default :
throw new IllegalStateException();
}
String serializedValue=ValueResultUtils.mergeElements(new ArrayList<Integer>(currentIds));
currentValue.setValue(serializedValue);
if (valueEvent.getChangeType() == ValueEventChangeType.EDIT) {
historize(historyDate,element,projectId,iterationId,user,valueEvent.getChangeType(),serializedValue,null,comment);
}
 else {
for (Integer id : multivaluedIdsValue) {
historize(historyDate,element,projectId,iterationId,user,valueEvent.getChangeType(),String.valueOf(id),null,comment);
}
}
}
 else {
if (LOG.isDebugEnabled()) {
LOG.debug("String_Node_Str");
}
final List<Integer> ids=ValueResultUtils.splitValuesAsInteger(currentValue.getValue());
if (LOG.isDebugEnabled()) {
LOG.debug("String_Node_Str" + ids + "String_Node_Str");
}
switch (valueEvent.getChangeType()) {
case ADD:
onAdd(updateListValue,ids,currentValue,historyDate,element,projectId,iterationId,user,comment);
break;
case REMOVE:
if (!onDelete(updateListValue,ids,currentValue,historyDate,element,projectId,iterationId,user,comment)) {
continue;
}
break;
case EDIT:
onEdit(updateListValue,historyDate,element,projectId,iterationId,user,comment);
break;
default :
LOG.debug("String_Node_Str" + valueEvent.getChangeType() + "String_Node_Str");
break;
}
LOG.debug("String_Node_Str" + ids + "String_Node_Str");
}
em().merge(currentValue);
}
final Project updatedProject=em().find(Project.class,projectId);
if (updatedProject != null) {
OrgUnit newOrgUnit=updatedProject.getOrgUnit();
if (newOrgUnit != null) {
final UserPermissionPolicy permissionPolicy=injector.getInstance(UserPermissionPolicy.class);
permissionPolicy.deleteUserPemissionByProject(projectId);
permissionPolicy.updateUserPermissionByOrgUnit(newOrgUnit);
}
if (coreVersionHasBeenModified) {
updatedProject.setAmendmentRevision(updatedProject.getAmendmentRevision() == null ? 2 : updatedProject.getAmendmentRevision() + 1);
em().merge(updatedProject);
}
}
if (!conflicts.isEmpty()) {
throw new UpdateConflictException(updatedProject.toContainerInformation(),conflicts.toArray(new String[0]));
}
}

Fixed version (ground truth):
/** 
 * Update the project identified by <code>projectId</code> with the given values.
 * @param values Values to update.
 * @param projectId Identifier of the project to update.
 * @param context User context.
 * @param comment Update comment.
 * @throws CommandException If an error occurs during update.
 */
@Transactional(rollbackOn=CommandException.class) protected void updateProject(final List<ValueEventWrapper> values,final Integer projectId,UserExecutionContext context,String comment) throws CommandException {
  final Date historyDate=new Date();
  final User user=context.getUser();
  final Project project=em().find(Project.class,projectId);
  if (project != null) {
    if (!Handlers.isProjectEditable(project,user)) {
      throw new IllegalStateException();
    }
  }
 else {
    OrgUnit orgUnit=em().find(OrgUnit.class,projectId);
    if (!Handlers.isOrgUnitVisible(orgUnit,user)) {
      throw new IllegalStateException();
    }
  }
  final List<String> conflicts=searchForConflicts(project,values,context);
  boolean coreVersionHasBeenModified=false;
  for (  final ValueEventWrapper valueEvent : values) {
    final FlexibleElementDTO source=valueEvent.getSourceElement();
    final FlexibleElement element=em().find(FlexibleElement.class,source.getId());
    final TripletValueDTO updateListValue=valueEvent.getTripletValue();
    final String updateSingleValue=valueEvent.getSingleValue();
    final Set<Integer> multivaluedIdsValue=valueEvent.getMultivaluedIdsValue();
    final boolean isProjectCountryChanged=valueEvent.isProjectCountryChanged();
    final Integer iterationId=valueEvent.getIterationId();
    LOG.debug("String_Node_Str",source.getId(),source.getEntityName());
    LOG.debug("String_Node_Str",valueEvent.getChangeType(),updateSingleValue,updateListValue,iterationId);
    coreVersionHasBeenModified=coreVersionHasBeenModified || element != null && element.isAmendable();
    if (source instanceof DefaultFlexibleElementDTO && !((DefaultFlexibleElementType.BUDGET.equals(((DefaultFlexibleElementDTO)source).getType())))) {
      final DefaultFlexibleElementDTO defaultElement=(DefaultFlexibleElementDTO)source;
      LOG.debug("String_Node_Str",defaultElement.getType());
      final String oldValue=saveDefaultElement(projectId,defaultElement.getType(),updateSingleValue,isProjectCountryChanged);
      List<HistoryToken> results=null;
      if (element != null) {
        final TypedQuery<HistoryToken> query=em().createQuery("String_Node_Str",HistoryToken.class);
        query.setParameter("String_Node_Str",element.getId());
        query.setParameter("String_Node_Str",projectId);
        results=query.getResultList();
      }
      if (results == null || results.isEmpty()) {
        final Date oldDate;
        final User oldOwner;
        if (project != null) {
          oldDate=project.getLastSchemaUpdate();
          oldOwner=project.getOwner();
        }
 else {
          oldDate=new Date(historyDate.getTime() - 1);
          oldOwner=null;
        }
        if (oldValue != null) {
          historize(oldDate,element,projectId,null,oldOwner,ValueEventChangeType.ADD,oldValue,null,null);
        }
      }
      historize(historyDate,element,projectId,null,user,ValueEventChangeType.EDIT,updateSingleValue,null,comment);
      continue;
    }
    final Value currentValue=retrieveOrCreateValue(projectId,source.getId(),iterationId,user);
    if (updateSingleValue != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("String_Node_Str");
      }
      currentValue.setValue(updateSingleValue);
      historize(historyDate,element,projectId,iterationId,user,ValueEventChangeType.EDIT,updateSingleValue,null,comment);
    }
 else     if (multivaluedIdsValue != null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("String_Node_Str");
      }
      Set<Integer> currentIds=new HashSet<>();
      if (currentValue != null && currentValue.getValue() != null && !currentValue.getValue().isEmpty()) {
        currentIds.addAll(ValueResultUtils.splitValuesAsInteger(currentValue.getValue()));
      }
switch (valueEvent.getChangeType()) {
case ADD:
        currentIds.addAll(multivaluedIdsValue);
      break;
case REMOVE:
    currentIds.removeAll(multivaluedIdsValue);
  break;
case EDIT:
currentIds=multivaluedIdsValue;
break;
default :
throw new IllegalStateException();
}
String serializedValue=ValueResultUtils.mergeElements(new ArrayList<Integer>(currentIds));
currentValue.setValue(serializedValue);
if (valueEvent.getChangeType() == ValueEventChangeType.EDIT) {
historize(historyDate,element,projectId,iterationId,user,valueEvent.getChangeType(),serializedValue,null,comment);
}
 else {
for (Integer id : multivaluedIdsValue) {
historize(historyDate,element,projectId,iterationId,user,valueEvent.getChangeType(),String.valueOf(id),null,comment);
}
}
}
 else {
if (LOG.isDebugEnabled()) {
LOG.debug("String_Node_Str");
}
final List<Integer> ids=ValueResultUtils.splitValuesAsInteger(currentValue.getValue());
if (LOG.isDebugEnabled()) {
LOG.debug("String_Node_Str" + ids + "String_Node_Str");
}
switch (valueEvent.getChangeType()) {
case ADD:
onAdd(updateListValue,ids,currentValue,historyDate,element,projectId,iterationId,user,comment);
break;
case REMOVE:
if (!onDelete(updateListValue,ids,currentValue,historyDate,element,projectId,iterationId,user,comment)) {
continue;
}
break;
case EDIT:
onEdit(updateListValue,historyDate,element,projectId,iterationId,user,comment);
break;
default :
LOG.debug("String_Node_Str" + valueEvent.getChangeType() + "String_Node_Str");
break;
}
LOG.debug("String_Node_Str" + ids + "String_Node_Str");
}
em().merge(currentValue);
}
final Project updatedProject=em().find(Project.class,projectId);
if (updatedProject != null) {
OrgUnit newOrgUnit=updatedProject.getOrgUnit();
if (newOrgUnit != null) {
final UserPermissionPolicy permissionPolicy=injector.getInstance(UserPermissionPolicy.class);
permissionPolicy.deleteUserPemissionByProject(projectId);
permissionPolicy.updateUserPermissionByOrgUnit(newOrgUnit);
}
if (coreVersionHasBeenModified) {
updatedProject.setAmendmentRevision(updatedProject.getAmendmentRevision() == null ? 2 : updatedProject.getAmendmentRevision() + 1);
em().merge(updatedProject);
}
}
if (!conflicts.isEmpty()) {
throw new UpdateConflictException(updatedProject.toContainerInformation(),conflicts.toArray(new String[0]));
}
}