Hide TagData cursors

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

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

@ -13,6 +13,7 @@ import android.util.Xml;
import android.widget.Toast; import android.widget.Toast;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.PropertyVisitor; import com.todoroo.andlib.data.Property.PropertyVisitor;
import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.data.TodorooCursor;
@ -175,26 +176,18 @@ public class TasksXmlExporter {
} }
private void serializeTagDatas() throws IOException { private void serializeTagDatas() throws IOException {
TodorooCursor<TagData> cursor; tagDataDao.allTags(new Callback<TagData>() {
cursor = tagDataDao.query(Query.select( @Override
TagData.PROPERTIES).orderBy(Order.asc(TagData.ID))); public void apply(TagData tag) {
try { 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); xml.startTag(null, BackupConstants.TAGDATA_TAG);
serializeModel(tag, TagData.PROPERTIES, TagData.ID); serializeModel(tag, TagData.PROPERTIES, TagData.ID);
xml.endTag(null, BackupConstants.TAGDATA_TAG); xml.endTag(null, BackupConstants.TAGDATA_TAG);
//this.exportCount++; } catch(IOException e) {
throw new RuntimeException(e);
} }
} finally {
cursor.close();
} }
});
} }
private void serializeTasks() throws IOException { private void serializeTasks() throws IOException {

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

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

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

Loading…
Cancel
Save