<commit_before>/*
 * Copyright (C) 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.simonvt.cathode.remote.sync.shows;

import android.database.Cursor;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import net.simonvt.cathode.api.entity.Episode;
import net.simonvt.cathode.api.entity.Show;
import net.simonvt.cathode.api.entity.WatchlistItem;
import net.simonvt.cathode.api.service.SyncService;
import net.simonvt.cathode.provider.DatabaseContract.EpisodeColumns;
import net.simonvt.cathode.provider.DatabaseSchematic;
import net.simonvt.cathode.provider.EpisodeDatabaseHelper;
import net.simonvt.cathode.provider.ProviderSchematic.Episodes;
import net.simonvt.cathode.provider.SeasonDatabaseHelper;
import net.simonvt.cathode.provider.ShowDatabaseHelper;
import net.simonvt.cathode.remote.CallJob;
import net.simonvt.cathode.remote.Flags;
import net.simonvt.schematic.Cursors;
import retrofit2.Call;

public class SyncEpisodeWatchlist extends CallJob<List<WatchlistItem>> {

  @Inject transient SyncService syncService;

  @Inject transient ShowDatabaseHelper showHelper;
  @Inject transient SeasonDatabaseHelper seasonHelper;
  @Inject transient EpisodeDatabaseHelper episodeHelper;

  public SyncEpisodeWatchlist() {
    super(Flags.REQUIRES_AUTH);
  }

  @Override public String key() {
    return "SyncEpisodeWatchlist";
  }

  @Override public int getPriority() {
    return PRIORITY_USER_DATA;
  }

  @Override public Call<List<WatchlistItem>> getCall() {
    return syncService.getEpisodeWatchlist();
  }

  @Override public void handleResponse(List<WatchlistItem> watchlist) {
    Cursor c = getContentResolver().query(Episodes.EPISODES_IN_WATCHLIST, new String[] {
        DatabaseSchematic.Tables.EPISODES + "." + EpisodeColumns.ID,
    }, null, null, null);

    List<Long> episodeIds = new ArrayList<>();

    while (c.moveToNext()) {
      episodeIds.add(Cursors.getLong(c, EpisodeColumns.ID));
    }
    c.close();

    for (WatchlistItem item : watchlist) {
      final Show show = item.getShow();
      final Episode episode = item.getEpisode();
      final long showTraktId = show.getIds().getTrakt();
      final int seasonNumber = episode.getSeason();
      final int episodeNumber = episode.getNumber();
      final long listedAt = item.getListedAt().getTimeInMillis();

      ShowDatabaseHelper.IdResult showResult = showHelper.getIdOrCreate(showTraktId);
      final long showId = showResult.showId;
      final boolean didShowExist = !showResult.didCreate;
      if (showResult.didCreate) {
        queue(new SyncShow(showTraktId));
      }

      SeasonDatabaseHelper.IdResult seasonResult = seasonHelper.getIdOrCreate(showId, seasonNumber);<commit_msg>Update show with ids when adding episode to watchlist.<commit_after>/*
 * Copyright (C) 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.simonvt.cathode.remote.sync.shows;

import android.database.Cursor;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import net.simonvt.cathode.api.entity.Episode;
import net.simonvt.cathode.api.entity.Show;
import net.simonvt.cathode.api.entity.WatchlistItem;
import net.simonvt.cathode.api.service.SyncService;
import net.simonvt.cathode.provider.DatabaseContract.EpisodeColumns;
import net.simonvt.cathode.provider.DatabaseSchematic;
import net.simonvt.cathode.provider.EpisodeDatabaseHelper;
import net.simonvt.cathode.provider.ProviderSchematic.Episodes;
import net.simonvt.cathode.provider.SeasonDatabaseHelper;
import net.simonvt.cathode.provider.ShowDatabaseHelper;
import net.simonvt.cathode.remote.CallJob;
import net.simonvt.cathode.remote.Flags;
import net.simonvt.schematic.Cursors;
import retrofit2.Call;

public class SyncEpisodeWatchlist extends CallJob<List<WatchlistItem>> {

  @Inject transient SyncService syncService;

  @Inject transient ShowDatabaseHelper showHelper;
  @Inject transient SeasonDatabaseHelper seasonHelper;
  @Inject transient EpisodeDatabaseHelper episodeHelper;

  public SyncEpisodeWatchlist() {
    super(Flags.REQUIRES_AUTH);
  }

  @Override public String key() {
    return "SyncEpisodeWatchlist";
  }

  @Override public int getPriority() {
    return PRIORITY_USER_DATA;
  }

  @Override public Call<List<WatchlistItem>> getCall() {
    return syncService.getEpisodeWatchlist();
  }

  @Override public void handleResponse(List<WatchlistItem> watchlist) {
    Cursor c = getContentResolver().query(Episodes.EPISODES_IN_WATCHLIST, new String[] {
        DatabaseSchematic.Tables.EPISODES + "." + EpisodeColumns.ID,
    }, null, null, null);

    List<Long> episodeIds = new ArrayList<>();

    while (c.moveToNext()) {
      episodeIds.add(Cursors.getLong(c, EpisodeColumns.ID));
    }
    c.close();

    for (WatchlistItem item : watchlist) {
      final Show show = item.getShow();
      final Episode episode = item.getEpisode();
      final long showTraktId = show.getIds().getTrakt();
      final int seasonNumber = episode.getSeason();
      final int episodeNumber = episode.getNumber();
      final long listedAt = item.getListedAt().getTimeInMillis();

      ShowDatabaseHelper.IdResult showResult = showHelper.getIdOrCreate(showTraktId);
      final long showId = showResult.showId;
      final boolean didShowExist = !showResult.didCreate;
      if (showResult.didCreate) {
        showHelper.partialUpdate(show);
        queue(new SyncShow(showTraktId));
      }

      SeasonDatabaseHelper.IdResult seasonResult = seasonHelper.getIdOrCreate(showId, seasonNumber);
{'fraction_non_alphanumeric': 0.09660733302905827, 'fraction_numerical': 0.0027384755819260614, 'mean_word_length': 4.496655518394649, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '986733', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1951, 'n_words': 476}