001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.parser;
019:
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Vector;
023:
024: import org.columba.addressbook.facade.IContactFacade;
025: import org.columba.addressbook.facade.IContactItem;
026: import org.columba.addressbook.facade.IFolder;
027: import org.columba.addressbook.facade.IFolderFacade;
028: import org.columba.addressbook.facade.IGroupItem;
029: import org.columba.addressbook.facade.IHeaderItem;
030: import org.columba.api.exception.ServiceNotFoundException;
031: import org.columba.mail.connector.ServiceConnector;
032:
033: /**
034: * Provides methods for creating new lists from other list formats.
035: *
036: * @author fdietz
037: */
038: public class ListBuilder {
039:
040: private static IContactItem retrieveContactItem(String name) {
041:
042: try {
043: IContactFacade facade = ServiceConnector.getContactFacade();
044: IFolderFacade folderFacade = ServiceConnector
045: .getFolderFacade();
046: List<IFolder> list = folderFacade.getAllFolders();
047: Iterator<IFolder> it = list.iterator();
048: while (it.hasNext()) {
049: IFolder folder = it.next();
050: String id = facade.findByName(folder.getId(), name);
051: if (id != null) {
052: IContactItem contactItem = facade.getContactItem(
053: folder.getId(), id);
054: return contactItem;
055: }
056: }
057: } catch (ServiceNotFoundException e) {
058: e.printStackTrace();
059: }
060:
061: return null;
062: }
063:
064: /**
065: * @param name
066: * @return groupItem
067: */
068: private static IGroupItem retrieveGroupItem(String name) {
069:
070: try {
071: IContactFacade facade = ServiceConnector.getContactFacade();
072: IFolderFacade folderFacade = ServiceConnector
073: .getFolderFacade();
074: List<IFolder> list = folderFacade.getAllFolders();
075: Iterator<IFolder> it = list.iterator();
076: while (it.hasNext()) {
077: IFolder folder = it.next();
078: List<IGroupItem> groupList = facade.getAllGroups(folder
079: .getId());
080: Iterator<IGroupItem> groupIt = groupList.iterator();
081: while (groupIt.hasNext()) {
082: IGroupItem groupItem = groupIt.next();
083: if (name.equals(groupItem.getName()))
084: return groupItem;
085: }
086: }
087: } catch (ServiceNotFoundException e) {
088: e.printStackTrace();
089: }
090:
091: return null;
092: }
093:
094: /**
095: * Flatten mixed list containing contacts and groups to a new list
096: * containing only contacts.
097: *
098: * @param list
099: * mixed list
100: * @return list containing only contacts. Never <code>null</code>
101: */
102: public static List<String> createFlatList(List<String> list) {
103: if (list == null)
104: throw new IllegalArgumentException("list == null");
105:
106: List<String> result = new Vector<String>();
107:
108: Iterator<String> it = list.iterator();
109: while (it.hasNext()) {
110: String str = it.next();
111:
112: // remove leading or trailing whitespaces
113: str = str.trim();
114:
115: IContactItem contactItem = retrieveContactItem(str);
116: if (contactItem != null) {
117: // found contact item in contact component
118: result.add(contactItem.getEmailAddress());
119: } else {
120: // check if its a group item
121:
122: IGroupItem groupItem = retrieveGroupItem(str);
123: if (groupItem != null) {
124:
125: List<IContactItem> contactItemList = groupItem
126: .getContacts();
127:
128: Iterator<IContactItem> it2 = contactItemList
129: .iterator();
130: while (it2.hasNext()) {
131: IContactItem i = it2.next();
132: String address = i.getEmailAddress();
133:
134: if (address == null) {
135: continue;
136: }
137:
138: result.add(address);
139: }
140: } else {
141: result.add(str);
142: }
143: }
144: }
145:
146: return result;
147: }
148:
149: /**
150: * Create list containing only strings from a HeaderItemList containing
151: * HeaderItem objects.
152: *
153: * @param list
154: * HeaderItemList containing HeaderItem objects
155: * @return list containing only strings
156: */
157: public static List<String> createStringListFromItemList(
158: List<IHeaderItem> list) {
159: List<String> result = new Vector<String>();
160:
161: for (Iterator it = list.iterator(); it.hasNext();) {
162: IHeaderItem item = (IHeaderItem) it.next();
163: result.add(item.getName());
164: }
165:
166: return result;
167: }
168: }
|