<commit_before>/*
 * Copyright (C) 2014 <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.content.ContentProviderOperation;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import net.simonvt.cathode.BuildConfig;
import net.simonvt.cathode.api.entity.ShowProgress;
import net.simonvt.cathode.api.service.ShowsService;
import net.simonvt.cathode.provider.DatabaseContract;
import net.simonvt.cathode.provider.EpisodeWrapper;
import net.simonvt.cathode.provider.ProviderSchematic;
import net.simonvt.cathode.provider.ShowWrapper;
import net.simonvt.cathode.jobqueue.Job;
import net.simonvt.cathode.jobqueue.JobFailedException;
import timber.log.Timber;

public class SyncShowWatchedStatus extends Job {

  @Inject transient ShowsService showsService;

  private long traktId;

  public SyncShowWatchedStatus(long traktId) {
    this.traktId = traktId;
  }

  @Override public String key() {
    return "SyncShowWatchedStatus" + "&traktId=" + traktId;
  }

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

  @Override public void perform() {
    ShowProgress progress = showsService.getWatchedProgress(traktId);
    final long showId = ShowWrapper.getShowId(getContentResolver(), traktId);

    List<ShowProgress.Season> seasons = progress.getSeasons();

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    for (ShowProgress.Season season : seasons) {
      final int seasonNumber = season.getNumber();

      List<ShowProgress.Episode> episodes = season.getEpisodes();
      for (ShowProgress.Episode episode : episodes) {
        final long episodeId =
            EpisodeWrapper.getEpisodeId(getContentResolver(), showId, seasonNumber,
                episode.getNumber());
        ContentProviderOperation.Builder builder =
            ContentProviderOperation.newUpdate(ProviderSchematic.Episodes.withId(episodeId));
        builder.withValue(DatabaseContract.EpisodeColumns.WATCHED, episode.getCompleted());
        ops.add(builder.build());
      }
    }

    try {
      getContentResolver().applyBatch(BuildConfig.PROVIDER_AUTHORITY, ops);<commit_msg>Create episode if it doesn't exist.<commit_after>/*
 * Copyright (C) 2014 <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.content.ContentProviderOperation;
import android.content.OperationApplicationException;
import android.os.RemoteException;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import net.simonvt.cathode.BuildConfig;
import net.simonvt.cathode.api.entity.ShowProgress;
import net.simonvt.cathode.api.service.ShowsService;
import net.simonvt.cathode.jobqueue.Job;
import net.simonvt.cathode.jobqueue.JobFailedException;
import net.simonvt.cathode.provider.DatabaseContract;
import net.simonvt.cathode.provider.EpisodeWrapper;
import net.simonvt.cathode.provider.ProviderSchematic;
import net.simonvt.cathode.provider.SeasonWrapper;
import net.simonvt.cathode.provider.ShowWrapper;
import timber.log.Timber;

public class SyncShowWatchedStatus extends Job {

  @Inject transient ShowsService showsService;

  private long traktId;

  public SyncShowWatchedStatus(long traktId) {
    this.traktId = traktId;
  }

  @Override public String key() {
    return "SyncShowWatchedStatus" + "&traktId=" + traktId;
  }

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

  @Override public void perform() {
    ShowProgress progress = showsService.getWatchedProgress(traktId);
    long showId = ShowWrapper.getShowId(getContentResolver(), traktId);
    if (showId == -1L) {
      showId = ShowWrapper.createShow(getContentResolver(), traktId);
      queue(new SyncShow(traktId));
    }

    List<ShowProgress.Season> seasons = progress.getSeasons();

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    for (ShowProgress.Season season : seasons) {
      final int seasonNumber = season.getNumber();

      List<ShowProgress.Episode> episodes = season.getEpisodes();
      for (ShowProgress.Episode episode : episodes) {
        final int episodeNumber = episode.getNumber();
        long episodeId =
            EpisodeWrapper.getEpisodeId(getContentResolver(), showId, seasonNumber, episodeNumber);

        if (episodeId == -1L) {
          long seasonId = SeasonWrapper.getSeasonId(getContentResolver(), showId, seasonNumber);
          if (seasonId == -1L) {
            seasonId = SeasonWrapper.createSeason(getContentResolver(), showId, seasonNumber);
          }

          episodeId =
              EpisodeWrapper.createEpisode(getContentResolver(), showId, seasonId, episodeNumber);
          queue(new SyncSeason(traktId, seasonNumber));
        }

        ContentProviderOperation.Builder builder =
            ContentProviderOperation.newUpdate(ProviderSchematic.Episodes.withId(episodeId));
        builder.withValue(DatabaseContract.EpisodeColumns.WATCHED, episode.getCompleted());
        ops.add(builder.build());
      }
    }

    try {
      getContentResolver().applyBatch(BuildConfig.PROVIDER_AUTHORITY, ops);
{'fraction_non_alphanumeric': 0.09405627568763832, 'fraction_numerical': 0.0033196332595637054, 'mean_word_length': 4.194581280788177, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3960801', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1781, 'n_words': 435}