private TodorooCursor<Task> constructCursor() { String tagName = null; if (getActiveTagData() != null) { tagName = getActiveTagData().getName(); } Criterion tagsJoinCriterion = Criterion.and( Field.field(TAGS_METADATA_JOIN + "." + Metadata.KEY.name) .eq(TaskToTagMetadata.KEY), // $NON-NLS-1$ Field.field(TAGS_METADATA_JOIN + "." + Metadata.DELETION_DATE.name).eq(0), Task.ID.eq(Field.field(TAGS_METADATA_JOIN + "." + Metadata.TASK.name))); if (tagName != null) { tagsJoinCriterion = Criterion.and( tagsJoinCriterion, Field.field(TAGS_METADATA_JOIN + "." + TaskToTagMetadata.TAG_NAME.name).neq(tagName)); } // TODO: For now, we'll modify the query to join and include the things like tag data here. // Eventually, we might consider restructuring things so that this query is constructed // elsewhere. String joinedQuery = Join.left(Metadata.TABLE.as(TAGS_METADATA_JOIN), tagsJoinCriterion) .toString() //$NON-NLS-1$ + Join.left( TaskAttachment.TABLE.as(FILE_METADATA_JOIN), Task.UUID.eq(Field.field(FILE_METADATA_JOIN + "." + TaskAttachment.TASK_UUID.name))) + 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 log.error(e.getMessage(), e); return null; } }
public static String buildOrderString(String[] ids) { StringBuilder builder = new StringBuilder(); if (ids.length == 0) { return "(1)"; //$NON-NLS-1$ } for (int i = ids.length - 1; i >= 0; i--) { builder.append(Task.UUID.eq(ids[i]).toString()); if (i > 0) { builder.append(", "); // $NON-NLS-1$ } } return builder.toString(); }