Add/update in one transaction

Refs #41
pull/50/head
M66B 6 years ago
parent 1a9e54b3c4
commit 6af53cf1a1

@ -1265,6 +1265,7 @@ public class ServiceSynchronize extends LifecycleService {
boolean outbox = EntityFolder.OUTBOX.equals(dfolder.type); boolean outbox = EntityFolder.OUTBOX.equals(dfolder.type);
Log.i(Helper.TAG, folder.name + " found as id=" + dup.id + Log.i(Helper.TAG, folder.name + " found as id=" + dup.id +
" folder=" + dfolder.type + ":" + dup.folder + "/" + folder.type + ":" + folder.id); " folder=" + dfolder.type + ":" + dup.folder + "/" + folder.type + ":" + folder.id);
if (dup.folder.equals(folder.id) || outbox) { if (dup.folder.equals(folder.id) || outbox) {
Log.i(Helper.TAG, folder.name + " found as id=" + dup.id + " uid=" + dup.uid + " msgid=" + msgid); Log.i(Helper.TAG, folder.name + " found as id=" + dup.id + " uid=" + dup.uid + " msgid=" + msgid);
dup.folder = folder.id; dup.folder = folder.id;
@ -1285,76 +1286,66 @@ public class ServiceSynchronize extends LifecycleService {
db.message().updateMessage(message); db.message().updateMessage(message);
Log.i(Helper.TAG, folder.name + " updated id=" + message.id + " uid=" + message.uid + " seen=" + seen); Log.i(Helper.TAG, folder.name + " updated id=" + message.id + " uid=" + message.uid + " seen=" + seen);
result = -1; result = -1;
} else }
; //Log.v(Helper.TAG, folder.name + " unchanged id=" + message.id + " uid=" + message.uid);
} }
db.setTransactionSuccessful(); if (message == null) {
// Will fetch message within database transaction
if (message != null) FetchProfile fp1 = new FetchProfile();
return result; fp1.add(FetchProfile.Item.ENVELOPE);
fp1.add(FetchProfile.Item.CONTENT_INFO);
} finally { fp1.add(IMAPFolder.FetchProfileItem.HEADERS);
db.endTransaction(); fp1.add(IMAPFolder.FetchProfileItem.MESSAGE);
} ifolder.fetch(new Message[]{imessage}, fp1);
FetchProfile fp1 = new FetchProfile(); message = new EntityMessage();
fp1.add(FetchProfile.Item.ENVELOPE); message.account = folder.account;
fp1.add(FetchProfile.Item.CONTENT_INFO); message.folder = folder.id;
fp1.add(IMAPFolder.FetchProfileItem.HEADERS); message.uid = uid;
fp1.add(IMAPFolder.FetchProfileItem.MESSAGE);
ifolder.fetch(new Message[]{imessage}, fp1); if (!EntityFolder.ARCHIVE.equals(folder.type)) {
message.msgid = helper.getMessageID();
try { if (TextUtils.isEmpty(message.msgid))
db.beginTransaction(); Log.w(Helper.TAG, "No Message-ID id=" + message.id + " uid=" + message.uid);
}
EntityMessage message = new EntityMessage();
message.account = folder.account;
message.folder = folder.id;
message.uid = uid;
if (!EntityFolder.ARCHIVE.equals(folder.type)) { message.references = TextUtils.join(" ", helper.getReferences());
message.msgid = helper.getMessageID(); message.inreplyto = helper.getInReplyTo();
if (TextUtils.isEmpty(message.msgid)) message.thread = helper.getThreadId(uid);
Log.w(Helper.TAG, "No Message-ID id=" + message.id + " uid=" + message.uid); message.from = helper.getFrom();
} message.to = helper.getTo();
message.cc = helper.getCc();
message.bcc = helper.getBcc();
message.reply = helper.getReply();
message.subject = imessage.getSubject();
message.received = imessage.getReceivedDate().getTime();
message.sent = (imessage.getSentDate() == null ? null : imessage.getSentDate().getTime());
message.seen = seen;
message.ui_seen = seen;
message.ui_hide = false;
message.id = db.message().insertMessage(message);
message.write(this, helper.getHtml());
Log.i(Helper.TAG, folder.name + " added id=" + message.id + " uid=" + message.uid);
int sequence = 0;
for (EntityAttachment attachment : helper.getAttachments()) {
sequence++;
Log.i(Helper.TAG, "attachment seq=" + sequence +
" name=" + attachment.name + " type=" + attachment.type);
attachment.message = message.id;
attachment.sequence = sequence;
attachment.id = db.attachment().insertAttachment(attachment);
}
message.references = TextUtils.join(" ", helper.getReferences()); result = 1;
message.inreplyto = helper.getInReplyTo();
message.thread = helper.getThreadId(uid);
message.from = helper.getFrom();
message.to = helper.getTo();
message.cc = helper.getCc();
message.bcc = helper.getBcc();
message.reply = helper.getReply();
message.subject = imessage.getSubject();
message.received = imessage.getReceivedDate().getTime();
message.sent = (imessage.getSentDate() == null ? null : imessage.getSentDate().getTime());
message.seen = seen;
message.ui_seen = seen;
message.ui_hide = false;
message.id = db.message().insertMessage(message);
message.write(this, helper.getHtml());
Log.i(Helper.TAG, folder.name + " added id=" + message.id + " uid=" + message.uid);
int sequence = 0;
for (EntityAttachment attachment : helper.getAttachments()) {
sequence++;
Log.i(Helper.TAG, "attachment seq=" + sequence +
" name=" + attachment.name + " type=" + attachment.type);
attachment.message = message.id;
attachment.sequence = sequence;
attachment.id = db.attachment().insertAttachment(attachment);
} }
db.setTransactionSuccessful(); db.setTransactionSuccessful();
return result;
} finally { } finally {
db.endTransaction(); db.endTransaction();
} }
return 1;
} finally { } finally {
//Log.v(Helper.TAG, folder.name + " end sync uid=" + uid); //Log.v(Helper.TAG, folder.name + " end sync uid=" + uid);
} }

Loading…
Cancel
Save