Program: 35410
Project: 35410
Buggy source:
public void persistFlexibleElement(final PropertyMap changes,final Object model){
  if (changes.get(AdminUtil.PROP_FX_FLEXIBLE_ELEMENT) == null) {
    return;
  }
  EntityManager em=entityManagerProvider.get();
  final String name=changes.get(AdminUtil.PROP_FX_NAME);
  final String code=changes.get(AdminUtil.PROP_FX_CODE);
  final ElementTypeEnum type=changes.get(AdminUtil.PROP_FX_TYPE);
  final Boolean isCompulsory=changes.get(AdminUtil.PROP_FX_IS_COMPULSARY);
  final PrivacyGroupDTO pg=changes.get(AdminUtil.PROP_FX_PRIVACY_GROUP);
  final Boolean amend=changes.get(AdminUtil.PROP_FX_AMENDABLE);
  final Boolean exportable=changes.get(AdminUtil.PROP_FX_EXPORTABLE);
  final LayoutGroupDTO group=changes.get(AdminUtil.PROP_FX_GROUP);
  final Integer order=changes.get(AdminUtil.PROP_FX_ORDER_IN_GROUP);
  final Boolean inBanner=changes.get(AdminUtil.PROP_FX_IN_BANNER);
  final Integer posB;
  if (changes.get(AdminUtil.PROP_FX_POS_IN_BANNER) instanceof Integer) {
    posB=((Integer)changes.get(AdminUtil.PROP_FX_POS_IN_BANNER)) - 1;
  }
 else {
    posB=null;
  }
  final Map<String,Object> oldLayoutFields=(Map<String,Object>)changes.get(AdminUtil.PROP_FX_OLD_FIELDS);
  final LayoutConstraintDTO oldLayoutConstraintDTO=(LayoutConstraintDTO)oldLayoutFields.get(AdminUtil.PROP_FX_LC);
  final LayoutConstraintDTO oldBannerLayoutConstraintDTO=(LayoutConstraintDTO)oldLayoutFields.get(AdminUtil.PROP_FX_LC_BANNER);
  final ElementTypeEnum oldType=(ElementTypeEnum)oldLayoutFields.get(AdminUtil.PROP_FX_TYPE);
  final Integer oldOrder=(Integer)oldLayoutFields.get(AdminUtil.PROP_FX_ORDER_IN_GROUP);
  final Character textType=changes.get(AdminUtil.PROP_FX_TEXT_TYPE);
  final Number maxLimit=changes.get(AdminUtil.PROP_FX_MAX_LIMIT);
  final Number minLimit=changes.get(AdminUtil.PROP_FX_MIN_LIMIT);
  final Integer length=changes.get(AdminUtil.PROP_FX_LENGTH);
  final Boolean decimal=changes.get(AdminUtil.PROP_FX_DECIMAL);
  final ReportModelDTO reportModel=changes.get(AdminUtil.PROP_FX_REPORT_MODEL);
  final Boolean isMultiple=changes.get(AdminUtil.PROP_FX_Q_MULTIPLE);
  final CategoryTypeDTO category=changes.get(AdminUtil.PROP_FX_Q_CATEGORY);
  final List<String> qChoices=changes.get(AdminUtil.PROP_FX_Q_CHOICES);
  Set<String> qChoicesDisabled=changes.get(AdminUtil.PROP_FX_Q_CHOICES_DISABLED);
  final List<BudgetSubFieldDTO> bSubFields=changes.get(AdminUtil.PROP_FX_B_BUDGETSUBFIELDS);
  final BudgetSubFieldDTO ratioDividend=changes.get(AdminUtil.PROP_FX_B_BUDGET_RATIO_DIVIDEND);
  final BudgetSubFieldDTO ratioDivisor=changes.get(AdminUtil.PROP_FX_B_BUDGET_RATIO_DIVISOR);
  final String computationRule=changes.get(AdminUtil.PROP_FX_COMPUTATION_RULE);
  final FlexibleElementDTO budgetSpent=changes.get(AdminUtil.PROP_BUDGET_SPENT);
  final FlexibleElementDTO budgetPlanned=changes.get(AdminUtil.PROP_BUDGET_PLANNED);
  Number contactListLimit=changes.get(AdminUtil.PROP_FX_CONTACT_LIST_LIMIT);
  boolean contactListIsMember=changes.get(AdminUtil.PROP_FX_CONTACT_LIST_IS_MEMBER);
  ContactModelType contactListType=changes.get(AdminUtil.PROP_FX_CONTACT_LIST_ALLOWED_TYPE);
  Set<Integer> contactListAllowedModelIds=changes.get(AdminUtil.PROP_FX_CONTACT_LIST_ALLOWED_MODEL_IDS);
  final FlexibleElementDTO flexibleEltDTO=changes.get(AdminUtil.PROP_FX_FLEXIBLE_ELEMENT);
  FlexibleElement flexibleElt=null;
  if (flexibleEltDTO.getId() != null && flexibleEltDTO.getId() > 0) {
    flexibleElt=em.find(FlexibleElement.class,flexibleEltDTO.getId());
  }
 else {
    flexibleElt=(FlexibleElement)createNewFlexibleElement(em,oldType,type,flexibleElt);
  }
  if (qChoicesDisabled == null) {
    qChoicesDisabled=Collections.emptySet();
  }
  LOG.debug("String_Node_Str",type,group,order,inBanner,posB,isCompulsory,pg,amend,exportable);
  LOG.debug("String_Node_Str",maxLimit,minLimit,textType,length,decimal,reportModel);
  Boolean basicChanges=false;
  if (flexibleElt != null) {
    if (name != null) {
      flexibleElt.setLabel(name);
      basicChanges=true;
    }
    if (code != null) {
      flexibleElt.setCode(code);
      basicChanges=true;
    }
    if (amend != null) {
      flexibleElt.setAmendable(amend);
      basicChanges=true;
    }
    if (exportable != null) {
      flexibleElt.setExportable(exportable);
      basicChanges=true;
    }
    if (isCompulsory != null) {
      flexibleElt.setValidates(isCompulsory);
      basicChanges=true;
    }
    if (pg != null) {
      PrivacyGroup pgToPersist=em.find(PrivacyGroup.class,pg.getId());
      if (pgToPersist != null) {
        flexibleElt.setPrivacyGroup(pgToPersist);
        basicChanges=true;
      }
    }
 else     if (changes.containsKey(AdminUtil.PROP_FX_PRIVACY_GROUP)) {
      flexibleElt.setPrivacyGroup(null);
      basicChanges=true;
    }
    if (basicChanges && flexibleElt.getId() != null)     flexibleElt=em.merge(flexibleElt);
 else     em.persist(flexibleElt);
  }
  if (group != null) {
    LayoutGroup parentLayoutGroup=em.find(LayoutGroup.class,group.getId());
    LayoutConstraint newLayoutConstraint=new LayoutConstraint();
    if (parentLayoutGroup != null) {
      newLayoutConstraint.setElement(flexibleElt);
      newLayoutConstraint.setParentLayoutGroup(parentLayoutGroup);
      if (oldOrder != null)       newLayoutConstraint.setSortOrder(oldOrder);
      if (order != null)       newLayoutConstraint.setSortOrder(order);
      if (order == null && oldOrder == null)       newLayoutConstraint.setSortOrder(new Integer(parentLayoutGroup.getConstraints().size()));
      if (oldLayoutConstraintDTO != null) {
        newLayoutConstraint.setId(oldLayoutConstraintDTO.getId());
        newLayoutConstraint=em.merge(newLayoutConstraint);
      }
 else {
        em.persist(newLayoutConstraint);
      }
    }
  }
  if (inBanner != null) {
    if (inBanner) {
      if (model instanceof ProjectModel || model instanceof OrgUnitModel || model instanceof ContactModel) {
        changeBanner(em,posB,model,flexibleElt);
      }
    }
 else {
      if (oldBannerLayoutConstraintDTO != null) {
        LayoutConstraint oldBannerLayoutConstraint=mapper.map(oldBannerLayoutConstraintDTO,new LayoutConstraint());
        oldBannerLayoutConstraint=em.find(LayoutConstraint.class,oldBannerLayoutConstraint.getId());
        em.remove(oldBannerLayoutConstraint);
      }
    }
  }
 else {
    if (posB != null) {
      if (model instanceof ProjectModel || model instanceof OrgUnitModel || model instanceof ContactModel) {
        LayoutConstraint oldBannerLayoutConstraint=mapper.map(oldBannerLayoutConstraintDTO,new LayoutConstraint());
        changePositionInBanner(em,posB,model,flexibleElt,oldBannerLayoutConstraint);
      }
    }
  }
  if (oldType != null && type != null) {
    flexibleElt=(FlexibleElement)createNewFlexibleElement(em,oldType,type,flexibleElt);
    LOG.debug("String_Node_Str",flexibleElt.getClass());
  }
  em.flush();
  em.clear();
  flexibleElt=em.find(FlexibleElement.class,flexibleElt.getId());
  Boolean specificChanges=false;
  final LogicalElementType logicalElementType=ServerComputations.logicalElementTypeOf(flexibleElt);
  if ((ElementTypeEnum.DEFAULT.equals(oldType) && type == null) && DefaultFlexibleElementType.BUDGET == logicalElementType.toDefaultFlexibleElementType()) {
    List<BudgetSubField> budgetFieldsToDelete=new ArrayList<BudgetSubField>();
    BudgetElement budgetElement=(BudgetElement)flexibleElt;
    budgetFieldsToDelete.addAll(budgetElement.getBudgetSubFields());
    budgetElement.getBudgetSubFields().clear();
    for (    BudgetSubFieldDTO budgetFieldDTO : bSubFields) {
      if (budgetFieldDTO.getId() != null && budgetFieldDTO.getId() > 0) {
        BudgetSubField b=em.find(BudgetSubField.class,budgetFieldDTO.getId());
        if (b != null) {
          budgetFieldsToDelete.remove(b);
          b.setLabel(budgetFieldDTO.getLabel());
          b.setFieldOrder(budgetFieldDTO.getFieldOrder());
          b=em.merge(b);
          budgetElement.getBudgetSubFields().add(b);
        }
      }
 else {
        BudgetSubField budgetSubFieldToPersist=new BudgetSubField();
        budgetSubFieldToPersist.setLabel(budgetFieldDTO.getLabel());
        budgetSubFieldToPersist.setFieldOrder(budgetFieldDTO.getFieldOrder());
        budgetSubFieldToPersist.setBudgetElement(budgetElement);
        em.persist(budgetSubFieldToPersist);
        budgetElement.getBudgetSubFields().add(budgetSubFieldToPersist);
      }
    }
    for (    BudgetSubField budgetFieldTODelete : budgetFieldsToDelete) {
      budgetFieldTODelete.setBudgetElement(null);
      em.remove(budgetFieldTODelete);
    }
    if (ratioDividend != null) {
      BudgetSubField budgetRatio=new BudgetSubField();
      budgetRatio.setId(ratioDividend.getId());
      budgetElement.setRatioDividend(budgetRatio);
    }
    if (ratioDivisor != null) {
      BudgetSubField budgetRatio=new BudgetSubField();
      budgetRatio.setId(ratioDivisor.getId());
      budgetElement.setRatioDivisor(budgetRatio);
    }
    flexibleElt=em.merge(budgetElement);
  }
 else   if ((ElementTypeEnum.DEFAULT.equals(oldType) && type == null) && DefaultFlexibleElementType.BUDGET_RATIO == logicalElementType.toDefaultFlexibleElementType()) {
    BudgetRatioElement budgetRatioElement=(BudgetRatioElement)flexibleElt;
    if (budgetSpent != null) {
      FlexibleElement flexibleElement=new DefaultFlexibleElement();
      flexibleElement.setId(budgetSpent.getId());
      budgetRatioElement.setSpentBudget(flexibleElement);
    }
    if (budgetPlanned != null) {
      FlexibleElement flexibleElement=new DefaultFlexibleElement();
      flexibleElement.setId(budgetPlanned.getId());
      budgetRatioElement.setPlannedBudget(flexibleElement);
    }
    flexibleElt=em.merge(budgetRatioElement);
  }
 else   if (ElementTypeEnum.FILES_LIST.equals(type) || (ElementTypeEnum.FILES_LIST.equals(oldType) && type == null)) {
    FilesListElement filesListElement=(FilesListElement)flexibleElt;
    if (filesListElement != null) {
      if (maxLimit != null) {
        filesListElement.setLimit(maxLimit.intValue());
        specificChanges=true;
      }
      if (specificChanges) {
        filesListElement=em.merge(filesListElement);
        flexibleElt=filesListElement;
      }
    }
  }
 else   if (ElementTypeEnum.TEXT_AREA.equals(type) || (ElementTypeEnum.TEXT_AREA.equals(oldType) && type == null)) {
    TextAreaElement textAreaElement=(TextAreaElement)flexibleElt;
    if (textAreaElement != null) {
      if (maxLimit != null) {
        ((TextAreaElement)flexibleElt).setMaxValue(maxLimit.longValue());
        specificChanges=true;
      }
      if (minLimit != null) {
        ((TextAreaElement)flexibleElt).setMinValue(minLimit.longValue());
        specificChanges=true;
      }
      if (length != null) {
        ((TextAreaElement)flexibleElt).setLength(length);
        specificChanges=true;
      }
      if (decimal != null) {
        ((TextAreaElement)flexibleElt).setIsDecimal(decimal);
        specificChanges=true;
      }
      if (textType != null) {
        ((TextAreaElement)flexibleElt).setType(textType);
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge((TextAreaElement)flexibleElt);
      }
    }
  }
 else   if (ElementTypeEnum.REPORT.equals(type) || (ElementTypeEnum.REPORT.equals(oldType) && type == null)) {
    ReportElement reportElement=em.find(ReportElement.class,flexibleElt.getId());
    if (reportElement != null) {
      if (reportModel != null && reportModel.getName() != null) {
        ProjectReportModel reportId=findReportModel(em,reportModel.getName());
        ((ReportElement)flexibleElt).setModel(reportId);
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge((ReportElement)flexibleElt);
      }
    }
  }
 else   if (ElementTypeEnum.REPORT_LIST.equals(type) || (ElementTypeEnum.REPORT_LIST.equals(oldType) && type == null)) {
    ReportListElement reportElement=em.find(ReportListElement.class,flexibleElt.getId());
    if (reportElement != null) {
      if (reportModel != null && reportModel.getName() != null) {
        ProjectReportModel reportId=findReportModel(em,reportModel.getName());
        ((ReportListElement)flexibleElt).setModel(reportId);
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge((ReportListElement)flexibleElt);
      }
    }
  }
 else   if (ElementTypeEnum.QUESTION.equals(type) || (ElementTypeEnum.QUESTION.equals(oldType) && type == null)) {
    QuestionElement questionElement=em.find(QuestionElement.class,flexibleElt.getId());
    if (questionElement != null) {
      if (isMultiple != null) {
        ((QuestionElement)flexibleElt).setMultiple(isMultiple);
        specificChanges=true;
      }
      if (category != null) {
        for (        QuestionChoiceElement choiceElt : ((QuestionElement)flexibleElt).getChoices()) {
          em.remove(choiceElt);
        }
        CategoryType categoryType=em.find(CategoryType.class,category.getId());
        if (categoryType != null) {
          ((QuestionElement)flexibleElt).setCategoryType(categoryType);
          List<QuestionChoiceElement> choices=new ArrayList<QuestionChoiceElement>();
          int i=0;
          for (          CategoryElement catElt : categoryType.getElements()) {
            QuestionChoiceElement qChoice=new QuestionChoiceElement();
            qChoice.setLabel("String_Node_Str");
            qChoice.setCategoryElement(catElt);
            qChoice.setParentQuestion(questionElement);
            qChoice.setSortOrder(i++);
            choices.add(qChoice);
          }
          ((QuestionElement)flexibleElt).setChoices(choices);
          specificChanges=true;
        }
      }
 else       if (qChoices != null && qChoices.size() > 0) {
        for (        QuestionChoiceElement choiceElt : ((QuestionElement)flexibleElt).getChoices()) {
          em.remove(choiceElt);
        }
        ((QuestionElement)flexibleElt).setCategoryType(null);
        List<QuestionChoiceElement> choices=new ArrayList<QuestionChoiceElement>();
        int i=0;
        for (        String choiceLabel : qChoices) {
          QuestionChoiceElement qChoice=new QuestionChoiceElement();
          qChoice.setLabel(choiceLabel);
          qChoice.setParentQuestion(questionElement);
          qChoice.setSortOrder(i++);
          qChoice.setDisabled(qChoicesDisabled.contains(choiceLabel));
          choices.add(qChoice);
        }
        ((QuestionElement)flexibleElt).setChoices(choices);
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge((QuestionElement)flexibleElt);
      }
    }
  }
 else   if (type == ElementTypeEnum.COMPUTATION || (type == null && oldType == ElementTypeEnum.COMPUTATION)) {
    ComputationElement computationElement=(ComputationElement)flexibleElt;
    if (computationElement != null) {
      if (computationRule != null) {
        final String rule=resolveComputationRule(em,dependencyResolver,model,computationRule);
        computationElement.setRule(rule);
        specificChanges=true;
        removeAllValuesForElement(computationElement,em);
      }
      if (minLimit != null) {
        computationElement.setMinimumValue(minLimit.toString());
        specificChanges=true;
      }
      if (maxLimit != null) {
        computationElement.setMaximumValue(maxLimit.toString());
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge(computationElement);
      }
    }
  }
 else   if (type == ElementTypeEnum.CONTACT_LIST || (type == null && oldType == ElementTypeEnum.CONTACT_LIST)) {
    ContactListElement contactListElement=(ContactListElement)flexibleElt;
    if (contactListElement != null) {
      if ((contactListElement.getLimit() > 0 && contactListLimit == null) || (contactListLimit != null && contactListElement.getLimit() != contactListLimit.intValue())) {
        contactListElement.setLimit(contactListLimit == null ? 0 : contactListLimit.intValue());
        specificChanges=true;
      }
      if (contactListElement.getAllowedType() != contactListType) {
        contactListElement.setAllowedType(contactListType);
        specificChanges=true;
      }
      if (contactListElement.isMember() != contactListIsMember) {
        contactListElement.setMember(contactListIsMember);
        specificChanges=true;
      }
      Set<Integer> modelsToCompare=new HashSet<>(contactListAllowedModelIds);
      boolean contactModelsModified=false;
      for (      ContactModel contactModel : contactListElement.getAllowedModels()) {
        if (!modelsToCompare.remove(contactModel.getId())) {
          contactModelsModified=true;
          break;
        }
      }
      if (!modelsToCompare.isEmpty()) {
        contactModelsModified=true;
      }
      if (contactModelsModified) {
        contactListElement.setAllowedModels(contactModelDAO.findByIds(contactListAllowedModelIds));
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge(contactListElement);
      }
    }
  }
  em.flush();
  em.clear();
}

Fixed version (ground truth):
public void persistFlexibleElement(final PropertyMap changes,final Object model){
  if (changes.get(AdminUtil.PROP_FX_FLEXIBLE_ELEMENT) == null) {
    return;
  }
  EntityManager em=entityManagerProvider.get();
  final String name=changes.get(AdminUtil.PROP_FX_NAME);
  final String code=changes.get(AdminUtil.PROP_FX_CODE);
  final ElementTypeEnum type=changes.get(AdminUtil.PROP_FX_TYPE);
  final Boolean isCompulsory=changes.get(AdminUtil.PROP_FX_IS_COMPULSARY);
  final PrivacyGroupDTO pg=changes.get(AdminUtil.PROP_FX_PRIVACY_GROUP);
  final Boolean amend=changes.get(AdminUtil.PROP_FX_AMENDABLE);
  final Boolean exportable=changes.get(AdminUtil.PROP_FX_EXPORTABLE);
  final LayoutGroupDTO group=changes.get(AdminUtil.PROP_FX_GROUP);
  final Integer order=changes.get(AdminUtil.PROP_FX_ORDER_IN_GROUP);
  final Boolean inBanner=changes.get(AdminUtil.PROP_FX_IN_BANNER);
  final Integer posB;
  if (changes.get(AdminUtil.PROP_FX_POS_IN_BANNER) instanceof Integer) {
    posB=((Integer)changes.get(AdminUtil.PROP_FX_POS_IN_BANNER)) - 1;
  }
 else {
    posB=null;
  }
  final Map<String,Object> oldLayoutFields=(Map<String,Object>)changes.get(AdminUtil.PROP_FX_OLD_FIELDS);
  final LayoutConstraintDTO oldLayoutConstraintDTO=(LayoutConstraintDTO)oldLayoutFields.get(AdminUtil.PROP_FX_LC);
  final LayoutConstraintDTO oldBannerLayoutConstraintDTO=(LayoutConstraintDTO)oldLayoutFields.get(AdminUtil.PROP_FX_LC_BANNER);
  final ElementTypeEnum oldType=(ElementTypeEnum)oldLayoutFields.get(AdminUtil.PROP_FX_TYPE);
  final Integer oldOrder=(Integer)oldLayoutFields.get(AdminUtil.PROP_FX_ORDER_IN_GROUP);
  final Character textType=changes.get(AdminUtil.PROP_FX_TEXT_TYPE);
  final Number maxLimit=changes.get(AdminUtil.PROP_FX_MAX_LIMIT);
  final Number minLimit=changes.get(AdminUtil.PROP_FX_MIN_LIMIT);
  final Integer length=changes.get(AdminUtil.PROP_FX_LENGTH);
  final Boolean decimal=changes.get(AdminUtil.PROP_FX_DECIMAL);
  final ReportModelDTO reportModel=changes.get(AdminUtil.PROP_FX_REPORT_MODEL);
  final Boolean isMultiple=changes.get(AdminUtil.PROP_FX_Q_MULTIPLE);
  final CategoryTypeDTO category=changes.get(AdminUtil.PROP_FX_Q_CATEGORY);
  final List<String> qChoices=changes.get(AdminUtil.PROP_FX_Q_CHOICES);
  Set<String> qChoicesDisabled=changes.get(AdminUtil.PROP_FX_Q_CHOICES_DISABLED);
  final List<BudgetSubFieldDTO> bSubFields=changes.get(AdminUtil.PROP_FX_B_BUDGETSUBFIELDS);
  final BudgetSubFieldDTO ratioDividend=changes.get(AdminUtil.PROP_FX_B_BUDGET_RATIO_DIVIDEND);
  final BudgetSubFieldDTO ratioDivisor=changes.get(AdminUtil.PROP_FX_B_BUDGET_RATIO_DIVISOR);
  final String computationRule=changes.get(AdminUtil.PROP_FX_COMPUTATION_RULE);
  final FlexibleElementDTO budgetSpent=changes.get(AdminUtil.PROP_BUDGET_SPENT);
  final FlexibleElementDTO budgetPlanned=changes.get(AdminUtil.PROP_BUDGET_PLANNED);
  Number contactListLimit=changes.get(AdminUtil.PROP_FX_CONTACT_LIST_LIMIT);
  Boolean contactListIsMember=changes.get(AdminUtil.PROP_FX_CONTACT_LIST_IS_MEMBER);
  ContactModelType contactListType=changes.get(AdminUtil.PROP_FX_CONTACT_LIST_ALLOWED_TYPE);
  Set<Integer> contactListAllowedModelIds=changes.get(AdminUtil.PROP_FX_CONTACT_LIST_ALLOWED_MODEL_IDS);
  final FlexibleElementDTO flexibleEltDTO=changes.get(AdminUtil.PROP_FX_FLEXIBLE_ELEMENT);
  FlexibleElement flexibleElt=null;
  if (flexibleEltDTO.getId() != null && flexibleEltDTO.getId() > 0) {
    flexibleElt=em.find(FlexibleElement.class,flexibleEltDTO.getId());
  }
 else {
    flexibleElt=(FlexibleElement)createNewFlexibleElement(em,oldType,type,flexibleElt);
  }
  if (qChoicesDisabled == null) {
    qChoicesDisabled=Collections.emptySet();
  }
  LOG.debug("String_Node_Str",type,group,order,inBanner,posB,isCompulsory,pg,amend,exportable);
  LOG.debug("String_Node_Str",maxLimit,minLimit,textType,length,decimal,reportModel);
  Boolean basicChanges=false;
  if (flexibleElt != null) {
    if (name != null) {
      flexibleElt.setLabel(name);
      basicChanges=true;
    }
    if (code != null) {
      flexibleElt.setCode(code);
      basicChanges=true;
    }
    if (amend != null) {
      flexibleElt.setAmendable(amend);
      basicChanges=true;
    }
    if (exportable != null) {
      flexibleElt.setExportable(exportable);
      basicChanges=true;
    }
    if (isCompulsory != null) {
      flexibleElt.setValidates(isCompulsory);
      basicChanges=true;
    }
    if (pg != null) {
      PrivacyGroup pgToPersist=em.find(PrivacyGroup.class,pg.getId());
      if (pgToPersist != null) {
        flexibleElt.setPrivacyGroup(pgToPersist);
        basicChanges=true;
      }
    }
 else     if (changes.containsKey(AdminUtil.PROP_FX_PRIVACY_GROUP)) {
      flexibleElt.setPrivacyGroup(null);
      basicChanges=true;
    }
    if (basicChanges && flexibleElt.getId() != null)     flexibleElt=em.merge(flexibleElt);
 else     em.persist(flexibleElt);
  }
  if (group != null) {
    LayoutGroup parentLayoutGroup=em.find(LayoutGroup.class,group.getId());
    LayoutConstraint newLayoutConstraint=new LayoutConstraint();
    if (parentLayoutGroup != null) {
      newLayoutConstraint.setElement(flexibleElt);
      newLayoutConstraint.setParentLayoutGroup(parentLayoutGroup);
      if (oldOrder != null)       newLayoutConstraint.setSortOrder(oldOrder);
      if (order != null)       newLayoutConstraint.setSortOrder(order);
      if (order == null && oldOrder == null)       newLayoutConstraint.setSortOrder(new Integer(parentLayoutGroup.getConstraints().size()));
      if (oldLayoutConstraintDTO != null) {
        newLayoutConstraint.setId(oldLayoutConstraintDTO.getId());
        newLayoutConstraint=em.merge(newLayoutConstraint);
      }
 else {
        em.persist(newLayoutConstraint);
      }
    }
  }
  if (inBanner != null) {
    if (inBanner) {
      if (model instanceof ProjectModel || model instanceof OrgUnitModel || model instanceof ContactModel) {
        changeBanner(em,posB,model,flexibleElt);
      }
    }
 else {
      if (oldBannerLayoutConstraintDTO != null) {
        LayoutConstraint oldBannerLayoutConstraint=mapper.map(oldBannerLayoutConstraintDTO,new LayoutConstraint());
        oldBannerLayoutConstraint=em.find(LayoutConstraint.class,oldBannerLayoutConstraint.getId());
        em.remove(oldBannerLayoutConstraint);
      }
    }
  }
 else {
    if (posB != null) {
      if (model instanceof ProjectModel || model instanceof OrgUnitModel || model instanceof ContactModel) {
        LayoutConstraint oldBannerLayoutConstraint=mapper.map(oldBannerLayoutConstraintDTO,new LayoutConstraint());
        changePositionInBanner(em,posB,model,flexibleElt,oldBannerLayoutConstraint);
      }
    }
  }
  if (oldType != null && type != null) {
    flexibleElt=(FlexibleElement)createNewFlexibleElement(em,oldType,type,flexibleElt);
    LOG.debug("String_Node_Str",flexibleElt.getClass());
  }
  em.flush();
  em.clear();
  flexibleElt=em.find(FlexibleElement.class,flexibleElt.getId());
  Boolean specificChanges=false;
  final LogicalElementType logicalElementType=ServerComputations.logicalElementTypeOf(flexibleElt);
  if ((ElementTypeEnum.DEFAULT.equals(oldType) && type == null) && DefaultFlexibleElementType.BUDGET == logicalElementType.toDefaultFlexibleElementType()) {
    List<BudgetSubField> budgetFieldsToDelete=new ArrayList<BudgetSubField>();
    BudgetElement budgetElement=(BudgetElement)flexibleElt;
    budgetFieldsToDelete.addAll(budgetElement.getBudgetSubFields());
    budgetElement.getBudgetSubFields().clear();
    for (    BudgetSubFieldDTO budgetFieldDTO : bSubFields) {
      if (budgetFieldDTO.getId() != null && budgetFieldDTO.getId() > 0) {
        BudgetSubField b=em.find(BudgetSubField.class,budgetFieldDTO.getId());
        if (b != null) {
          budgetFieldsToDelete.remove(b);
          b.setLabel(budgetFieldDTO.getLabel());
          b.setFieldOrder(budgetFieldDTO.getFieldOrder());
          b=em.merge(b);
          budgetElement.getBudgetSubFields().add(b);
        }
      }
 else {
        BudgetSubField budgetSubFieldToPersist=new BudgetSubField();
        budgetSubFieldToPersist.setLabel(budgetFieldDTO.getLabel());
        budgetSubFieldToPersist.setFieldOrder(budgetFieldDTO.getFieldOrder());
        budgetSubFieldToPersist.setBudgetElement(budgetElement);
        em.persist(budgetSubFieldToPersist);
        budgetElement.getBudgetSubFields().add(budgetSubFieldToPersist);
      }
    }
    for (    BudgetSubField budgetFieldTODelete : budgetFieldsToDelete) {
      budgetFieldTODelete.setBudgetElement(null);
      em.remove(budgetFieldTODelete);
    }
    if (ratioDividend != null) {
      BudgetSubField budgetRatio=new BudgetSubField();
      budgetRatio.setId(ratioDividend.getId());
      budgetElement.setRatioDividend(budgetRatio);
    }
    if (ratioDivisor != null) {
      BudgetSubField budgetRatio=new BudgetSubField();
      budgetRatio.setId(ratioDivisor.getId());
      budgetElement.setRatioDivisor(budgetRatio);
    }
    flexibleElt=em.merge(budgetElement);
  }
 else   if ((ElementTypeEnum.DEFAULT.equals(oldType) && type == null) && DefaultFlexibleElementType.BUDGET_RATIO == logicalElementType.toDefaultFlexibleElementType()) {
    BudgetRatioElement budgetRatioElement=(BudgetRatioElement)flexibleElt;
    if (budgetSpent != null) {
      FlexibleElement flexibleElement=new DefaultFlexibleElement();
      flexibleElement.setId(budgetSpent.getId());
      budgetRatioElement.setSpentBudget(flexibleElement);
    }
    if (budgetPlanned != null) {
      FlexibleElement flexibleElement=new DefaultFlexibleElement();
      flexibleElement.setId(budgetPlanned.getId());
      budgetRatioElement.setPlannedBudget(flexibleElement);
    }
    flexibleElt=em.merge(budgetRatioElement);
  }
 else   if (ElementTypeEnum.FILES_LIST.equals(type) || (ElementTypeEnum.FILES_LIST.equals(oldType) && type == null)) {
    FilesListElement filesListElement=(FilesListElement)flexibleElt;
    if (filesListElement != null) {
      if (maxLimit != null) {
        filesListElement.setLimit(maxLimit.intValue());
        specificChanges=true;
      }
      if (specificChanges) {
        filesListElement=em.merge(filesListElement);
        flexibleElt=filesListElement;
      }
    }
  }
 else   if (ElementTypeEnum.TEXT_AREA.equals(type) || (ElementTypeEnum.TEXT_AREA.equals(oldType) && type == null)) {
    TextAreaElement textAreaElement=(TextAreaElement)flexibleElt;
    if (textAreaElement != null) {
      if (maxLimit != null) {
        ((TextAreaElement)flexibleElt).setMaxValue(maxLimit.longValue());
        specificChanges=true;
      }
      if (minLimit != null) {
        ((TextAreaElement)flexibleElt).setMinValue(minLimit.longValue());
        specificChanges=true;
      }
      if (length != null) {
        ((TextAreaElement)flexibleElt).setLength(length);
        specificChanges=true;
      }
      if (decimal != null) {
        ((TextAreaElement)flexibleElt).setIsDecimal(decimal);
        specificChanges=true;
      }
      if (textType != null) {
        ((TextAreaElement)flexibleElt).setType(textType);
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge((TextAreaElement)flexibleElt);
      }
    }
  }
 else   if (ElementTypeEnum.REPORT.equals(type) || (ElementTypeEnum.REPORT.equals(oldType) && type == null)) {
    ReportElement reportElement=em.find(ReportElement.class,flexibleElt.getId());
    if (reportElement != null) {
      if (reportModel != null && reportModel.getName() != null) {
        ProjectReportModel reportId=findReportModel(em,reportModel.getName());
        ((ReportElement)flexibleElt).setModel(reportId);
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge((ReportElement)flexibleElt);
      }
    }
  }
 else   if (ElementTypeEnum.REPORT_LIST.equals(type) || (ElementTypeEnum.REPORT_LIST.equals(oldType) && type == null)) {
    ReportListElement reportElement=em.find(ReportListElement.class,flexibleElt.getId());
    if (reportElement != null) {
      if (reportModel != null && reportModel.getName() != null) {
        ProjectReportModel reportId=findReportModel(em,reportModel.getName());
        ((ReportListElement)flexibleElt).setModel(reportId);
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge((ReportListElement)flexibleElt);
      }
    }
  }
 else   if (ElementTypeEnum.QUESTION.equals(type) || (ElementTypeEnum.QUESTION.equals(oldType) && type == null)) {
    QuestionElement questionElement=em.find(QuestionElement.class,flexibleElt.getId());
    if (questionElement != null) {
      if (isMultiple != null) {
        ((QuestionElement)flexibleElt).setMultiple(isMultiple);
        specificChanges=true;
      }
      if (category != null) {
        for (        QuestionChoiceElement choiceElt : ((QuestionElement)flexibleElt).getChoices()) {
          em.remove(choiceElt);
        }
        CategoryType categoryType=em.find(CategoryType.class,category.getId());
        if (categoryType != null) {
          ((QuestionElement)flexibleElt).setCategoryType(categoryType);
          List<QuestionChoiceElement> choices=new ArrayList<QuestionChoiceElement>();
          int i=0;
          for (          CategoryElement catElt : categoryType.getElements()) {
            QuestionChoiceElement qChoice=new QuestionChoiceElement();
            qChoice.setLabel("String_Node_Str");
            qChoice.setCategoryElement(catElt);
            qChoice.setParentQuestion(questionElement);
            qChoice.setSortOrder(i++);
            choices.add(qChoice);
          }
          ((QuestionElement)flexibleElt).setChoices(choices);
          specificChanges=true;
        }
      }
 else       if (qChoices != null && qChoices.size() > 0) {
        for (        QuestionChoiceElement choiceElt : ((QuestionElement)flexibleElt).getChoices()) {
          em.remove(choiceElt);
        }
        ((QuestionElement)flexibleElt).setCategoryType(null);
        List<QuestionChoiceElement> choices=new ArrayList<QuestionChoiceElement>();
        int i=0;
        for (        String choiceLabel : qChoices) {
          QuestionChoiceElement qChoice=new QuestionChoiceElement();
          qChoice.setLabel(choiceLabel);
          qChoice.setParentQuestion(questionElement);
          qChoice.setSortOrder(i++);
          qChoice.setDisabled(qChoicesDisabled.contains(choiceLabel));
          choices.add(qChoice);
        }
        ((QuestionElement)flexibleElt).setChoices(choices);
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge((QuestionElement)flexibleElt);
      }
    }
  }
 else   if (type == ElementTypeEnum.COMPUTATION || (type == null && oldType == ElementTypeEnum.COMPUTATION)) {
    ComputationElement computationElement=(ComputationElement)flexibleElt;
    if (computationElement != null) {
      if (computationRule != null) {
        final String rule=resolveComputationRule(em,dependencyResolver,model,computationRule);
        computationElement.setRule(rule);
        specificChanges=true;
        removeAllValuesForElement(computationElement,em);
      }
      if (minLimit != null) {
        computationElement.setMinimumValue(minLimit.toString());
        specificChanges=true;
      }
      if (maxLimit != null) {
        computationElement.setMaximumValue(maxLimit.toString());
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge(computationElement);
      }
    }
  }
 else   if (type == ElementTypeEnum.CONTACT_LIST || (type == null && oldType == ElementTypeEnum.CONTACT_LIST)) {
    ContactListElement contactListElement=(ContactListElement)flexibleElt;
    if (contactListElement != null) {
      if ((contactListElement.getLimit() > 0 && contactListLimit == null) || (contactListLimit != null && contactListElement.getLimit() != contactListLimit.intValue())) {
        contactListElement.setLimit(contactListLimit == null ? 0 : contactListLimit.intValue());
        specificChanges=true;
      }
      if (contactListElement.getAllowedType() != contactListType) {
        contactListElement.setAllowedType(contactListType);
        specificChanges=true;
      }
      if (contactListElement.isMember() != contactListIsMember) {
        contactListElement.setMember(contactListIsMember);
        specificChanges=true;
      }
      Set<Integer> modelsToCompare=new HashSet<>(contactListAllowedModelIds);
      boolean contactModelsModified=false;
      for (      ContactModel contactModel : contactListElement.getAllowedModels()) {
        if (!modelsToCompare.remove(contactModel.getId())) {
          contactModelsModified=true;
          break;
        }
      }
      if (!modelsToCompare.isEmpty()) {
        contactModelsModified=true;
      }
      if (contactModelsModified) {
        contactListElement.setAllowedModels(contactModelDAO.findByIds(contactListAllowedModelIds));
        specificChanges=true;
      }
      if (specificChanges) {
        flexibleElt=em.merge(contactListElement);
      }
    }
  }
  em.flush();
  em.clear();
}