Reimplemented synchronizeTags to work with the new tag scheme

pull/14/head
Sam Bosley 12 years ago
parent abc587c07a
commit f4f22a43ae

@ -471,7 +471,7 @@ public class TasksXmlImporter {
private void saveTags() {
if(currentTask != null && tags.size() > 0) {
TagService.getInstance().synchronizeTags(currentTask.getId(), tags);
TagService.getInstance().synchronizeTags(currentTask.getId(), currentTask.getValue(Task.REMOTE_ID), tags);
}
tags.clear();
}

@ -38,10 +38,15 @@ public class TagMetadata {
* @return
*/
public static Metadata newTagMetadata(Task task, String tagName, long tagUuid) {
return newTagMetadata(task.getId(), task.getValue(Task.REMOTE_ID), tagName, tagUuid);
}
public static Metadata newTagMetadata(long taskId, long taskUuid, String tagName, long tagUuid) {
Metadata link = new Metadata();
link.setValue(Metadata.KEY, KEY);
link.setValue(Metadata.TASK, task.getId());
link.setValue(TASK_UUID, task.getValue(Task.REMOTE_ID));
link.setValue(Metadata.TASK, taskId);
link.setValue(TAG_NAME, tagName);
link.setValue(TASK_UUID, taskUuid);
link.setValue(TAG_UUID, tagUuid);
return link;
}

@ -10,7 +10,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import android.content.Context;
import android.content.Intent;
@ -317,7 +317,6 @@ public final class TagService {
return tagBuilder.toString();
}
// TODO: UPDATE
public boolean deleteOrLeaveTag(Context context, String tag, String sql) {
int deleted = deleteTagMetadata(tag);
TagData tagData = PluginServices.getTagDataService().getTag(tag, TagData.ID, TagData.DELETION_DATE, TagData.MEMBER_COUNT, TagData.USER_ID);
@ -398,28 +397,41 @@ public final class TagService {
* @param taskId
* @param tags
*/
// TODO: UPDATE
public boolean synchronizeTags(long taskId, LinkedHashSet<String> tags) {
MetadataService service = PluginServices.getMetadataService();
public boolean synchronizeTags(long taskId, long taskUuid, Set<String> tags) {
HashSet<Long> existingLinks = new HashSet<Long>();
TodorooCursor<Metadata> links = metadataDao.query(Query.select(Metadata.PROPERTIES)
.where(Criterion.and(TagMetadata.TASK_UUID.eq(taskUuid), Metadata.DELETION_DATE.eq(0))));
try {
for (links.moveToFirst(); !links.isAfterLast(); links.moveToNext()) {
Metadata link = new Metadata(links);
existingLinks.add(link.getValue(TagMetadata.TAG_UUID));
}
} finally {
links.close();
}
HashSet<String> addedTags = new HashSet<String>();
ArrayList<Metadata> metadata = new ArrayList<Metadata>();
for(String tag : tags) {
String tagWithCase = getTagWithCase(tag); // Find if any tag exists that matches with case ignore
if (addedTags.contains(tagWithCase)) // Prevent two identical tags from being added twice (e.g. don't add "Tag, tag" as "tag, tag")
continue;
addedTags.add(tagWithCase);
Metadata item = new Metadata();
item.setValue(Metadata.KEY, TagMetadata.KEY);
item.setValue(TagMetadata.TAG_NAME, tagWithCase);
TagData tagData = tagDataService.getTag(tagWithCase, TagData.REMOTE_ID);
if(tagData != null)
item.setValue(TagMetadata.TAG_UUID, tagData.getValue(TagData.REMOTE_ID));
metadata.add(item);
for (String tag : tags) {
TagData tagData = getTagDataWithCase(tag, TagData.NAME, TagData.REMOTE_ID);
if (tagData == null) {
tagData = new TagData();
tagData.setValue(TagData.NAME, tag);
tagDataService.save(tagData);
}
if (existingLinks.contains(tagData.getValue(TagData.REMOTE_ID))) {
existingLinks.remove(tagData.getValue(TagData.REMOTE_ID));
} else {
Metadata newLink = TagMetadata.newTagMetadata(taskId, taskUuid, tag, tagData.getValue(TagData.REMOTE_ID));
metadataDao.createNew(newLink);
}
}
return service.synchronizeMetadata(taskId, metadata, Metadata.KEY.eq(TagMetadata.KEY));
// Mark as deleted links that don't exist anymore
Metadata deletedLinkTemplate = new Metadata();
deletedLinkTemplate.setValue(Metadata.DELETION_DATE, DateUtilities.now());
metadataDao.update(Criterion.and(MetadataCriteria.withKey(TagMetadata.KEY), TagMetadata.TASK_UUID.eq(taskUuid),
TagMetadata.TAG_UUID.in(existingLinks.toArray(new Long[existingLinks.size()]))), deletedLinkTemplate);
return true;
}
/**
@ -454,10 +466,25 @@ public final class TagService {
return tagWithCase;
}
// TODO: UPDATE
public int deleteTagMetadata(String tag) {
public TagData getTagDataWithCase(String tag, Property<?>... properties) {
TodorooCursor<TagData> tagData = tagDataService.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;
}
private int deleteTagMetadata(String tag) {
invalidateTaskCache(tag);
return PluginServices.getMetadataService().deleteWhere(tagEqIgnoreCase(tag, Criterion.all));
Metadata deleted = new Metadata();
deleted.setValue(Metadata.DELETION_DATE, DateUtilities.now());
return metadataDao.update(tagEqIgnoreCase(tag, Criterion.all), deleted);
}
public int rename(String oldTag, String newTag) {

@ -289,7 +289,7 @@ public final class TagsControlSet extends PopupControlSet {
LinkedHashSet<String> tags = getTagSet();
if(TagService.getInstance().synchronizeTags(task.getId(), tags)) {
if(TagService.getInstance().synchronizeTags(task.getId(), task.getValue(Task.REMOTE_ID), tags)) {
Flags.set(Flags.TAGS_CHANGED);
task.setValue(Task.MODIFICATION_DATE, DateUtilities.now());
}

Loading…
Cancel
Save