001: package org.columba.mail.search;
002:
003: import java.util.Enumeration;
004: import java.util.List;
005: import java.util.Vector;
006:
007: import org.columba.core.filter.FilterCriteria;
008: import org.columba.core.filter.FilterRule;
009: import org.columba.mail.folder.IMailFolder;
010: import org.columba.mail.folder.IMailbox;
011: import org.columba.mail.folder.virtual.VirtualFolder;
012: import org.columba.mail.gui.tree.FolderTreeModel;
013:
014: public class SearchFolderFactory {
015:
016: /**
017: * Create search folder for simple search with a single criteria
018: */
019: public static VirtualFolder createSearchFolder(FilterCriteria c,
020: IMailFolder folder) throws Exception {
021:
022: // create search folder
023: VirtualFolder searchFolder = createVirtualFolder(folder);
024:
025: // add filter criteria
026: searchFolder.getFilter().getFilterRule().add(c);
027:
028: return searchFolder;
029: }
030:
031: /**
032: * Create new search folder but use existing virtual folder for an "search
033: * inside results" search
034: */
035: public static VirtualFolder prepareSearchFolder(FilterCriteria c,
036: IMailFolder folder) throws Exception {
037:
038: // create search folder
039: VirtualFolder searchFolder = createVirtualFolder(folder);
040:
041: // add filter criteria
042: searchFolder.getFilter().getFilterRule().add(c);
043:
044: return searchFolder;
045: }
046:
047: /**
048: * Create search for folder complex search with multiple criteria
049: */
050: public static VirtualFolder createSearchFolder(FilterRule rule,
051: IMailFolder folder) throws Exception {
052: // create search folder
053: VirtualFolder searchFolder = createVirtualFolder(folder);
054:
055: searchFolder.getFilter().getFilterRule().setCondition(
056: rule.getConditionInt());
057:
058: for (int i = 0; i < rule.getChildCount(); i++) {
059: // add filter criteria
060: searchFolder.getFilter().getFilterRule().add(rule.get(i));
061: }
062:
063: return searchFolder;
064: }
065:
066: /**
067: * Update already existing virtual folder for an "search inside results"
068: * search
069: */
070: public static VirtualFolder prepareSearchFolder(FilterRule rule,
071: IMailFolder folder) throws Exception {
072:
073: // create search folder
074: VirtualFolder searchFolder = createVirtualFolder(folder);
075:
076: searchFolder.getFilter().getFilterRule().setCondition(
077: rule.getConditionInt());
078:
079: for (int i = 0; i < rule.getChildCount(); i++) {
080: // add filter criteria
081: searchFolder.getFilter().getFilterRule().add(rule.get(i));
082: }
083:
084: return searchFolder;
085: }
086:
087: /**
088: * Create virtual folder.
089: *
090: * @param folder
091: * @return
092: */
093: private static VirtualFolder createVirtualFolder(IMailFolder folder) {
094:
095: VirtualFolder searchFolder = new VirtualFolder("Search Result",
096: folder);
097:
098: // remove old filters
099: searchFolder.getFilter().getFilterRule().removeAll();
100:
101: // search in subfolders recursively
102: searchFolder.getConfiguration().setString("property",
103: "include_subfolders", "true");
104:
105: String uid = folder.getId();
106:
107: // set source folder UID
108: searchFolder.getConfiguration().setString("property",
109: "source_uid", uid);
110:
111: return searchFolder;
112: }
113:
114: /**
115: * Return list of all source folders we going to query.
116: *
117: * @return list of source folders
118: */
119: public static List<IMailbox> getAllSourceFolders() {
120: IMailFolder rootFolder = (IMailFolder) FolderTreeModel
121: .getInstance().getRoot();
122:
123: List<IMailbox> v = new Vector<IMailbox>();
124: getChildren(rootFolder, v);
125:
126: return v;
127: }
128:
129: private static void getChildren(IMailFolder parentFolder,
130: List<IMailbox> list) {
131: IMailFolder child;
132:
133: for (Enumeration e = parentFolder.children(); e
134: .hasMoreElements();) {
135: child = (IMailFolder) e.nextElement();
136:
137: if (child instanceof IMailbox) {
138: if (child.getName().equalsIgnoreCase("Inbox")) {
139: list.add((IMailbox) child);
140: }
141: }
142:
143: getChildren(child, list);
144: }
145: }
146: }
|