View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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             // todo why don't we allow to remove entries? // allEntries.remove();
159             throw new UnsupportedOperationException();
160         }
161     }
162 }