private void deleteIndicatorsWithIndicatorType(int indicatorTypeId) {
    Collection<ImportObject> importObjects = importObjectStore.getImportObjects(Indicator.class);

    for (ImportObject importObject : importObjects) {
      Indicator indicator = (Indicator) importObject.getObject();

      if (indicator.getIndicatorType().getId() == indicatorTypeId) {
        importObjectStore.deleteImportObject(importObject);

        deleteMemberAssociations(GroupMemberType.INDICATORGROUP, indicator.getId());

        deleteMemberAssociations(GroupMemberType.DATADICTIONARY_INDICATOR, indicator.getId());
      }
    }
  }
  private void deleteIndicatorsContainingDataElement(int dataElementId) {
    Collection<ImportObject> importObjects = importObjectStore.getImportObjects(Indicator.class);

    for (ImportObject importObject : importObjects) {
      Indicator indicator = (Indicator) importObject.getObject();

      if (containsIdentifier(indicator.getNumerator(), dataElementId)
          || containsIdentifier(indicator.getDenominator(), dataElementId)) {
        importObjectStore.deleteImportObject(importObject);

        deleteMemberAssociations(GroupMemberType.INDICATORGROUP, indicator.getId());

        deleteMemberAssociations(GroupMemberType.DATADICTIONARY_INDICATOR, indicator.getId());
      }
    }
  }
  public void write(XMLWriter writer, ExportParams params) {
    Collection<DataDictionary> dataDictionaries =
        dataDictionaryService.getDataDictionaries(params.getDataDictionaries());

    if (dataDictionaries != null && dataDictionaries.size() > 0) {
      writer.openElement(COLLECTION_NAME);

      for (DataDictionary dictionary : dataDictionaries) {
        if (dictionary.getDataElements() != null) {
          for (Indicator indicator : dictionary.getIndicators()) {
            writer.openElement(ELEMENT_NAME);

            writer.writeElement(FIELD_DATADICTIONARY, String.valueOf(dictionary.getId()));
            writer.writeElement(FIELD_INDICATOR, String.valueOf(indicator.getId()));

            writer.closeElement();
          }
        }
      }

      writer.closeElement();
    }
  }
  @Transactional
  public void cascadeDeleteImportObject(int importObjectId) {
    ImportObject importObject = importObjectStore.getImportObject(importObjectId);

    if (importObject != null) {
      if (importObject.getClassName().equals(DataElement.class.getName())) {
        DataElement element = (DataElement) importObject.getObject();

        deleteMemberAssociations(GroupMemberType.DATAELEMENTGROUP, element.getId());
        deleteMemberAssociations(GroupMemberType.DATASET, element.getId());
        deleteMemberAssociations(GroupMemberType.DATADICTIONARY_DATAELEMENT, element.getId());

        deleteIndicatorsContainingDataElement(element.getId());

        importDataValueService.deleteImportDataValuesByDataElement(element.getId());
      } else if (importObject.getClassName().equals(DataElementGroup.class.getName())) {
        DataElementGroup group = (DataElementGroup) importObject.getObject();

        deleteGroupAssociations(GroupMemberType.DATAELEMENTGROUP, group.getId());
        deleteMemberAssociations(GroupMemberType.DATAELEMENTGROUPSET, group.getId());
      } else if (importObject.getClassName().equals(DataElementGroupSet.class.getName())) {
        DataElementGroupSet groupSet = (DataElementGroupSet) importObject.getObject();

        deleteGroupAssociations(GroupMemberType.DATAELEMENTGROUPSET, groupSet.getId());
      } else if (importObject.getClassName().equals(IndicatorType.class.getName())) {
        IndicatorType type = (IndicatorType) importObject.getObject();

        deleteIndicatorsWithIndicatorType(type.getId());
      } else if (importObject.getClassName().equals(Indicator.class.getName())) {
        Indicator indicator = (Indicator) importObject.getObject();

        deleteMemberAssociations(GroupMemberType.INDICATORGROUP, indicator.getId());
        deleteMemberAssociations(GroupMemberType.DATADICTIONARY_INDICATOR, indicator.getId());
      } else if (importObject.getClassName().equals(IndicatorGroup.class.getName())) {
        IndicatorGroup group = (IndicatorGroup) importObject.getObject();

        deleteGroupAssociations(GroupMemberType.INDICATORGROUP, group.getId());
        deleteMemberAssociations(GroupMemberType.INDICATORGROUPSET, group.getId());
      } else if (importObject.getClassName().equals(IndicatorGroupSet.class.getName())) {
        IndicatorGroupSet groupSet = (IndicatorGroupSet) importObject.getObject();

        deleteGroupAssociations(GroupMemberType.INDICATORGROUPSET, groupSet.getId());
      } else if (importObject.getClassName().equals(DataDictionary.class.getName())) {
        DataDictionary dictionary = (DataDictionary) importObject.getObject();

        deleteGroupAssociations(GroupMemberType.DATADICTIONARY_DATAELEMENT, dictionary.getId());
        deleteGroupAssociations(GroupMemberType.DATADICTIONARY_INDICATOR, dictionary.getId());
      } else if (importObject.getClassName().equals(DataSet.class.getName())) {
        DataSet dataSet = (DataSet) importObject.getObject();

        deleteGroupAssociations(GroupMemberType.DATASET, dataSet.getId());
        deleteMemberAssociations(GroupMemberType.DATASET_SOURCE, dataSet.getId());

        deleteCompleteDataSetRegistrationsByDataSet(dataSet.getId());
      } else if (importObject.getClassName().equals(OrganisationUnit.class.getName())) {
        OrganisationUnit unit = (OrganisationUnit) importObject.getObject();

        deleteMemberAssociations(GroupMemberType.ORGANISATIONUNITGROUP, unit.getId());
        deleteGroupAssociations(GroupMemberType.ORGANISATIONUNITRELATIONSHIP, unit.getId());
        deleteMemberAssociations(GroupMemberType.DATASET_SOURCE, unit.getId());
        deleteMemberAssociations(GroupMemberType.ORGANISATIONUNITRELATIONSHIP, unit.getId());

        deleteCompleteDataSetRegistrationsBySource(unit.getId());

        importDataValueService.deleteImportDataValuesBySource(unit.getId());
      } else if (importObject.getClassName().equals(OrganisationUnitGroup.class.getName())) {
        OrganisationUnitGroup group = (OrganisationUnitGroup) importObject.getObject();

        deleteGroupAssociations(GroupMemberType.ORGANISATIONUNITGROUP, group.getId());
        deleteMemberAssociations(GroupMemberType.ORGANISATIONUNITGROUPSET, group.getId());
      } else if (importObject.getClassName().equals(OrganisationUnitGroupSet.class.getName())) {
        OrganisationUnitGroupSet groupSet = (OrganisationUnitGroupSet) importObject.getObject();

        deleteGroupAssociations(GroupMemberType.ORGANISATIONUNITGROUPSET, groupSet.getId());
      }
    }

    deleteImportObject(importObjectId);
  }