1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.groupware.map;
24
25 import java.io.IOException;
26 import java.util.Iterator;
27
28 import de.bea.domingo.DNotesException;
29 import de.bea.domingo.DSession;
30 import de.bea.domingo.DView;
31 import de.bea.domingo.DViewEntry;
32 import de.bea.domingo.groupware.AddressBook;
33 import de.bea.domingo.groupware.ContactDigest;
34 import de.bea.domingo.groupware.GroupwareRuntimeException;
35 import de.bea.domingo.map.BaseDatabase;
36 import de.bea.domingo.map.MapperRegistrationException;
37 import de.bea.domingo.map.MappingException;
38 import de.bea.domingo.map.NotesLocation;
39
40 /***
41 * Interface to public and private name and address book databases.
42 *
43 * @author <a href=mailto:schwarz_dot_dan_at_gmail_dot_com>Daniel Schwarz</a>
44 */
45 public final class NamesDatabase extends BaseDatabase implements AddressBook {
46
47 /***
48 * Constructor.
49 *
50 * @param locationUri URI of location of database.
51 * @throws IOException if the location is not a valid notes location
52 * @throws DNotesException if the database cannot be opened
53 */
54 public NamesDatabase(final String locationUri) throws IOException, DNotesException {
55 super(locationUri);
56 }
57
58 /***
59 * Constructor.
60 *
61 * @param session an existing domingo session
62 * @param location location of database.
63 * @throws DNotesException if the uri is invalid or the database cannot be
64 * opened
65 */
66 public NamesDatabase(final DSession session, final NotesLocation location) throws DNotesException {
67 super(session, location);
68 }
69
70 /***
71 * {@inheritDoc}
72 * @see de.bea.domingo.groupware.AddressBook#getContacts()
73 */
74 public Iterator getContacts() {
75 DView view;
76 if (this.isPrivate()) {
77 view = getDatabase().getView("Contacts");
78 } else {
79 view = getDatabase().getView("People");
80 }
81 if (view == null) {
82 throw new GroupwareRuntimeException("Cannot get contacts or people view");
83 }
84 return new ContactIterator(view.getAllEntries());
85 }
86
87 /***
88 * {@inheritDoc}
89 * @see de.bea.domingo.groupware.AddressBook#isPrivate()
90 */
91 public boolean isPrivate() {
92 return (getDatabase().isPrivateAddressBook());
93 }
94
95 /***
96 * {@inheritDoc}
97 * @see de.bea.domingo.groupware.AddressBook#isPublic()
98 */
99 public boolean isPublic() {
100 return (getDatabase().isPublicAddressBook());
101 }
102
103 /***
104 * {@inheritDoc}
105 * @see de.bea.domingo.map.BaseDatabase#registerMappers()
106 */
107 protected void registerMappers() throws MapperRegistrationException {
108 if (isPrivate()) {
109 register(PrivateContactMapper.class);
110 } else {
111 register(PublicContactMapper.class);
112 }
113 }
114
115 /***
116 * Iterates thru a collection of view entries and returns for each
117 * view-entry the corresponding email digest.
118 */
119 private class ContactIterator implements Iterator {
120
121 private final Iterator allEntries;
122
123 /***
124 * Constructor.
125 *
126 * @param allEntries collection of view-entries
127 */
128 public ContactIterator(final Iterator allEntries) {
129 this.allEntries = allEntries;
130 }
131
132 /***
133 * @see java.util.Iterator#hasNext()
134 */
135 public boolean hasNext() {
136 return allEntries.hasNext();
137 }
138
139 /***
140 * Returns instances of class {@link ContactDigest}.
141 * @see java.util.Iterator#next()
142 */
143 public Object next() {
144 DViewEntry entry = (DViewEntry) allEntries.next();
145 ContactDigest memoDigest = new ContactDigest();
146 try {
147 map(entry, memoDigest);
148 } catch (MappingException e) {
149 throw new GroupwareRuntimeException("Cannot get next contact", e);
150 }
151 return memoDigest;
152 }
153
154 /***
155 * @see java.util.Iterator#remove()
156 */
157 public void remove() {
158
159 throw new UnsupportedOperationException();
160 }
161 }
162 }