KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > parser > ListBuilder


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.parser;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.columba.addressbook.facade.IContactFacade;
25 import org.columba.addressbook.facade.IContactItem;
26 import org.columba.addressbook.facade.IFolder;
27 import org.columba.addressbook.facade.IFolderFacade;
28 import org.columba.addressbook.facade.IGroupItem;
29 import org.columba.addressbook.facade.IHeaderItem;
30 import org.columba.api.exception.ServiceNotFoundException;
31 import org.columba.mail.connector.ServiceConnector;
32
33 /**
34  * Provides methods for creating new lists from other list formats.
35  *
36  * @author fdietz
37  */

38 public class ListBuilder {
39
40     private static IContactItem retrieveContactItem(String JavaDoc name) {
41
42         try {
43             IContactFacade facade = ServiceConnector.getContactFacade();
44             IFolderFacade folderFacade = ServiceConnector.getFolderFacade();
45             List JavaDoc<IFolder> list = folderFacade.getAllFolders();
46             Iterator JavaDoc<IFolder> it = list.iterator();
47             while (it.hasNext()) {
48                 IFolder folder = it.next();
49                 String JavaDoc id = facade.findByName(folder.getId(), name);
50                 if (id != null) {
51                     IContactItem contactItem = facade.getContactItem(folder
52                             .getId(), id);
53                     return contactItem;
54                 }
55             }
56         } catch (ServiceNotFoundException e) {
57             e.printStackTrace();
58         }
59
60         return null;
61     }
62
63     /**
64      * @param name
65      * @return groupItem
66      */

67     private static IGroupItem retrieveGroupItem(String JavaDoc name) {
68
69         try {
70             IContactFacade facade = ServiceConnector.getContactFacade();
71             IFolderFacade folderFacade = ServiceConnector.getFolderFacade();
72             List JavaDoc<IFolder> list = folderFacade.getAllFolders();
73             Iterator JavaDoc<IFolder> it = list.iterator();
74             while (it.hasNext()) {
75                 IFolder folder = it.next();
76                 List JavaDoc<IGroupItem> groupList = facade
77                         .getAllGroups(folder.getId());
78                 Iterator JavaDoc<IGroupItem> groupIt = groupList.iterator();
79                 while (groupIt.hasNext()) {
80                     IGroupItem groupItem = groupIt.next();
81                     if (name.equals(groupItem.getName()))
82                         return groupItem;
83                 }
84             }
85         } catch (ServiceNotFoundException e) {
86             e.printStackTrace();
87         }
88
89         return null;
90     }
91
92     /**
93      * Flatten mixed list containing contacts and groups to a new list
94      * containing only contacts.
95      *
96      * @param list
97      * mixed list
98      * @return list containing only contacts. Never <code>null</code>
99      */

100     public static List JavaDoc<String JavaDoc> createFlatList(List JavaDoc<String JavaDoc> list) {
101         if (list == null)
102             throw new IllegalArgumentException JavaDoc("list == null");
103
104         List JavaDoc<String JavaDoc> result = new Vector JavaDoc<String JavaDoc>();
105
106         Iterator JavaDoc<String JavaDoc> it = list.iterator();
107         while (it.hasNext()) {
108             String JavaDoc str = it.next();
109
110             // remove leading or trailing whitespaces
111
str = str.trim();
112
113             IContactItem contactItem = retrieveContactItem(str);
114             if (contactItem != null) {
115                 // found contact item in contact component
116
result.add(contactItem.getEmailAddress());
117             } else {
118                 // check if its a group item
119

120                 IGroupItem groupItem = retrieveGroupItem(str);
121                 if (groupItem != null) {
122
123                     List JavaDoc<IContactItem> contactItemList = groupItem
124                             .getContacts();
125
126                     Iterator JavaDoc<IContactItem> it2 = contactItemList.iterator();
127                     while (it2.hasNext()) {
128                         IContactItem i = it2.next();
129                         String JavaDoc address = i.getEmailAddress();
130
131                         if (address == null) {
132                             continue;
133                         }
134
135                         result.add(address);
136                     }
137                 } else {
138                     result.add(str);
139                 }
140             }
141         }
142
143         return result;
144     }
145
146     /**
147      * Create list containing only strings from a HeaderItemList containing
148      * HeaderItem objects.
149      *
150      * @param list
151      * HeaderItemList containing HeaderItem objects
152      * @return list containing only strings
153      */

154     public static List JavaDoc<String JavaDoc> createStringListFromItemList(
155             List JavaDoc<IHeaderItem> list) {
156         List JavaDoc<String JavaDoc> result = new Vector JavaDoc<String JavaDoc>();
157
158         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
159             IHeaderItem item = (IHeaderItem) it.next();
160             result.add(item.getName());
161         }
162
163         return result;
164     }
165 }
Popular Tags