// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String CREATED_AT = "created_at";
// 
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String REMOTE_ID = "remote_id";
// 
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String UPDATED_AT = "updated_at";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_ASSETS = "assets";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_ENTRY_TYPES = "entry_types";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_LINKS = "links";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_SYNC_INFO = "sync_info";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static String escape(String name) {
//   return String.format("`%s`", name);
// }
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static String localizeName(String name, String locale) {
//   return String.format("%s$%s", name, locale);
// }


import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import com.contentful.java.cda.CDAAsset;
import com.contentful.java.cda.CDAEntry;
import com.contentful.java.cda.CDAResource;
import com.contentful.java.cda.CDAType;
import com.contentful.java.cda.LocalizedResource;
import com.contentful.java.cda.SynchronizedSpace;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import okhttp3.HttpUrl;
import static android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE;
import static com.contentful.java.cda.CDAType.ASSET;
import static com.contentful.java.cda.CDAType.ENTRY;
import static com.contentful.vault.BaseFields.CREATED_AT;
import static com.contentful.vault.BaseFields.REMOTE_ID;
import static com.contentful.vault.BaseFields.UPDATED_AT;
import static com.contentful.vault.Sql.TABLE_ASSETS;
import static com.contentful.vault.Sql.TABLE_ENTRY_TYPES;
import static com.contentful.vault.Sql.TABLE_LINKS;
import static com.contentful.vault.Sql.TABLE_SYNC_INFO;
import static com.contentful.vault.Sql.escape;
import static com.contentful.vault.Sql.localizeName;
    String contentTypeId = LinkResolver.fetchEntryType(db, id);
    if (contentTypeId != null) {
      Class<?> clazz = spaceHelper.getTypes().get(contentTypeId);
      if (clazz != null) {
        deleteResource(id, spaceHelper.getModels().get(clazz).getTableName());
        deleteEntryType(id);
      }
    }
  }

  private void deleteEntryType(String remoteId) {
    String whereClause = REMOTE_ID + " = ?";
    String[] whereArgs = new String[]{remoteId};
    db.delete(TABLE_ENTRY_TYPES, whereClause, whereArgs);
  }

  private void deleteResource(String remoteId, String tableName) {
    // resource
    String resWhere = REMOTE_ID + " = ?";
    String resArgs[] = new String[]{remoteId};

    // links
    String linksWhere = "`parent` = ? OR `child` = ?";
    String linkArgs[] = new String[]{
        remoteId,
        remoteId
    };

    for (String locale : spaceHelper.getLocales()) {
      db.delete(escape(localizeName(tableName, locale)), resWhere, resArgs);
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String CREATED_AT = "created_at";
// 
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String REMOTE_ID = "remote_id";
// 
// Path: core/src/main/java/com/contentful/vault/BaseFields.java
// public static final String UPDATED_AT = "updated_at";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_ASSETS = "assets";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_ENTRY_TYPES = "entry_types";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_LINKS = "links";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static final String TABLE_SYNC_INFO = "sync_info";
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static String escape(String name) {
//   return String.format("`%s`", name);
// }
// 
// Path: core/src/main/java/com/contentful/vault/Sql.java
// static String localizeName(String name, String locale) {
//   return String.format("%s$%s", name, locale);
// }


// Path: core/src/main/java/com/contentful/vault/SyncRunnable.java
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import com.contentful.java.cda.CDAAsset;
import com.contentful.java.cda.CDAEntry;
import com.contentful.java.cda.CDAResource;
import com.contentful.java.cda.CDAType;
import com.contentful.java.cda.LocalizedResource;
import com.contentful.java.cda.SynchronizedSpace;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import okhttp3.HttpUrl;
import static android.database.sqlite.SQLiteDatabase.CONFLICT_REPLACE;
import static com.contentful.java.cda.CDAType.ASSET;
import static com.contentful.java.cda.CDAType.ENTRY;
import static com.contentful.vault.BaseFields.CREATED_AT;
import static com.contentful.vault.BaseFields.REMOTE_ID;
import static com.contentful.vault.BaseFields.UPDATED_AT;
import static com.contentful.vault.Sql.TABLE_ASSETS;
import static com.contentful.vault.Sql.TABLE_ENTRY_TYPES;
import static com.contentful.vault.Sql.TABLE_LINKS;
import static com.contentful.vault.Sql.TABLE_SYNC_INFO;
import static com.contentful.vault.Sql.escape;
import static com.contentful.vault.Sql.localizeName;

    String contentTypeId = LinkResolver.fetchEntryType(db, id);
    if (contentTypeId != null) {
      Class<?> clazz = spaceHelper.getTypes().get(contentTypeId);
      if (clazz != null) {
        deleteResource(id, spaceHelper.getModels().get(clazz).getTableName());
        deleteEntryType(id);
      }
    }
  }

  private void deleteEntryType(String remoteId) {
    String whereClause = REMOTE_ID + " = ?";
    String[] whereArgs = new String[]{remoteId};
    db.delete(TABLE_ENTRY_TYPES, whereClause, whereArgs);
  }

  private void deleteResource(String remoteId, String tableName) {
    // resource
    String resWhere = REMOTE_ID + " = ?";
    String resArgs[] = new String[]{remoteId};

    // links
    String linksWhere = "`parent` = ? OR `child` = ?";
    String linkArgs[] = new String[]{
        remoteId,
        remoteId
    };

    for (String locale : spaceHelper.getLocales()) {
      db.delete(escape(localizeName(tableName, locale)), resWhere, resArgs);
      db.delete(escape(localizeName(TABLE_LINKS, locale)), linksWhere, linkArgs);