Program: 35621
Project: 35621
Buggy source:
private void saveLayoutGroups(final List<LayoutGroup> layoutGroups,EntityManager em,HashMap<Object,Object> modelesReset,HashSet<Object> modelesImport,Authentication authentication){
  final HashSet<Integer> reportModelsId=new HashSet<Integer>();
  if (layoutGroups != null) {
    for (    LayoutGroup layoutGroup : layoutGroups) {
      final List<LayoutConstraint> layoutConstraints;
      if (layoutGroup != null)       layoutConstraints=layoutGroup.getConstraints();
 else       layoutConstraints=null;
      if (layoutConstraints != null) {
        for (        LayoutConstraint layoutConstraint : layoutConstraints) {
          if (layoutConstraint != null && layoutConstraint.getElement() != null) {
            if (em.contains(layoutConstraint.getElement())) {
              continue;
            }
            if (layoutConstraint.getElement() instanceof QuestionElement) {
              List<QuestionChoiceElement> questionChoiceElements=((QuestionElement)layoutConstraint.getElement()).getChoices();
              CategoryType type=((QuestionElement)layoutConstraint.getElement()).getCategoryType();
              if (questionChoiceElements != null || type != null) {
                FlexibleElement parent=(FlexibleElement)layoutConstraint.getElement();
                ((QuestionElement)parent).setChoices(null);
                ((QuestionElement)parent).setCategoryType(null);
                em.persist(parent);
                if (questionChoiceElements != null) {
                  for (                  QuestionChoiceElement questionChoiceElement : questionChoiceElements) {
                    if (questionChoiceElement != null) {
                      questionChoiceElement.setId(null);
                      questionChoiceElement.setParentQuestion((QuestionElement)parent);
                      CategoryElement categoryElement=questionChoiceElement.getCategoryElement();
                      if (categoryElement != null) {
                        questionChoiceElement.setCategoryElement(null);
                        em.persist(questionChoiceElement);
                        saveProjectModelCategoryElement(categoryElement,em,modelesReset,modelesImport);
                        questionChoiceElement.setCategoryElement(categoryElement);
                        em.merge(questionChoiceElement);
                      }
 else {
                        em.persist(questionChoiceElement);
                      }
                    }
                  }
                  ((QuestionElement)parent).setChoices(questionChoiceElements);
                }
                if (type != null) {
                  if (em.find(CategoryType.class,type.getId()) == null) {
                    List<CategoryElement> typeElements=type.getElements();
                    if (typeElements != null) {
                      type.setElements(null);
                      em.merge(type);
                      for (                      CategoryElement element : typeElements) {
                        if (em.find(CategoryElement.class,element.getId()) == null) {
                          element.setParentType(type);
                          saveProjectModelCategoryElement(element,em,modelesReset,modelesImport);
                        }
                      }
                      type.setElements(typeElements);
                      em.merge(type);
                    }
                  }
                  ((QuestionElement)parent).setCategoryType(type);
                }
                em.merge(parent);
              }
 else {
                em.persist(layoutConstraint.getElement());
              }
            }
 else             if (layoutConstraint.getElement() instanceof ReportElement) {
              final ReportElement element=(ReportElement)layoutConstraint.getElement();
              final ProjectReportModel oldModel=element.getModel();
              if (oldModel != null) {
                final int oldModelId=oldModel.getId();
                final ProjectReportModel newModel;
                if (!reportModelsId.contains(oldModelId)) {
                  oldModel.resetImport(new HashMap<Object,Object>(),new HashSet<Object>());
                  oldModel.setOrganization(authentication.getUser().getOrganization());
                  newModel=oldModel;
                  ProjectReportModelHandler.saveProjectReportModelElement(newModel,em);
                  em.persist(newModel);
                  element.setModel(newModel);
                  em.persist(element);
                  reportModelsId.add(element.getModel().getId());
                }
 else {
                  newModel=em.find(ProjectReportModel.class,oldModelId);
                  element.setModel(newModel);
                  em.persist(element);
                }
              }
            }
 else             if (layoutConstraint.getElement() instanceof ReportListElement) {
              final ReportListElement element=(ReportListElement)layoutConstraint.getElement();
              final ProjectReportModel oldModel=element.getModel();
              if (oldModel != null) {
                final int oldModelId=oldModel.getId();
                final ProjectReportModel newModel;
                if (!reportModelsId.contains(oldModelId)) {
                  oldModel.resetImport(new HashMap<Object,Object>(),new HashSet<Object>());
                  oldModel.setOrganization(authentication.getUser().getOrganization());
                  newModel=oldModel;
                  em.persist(element);
                  reportModelsId.add(element.getModel().getId());
                }
 else {
                  newModel=em.find(ProjectReportModel.class,oldModelId);
                  element.setModel(newModel);
                  em.persist(element);
                }
              }
            }
 else {
              em.persist(layoutConstraint.getElement());
            }
          }
        }
      }
    }
  }
}

Fixed version (ground truth):
private void saveLayoutGroups(final List<LayoutGroup> layoutGroups,EntityManager em,HashMap<Object,Object> modelesReset,HashSet<Object> modelesImport,Authentication authentication){
  final HashSet<Integer> reportModelsId=new HashSet<Integer>();
  if (layoutGroups != null) {
    for (    LayoutGroup layoutGroup : layoutGroups) {
      final List<LayoutConstraint> layoutConstraints;
      if (layoutGroup != null)       layoutConstraints=layoutGroup.getConstraints();
 else       layoutConstraints=null;
      if (layoutConstraints != null) {
        for (        LayoutConstraint layoutConstraint : layoutConstraints) {
          if (layoutConstraint != null && layoutConstraint.getElement() != null) {
            if (em.contains(layoutConstraint.getElement())) {
              continue;
            }
            if (layoutConstraint.getElement() instanceof QuestionElement) {
              List<QuestionChoiceElement> questionChoiceElements=((QuestionElement)layoutConstraint.getElement()).getChoices();
              CategoryType type=((QuestionElement)layoutConstraint.getElement()).getCategoryType();
              if (questionChoiceElements != null || type != null) {
                FlexibleElement parent=(FlexibleElement)layoutConstraint.getElement();
                ((QuestionElement)parent).setChoices(null);
                ((QuestionElement)parent).setCategoryType(null);
                em.persist(parent);
                if (questionChoiceElements != null) {
                  for (                  QuestionChoiceElement questionChoiceElement : questionChoiceElements) {
                    if (questionChoiceElement != null) {
                      questionChoiceElement.setId(null);
                      questionChoiceElement.setParentQuestion((QuestionElement)parent);
                      CategoryElement categoryElement=questionChoiceElement.getCategoryElement();
                      if (categoryElement != null) {
                        questionChoiceElement.setCategoryElement(null);
                        em.persist(questionChoiceElement);
                        saveProjectModelCategoryElement(categoryElement,em,modelesReset,modelesImport);
                        questionChoiceElement.setCategoryElement(categoryElement);
                        em.merge(questionChoiceElement);
                      }
 else {
                        em.persist(questionChoiceElement);
                      }
                    }
                  }
                  ((QuestionElement)parent).setChoices(questionChoiceElements);
                }
                if (type != null) {
                  if (em.find(CategoryType.class,type.getId()) == null) {
                    List<CategoryElement> typeElements=type.getElements();
                    if (typeElements != null) {
                      type.setElements(null);
                      em.merge(type);
                      for (                      CategoryElement element : typeElements) {
                        if (em.find(CategoryElement.class,element.getId()) == null) {
                          element.setParentType(type);
                          saveProjectModelCategoryElement(element,em,modelesReset,modelesImport);
                        }
                      }
                      type.setElements(typeElements);
                      em.merge(type);
                    }
                  }
                  ((QuestionElement)parent).setCategoryType(type);
                }
                em.merge(parent);
              }
 else {
                em.persist(layoutConstraint.getElement());
              }
            }
 else             if (layoutConstraint.getElement() instanceof ReportElement) {
              final ReportElement element=(ReportElement)layoutConstraint.getElement();
              final ProjectReportModel oldModel=element.getModel();
              if (oldModel != null) {
                final int oldModelId=oldModel.getId();
                final ProjectReportModel newModel;
                if (!reportModelsId.contains(oldModelId)) {
                  oldModel.resetImport(new HashMap<Object,Object>(),new HashSet<Object>());
                  oldModel.setOrganization(authentication.getUser().getOrganization());
                  newModel=oldModel;
                  ProjectReportModelHandler.saveProjectReportModelElement(newModel,em);
                  em.persist(newModel);
                  element.setModel(newModel);
                  em.persist(element);
                  reportModelsId.add(element.getModel().getId());
                }
 else {
                  newModel=em.find(ProjectReportModel.class,oldModelId);
                  element.setModel(newModel);
                  em.persist(element);
                }
              }
            }
 else             if (layoutConstraint.getElement() instanceof ReportListElement) {
              final ReportListElement element=(ReportListElement)layoutConstraint.getElement();
              final ProjectReportModel oldModel=element.getModel();
              if (oldModel != null) {
                final int oldModelId=oldModel.getId();
                final ProjectReportModel newModel;
                if (!reportModelsId.contains(oldModelId)) {
                  oldModel.resetImport(new HashMap<Object,Object>(),new HashSet<Object>());
                  oldModel.setOrganization(authentication.getUser().getOrganization());
                  newModel=oldModel;
                  ProjectReportModelHandler.saveProjectReportModelElement(newModel,em);
                  em.persist(newModel);
                  element.setModel(newModel);
                  em.persist(element);
                  reportModelsId.add(element.getModel().getId());
                }
 else {
                  newModel=em.find(ProjectReportModel.class,oldModelId);
                  element.setModel(newModel);
                  em.persist(element);
                }
              }
            }
 else {
              em.persist(layoutConstraint.getElement());
            }
          }
        }
      }
    }
  }
}