Hide TagData cursors

pull/189/head
Alex Baker 10 years ago
parent 379e321475
commit 2ad63acef9

@ -12,7 +12,6 @@ import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.data.Table;
import com.todoroo.andlib.data.TodorooCursor;
/**
* Data Model which represents a collaboration space for users / tasks.
@ -76,16 +75,6 @@ public final class TagData extends RemoteModel {
return defaultValues;
}
// --- data access boilerplate
public TagData() {
super();
}
public TagData(TodorooCursor<TagData> cursor) {
super(cursor);
}
@Override
public long getId() {
return getIdHelper(ID);

@ -15,8 +15,6 @@ import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.activity.AstridActivity;
import com.todoroo.astrid.activity.FilterListFragment;
@ -115,25 +113,15 @@ public class TagViewFragment extends TaskListFragment {
return;
}
TodorooCursor<TagData> cursor;
if (!RemoteModel.isUuidEmpty(uuid)) {
cursor = tagDataDao.query(Query.select(TagData.PROPERTIES).where(TagData.UUID.eq(uuid)));
} else {
cursor = tagDataDao.query(Query.select(TagData.PROPERTIES).where(TagData.NAME.eqCaseInsensitive(tag)));
}
TagData tagData = RemoteModel.isUuidEmpty(uuid)
? tagDataDao.getTagByName(tag, TagData.PROPERTIES)
: tagDataDao.getByUuid(uuid, TagData.PROPERTIES);
try {
if(cursor.getCount() == 0) {
tagData = new TagData();
tagData.setName(tag);
tagData.setUUID(uuid);
tagDataDao.persist(tagData);
} else {
cursor.moveToFirst();
tagData = new TagData(cursor);
}
} finally {
cursor.close();
if (tagData == null) {
tagData = new TagData();
tagData.setName(tag);
tagData.setUUID(uuid);
tagDataDao.persist(tagData);
}
super.initializeData();

@ -13,6 +13,7 @@ import android.util.Xml;
import android.widget.Toast;
import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.PropertyVisitor;
import com.todoroo.andlib.data.TodorooCursor;
@ -174,27 +175,19 @@ public class TasksXmlExporter {
fos.close();
}
private void serializeTagDatas() throws IOException {
TodorooCursor<TagData> cursor;
cursor = tagDataDao.query(Query.select(
TagData.PROPERTIES).orderBy(Order.asc(TagData.ID)));
try {
int length = cursor.getCount();
for(int i = 0; i < length; i++) {
cursor.moveToNext();
TagData tag = new TagData(cursor);
//TODO setProgress(i, length);
xml.startTag(null, BackupConstants.TAGDATA_TAG);
serializeModel(tag, TagData.PROPERTIES, TagData.ID);
xml.endTag(null, BackupConstants.TAGDATA_TAG);
//this.exportCount++;
private void serializeTagDatas() throws IOException {
tagDataDao.allTags(new Callback<TagData>() {
@Override
public void apply(TagData tag) {
try {
xml.startTag(null, BackupConstants.TAGDATA_TAG);
serializeModel(tag, TagData.PROPERTIES, TagData.ID);
xml.endTag(null, BackupConstants.TAGDATA_TAG);
} catch(IOException e) {
throw new RuntimeException(e);
}
}
} finally {
cursor.close();
}
});
}
private void serializeTasks() throws IOException {

@ -286,20 +286,15 @@ public class TasksXmlImporter {
String uuid = metadata.getValue(Metadata.VALUE2);
long deletionDate = metadata.getDeletionDate();
// UUID is uniquely for every TagData, so we don't need to test the name
TodorooCursor<TagData> cursor = tagDataDao.query(Query.select(TagData.ID).
where(TagData.UUID.eq(uuid)));
try {
//If you sync with Google tasks it adds some Google task metadata.
//For this metadata we don't create a list!
if(key.equals(TaskToTagMetadata.KEY) && cursor.getCount() == 0 && deletionDate == 0) {
tagdata.clear();
tagdata.setId(TagData.NO_ID);
tagdata.setUuid(uuid);
tagdata.setName(name);
tagDataDao.persist(tagdata);
}
} finally {
cursor.close();
TagData tagData = tagDataDao.getByUuid(uuid, TagData.ID);
//If you sync with Google tasks it adds some Google task metadata.
//For this metadata we don't create a list!
if(key.equals(TaskToTagMetadata.KEY) && tagData == null && deletionDate == 0) {
tagdata.clear();
tagdata.setId(TagData.NO_ID);
tagdata.setUuid(uuid);
tagdata.setName(name);
tagDataDao.persist(tagdata);
}
}
}

@ -5,7 +5,11 @@
*/
package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Functions;
import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TagData;
@ -37,5 +41,23 @@ public class TagDataDao extends RemoteModelDao<TagData> {
public TagData getTagByName(String name, Property<?>... properties) {
return getFirst(Query.select(properties).where(TagData.NAME.eqCaseInsensitive(name)));
}
public void allTags(Callback<TagData> callback) {
// TODO: does this need to be ordered?
query(callback, Query.select(TagData.PROPERTIES)
.where(TagData.DELETION_DATE.eq(0))
.orderBy(Order.asc(TagData.ID)));
}
public TagData getByUuid(String uuid, Property<?>... properties) {
return getFirst(Query.select(properties).where(TagData.UUID.eq(uuid)));
}
public void tagDataOrderedByName(Callback<TagData> callback) {
query(callback, Query.select(TagData.PROPERTIES).where(Criterion.and(
TagData.DELETION_DATE.eq(0),
TagData.NAME.isNotNull())
).orderBy(Order.asc(Functions.upper(TagData.NAME))));
}
}

@ -7,7 +7,7 @@ package com.todoroo.astrid.tags;
import android.text.TextUtils;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.data.Property.CountProperty;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Criterion;
@ -32,6 +32,7 @@ import org.tasks.R;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.inject.Inject;
@ -149,37 +150,18 @@ public final class TagService {
}
private Tag tagFromUUID(String uuid) {
TodorooCursor<TagData> tagData = tagDataDao.query(Query.select(TagData.PROPERTIES).where(TagData.UUID.eq(uuid)));
try {
if (tagData.getCount() > 0) {
tagData.moveToFirst();
return new Tag(new TagData(tagData));
} else {
return null;
}
} finally {
tagData.close();
}
TagData tagData = tagDataDao.getByUuid(uuid, TagData.PROPERTIES);
return tagData == null ? null : new Tag(tagData);
}
public void createLink(Task task, String tagName) {
TodorooCursor<TagData> existingTag = tagDataDao.query(Query.select(TagData.NAME, TagData.UUID)
.where(TagData.NAME.eqCaseInsensitive(tagName)));
try {
TagData tagData;
if (existingTag.getCount() == 0) {
tagData = new TagData();
tagData.setName(tagName);
tagDataDao.persist(tagData);
} else {
existingTag.moveToFirst();
tagData = new TagData(existingTag);
}
createLink(task, tagData.getName(), tagData.getUUID());
} finally {
existingTag.close();
TagData tagData = tagDataDao.getTagByName(tagName, TagData.NAME, TagData.UUID);
if (tagData == null) {
tagData = new TagData();
tagData.setName(tagName);
tagDataDao.persist(tagData);
}
createLink(task, tagData.getName(), tagData.getUUID());
}
public void createLink(Task task, String tagName, String tagUuid) {
@ -223,23 +205,16 @@ public final class TagService {
/**
* Return all tags (including metadata tags and TagData tags) in an array list
*/
public ArrayList<Tag> getTagList() {
ArrayList<Tag> tagList = new ArrayList<>();
TodorooCursor<TagData> cursor = tagDataDao.query(Query.select(TagData.PROPERTIES).where(Criterion.and(
TagData.DELETION_DATE.eq(0),
TagData.NAME.isNotNull())).orderBy(Order.asc(Functions.upper(TagData.NAME))));
try {
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
TagData tagData = new TagData(cursor);
Tag tag = new Tag(tagData);
if(TextUtils.isEmpty(tag.tag)) {
continue;
public List<Tag> getTagList() {
final List<Tag> tagList = new ArrayList<>();
tagDataDao.tagDataOrderedByName(new Callback<TagData>() {
@Override
public void apply(TagData tagData) {
if (!TextUtils.isEmpty(tagData.getName())) {
tagList.add(new Tag(tagData));
}
tagList.add(tag);
}
} finally {
cursor.close();
}
});
return tagList;
}
@ -260,7 +235,7 @@ public final class TagService {
}
for (String tag : tags) {
TagData tagData = getTagDataWithCase(tag, TagData.NAME, TagData.UUID);
TagData tagData = tagDataDao.getTagByName(tag, TagData.NAME, TagData.UUID);
if (tagData == null) {
tagData = new TagData();
tagData.setName(tag);
@ -291,14 +266,9 @@ public final class TagService {
Metadata tagMatch = new Metadata(tagMetadata);
tagWithCase = tagMatch.getValue(TaskToTagMetadata.TAG_NAME);
} else {
TodorooCursor<TagData> tagData = tagDataDao.query(Query.select(TagData.NAME).where(TagData.NAME.eqCaseInsensitive(tag)));
try {
if (tagData.getCount() > 0) {
tagData.moveToFirst();
tagWithCase = new TagData(tagData).getName();
}
} finally {
tagData.close();
TagData tagData = tagDataDao.getTagByName(tag, TagData.NAME);
if (tagData != null) {
tagWithCase = tagData.getName();
}
}
} finally {
@ -307,19 +277,6 @@ public final class TagService {
return tagWithCase;
}
public TagData getTagDataWithCase(String tag, Property<?>... properties) {
TodorooCursor<TagData> tagData = tagDataDao.query(Query.select(properties).where(TagData.NAME.eqCaseInsensitive(tag)));
try {
if (tagData.getCount() > 0) {
tagData.moveToFirst();
return new TagData(tagData);
}
} finally {
tagData.close();
}
return null;
}
public int rename(String uuid, String newName) {
TagData template = new TagData();
template.setName(newName);

@ -38,6 +38,7 @@ import org.tasks.preferences.ActivityPreferences;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import static org.tasks.preferences.ResourceResolver.getResource;
@ -74,7 +75,7 @@ public final class TagsControlSet extends PopupControlSet {
}
private Tag[] getTagArray() {
ArrayList<Tag> tagsList = tagService.getTagList();
List<Tag> tagsList = tagService.getTagList();
return tagsList.toArray(new Tag[tagsList.size()]);
}

Loading…
Cancel
Save