Remove property visitors from backup

pull/618/head
Alex Baker 6 years ago
parent 985b1f3485
commit c93a1a8c21

@ -147,7 +147,6 @@ public class TasksXmlExporter {
}).start();
}
private void doTasksExport(String output) throws IOException {
File xmlFile = new File(output);
xmlFile.createNewFile();
@ -194,32 +193,15 @@ public class TasksXmlExporter {
setProgress(i, length);
xml.startTag(null, BackupConstants.TASK_TAG);
serializeModel(task, Task.PROPERTIES, Task.ID);
serializeMetadata(task);
serializeComments(task);
serializeTask(task);
xml.endTag(null, BackupConstants.TASK_TAG);
this.exportCount++;
}
}
private synchronized void serializeComments(Task task) {
for (UserActivity comment : userActivityDao.getCommentsForTask(task.getUuid())) {
writeComment(comment);
}
}
private void writeComment(UserActivity userActivity) {
try {
xml.startTag(null, BackupConstants.COMMENT_TAG);
userActivity.writeToXml(new XmlWriter(xml));
xml.endTag(null, BackupConstants.COMMENT_TAG);
} catch (IOException e) {
// ignore
}
}
private synchronized void serializeMetadata(Task task) {
private synchronized void serializeTask(Task task) {
XmlWriter writer = new XmlWriter(xml);
task.writeToXml(writer);
for (Alarm alarm : alarmDao.getAlarms(task.getId())) {
try {
xml.startTag(null, BackupConstants.ALARM_TAG);
@ -256,94 +238,19 @@ public class TasksXmlExporter {
throw new RuntimeException(e);
}
}
}
/**
* Turn a model into xml attributes
*/
private void serializeModel(AbstractModel model, Property<?>[] properties, Property<?>... excludes) {
outer: for(Property<?> property : properties) {
for(Property<?> exclude : excludes) {
if (property.name.equals(exclude.name)) {
continue outer;
}
}
for (UserActivity comment : userActivityDao.getCommentsForTask(task.getUuid())) {
try {
property.accept(xmlWritingVisitor, model);
} catch (Exception e) {
Timber.e(e, e.getMessage());
xml.startTag(null, BackupConstants.COMMENT_TAG);
comment.writeToXml(new XmlWriter(xml));
xml.endTag(null, BackupConstants.COMMENT_TAG);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private final XmlWritingPropertyVisitor xmlWritingVisitor = new XmlWritingPropertyVisitor();
public static final String XML_NULL = "null"; //$NON-NLS-1$
public class XmlWritingPropertyVisitor implements PropertyVisitor<Void, AbstractModel> {
@Override
public Void visitInteger(Property<Integer> property, AbstractModel data) {
try {
Integer value = data.getValue(property);
String valueString = (value == null) ? XML_NULL : value.toString();
xml.attribute(null, property.name, valueString);
} catch (UnsupportedOperationException e) {
// didn't read this value, do nothing
Timber.e(e, e.getMessage());
} catch (IllegalArgumentException | IOException | IllegalStateException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void visitLong(Property<Long> property, AbstractModel data) {
try {
Long value = data.getValue(property);
String valueString = (value == null) ? XML_NULL : value.toString();
xml.attribute(null, property.name, valueString);
} catch (UnsupportedOperationException e) {
// didn't read this value, do nothing
Timber.e(e, e.getMessage());
} catch (IllegalArgumentException | IOException | IllegalStateException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void visitDouble(Property<Double> property, AbstractModel data) {
try {
Double value = data.getValue(property);
String valueString = (value == null) ? XML_NULL : value.toString();
xml.attribute(null, property.name, valueString);
} catch (UnsupportedOperationException e) {
// didn't read this value, do nothing
} catch (IllegalArgumentException | IllegalStateException | IOException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
public Void visitString(Property<String> property, AbstractModel data) {
try {
String value = data.getValue(property);
if(value == null) {
return null;
}
xml.attribute(null, property.name, value);
} catch (UnsupportedOperationException e) {
// didn't read this value, do nothing
Timber.v(e, e.getMessage());
} catch (IllegalArgumentException | IOException | IllegalStateException e) {
throw new RuntimeException(e);
}
return null;
}
}
private void onFinishExport(final String outputFile) {
post(() -> {
if(exportCount == 0) {

@ -11,18 +11,11 @@ import android.content.res.Resources;
import android.os.Handler;
import android.text.TextUtils;
import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.PropertyVisitor;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.DialogUtilities;
import org.tasks.data.TagDataDao;
import com.todoroo.astrid.dao.TaskDao;
import org.tasks.data.UserActivityDao;
import org.tasks.data.TagData;
import com.todoroo.astrid.data.Task;
import org.tasks.data.UserActivity;
import org.tasks.LocalBroadcastManager;
import org.tasks.R;
@ -35,6 +28,10 @@ import org.tasks.data.Location;
import org.tasks.data.LocationDao;
import org.tasks.data.Tag;
import org.tasks.data.TagDao;
import org.tasks.data.TagData;
import org.tasks.data.TagDataDao;
import org.tasks.data.UserActivity;
import org.tasks.data.UserActivityDao;
import org.tasks.dialogs.DialogBuilder;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -198,7 +195,6 @@ public class TasksXmlImporter {
void parseTask() {
taskCount++;
setProgressMessage(activity.getString(R.string.import_progress_read, taskCount));
currentTask = new Task();
String title = xpp.getAttributeValue(null, Task.TITLE.name);
String created = xpp.getAttributeValue(null, Task.CREATION_DATE.name);
@ -215,8 +211,7 @@ public class TasksXmlImporter {
if (taskDao.count(query) > 0) {
skipCount++;
} else {
deserializeModel(currentTask, Task.PROPERTIES);
currentTask = new Task(new XmlReader(xpp));
// Save the task to the database.
taskDao.save(currentTask);
importCount++;
@ -326,66 +321,6 @@ public class TasksXmlImporter {
googleTaskDao.insert(googleTask);
}
}
/**
* Turn a model into xml attributes
*/
void deserializeModel(AbstractModel model, Property<?>[] properties) {
for(Property<?> property : properties) {
try {
property.accept(xmlReadingVisitor, model);
} catch (Exception e) {
Timber.e(e, e.getMessage());
}
}
}
private final XmlReadingPropertyVisitor xmlReadingVisitor = new XmlReadingPropertyVisitor();
private class XmlReadingPropertyVisitor implements PropertyVisitor<Void, AbstractModel> {
@Override
public Void visitInteger(Property<Integer> property,
AbstractModel data) {
String value = xpp.getAttributeValue(null, property.name);
if(value != null) {
data.setValue(property, TasksXmlExporter.XML_NULL.equals(value) ?
null : Integer.parseInt(value));
}
return null;
}
@Override
public Void visitLong(Property<Long> property, AbstractModel data) {
String value = xpp.getAttributeValue(null, property.name);
if(value != null) {
data.setValue(property, TasksXmlExporter.XML_NULL.equals(value) ?
null : Long.parseLong(value));
}
return null;
}
@Override
public Void visitDouble(Property<Double> property, AbstractModel data) {
String value = xpp.getAttributeValue(null, property.name);
if (value != null) {
data.setValue(property, TasksXmlExporter.XML_NULL.equals(value) ?
null : Double.parseDouble(value));
}
return null;
}
@Override
public Void visitString(Property<String> property,
AbstractModel data) {
String value = xpp.getAttributeValue(null, property.name);
if(value != null) {
data.setValue(property, value);
}
return null;
}
}
}
// =============================================================== FORMAT3

@ -27,6 +27,8 @@ import com.todoroo.andlib.data.Table;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.utility.DateUtilities;
import org.tasks.backup.XmlReader;
import org.tasks.backup.XmlWriter;
import org.tasks.time.DateTime;
import java.util.HashMap;
@ -271,6 +273,53 @@ public class Task extends AbstractModel implements Parcelable {
super(cursor);
}
@Ignore
public Task(XmlReader reader) {
calendarUri = reader.readString("calendarUri");
completed = reader.readLong("completed");
created = reader.readLong("created");
deleted = reader.readLong("deleted");
dueDate = reader.readLong("dueDate");
elapsedSeconds = reader.readInteger("elapsedSeconds");
estimatedSeconds = reader.readInteger("estimatedSeconds");
hideUntil = reader.readLong("hideUntil");
importance = reader.readInteger("importance");
modified = reader.readLong("modified");
notes = reader.readString("notes");
recurrence = reader.readString("recurrence");
notificationFlags = reader.readInteger("notificationFlags");
lastNotified = reader.readLong("lastNotified");
notifications = reader.readLong("notifications");
snoozeTime = reader.readLong("snoozeTime");
repeatUntil = reader.readLong("repeatUntil");
timerStart = reader.readLong("timerStart");
title = reader.readString("title");
remoteId = reader.readString("remoteId");
}
public void writeToXml(XmlWriter writer) {
writer.writeString("calendarUri", calendarUri);
writer.writeLong("completed", completed);
writer.writeLong("created", created);
writer.writeLong("deleted", deleted);
writer.writeLong("dueDate", dueDate);
writer.writeInteger("elapsedSeconds", elapsedSeconds);
writer.writeInteger("estimatedSeconds", estimatedSeconds);
writer.writeLong("hideUntil", hideUntil);
writer.writeInteger("importance", importance);
writer.writeLong("modified", modified);
writer.writeString("notes", notes);
writer.writeString("recurrence", recurrence);
writer.writeInteger("notificationFlags", notificationFlags);
writer.writeLong("lastNotified", lastNotified);
writer.writeLong("notifications", notifications);
writer.writeLong("snoozeTime", snoozeTime);
writer.writeLong("repeatUntil", repeatUntil);
writer.writeLong("timerStart", timerStart);
writer.writeString("title", title);
writer.writeString("remoteId", remoteId);
}
@Ignore
public Task(Parcel parcel) {
calendarUri = parcel.readString();

Loading…
Cancel
Save