private Filter filterUserAssignedByOthers(
      Context context, ProducteevUser user, long currentUserId) {
    String title = context.getString(R.string.producteev_FEx_responsible_title, user.toString());
    ContentValues values = new ContentValues();
    values.put(Metadata.KEY.name, ProducteevTask.METADATA_KEY);
    values.put(ProducteevTask.ID.name, 0);
    values.put(ProducteevTask.CREATOR_ID.name, 0);
    values.put(ProducteevTask.RESPONSIBLE_ID.name, user.getId());
    Filter filter =
        new Filter(
            user.toString(),
            title,
            new QueryTemplate()
                .join(ProducteevDataService.METADATA_JOIN)
                .where(
                    Criterion.and(
                        MetadataCriteria.withKey(ProducteevTask.METADATA_KEY),
                        TaskCriteria.isActive(),
                        TaskCriteria.isVisible(),
                        Criterion.not(ProducteevTask.CREATOR_ID.eq(currentUserId)),
                        ProducteevTask.RESPONSIBLE_ID.eq(user.getId()))),
            values);

    return filter;
  }
 /**
  * Build inbox filter
  *
  * @return
  */
 public static Filter buildInboxFilter(Resources r) {
   Filter inbox =
       new Filter(
           r.getString(R.string.BFE_Active),
           r.getString(R.string.BFE_Active),
           new QueryTemplate()
               .where(
                   Criterion.and(
                       TaskCriteria.activeVisibleMine(),
                       Criterion.not(
                           Task.ID.in(
                               Query.select(Metadata.TASK)
                                   .from(Metadata.TABLE)
                                   .where(
                                       Criterion.and(
                                           MetadataCriteria.withKey(TaskToTagMetadata.KEY),
                                           TaskToTagMetadata.TAG_NAME.like(
                                               "x_%", "x"))))))), // $NON-NLS-1$ //$NON-NLS-2$
           null);
   int themeFlags = ThemeService.getFilterThemeFlags();
   inbox.listingIcon =
       ((BitmapDrawable)
               r.getDrawable(ThemeService.getDrawable(R.drawable.filter_inbox, themeFlags)))
           .getBitmap();
   return inbox;
 }
  private synchronized void synchronizeListHelper(
      StoreObject list,
      GtasksInvoker invoker,
      boolean manual,
      SyncExceptionHandler errorHandler,
      SyncResultCallback callback) {
    String listId = list.getValue(GtasksList.REMOTE_ID);
    long lastSyncDate;
    if (!manual && list.containsNonNullValue(GtasksList.LAST_SYNC)) {
      lastSyncDate = list.getValue(GtasksList.LAST_SYNC);
    } else {
      lastSyncDate = 0;
    }
    boolean includeDeletedAndHidden = lastSyncDate != 0;
    try {
      Tasks taskList =
          invoker.getAllGtasksFromListId(
              listId, includeDeletedAndHidden, includeDeletedAndHidden, lastSyncDate);
      List<com.google.api.services.tasks.model.Task> tasks = taskList.getItems();
      if (tasks != null) {
        callback.incrementMax(tasks.size() * 10);
        HashSet<Long> localIds = new HashSet<Long>(tasks.size());
        for (com.google.api.services.tasks.model.Task t : tasks) {
          GtasksTaskContainer container = parseRemoteTask(t, listId);
          gtasksMetadataService.findLocalMatch(container);
          container.gtaskMetadata.setValue(
              GtasksMetadata.GTASKS_ORDER, Long.parseLong(t.getPosition()));
          container.gtaskMetadata.setValue(
              GtasksMetadata.PARENT_TASK, gtasksMetadataService.localIdForGtasksId(t.getParent()));
          container.gtaskMetadata.setValue(GtasksMetadata.LAST_SYNC, DateUtilities.now() + 1000L);
          write(container);
          localIds.add(container.task.getId());
          callback.incrementProgress(10);
        }
        list.setValue(GtasksList.LAST_SYNC, DateUtilities.now());
        storeObjectDao.persist(list);

        if (lastSyncDate == 0) {
          Long[] localIdArray = localIds.toArray(new Long[localIds.size()]);
          Criterion delete =
              Criterion.and(
                  Metadata.KEY.eq(GtasksMetadata.METADATA_KEY),
                  GtasksMetadata.LIST_ID.eq(listId),
                  Criterion.not(Metadata.TASK.in(localIdArray)));
          taskService.deleteWhere(
              Task.ID.in(Query.select(Metadata.TASK).from(Metadata.TABLE).where(delete)));
          metadataService.deleteWhere(delete);
        }

        gtasksTaskListUpdater.correctOrderAndIndentForList(listId);
      }
    } catch (GoogleTasksException e) {
      if (errorHandler != null)
        errorHandler.handleException("gtasks-sync-io", e, e.getType()); // $NON-NLS-1$
    } catch (IOException e) {
      if (errorHandler != null)
        errorHandler.handleException("gtasks-sync-io", e, e.toString()); // $NON-NLS-1$
    }
  }
  @SuppressWarnings("nls")
  private TodorooCursor<Task> constructCursor() {
    String tagName = null;
    if (getActiveTagData() != null) tagName = getActiveTagData().getValue(TagData.NAME);

    String[] emergentTags = TagService.getInstance().getEmergentTags();
    StringProperty tagProperty =
        new StringProperty(null, TAGS_METADATA_JOIN + "." + TagService.TAG.name);

    Criterion tagsJoinCriterion =
        Criterion.and(
            Field.field(TAGS_METADATA_JOIN + "." + Metadata.KEY.name)
                .eq(TagService.KEY), // $NON-NLS-1$
            Task.ID.eq(Field.field(TAGS_METADATA_JOIN + "." + Metadata.TASK.name)),
            Criterion.not(tagProperty.in(emergentTags)));
    if (tagName != null)
      tagsJoinCriterion =
          Criterion.and(
              tagsJoinCriterion,
              Field.field(TAGS_METADATA_JOIN + "." + TagService.TAG.name).neq(tagName));

    // TODO: For now, we'll modify the query to join and include the task rabbit and tag data here.
    // Eventually, we might consider restructuring things so that this query is constructed
    // elsewhere.
    String joinedQuery =
        Join.left(
                    Metadata.TABLE.as(TR_METADATA_JOIN),
                    Criterion.and(
                        Field.field(TR_METADATA_JOIN + "." + Metadata.KEY.name)
                            .eq(TaskRabbitMetadata.METADATA_KEY), // $NON-NLS-1$
                        Task.ID.eq(Field.field(TR_METADATA_JOIN + "." + Metadata.TASK.name))))
                .toString() //$NON-NLS-1$
            + Join.left(Metadata.TABLE.as(TAGS_METADATA_JOIN), tagsJoinCriterion)
                .toString() //$NON-NLS-1$
            + filter.getSqlQuery();

    sqlQueryTemplate.set(SortHelper.adjustQueryForFlagsAndSort(joinedQuery, sortFlags, sortSort));

    String groupedQuery;
    if (sqlQueryTemplate.get().contains("GROUP BY")) groupedQuery = sqlQueryTemplate.get();
    else if (sqlQueryTemplate.get().contains("ORDER BY")) // $NON-NLS-1$
    groupedQuery =
          sqlQueryTemplate
              .get()
              .replace("ORDER BY", "GROUP BY " + Task.ID + " ORDER BY"); // $NON-NLS-1$
    else groupedQuery = sqlQueryTemplate.get() + " GROUP BY " + Task.ID;
    sqlQueryTemplate.set(groupedQuery);

    // Peform query
    try {
      return taskService.fetchFiltered(sqlQueryTemplate.get(), null, taskProperties());
    } catch (SQLiteException e) {
      // We don't show this error anymore--seems like this can get triggered
      // by a strange bug, but there seems to not be any negative side effect.
      // For now, we'll suppress the error
      // See http://astrid.com/home#tags-7tsoi/task-1119pk
      return null;
    }
  }
  private void addTags(ArrayList<FilterListItem> list) {
    HashSet<String> tagNames = new HashSet<String>();

    // active tags
    Tag[] myTags =
        tagService.getGroupedTags(
            TagService.GROUPED_TAGS_BY_SIZE,
            Criterion.and(TaskCriteria.ownedByMe(), TaskCriteria.activeAndVisible()));
    for (Tag tag : myTags) tagNames.add(tag.tag);
    if (myTags.length > 0) list.add(filterFromTags(myTags, R.string.tag_FEx_category_mine));

    // find all tag data not in active tag list
    TodorooCursor<TagData> cursor =
        tagDataService.query(
            Query.select(TagData.NAME, TagData.TASK_COUNT, TagData.REMOTE_ID)
                .where(TagData.DELETION_DATE.eq(0)));
    ArrayList<Tag> notListed = new ArrayList<Tag>();
    try {
      ArrayList<Tag> sharedTags = new ArrayList<Tag>();
      for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        String tagName = cursor.get(TagData.NAME);
        if (tagNames.contains(tagName)) continue;
        Tag tag = new Tag(tagName, cursor.get(TagData.TASK_COUNT), cursor.get(TagData.REMOTE_ID));
        if (tag.count > 0) sharedTags.add(tag);
        else notListed.add(tag);
        tagNames.add(tagName);
      }
      if (sharedTags.size() > 0)
        list.add(
            filterFromTags(
                sharedTags.toArray(new Tag[sharedTags.size()]), R.string.tag_FEx_category_shared));
    } finally {
      cursor.close();
    }

    // find inactive tags, intersect tag list
    Tag[] inactiveTags =
        tagService.getGroupedTags(
            TagService.GROUPED_TAGS_BY_ALPHA,
            Criterion.and(
                TaskCriteria.notDeleted(), Criterion.not(TaskCriteria.activeAndVisible())));
    for (Tag tag : inactiveTags) {
      if (!tagNames.contains(tag.tag) && !TextUtils.isEmpty(tag.tag)) {
        notListed.add(tag);
        tag.count = 0;
      }
    }
    if (notListed.size() > 0)
      list.add(
          filterFromTags(
              notListed.toArray(new Tag[notListed.size()]), R.string.tag_FEx_category_inactive));
  }