messagemanager.java
来自「moblie syncml mail javame」· Java 代码 · 共 1,265 行 · 第 1/4 页
JAVA
1,265 行
*
*@param msg the the message to be put in the queue
*@param mf the flags to be set
*/
public void updateMessageFlags(Message msg, MessageFlags mf) {
if(msg.getParent().getName().equals(Store.INBOX)) {
FlagQueue.put(msg.getKey(), mf);
}
}
/**
* Remove message flags from the queue
* with this implementation the flags are managed only for the Inbox folder
*
*@param msg the the message to be put in the queue
*@param mf the flags to be set
*/
public void removeFromFlagQueue(Message msg) {
if(msg.getParent().getName().equals(Store.INBOX)) {
FlagQueue.remove(msg.getKey());
}
}
////////////////////////////////////////////////////////////////////////////
// Methods invoked by the client to apply changes to messages. The changes
// are originated on the server side.
////////////////////////////////////////////////////////////////////////////
/**
* Creates a new Message from a SyncML byte stream. The message is also
* saved in its folder.
*
* @param parentName parent folder name
* @param guid server side message id
* @param emailData byte stream of the SyncML email
*
* @return a new Message object with the proper key defined or
* null if either the folder is invalid or the byte stream cannot be
* parsed properly.
*
* @throws XmlException if the byte stream cannot be parsed
* @throws MailException if the message cannot be saved in the folder
*/
public Message createNewMessage(String parentName, String guid,
byte[] emailData)
throws XmlException, MailException {
// Dump memory stats
Log.stats("Begin of MessageManager.createNewMessage");
String folderName = folderFromParent(parentName);
if(folderName == null) {
Log.error("[handleEmailItem]Invalid parent from server: "
+parentName);
// TODO FIXME throw an exception?
return null;
}
String luid;
Message msg = parseMessage(emailData);
if (msg != null) {
String msgid = msg.getMessageId();
Folder f = msgStore.getFolder(folderName);
Log.info("[addMessage]Adding message " + msgid +
" to folder " + f.getName());
f.appendMessage(msg);
// We set the luid to be more information than just the recordId
// Our LUID is "folder"/"recordid"
msg.setKey(makeLuid(msg));
}
// Dump memory stats
Log.stats("End of MessageManager.createNewMessage");
return msg;
}
/**
* Creates a new Message from a SyncML byte stream. The message is NOT
* saved in any folder. (@see createNewMessage(String, String, byte[])).
* The message must already be present in the client because the method
* expects an existing valid luid. The newly created message will have such
* a luid.
* Note that after this method is invoked we have two messages with the same
* LUID but different contents. It is up to the client to preserve the
* system integrity.
* In the J2ME email client this method is used in case of "get more" to
* retireve a full message which is not saved in the RMS but just fetched to
* be displayed.
*
* @param luid client side message id
* @param emailData byte stream of the SyncML email
*
* @return a new Message object with the proper key defined or
* null if the byte stream cannot be parsed properly.
*
* @throws XmlException if the byte stream cannot be parsed
*/
public Message updateMessageContent(String luid, byte[] emailData)
throws XmlException {
Message msg = parseMessage(emailData);
if (msg != null) {
// We set the luid to be more information than just the recordId
// Our LUID is "folder"/"recordid"
msg.setKey(luid);
}
return msg;
}
/**
* Updates an existing message by redefining the sent date and/or the
* received date. The message is also written into the folder that gets
* updated immediately.
*
* @param sentDate sent date. If null the message sent date is left
* unchanged
*
* @param receivedDate received date. If null the message received date is
* left unchanged
*
* @throws MailException if the folder update fails
*/
public void updateMessageDates(Message msg, Date sentDate, Date receivedDate)
throws MailException {
String luid = msg.getKey();
String parent = folderFromKey(luid);
Folder f = msgStore.getFolder(parent);
String recordId = msgIdFromKey(luid);
if (sentDate != null) {
msg.setSentDate(sentDate);
}
if (receivedDate != null) {
msg.setReceivedDate(receivedDate);
}
f.updateMessage(msg);
}
/**
* Updates an existing message by redefining its flags. The message is also
* written into the folder that gets updated immediately. The method accepts
* the SyncML complete description of a message, but the content is ignored
* and only the flags are decoded to update the flags.
*
* @param parentName parent folder name
* @param luid message client id
* @param emailData SyncML byte stream describing the message
*
* @throws MailException if the folder update fails
* @throws XmlException if the byte stream is not SyncML complaint
*/
public MessageFlags updateMessageFlags(String parentName, String luid,
byte[] emailData)
throws XmlException, MailException {
String folderName = folderFromParent(parentName);
if(folderName == null) {
Log.error("[handleEmailItem]Invalid parent from server: "
+parentName);
// TODO FIXME throw an exception?
return null;
}
MessageFlags flags = parseFlags(emailData);
// Update the message in the store
try {
Folder f = msgStore.getFolder(parentName);
// Flag update only.
Log.debug("[updateMessageFlags]updating flags for: " + luid);
String recordId = msgIdFromKey(luid);
Message saved = f.getMessage(recordId);
// We reset the luid to be more information from what the RMSStore
// can define. Our LUID is "folder"/"recordid"
if (saved!=null) {
//if the message has been deleted this would generate NPE
saved.setKey(luid);
saved.setFlags(flags);
f.updateMessage(saved);
}
return flags;
} catch (MailException me) {
Log.error("[updateMessageFlags]Error saving message: " + luid
+ " " + me.toString() );
me.printStackTrace();
throw me;
}
}
/**
* Delete an existing message which is actually removed from the folder it
* belongs to.
* When we try to delete a message in the Outbox and such an operation
* fails, we check because the failure may be due to the fact that the
* server is notifying a succesfull sent. On a succesfull sent the server
* moves messages from Outbox to Sent. On the client we move from Outbox to
* Sent when the server accepts the message to be sent (assuming that sooner
* or later the message will be sent). Therefore we must ignore the
* subsequent move operation.
*
* @param luid message local id
* @throws MailException if the folder update fails
*/
public void deleteMessage(String luid) throws MailException {
String parent = folderFromKey(luid);
Folder f = msgStore.getFolder(parent);
String recordId = msgIdFromKey(luid);
try {
f.deleteMessage(recordId);
} catch (MailException exc) {
// If the message to delete was in the outbox
// it just means it has been sent. We move these
// messages from outbox to sent as soon as the server inform us that
// it accepted the item to send, therefore we do not have this email
// in the outbox any longer. We can ignore the exception in such a
// case.
// TODO: would it be even safer to check that the message is now in
// the sent folder?
if (!(exc.getCode() == MailException.MESSAGE_DELETE_ERROR &&
f.getName().equals(Store.OUTBOX))) {
throw exc;
}
}
}
/**
* This method should be called on a message that has been accepted by the
* server as to be sent. The method moves the message from local Outbox to
* local Sent and updates the folders.
*
* @param luid local message id to be moved from outbox to sent
*
* @throws MailException if one of the operations on the folder fails
* (delete from Outbox or add to the Sent)
*/
public void messageEffectivelySent(String luid) throws MailException {
// Move the message to the sent folder
Folder sent = msgStore.getFolder(Store.SENT);
Folder outbox = msgStore.getFolder(Store.OUTBOX);
Message msg = getMessage(outbox, luid);
msg.getFlags().setFlag(MessageFlags.TX_SENDING, false);
msg.getFlags().setFlag(MessageFlags.TX_SENT, true);
msg.reloadMessage();
outbox.deleteMessage(msg);
// Update the luid and the message parent folder
msg.setParent(sent);
msg.setKey(makeLuid(msg));
// And finally append the message into the sent folder
sent.appendMessage(msg);
}
/**
* Get the basic informations for the messages contained in
* a given folder.
* This method is not just a wrapper for the Folder.getMsgHeaders as it sets
* the key of all the Messages returned. In the client this method should
* always be used instead of the Folder counterpart.
*
* @param folderName is the folder name
* @return an array of Message held by this folder
* @throws MailException if errors occur retrieving the folder content
*/
public Message[] getMessagesInFolder(String folderName) throws MailException {
// Dump memory stats
//Log.stats("Beginning of MessageManager.getMessagesInFolder");
Folder folder = msgStore.getFolder(folderName);
/*Message[] msgs = folder.getMsgHeaders();
int count = msgs.length;
for(int i=0; i<count; i++) {
Message msg = msgs[i];
msg.setKey(makeLuid(msg));
}
// Dump memory stats
Log.stats("End of MessageManager.getMessagesInFolder");
return msgs;*/
return getMessagesInFolder(folder);
}
/**
* Get the basic informations for the messages contained in
* a given folder.
* This method is not just a wrapper for the Folder.getMsgHeaders as it sets
* the key of all the Messages returned. In the client this method should
* always be used instead of the Folder counterpart.
*
* @param folder is the folder
* @return an array of Message held by this folder
* @throws MailException if errors occur retrieving the folder content
*/
public Message[] getMessagesInFolder(Folder folder) throws MailException {
// Dump memory stats
Log.stats("Beginning of MessageManager.getMessagesInFolder");
Message[] msgs = folder.getMsgHeaders();
int count = msgs.length;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?