* null: item is silently ignored (with status 200 to the server)
* @throws DataAccessException
*/
public Record setRecord(Record record, boolean modify) throws DataAccessException {
Folder folder = null;
char type = ' '; // [I]nbox [O]utbox [D]raft [T]rash [S]ent
String locURI = record.getKey();
String mail = record.getUid();
try {
// Get mail message store for the default inistance
Store store = Session.getDefaultInstance().getStore();
if (locURI.startsWith("I/")) {
Folder[] folders = store.list(Folder.INBOX);
printFolderInfo(folders);
folder = folders[0];
type = 'I';
}
else if (locURI.startsWith("O/")) {
Folder[] folders = store.list(Folder.OUTBOX);
printFolderInfo(folders);
folder = folders[0];
type = 'O';
}
else if (locURI.startsWith("D/")) {
Folder[] folders = store.list(Folder.DRAFT);
printFolderInfo(folders);
folder = folders[0];
type ='D';
}
else if (locURI.startsWith("S/")) {
Folder[] folders = store.list(Folder.SENT);
printFolderInfo(folders);
folder = folders[0];
type = 'S';
}
else if (locURI.startsWith("T/")) {
StaticDataHelper.log("[INFO] Trash message: discard it.");
type ='T';
// invalidate the key: the record is discarded
record.setKey(null);
return record;
}
else if (locURI.startsWith("ROOT/")){
StaticDataHelper.log("[INFO] Folder info from server: not managed at the moment.");
// invalidate the key: the record is discarded
record.setKey(null);
return record;
}
else {
StaticDataHelper.log("[ERROR] Invalid folder Id from server: " + locURI);
throw new DataAccessException("Invalid folder Id from server");
}
}
catch (Exception e) {
StaticDataHelper.log("[EMAIL]Exception in MailDataStore.setRecord(:Record, :boolean): " + e.toString());
throw new DataAccessException(e);
}
// What if the server id contains another '/' ?
//String[] messageID = new StringUtil().split(locURI, "/");
String messageID = locURI.substring(2);
MessageIDStore messageIDStore = new MessageIDStore();
if (! (messageIDStore.checkIsDataThere(locURI) ||
checkIsDataThere(messageID, Constants.ITEMSKEY)) ) {
Message msg = mailParser.parseMessage(mail, locURI);
// Set the message flags depending on the message type
switch(type) {
case 'I': // Inbox
/*
* Sets the message status as an inbound (received) message:
* without using this method, the incoming message is shown
* in the Outbox instead of the Inbox
*/
msg.setInbound(true);
/*
* Indicates that the message has been received successfully
*/
msg.setStatus(Message.Status.RX_RECEIVED, 1);
/*
* This lets the user to answer to the message
*/
msg.setFlag(Message.Flag.REPLY_ALLOWED, true);
break;
case 'S': // Sent
msg.setStatus(Message.Status.TX_SENT, 1);
break;
case 'D': // Draft
msg.setFlag(Message.Flag.SAVED_THEN_ORPHANED, true);
break;
case 'T': // Trash
// FIXME: is it possible to store a message in the trash?
// Shall we just silently discard this?
break;
}
// Save the message to the correct folder
folder.appendMessage(msg);
// XXX: why this conversion?
Integer msgId = new Integer(msg.getMessageId());
String key = new String(type + "/" + msgId.toString());
// Set record key
record.setKey(key);