001: /**
002: * $Id: ViewBean.java,v 1.10 2005/07/15 00:19:52 rakeshn 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 java.util.*;
016: import java.io.*;
017:
018: import javax.mail.search.*;
019: import javax.mail.*;
020:
021: import javax.servlet.http.*;
022:
023: //import com.sun.portal.wireless.taglibs.base.Util;
024: import com.sun.ssoadapter.*;
025: import com.sun.ssoadapter.config.*;
026:
027: /**
028: * ViewBean
029: *
030: * @version 1.0
031: * @see
032: */
033: public class ViewBean {
034: private String name = null;
035: private int index = -1;
036: private String[] ruleNames = null;
037: private boolean applyAll = false;
038:
039: private SearchTerm searchRules = null;
040:
041: protected ViewBean(String name, int idx) {
042: this .name = name;
043: index = idx;
044: }
045:
046: /*
047: * Constructor
048: */
049: ViewBean(Configuration view, ConfigurationFactory rules,
050: HttpServletRequest request, int idx) throws Exception {
051: name = view.getConfigurationName();
052: index = idx;
053:
054: Boolean applyAll = new Boolean(view.getProperty("applyAll"));
055: this .applyAll = applyAll.booleanValue();
056:
057: ruleNames = view.getPropertyArray("ruleName");
058:
059: searchRules = parseRules(rules, request);
060: }
061:
062: /*
063: * Constructor
064: */
065: ViewBean(SearchTerm term) throws Exception {
066: searchRules = term;
067: }
068:
069: /*
070: * Default Constructor
071: */
072: public ViewBean() {
073: }
074:
075: /**
076: * Get name
077: *
078: * @return name String
079: */
080: public String getName() {
081: return name;
082: }
083:
084: public int getIndex() {
085: return index;
086: }
087:
088: public SearchTerm getRules() {
089: return searchRules;
090: }
091:
092: private SearchTerm parseRules(ConfigurationFactory allRules,
093: HttpServletRequest request) throws Exception {
094: Vector terms = new Vector();
095: Configuration conf = null;
096:
097: for (int i = 0; i < ruleNames.length; i++) {
098: String ruleName = ruleNames[i];
099:
100: conf = allRules.readConfiguration(ruleName, false,
101: new SSOAdapterSession(request));
102: String target = conf.getProperty("target");
103: String operator = conf.getProperty("op");
104: String string = conf.getProperty("ruleValue");
105:
106: terms.add(MakeRule(target.toLowerCase(), operator, string));
107: }
108:
109: SearchTerm finalTerm = (SearchTerm) terms.firstElement();
110:
111: if (terms.size() > 1) {
112: SearchTerm[] t = new SearchTerm[terms.size()];
113: terms.toArray(t);
114:
115: if (applyAll) {
116: finalTerm = new AndTerm(t);
117: } else {
118: finalTerm = new OrTerm(t);
119: }
120: }
121:
122: Flags flags = new Flags(Flags.Flag.DELETED);
123: return new AndTerm(finalTerm, new FlagTerm(flags, false));
124: }
125:
126: private SearchTerm MakeRule(String target, String operator,
127: String value) {
128:
129: SearchTerm term = null;
130:
131: if (target.equals("subject")) {
132: term = new SubjectTerm(value);
133: } else if (target.equals("sender")) {
134: term = new HeaderTerm("From", value);
135: } else if (target.equals("date")) {
136: term = new HeaderTerm("Date", value);
137: } else if (target.equals("priority")) {
138: term = new HeaderTerm("Priority", value);
139: } else if (target.equals("to")) {
140: term = new HeaderTerm("To", value);
141: } else if (target.equals("cc")) {
142: term = new HeaderTerm("Cc", value);
143: } else if (target.equals("flag")) {
144: boolean flag = false;
145:
146: if (operator.equals("is")) {
147: flag = true;
148: }
149:
150: // give flags a default value just in case
151: Flags flags = new Flags(Flags.Flag.DELETED);
152:
153: if (value.equals("read")) {
154: flags = new Flags(Flags.Flag.SEEN);
155: } else if (value.equals("recent")) {
156: flags = new Flags(Flags.Flag.RECENT);
157: }
158:
159: return new FlagTerm(flags, flag);
160: }
161:
162: if (operator.equals("contains")) {
163: return term;
164: } else {
165: return new NotTerm(term);
166: }
167: }
168:
169: public String toString() {
170: StringBuffer strbuf = new StringBuffer();
171:
172: strbuf.append("ViewBean[name=\"");
173: strbuf.append(name);
174: strbuf.append("\", index=\"" + index);
175: strbuf.append("\", applyAll=\"");
176: strbuf.append(applyAll);
177: strbuf.append("\", ruleNames=[");
178: for (int i = 0; i < ruleNames.length; i++) {
179: if (i > 0) {
180: strbuf.append(",");
181: }
182: strbuf.append(ruleNames[i]);
183: }
184: strbuf.append("]]");
185:
186: return strbuf.toString();
187: }
188:
189: }
|