// instance.
Pair<GoogleWavelet, ImmutableList<GoogleDocument>> snapshot =
// We convertGooglewaveToGmail here since we do a lot of checks on the
// participant list below, and it's easier if it's already converted.
convertGooglewaveToGmail(robotApi.getSnapshot(waveletName));
GoogleWavelet wavelet = snapshot.getFirst();
log.info("Snapshot fetch succeeded: version " + wavelet.getVersion() + ", "
+ wavelet.getParticipantCount() + " participants: " + wavelet.getParticipantList());
final boolean isPrivate;
switch (task.getSettings().getSharingMode()) {
case PRIVATE:
isPrivate = true;
break;
case SHARED:
isPrivate = false;
break;
case PRIVATE_UNLESS_PARTICIPANT:
isPrivate = !wavelet.getParticipantList().contains(importingUser.getAddress());
break;
default:
throw new AssertionError("Unexpected ImportSharingMode: "
+ task.getSettings().getSharingMode());
}
// Look up if already imported according to PerUserTable; if so, we have nothing to do.
boolean alreadyImportedForThisUser = new RetryHelper().run(
new RetryHelper.Body<Boolean>() {
@Override public Boolean run() throws RetryableFailure, PermanentFailure {
CheckedTransaction tx = datastore.beginTransaction();
try {
@Nullable RemoteConvWavelet entry =
perUserTable.getWavelet(tx, userId, instance, waveletName);
if (isPrivate) {
if (entry != null && entry.getPrivateLocalId() != null
&& !(task.hasExistingSlobIdToIgnore()
&& new SlobId(task.getExistingSlobIdToIgnore()).equals(
entry.getPrivateLocalId()))) {
log.info("Private import already exists, aborting: " + entry);
return true;
} else {
return false;
}
} else {
if (entry != null && entry.getSharedLocalId() != null
&& !(task.hasExistingSlobIdToIgnore()
&& new SlobId(task.getExistingSlobIdToIgnore()).equals(
entry.getSharedLocalId()))) {
log.info("Shared import already exists, aborting: " + entry);
return true;
} else {
return false;
}
}
} finally {
tx.close();
}
}
});
if (alreadyImportedForThisUser) {
throw TaskCompleted.noFollowup();
}
if (!isPrivate) {
@Nullable SlobId existingSharedImport =
sharedImportTable.lookupWithoutTx(instance, waveletName);
if (existingSharedImport != null
&& !(task.hasExistingSlobIdToIgnore()
&& new SlobId(task.getExistingSlobIdToIgnore()).equals(existingSharedImport))) {
log.info("Found existing shared import " + existingSharedImport + ", re-using");
ensureParticipant(existingSharedImport);
addToPerUserTableWithoutTx(existingSharedImport, isPrivate);
throw TaskCompleted.noFollowup();
}
}
Map<String, AttachmentId> attachmentMapping = getAttachmentsAndMapping(snapshot);
List<WaveletOperation> participantFixup = Lists.newArrayList();
if (isPrivate) {
for (String participant : ImmutableList.copyOf(wavelet.getParticipantList())) {
participantFixup.add(
HistorySynthesizer.newRemoveParticipant(importingUser.getAddress(),
wavelet.getLastModifiedTimeMillis(), participant));
}
participantFixup.add(
HistorySynthesizer.newAddParticipant(importingUser.getAddress(),
wavelet.getLastModifiedTimeMillis(), importingUser.getAddress()));
} else {
if (!wavelet.getParticipantList().contains(importingUser.getAddress())) {
log.info(
importingUser + " is not a participant, adding: " + wavelet.getParticipantList());
participantFixup.add(
HistorySynthesizer.newAddParticipant(importingUser.getAddress(),
wavelet.getLastModifiedTimeMillis(), importingUser.getAddress()));
}
}
log.info("participantFixup=" + participantFixup);
boolean preserveHistory = !task.getSettings().getSynthesizeHistory();
log.info("preserveHistory=" + preserveHistory);