001: /**
002: * $Id: MoveMessageTag.java,v 1.4 2005/02/08 06:27:16 sm140826 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.mail;
014:
015: import javax.servlet.jsp.*;
016: import javax.servlet.jsp.tagext.*;
017:
018: import javax.mail.*;
019: import javax.mail.internet.*;
020: import javax.mail.search.*;
021:
022: import com.sun.portal.wireless.taglibs.base.*;
023:
024: /**
025: *
026: * MoveMessageTag - Move or Copy message from one folder to another
027: *
028: * @version 1.0
029: */
030: public class MoveMessageTag extends CommandTag {
031:
032: private String src = null;
033: private String dest = null;
034: private String idx = null;
035: private boolean isCopy = false;
036:
037: public boolean execute() throws JspException {
038:
039: MailContext mc;
040: try {
041: mc = MailContext.getContext(pageContext);
042: } catch (Exception e) {
043: throw new JspException(this .getClass().getName()
044: + ".execute(): Couldn't get mail context: "
045: + e.getMessage());
046: }
047:
048: // Reset error code
049: mc.setErrorCode("");
050:
051: try {
052: mc = MailContext.getContext(pageContext);
053: } catch (Exception e) {
054: throw new JspException(this .getClass().getName()
055: + ".execute(): Couldn't get mail context: "
056: + e.getMessage());
057: }
058:
059: try {
060: Store store = mc.getMailStore();
061: Folder srcfolder = null;
062: Folder destfolder = store.getFolder(dest);
063:
064: int i = Integer.parseInt(idx);
065:
066: // If source folder is null, get the current folder
067: if (src == null)
068: srcfolder = mc.getCurrentFolder().getFolder();
069: else
070: srcfolder = store.getFolder(src);
071:
072: // Create destination folder if it doesn't exist
073: if (!destfolder.exists())
074: destfolder.create(Folder.HOLDS_MESSAGES);
075:
076: // If source is not null, we open the folder and append the message
077: // Otherwise, get the source from messages collection.
078: Message msg = null;
079:
080: if (src != null) {
081: // Source folder should be open
082: if (!srcfolder.isOpen())
083: srcfolder.open(Folder.READ_WRITE);
084:
085: // Get the message objects to copy
086: msg = srcfolder.getMessage(i);
087: } else {
088: MsgBean[] beans = mc.getMsgsCollection();
089:
090: if (i >= beans.length)
091: return false;
092:
093: msg = beans[i].getObject();
094: }
095:
096: // Append message to destination folder
097: Message[] msgs = { msg };
098: destfolder.appendMessages(msgs);
099:
100: // Delete message from source folder if this is a move
101: if (!isCopy)
102: msg.setFlag(Flags.Flag.DELETED, true);
103:
104: } catch (Exception e) {
105: release();
106: return false;
107: }
108:
109: release();
110: return true;
111: }
112:
113: /**
114: * Cleanup
115: */
116:
117: public void release() {
118: super .release();
119: src = null;
120: dest = null;
121: }
122:
123: /**
124: *
125: * Set the source folder name attribute
126: *
127: * @param set the source folder name attribute
128: */
129:
130: public void setSrc(String name) {
131: src = evalAttribute(name);
132: }
133:
134: /**
135: *
136: * Set the destination folder name attribute
137: *
138: * @param set the destinatioin folder name attribute
139: */
140:
141: public void setDest(String name) {
142: dest = evalAttribute(name);
143: }
144:
145: /**
146: *
147: * Set the operation (move/copy) attribute.
148: *
149: * @param set the operation attribute
150: */
151:
152: public void setOp(String op) {
153: if (op.equals("copy"))
154: isCopy = true;
155: }
156:
157: /**
158: * Specifies which message from the collection in which
159: * is to be moved.
160: *
161: * @param set message index attribute
162: */
163:
164: public void setIndex(String idx) {
165: this.idx = evalAttribute(idx);
166: }
167: }
|