private void manageImages(
      ArrayList<LayoutDatabase> newLayouts,
      ArrayList<LayoutDatabase> updatedLayouts,
      ArrayList<Integer> removedLayouts) {
    for (LayoutDatabase layout : newLayouts) {
      findAndSaveImages(layout);
    }

    for (LayoutDatabase layout : updatedLayouts) {
      findAndRemoveImages(layout.getId());
      findAndSaveImages(layout);
    }

    for (int layoutId : removedLayouts) {
      findAndRemoveImages(layoutId);
    }
  }
  private void findAndSaveImages(LayoutDatabase layout) {
    if (isTypeWithContent(layout.getType()) && !layout.getContentEn().isEmpty()) {
      findAndSaveImagesFromContent(layout.getContentEn());
    }

    if (isTypeWithContent(layout.getType()) && !layout.getContentPl().isEmpty()) {
      findAndSaveImagesFromContent(layout.getContentPl());
    }

    if (isTypeWithContent(layout.getType()) && !layout.getAdditionalEn().isEmpty()) {
      findAndSaveImagesFromContent(layout.getAdditionalEn());
    }

    if (isTypeWithContent(layout.getType()) && !layout.getAdditionalPl().isEmpty()) {
      findAndSaveImagesFromContent(layout.getAdditionalPl());
    }
  }
  private void findAndRemoveImages(int layoutId) {
    DatabaseAdapter databaseAdapter = new DatabaseAdapter(this.fragment.getActivity());

    LayoutDatabase layout = databaseAdapter.getLayoutDatabase(layoutId);

    if (isTypeWithContent(layout.getType()) && !layout.getContentEn().isEmpty()) {
      findAndRemoveImagesFromContent(layout.getContentEn());
    }

    if (isTypeWithContent(layout.getType()) && !layout.getContentPl().isEmpty()) {
      findAndRemoveImagesFromContent(layout.getContentPl());
    }

    if (isTypeWithContent(layout.getType()) && !layout.getAdditionalEn().isEmpty()) {
      findAndRemoveImagesFromContent(layout.getAdditionalEn());
    }

    if (isTypeWithContent(layout.getType()) && !layout.getAdditionalPl().isEmpty()) {
      findAndRemoveImagesFromContent(layout.getAdditionalPl());
    }
  }
  private LayoutDatabase jsonObjectToLayoutDatabase(JSONObject json) {
    LayoutDatabase layoutDatabase = new LayoutDatabase();

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_ID_KEY)) {
      layoutDatabase.setId(
          getIntFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_ID_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_PARENT_ID_KEY)) {
      layoutDatabase.setParentId(
          getIntFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_PARENT_ID_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_INDEX_KEY)) {
      layoutDatabase.setIndex(
          getIntFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_INDEX_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_TITLE_EN_KEY)) {
      layoutDatabase.setTitleEn(
          getStringFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_TITLE_EN_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_TITLE_PL_KEY)) {
      layoutDatabase.setTitlePl(
          getStringFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_TITLE_PL_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_VERSION_KEY)) {
      layoutDatabase.setVersion(
          getIntFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_VERSION_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_TYPE_KEY)) {
      layoutDatabase.setType(
          getStringFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_TYPE_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_CONTENT_EN_KEY)) {
      layoutDatabase.setContentEn(
          getStringFromJSONObject(
              json, ApplicationContentDownloaderAsyncTask.LAYOUT_CONTENT_EN_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_FAQ_EN_KEY)) {
      layoutDatabase.setContentEn(
          getStringFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_FAQ_EN_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_CONTENT_PL_KEY)) {
      layoutDatabase.setContentPl(
          getStringFromJSONObject(
              json, ApplicationContentDownloaderAsyncTask.LAYOUT_CONTENT_PL_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_FAQ_PL_KEY)) {
      layoutDatabase.setContentPl(
          getStringFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_FAQ_PL_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_ADDITIONAL_EN_KEY)) {
      layoutDatabase.setAdditionalEn(
          getStringFromJSONObject(
              json, ApplicationContentDownloaderAsyncTask.LAYOUT_ADDITIONAL_EN_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_ADDITIONAL_PL_KEY)) {
      layoutDatabase.setAdditionalPl(
          getStringFromJSONObject(
              json, ApplicationContentDownloaderAsyncTask.LAYOUT_ADDITIONAL_PL_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_LATITUDE_KEY)) {
      layoutDatabase.setLatitude(
          (float)
              getDoubleFromJSONObject(
                  json, ApplicationContentDownloaderAsyncTask.LAYOUT_LATITUDE_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_LONGITUDE_KEY)) {
      layoutDatabase.setLongitude(
          (float)
              getDoubleFromJSONObject(
                  json, ApplicationContentDownloaderAsyncTask.LAYOUT_LONGITUDE_KEY));
    }

    if (json.has(ApplicationContentDownloaderAsyncTask.LAYOUT_ZOOM_KEY)) {
      layoutDatabase.setZoom(
          getIntFromJSONObject(json, ApplicationContentDownloaderAsyncTask.LAYOUT_ZOOM_KEY));
    }

    return layoutDatabase;
  }