Skip to content

Commit

Permalink
Email does not appear in the new folder after moving #78
Browse files Browse the repository at this point in the history
  • Loading branch information
zaharchenko committed Mar 26, 2019
1 parent 5dfc14c commit 4ca8db9
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 66 deletions.
3 changes: 1 addition & 2 deletions modules/core/db/init/hsql/10.create-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ create table IMAP_MESSAGE_SYNC (
FLAGS longvarchar,
FOLDER_ID varchar(36) not null,
STATUS varchar(50) not null,
NEW_FOLDER_ID varchar(36),
NEW_FOLDER_NAME varchar(255),
OLD_FOLDER_ID varchar(36),
--
primary key (ID)
)^
Expand Down
4 changes: 2 additions & 2 deletions modules/core/db/init/hsql/20.create-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ create index IDX_IMAP_MESSAGE_ATTACHMENT_IMAP_MESSAGE on IMAP_MESSAGE_ATTACHMENT
-- begin IMAP_MESSAGE_SYNC
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_MESSAGE foreign key (MESSAGE_ID) references IMAP_MESSAGE(ID)^
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_FOLDER foreign key (FOLDER_ID) references IMAP_FOLDER(ID)^
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_NEW_FOLDER foreign key (NEW_FOLDER_ID) references IMAP_FOLDER(ID)^
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_OLD_FOLDER foreign key (OLD_FOLDER_ID) references IMAP_FOLDER(ID)^
create unique index IDX_IMAP_MESSAGE_SYNC_UNIQ_MESSAGE_ID on IMAP_MESSAGE_SYNC (MESSAGE_ID) ^
create index IDX_IMAP_MESSAGE_SYNC_FOLDER on IMAP_MESSAGE_SYNC (FOLDER_ID)^
create index IDX_IMAP_MESSAGE_SYNC_NEW_FOLDER on IMAP_MESSAGE_SYNC (NEW_FOLDER_ID)^
create index IDX_IMAP_MESSAGE_SYNC_OLD_FOLDER on IMAP_MESSAGE_SYNC (OLD_FOLDER_ID)^
-- end IMAP_MESSAGE_SYNC
3 changes: 1 addition & 2 deletions modules/core/db/init/postgres/10.create-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ create table IMAP_MESSAGE_SYNC (
FLAGS text,
FOLDER_ID uuid not null,
STATUS varchar(50) not null,
NEW_FOLDER_ID uuid,
NEW_FOLDER_NAME varchar(255),
OLD_FOLDER_ID uuid,
--
primary key (ID)
)^
Expand Down
4 changes: 2 additions & 2 deletions modules/core/db/init/postgres/20.create-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ create index IDX_IMAP_MESSAGE_ATTACHMENT_IMAP_MESSAGE on IMAP_MESSAGE_ATTACHMENT
-- begin IMAP_MESSAGE_SYNC
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_MESSAGE foreign key (MESSAGE_ID) references IMAP_MESSAGE(ID)^
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_FOLDER foreign key (FOLDER_ID) references IMAP_FOLDER(ID)^
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_NEW_FOLDER foreign key (NEW_FOLDER_ID) references IMAP_FOLDER(ID)^
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_OLD_FOLDER foreign key (OLD_FOLDER_ID) references IMAP_FOLDER(ID)^
create unique index IDX_IMAP_MESSAGE_SYNC_UK_MESSAGE_ID on IMAP_MESSAGE_SYNC (MESSAGE_ID) ^
create index IDX_IMAP_MESSAGE_SYNC_FOLDER on IMAP_MESSAGE_SYNC (FOLDER_ID)^
create index IDX_IMAP_MESSAGE_SYNC_NEW_FOLDER on IMAP_MESSAGE_SYNC (NEW_FOLDER_ID)^
create index IDX_IMAP_MESSAGE_SYNC_OLD_FOLDER on IMAP_MESSAGE_SYNC (OLD_FOLDER_ID)^
-- end IMAP_MESSAGE_SYNC
3 changes: 3 additions & 0 deletions modules/core/db/update/hsql/19/190326-2-updateMessageSync.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table IMAP_MESSAGE_SYNC alter column NEW_FOLDER_ID rename to OLD_FOLDER_ID ^
drop index IDX_IMAP_MESSAGE_SYNC_NEW_FOLDER ;
alter table IMAP_MESSAGE_SYNC drop constraint FK_IMAP_MESSAGE_SYNC_NEW_FOLDER ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_OLD_FOLDER foreign key (OLD_FOLDER_ID) references IMAP_FOLDER(ID);
create index IDX_IMAP_MESSAGE_SYNC_OLD_FOLDER on IMAP_MESSAGE_SYNC (OLD_FOLDER_ID);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table IMAP_MESSAGE_SYNC drop column NEW_FOLDER_NAME;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
insert into SYS_SCHEDULED_TASK
(ID, CREATE_TS, CREATED_BY, UPDATE_TS, UPDATED_BY, DELETE_TS, DELETED_BY, DEFINED_BY, BEAN_NAME, METHOD_NAME, CLASS_NAME, SCRIPT_NAME, USER_NAME, IS_SINGLETON, IS_ACTIVE, PERIOD, TIMEOUT, START_DATE, CRON, SCHEDULING_TYPE, TIME_FRAME, START_DELAY, PERMITTED_SERVERS, LOG_START, LOG_FINISH, LAST_START_TIME, LAST_START_SERVER, METHOD_PARAMS, DESCRIPTION)
values ('1082fe3b-3b75-d2f2-d858-dbae487c4549', '2019-03-22 10:26:18', 'admin', '2019-03-22 10:26:18', null, null, null, 'B', 'imap_ImapScheduler', 'syncImap', null, null, null, null, null, 30, null, null, null, 'P', null, null, null, true, true, null, null, '<?xml version="1.0" encoding="UTF-8"?>
<params/>
', null);
--insert into SYS_SCHEDULED_TASK
--(ID, CREATE_TS, CREATED_BY, UPDATE_TS, UPDATED_BY, DELETE_TS, DELETED_BY, DEFINED_BY, BEAN_NAME, METHOD_NAME, CLASS_NAME, SCRIPT_NAME, USER_NAME, IS_SINGLETON, IS_ACTIVE, PERIOD, TIMEOUT, START_DATE, CRON, SCHEDULING_TYPE, TIME_FRAME, START_DELAY, PERMITTED_SERVERS, LOG_START, LOG_FINISH, LAST_START_TIME, LAST_START_SERVER, METHOD_PARAMS, DESCRIPTION)
--values ('1082fe3b-3b75-d2f2-d858-dbae487c4549', '2019-03-22 10:26:18', 'admin', '2019-03-22 10:26:18', null, null, null, 'B', 'imap_ImapScheduler', 'syncImap', null, null, null, null, null, 30, null, null, null, 'P', null, null, null, true, true, null, null, '<?xml version="1.0" encoding="UTF-8"?>
--
--<params/>
--', null);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table IMAP_MESSAGE_SYNC rename column NEW_FOLDER_ID to OLD_FOLDER_ID ^
drop index IDX_IMAP_MESSAGE_SYNC_NEW_FOLDER ;
alter table IMAP_MESSAGE_SYNC drop constraint FK_IMAP_MESSAGE_SYNC_NEW_FOLDER ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table IMAP_MESSAGE_SYNC add constraint FK_IMAP_MESSAGE_SYNC_OLD_FOLDER foreign key (OLD_FOLDER_ID) references IMAP_FOLDER(ID);
create index IDX_IMAP_MESSAGE_SYNC_OLD_FOLDER on IMAP_MESSAGE_SYNC (OLD_FOLDER_ID);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table IMAP_MESSAGE_SYNC drop column NEW_FOLDER_NAME;
28 changes: 13 additions & 15 deletions modules/core/src/com/haulmont/addon/imap/core/ImapOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,21 @@ public List<IMAPMessage> searchMessageIds(IMAPFolder folder, SearchTerm searchTe
return fetch(folder, fetchProfile, messages);
}

public ImapMessage map(IMAPMessage msg, ImapFolder cubaFolder) throws MessagingException {
public ImapMessage map(ImapMessage cubaMessage, IMAPMessage msg, ImapFolder cubaFolder) throws MessagingException {
long uid = ((IMAPFolder) msg.getFolder()).getUID(msg);
Flags flags = new Flags(msg.getFlags());

ImapMessage entity = metadata.create(ImapMessage.class);
entity.setMsgUid(uid);
entity.setFolder(cubaFolder);
entity.setUpdateTs(timeSource.currentTimestamp());
entity.setImapFlags(flags);
entity.setReceivedDate(msg.getReceivedDate());
entity.setCaption(getSubject(msg));
entity.setMessageId(msg.getHeader(ImapOperations.MESSAGE_ID_HEADER, null));
entity.setReferenceId(getRefId(msg));
entity.setThreadId(getThreadId(msg, cubaFolder.getMailBox()));
entity.setMsgNum(msg.getMessageNumber());

return entity;
cubaMessage.setMsgUid(uid);
cubaMessage.setFolder(cubaFolder);
cubaMessage.setUpdateTs(timeSource.currentTimestamp());
cubaMessage.setImapFlags(flags);
cubaMessage.setReceivedDate(msg.getReceivedDate());
cubaMessage.setCaption(getSubject(msg));
cubaMessage.setMessageId(msg.getHeader(ImapOperations.MESSAGE_ID_HEADER, null));
cubaMessage.setReferenceId(getRefId(msg));
cubaMessage.setThreadId(getThreadId(msg, cubaFolder.getMailBox()));
cubaMessage.setMsgNum(msg.getMessageNumber());

return cubaMessage;
}

private String getRefId(IMAPMessage message) throws MessagingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void updateSyncStatus(ImapMessage message,
ImapSyncStatus syncStatus,
ImapSyncStatus oldStatus,
Flags flags,
ImapFolder newFolder) {
ImapFolder oldFolder) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();

Expand All @@ -153,9 +153,8 @@ public void updateSyncStatus(ImapMessage message,
if (flags != null) {
messageSync.setImapFlags(flags);
}
if (newFolder != null) {
messageSync.setNewFolder(newFolder);
messageSync.setNewFolderName(newFolder.getName());
if (oldFolder != null) {
messageSync.setOldFolder(oldFolder);
}
em.persist(messageSync);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,16 @@ private void handleMissedMessages(ImapMailBox mailBox, IMAPStore store, List<Ima
);
if (!imapMessages.isEmpty()) {
foundMessages.add(cubaMessage);
ImapFolder oldFolder = cubaMessage.getFolder();
updateCubaMessage(cubaMessage, imapMessages.get(0), cubaFolder);
if (cubaFolder.getName().equals(mailBox.getTrashFolderName())) {
messageSyncDao.updateSyncStatus(cubaMessage,
ImapSyncStatus.REMOVED, ImapSyncStatus.MISSED,
null, null);
} else {
messageSyncDao.updateSyncStatus(cubaMessage,
ImapSyncStatus.MOVED, ImapSyncStatus.MISSED,
null, cubaFolder);
null, oldFolder);
}
}
}
Expand All @@ -255,6 +257,16 @@ private void handleMissedMessages(ImapMailBox mailBox, IMAPStore store, List<Ima
}
}

private void updateCubaMessage(ImapMessage cubaMessage, IMAPMessage imapMessage, ImapFolder cubaFolder) throws MessagingException {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
cubaMessage = em.find(ImapMessage.class, cubaMessage.getId());
imapOperations.map(cubaMessage, imapMessage, cubaFolder);
em.merge(cubaMessage);
tx.commit();
}
}

private void setAnswersFlag(ImapMailBox mailBox, IMAPStore store, List<ImapMessage> checkAnswers) throws MessagingException {
Map<String, List<ImapMessage>> folderWithMessagesToAnswer = new HashMap<>();
for (ImapMessage cubaMessage : checkAnswers) {
Expand Down Expand Up @@ -329,7 +341,8 @@ ImapMessage insertNewMessage(IMAPMessage msg,
.size();
if (sameUIDs == 0) {
log.trace("Save new message {}", msg);
ImapMessage entity = imapOperations.map(msg, cubaFolder);
ImapMessage entity = metadata.create(ImapMessage.class);
imapOperations.map(entity, msg, cubaFolder);
em.persist(entity);

ImapMessageSync messageSync = metadata.create(ImapMessageSync.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.haulmont.addon.imap.api.ImapFlag;
import com.haulmont.addon.imap.config.ImapConfig;
import com.haulmont.addon.imap.core.ImapOperations;
import com.haulmont.addon.imap.dao.ImapMessageSyncDao;
import com.haulmont.addon.imap.entity.*;
import com.haulmont.addon.imap.events.*;
Expand Down Expand Up @@ -47,6 +48,9 @@ public class ImapStandardEventsGenerator extends ImapEventsBatchedGenerator {
@Inject
private ImapConfig imapConfig;

@Inject
private ImapOperations imapOperations;

@Override
public void init(ImapMailBox imapMailBox) {
batchSize = imapConfig.getEventsBatchSize();
Expand Down Expand Up @@ -208,11 +212,8 @@ public Collection<? extends BaseImapEvent> generateForMissedMessages(ImapFolder
}
for (ImapMessageSync imapMessageSync : moved) {
ImapMessage message = imapMessageSync.getMessage();
ImapFolder oldFolder = message.getFolder();
ImapFolder newFolder = imapMessageSync.getNewFolder();
message.setFolder(newFolder);
message = em.merge(message);
entityFetcher.fetch(message, "imap-msg-full");
ImapFolder oldFolder = imapMessageSync.getOldFolder();

imapMessageSync.setStatus(ImapSyncStatus.REMAIN);
em.merge(imapMessageSync);
missedMessageEvents.add(new EmailMovedImapEvent(message, oldFolder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void setImapFlags(Flags flags) {
try {
if (!internalFlags.equals(this.internalFlags)) {
log.debug("Convert imap flags {} to raw string", internalFlags);
this.flags = OBJECT_MAPPER.writeValueAsString(internalFlags);
setFlags(OBJECT_MAPPER.writeValueAsString(internalFlags));
this.internalFlags = internalFlags;
}
} catch (JsonProcessingException e) {
Expand All @@ -118,7 +118,8 @@ public Flags getImapFlags() {
if (flags == null) {
return new Flags();
}
this.internalFlags = OBJECT_MAPPER.readValue(this.flags, new TypeReference<List<ImapFlag>>() {});
this.internalFlags = OBJECT_MAPPER.readValue(this.flags, new TypeReference<List<ImapFlag>>() {
});
} catch (IOException e) {
throw new RuntimeException("Can't parse flags from string " + flags, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,19 @@ public class ImapMessageSync extends BaseUuidEntity implements Versioned, Updata
private static final long serialVersionUID = 4840104340467502496L;

private final static Logger log = LoggerFactory.getLogger(ImapMessage.class);

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

@NotNull
@OnDeleteInverse(DeletePolicy.CASCADE)
@OneToOne(fetch = FetchType.LAZY, optional = false)
@OneToOne(fetch = FetchType.LAZY, mappedBy = "", optional = false)
@JoinColumn(name = "MESSAGE_ID", unique = true)
private ImapMessage message;

@Lob
@Column(name = "FLAGS")
private String flags;

@Transient
private List<ImapFlag> internalFlags = Collections.emptyList();

@OnDeleteInverse(DeletePolicy.CASCADE)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "FOLDER_ID")
Expand All @@ -56,11 +54,8 @@ public class ImapMessageSync extends BaseUuidEntity implements Versioned, Updata

@OnDeleteInverse(DeletePolicy.CASCADE)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "NEW_FOLDER_ID")
private ImapFolder newFolder;

@Column(name = "NEW_FOLDER_NAME")
private String newFolderName;
@JoinColumn(name = "OLD_FOLDER_ID")
private ImapFolder oldFolder;

@Column(name = "UPDATE_TS")
protected Date updateTs;
Expand All @@ -78,6 +73,9 @@ public class ImapMessageSync extends BaseUuidEntity implements Versioned, Updata
@Column(name = "VERSION", nullable = false)
protected Integer version;

@Transient
private List<ImapFlag> internalFlags = Collections.emptyList();

public String getFlags() {
return flags;
}
Expand Down Expand Up @@ -200,19 +198,11 @@ public void setFolder(ImapFolder folder) {
this.folder = folder;
}

public String getNewFolderName() {
return newFolderName;
}

public void setNewFolderName(String newFolderName) {
this.newFolderName = newFolderName;
}

public ImapFolder getNewFolder() {
return newFolder;
public ImapFolder getOldFolder() {
return oldFolder;
}

public void setNewFolder(ImapFolder newFolder) {
this.newFolder = newFolder;
public void setOldFolder(ImapFolder oldFolder) {
this.oldFolder = oldFolder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ ImapMessageSync.status = Status
ImapMessageSync = IMAP Message Sync
ImapMessageSync.flags = Flags
ImapMessageSync.folder = Folder
ImapMessageSync.newFolder = New Folder
ImapMessageSync.newFolderName = New Folder Name
ImapMessageSync.oldFolder = Old Folder
ImapMessageSync.updateTs = Update Ts
ImapMessageSync.updatedBy = Updated By
ImapMessageSync.createTs = Create Ts
Expand Down
2 changes: 1 addition & 1 deletion modules/global/src/com/haulmont/addon/imap/views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
name="imap-msg-sync-with-message">
<property name="message"
view="imap-msg-full"/>
<property name="newFolder" view="imap-folder-full"/>
<property name="oldFolder" view="imap-folder-full"/>
</view>
<view entity="imap$MailBox" name="imapMailBox-browse" extends="_local" systemProperties="true"/>
</views>

0 comments on commit 4ca8db9

Please sign in to comment.