@Transactional
  public void deleteImportObject(int importObjectId) {
    ImportObject importObject = importObjectStore.getImportObject(importObjectId);

    if (importObject != null) {
      importObjectStore.deleteImportObject(importObject);
    }
  }
  private void deleteGroupAssociations(GroupMemberType groupMemberType, int groupId) {
    Collection<ImportObject> importObjects = importObjectStore.getImportObjects(groupMemberType);

    for (ImportObject importObject : importObjects) {
      GroupMemberAssociation association = (GroupMemberAssociation) importObject.getObject();

      if (association.getGroupId() == groupId) {
        importObjectStore.deleteImportObject(importObject);
      }
    }
  }
  private void deleteCompleteDataSetRegistrationsBySource(int sourceId) {
    Collection<ImportObject> importObjects =
        importObjectStore.getImportObjects(CompleteDataSetRegistration.class);

    for (ImportObject importObject : importObjects) {
      CompleteDataSetRegistration registration =
          (CompleteDataSetRegistration) importObject.getObject();

      if (registration.getSource().getId() == sourceId) {
        importObjectStore.deleteImportObject(importObject);
      }
    }
  }
  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());
      }
    }
  }
  @Transactional
  public int addImportObject(
      ImportObjectStatus status, ImportableObject object, ImportableObject compareObject) {
    ImportObject importObject =
        new ImportObject(status, object.getClass().getName(), object, compareObject);

    return importObjectStore.addImportObject(importObject);
  }
  @Transactional
  public int addImportObject(
      ImportObjectStatus status, GroupMemberType groupMemberType, ImportableObject object) {
    ImportObject importObject =
        new ImportObject(status, object.getClass().getName(), groupMemberType, object);

    return importObjectStore.addImportObject(importObject);
  }
  @Transactional
  public void matchObject(int importObjectId, int existingObjectId) {
    ImportObject importObject = importObjectStore.getImportObject(importObjectId);

    Object object = importObject.getObject();

    // ---------------------------------------------------------------------
    // Updates the name of the import object to the name of the existing
    // object.
    // ---------------------------------------------------------------------

    if (object.getClass().equals(DataElement.class)) {
      DataElement element = (DataElement) object;

      element.setName(dataElementService.getDataElement(existingObjectId).getName());
    } else if (object.getClass().equals(DataElementGroup.class)) {
      DataElementGroup group = (DataElementGroup) object;

      group.setName(dataElementService.getDataElementGroup(existingObjectId).getName());
    } else if (object.getClass().equals(DataElementGroupSet.class)) {
      DataElementGroupSet groupSet = (DataElementGroupSet) object;

      groupSet.setName(dataElementService.getDataElementGroupSet(existingObjectId).getName());
    } else if (object.getClass().equals(IndicatorType.class)) {
      IndicatorType type = (IndicatorType) object;

      type.setName(indicatorService.getIndicatorType(existingObjectId).getName());
    } else if (object.getClass().equals(Indicator.class)) {
      Indicator indicator = (Indicator) object;

      indicator.setName(indicatorService.getIndicator(existingObjectId).getName());
    } else if (object.getClass().equals(IndicatorGroup.class)) {
      IndicatorGroup group = (IndicatorGroup) object;

      group.setName(indicatorService.getIndicatorGroup(existingObjectId).getName());
    } else if (object.getClass().equals(IndicatorGroupSet.class)) {
      IndicatorGroupSet groupSet = (IndicatorGroupSet) object;

      groupSet.setName(indicatorService.getIndicatorGroupSet(existingObjectId).getName());
    } else if (object.getClass().equals(DataDictionary.class)) {
      DataDictionary dictionary = (DataDictionary) object;

      dictionary.setName(dataDictionaryService.getDataDictionary(existingObjectId).getName());
    } else if (object.getClass().equals(DataSet.class)) {
      DataSet dataSet = (DataSet) object;

      dataSet.setName(dataSetService.getDataSet(existingObjectId).getName());
    } else if (object.getClass().equals(OrganisationUnit.class)) {
      OrganisationUnit unit = (OrganisationUnit) object;

      unit.setName(organisationUnitService.getOrganisationUnit(existingObjectId).getName());
    } else if (object.getClass().equals(OrganisationUnitGroup.class)) {
      OrganisationUnitGroup group = (OrganisationUnitGroup) object;

      group.setName(
          organisationUnitGroupService.getOrganisationUnitGroup(existingObjectId).getName());
    } else if (object.getClass().equals(OrganisationUnitGroupSet.class)) {
      OrganisationUnitGroupSet groupSet = (OrganisationUnitGroupSet) object;

      groupSet.setName(
          organisationUnitGroupService.getOrganisationUnitGroupSet(existingObjectId).getName());
    } else if (object.getClass().equals(OrganisationUnitLevel.class)) {
      OrganisationUnitLevel level = (OrganisationUnitLevel) object;

      level.setName(organisationUnitService.getOrganisationUnitLevel(existingObjectId).getName());
    } else if (object.getClass().equals(ValidationRule.class)) {
      ValidationRule validationRule = (ValidationRule) object;

      validationRule.setName(validationRuleService.getValidationRule(existingObjectId).getName());
    } else if (object.getClass().equals(Report.class)) {
      Report report = (Report) object;

      report.setName(reportService.getReport(existingObjectId).getName());
    } else if (object.getClass().equals(ReportTable.class)) {
      ReportTable reportTable = (ReportTable) object;

      reportTable.setName(reportTableService.getReportTable(existingObjectId).getName());
    } else if (object.getClass().equals(Chart.class)) {
      Chart chart = (Chart) object;

      chart.setName(chartService.getChart(existingObjectId).getName());
    } else if (object.getClass().equals(DataValue.class)) {
      DataValue dataValue = (DataValue) object;

      dataValue =
          updateDataValue(
              dataValue,
              dataValueService.getDataValue(
                  dataValue.getSource(),
                  dataValue.getDataElement(),
                  dataValue.getPeriod(),
                  dataValue.getOptionCombo()));
    }

    // ---------------------------------------------------------------------
    // Sets the status of the import object to match, these objects will
    // later be ignored on import all but is needed for matching of
    // associations.
    // ---------------------------------------------------------------------

    importObject.setStatus(ImportObjectStatus.MATCH);

    importObjectStore.updateImportObject(importObject);
  }
  @Transactional
  public void cascadeDeleteImportObjects(Class<?> clazz) {
    importObjectStore.deleteImportObjects(clazz);

    if (clazz.equals(DataElement.class)) {
      importObjectStore.deleteImportObjects(DataElementCategoryOptionCombo.class);
      importObjectStore.deleteImportObjects(DataElementCategoryCombo.class);
      importObjectStore.deleteImportObjects(DataElementCategory.class);
      importObjectStore.deleteImportObjects(DataElementCategoryOption.class);
      importObjectStore.deleteImportObjects(GroupMemberType.CATEGORY_CATEGORYOPTION);
      importObjectStore.deleteImportObjects(GroupMemberType.CATEGORYCOMBO_CATEGORY);

      importObjectStore.deleteImportObjects(GroupMemberType.DATAELEMENTGROUP);

      importObjectStore.deleteImportObjects(DataSet.class);
      importObjectStore.deleteImportObjects(GroupMemberType.DATASET);
      importObjectStore.deleteImportObjects(GroupMemberType.DATASET_SOURCE);
      importObjectStore.deleteImportObjects(CompleteDataSetRegistration.class);

      importObjectStore.deleteImportObjects(Indicator.class);
      importObjectStore.deleteImportObjects(GroupMemberType.INDICATORGROUP);
      importObjectStore.deleteImportObjects(GroupMemberType.DATADICTIONARY_INDICATOR);

      importObjectStore.deleteImportObjects(GroupMemberType.DATADICTIONARY_DATAELEMENT);

      importDataValueService.deleteImportDataValues();
    } else if (clazz.equals(DataElementGroup.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.DATAELEMENTGROUP);
      importObjectStore.deleteImportObjects(GroupMemberType.DATAELEMENTGROUPSET);
    } else if (clazz.equals(DataElementGroupSet.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.DATAELEMENTGROUPSET);
    } else if (clazz.equals(IndicatorType.class)) {
      importObjectStore.deleteImportObjects(Indicator.class);
      importObjectStore.deleteImportObjects(GroupMemberType.INDICATORGROUP);
      importObjectStore.deleteImportObjects(GroupMemberType.DATADICTIONARY_INDICATOR);
    } else if (clazz.equals(Indicator.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.INDICATORGROUP);
      importObjectStore.deleteImportObjects(GroupMemberType.DATADICTIONARY_INDICATOR);
    } else if (clazz.equals(IndicatorGroup.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.INDICATORGROUP);
      importObjectStore.deleteImportObjects(GroupMemberType.INDICATORGROUPSET);
    } else if (clazz.equals(IndicatorGroupSet.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.INDICATORGROUPSET);
    } else if (clazz.equals(DataDictionary.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.DATADICTIONARY_DATAELEMENT);
      importObjectStore.deleteImportObjects(GroupMemberType.DATADICTIONARY_INDICATOR);
    } else if (clazz.equals(DataSet.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.DATASET);
      importObjectStore.deleteImportObjects(GroupMemberType.DATASET_SOURCE);
      importObjectStore.deleteImportObjects(CompleteDataSetRegistration.class);
    } else if (clazz.equals(OrganisationUnit.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.ORGANISATIONUNITGROUP);
      importObjectStore.deleteImportObjects(GroupMemberType.ORGANISATIONUNITRELATIONSHIP);
      importObjectStore.deleteImportObjects(GroupMemberType.DATASET_SOURCE);

      importObjectStore.deleteImportObjects(CompleteDataSetRegistration.class);

      importDataValueService.deleteImportDataValues();
    } else if (clazz.equals(OrganisationUnitGroup.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.ORGANISATIONUNITGROUP);
    } else if (clazz.equals(OrganisationUnitGroupSet.class)) {
      importObjectStore.deleteImportObjects(GroupMemberType.ORGANISATIONUNITGROUPSET);
    }
  }
  @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);
  }
 @Transactional
 public void deleteImportObjects() {
   importObjectStore.deleteImportObjects();
   importDataValueService.deleteImportDataValues();
 }
 @Transactional
 public void deleteImportObjects(Class<?> clazz) {
   importObjectStore.deleteImportObjects(clazz);
 }
 @Transactional
 public Collection<ImportObject> getImportObjects(GroupMemberType groupMemberType) {
   return importObjectStore.getImportObjects(groupMemberType);
 }
 @Transactional
 public Collection<ImportObject> getImportObjects(ImportObjectStatus status, Class<?> clazz) {
   return importObjectStore.getImportObjects(status, clazz);
 }
 @Transactional
 public Collection<ImportObject> getImportObjects(Class<?> clazz) {
   return importObjectStore.getImportObjects(clazz);
 }
 @Transactional
 public ImportObject getImportObject(int id) {
   return importObjectStore.getImportObject(id);
 }