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.proxy;
24  
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import lotus.domino.Database;
29  import lotus.domino.Document;
30  import lotus.domino.DocumentCollection;
31  import lotus.domino.MIMEEntity;
32  import lotus.domino.NotesException;
33  import lotus.domino.Session;
34  import lotus.domino.Stream;
35  import lotus.domino.View;
36  import de.bea.domingo.DBase;
37  import de.bea.domingo.DDatabase;
38  import de.bea.domingo.DDocument;
39  import de.bea.domingo.DNotesMonitor;
40  import de.bea.domingo.DView;
41  
42  /***
43   * Represents a document in a database.
44   */
45  public final class DocumentProxy extends BaseDocumentProxy implements DDocument {
46  
47      /*** serial version ID for serialization. */
48      private static final long serialVersionUID = 3258413915393045559L;
49  
50      /*** private cache of universal ID.
51       * (to make the UNID available even after recycle of the document) */
52      private String universalID;
53  
54      /***
55       * Constructor for DDocumentImpl.
56       *
57       * @param theFactory the controlling factory
58       * @param parent the parent object
59       * @param theDocument the Notes document object
60       * @param monitor the monitor
61       */
62      protected DocumentProxy(final NotesProxyFactory theFactory, final DBase parent,
63                              final Document theDocument, final DNotesMonitor monitor) {
64          super(theFactory, parent, theDocument, monitor);
65          universalID = getUniversalID();
66      }
67  
68      /***
69       * {@inheritDoc}
70       * @see de.bea.domingo.DDocument#isNewNote()
71       */
72      public boolean isNewNote() {
73          getFactory().preprocessMethod();
74          try {
75              return getDocument().isNewNote();
76          } catch (NotesException e) {
77              throw newRuntimeException("isNewNote(): ", e);
78          }
79      }
80  
81      /***
82       * {@inheritDoc}
83       * @see de.bea.domingo.DDocument#getUniversalID()
84       */
85      public String getUniversalID() {
86          getFactory().preprocessMethod();
87          try {
88              return getDocument().getUniversalID();
89          } catch (NotesException e) {
90              throw newRuntimeException("Cannot get universalID", e);
91          }
92      }
93  
94      /***
95       * {@inheritDoc}
96       * @see de.bea.domingo.DDocument#getNoteID()
97       */
98      public String getNoteID() {
99          getFactory().preprocessMethod();
100         try {
101             return getDocument().getNoteID();
102         } catch (NotesException e) {
103             throw newRuntimeException("Cannot get NoteID", e);
104         }
105     }
106 
107     /***
108      * {@inheritDoc}
109      * @see de.bea.domingo.DDocument#isResponse()
110      */
111     public boolean isResponse() {
112         getFactory().preprocessMethod();
113         try {
114             return getDocument().isResponse();
115         } catch (NotesException e) {
116             throw newRuntimeException("Cannot check if is response", e);
117         }
118     }
119 
120     /***
121      * {@inheritDoc}
122      * @see de.bea.domingo.DDocument#copyToDatabase(de.bea.domingo.DDatabase)
123      */
124     public DDocument copyToDatabase(final DDatabase database) {
125         if (!(database instanceof DatabaseProxy)) {
126             throw newRuntimeException("Cannot copy database (invalid argument)");
127         }
128         getFactory().preprocessMethod();
129         try {
130             final Database targetNotesDatabase = (Database) ((DatabaseProxy) database).getNotesObject();
131             final Document newNotesDoc = getDocument().copyToDatabase(targetNotesDatabase);
132             return (DDocument) BaseDocumentProxy.getInstance(getFactory(), database, newNotesDoc, getMonitor());
133         } catch (NotesException e) {
134             throw newRuntimeException("Cannot copy database", e);
135         }
136     }
137 
138     /***
139      * {@inheritDoc}
140      * @see de.bea.domingo.proxy.BaseDocumentProxy#toString()
141      */
142     public String toString() {
143         return universalID;
144     }
145 
146     /***
147      * {@inheritDoc}
148      * @see de.bea.domingo.DDocument#send(java.lang.String)
149      */
150     public void send(final String recipient) {
151         getFactory().preprocessMethod();
152         try {
153             if (recipient == null || "".equals(recipient)) {
154                 final List docRecipients = getRecipients();
155                 if (docRecipients == null) {
156                     throw newRuntimeException("recipients.missing");
157                 }
158                 send0(docRecipients);
159                 return;
160             }
161             send0(recipient);
162         } catch (NotesException e) {
163             throw newRuntimeException("send(String): ", e);
164         }
165     }
166 
167     /***
168      * {@inheritDoc}
169      * @see de.bea.domingo.DDocument#send(java.util.List)
170      */
171     public void send(final List recipients) {
172         getFactory().preprocessMethod();
173         try {
174             if (recipients == null || getRecipients(recipients) == null) {
175                 final List docRecipients = getRecipients();
176                 if (docRecipients == null) {
177                     throw newRuntimeException("recipients.missing");
178                 }
179                 send0(docRecipients);
180                 return;
181             }
182             send0(recipients);
183         } catch (NotesException e) {
184             throw newRuntimeException("send(List): ", e);
185         }
186     }
187 
188     /***
189      * Returns a list of recipients in the <tt>sendTo</tt> item, if at least
190      * one value is not an empty string.
191      *
192      * @return list of recipients or <code>null</code> if no recipients available.
193      * @throws NotesException if recipients cannot be read from the document.
194      */
195     private List getRecipients() throws NotesException {
196         final List recipients = getDocument().getItemValue("SendTo");
197         return getRecipients(recipients);
198     }
199 
200     /***
201      * Returns the given list of recipients, if at least
202      * one value is not an empty string, else <code>null</code>.
203      *
204      * @param recipients list of recipients
205      * @return given list of recipients or <code>null</code> no recipients in list found
206      */
207     private List getRecipients(final List recipients) {
208         if (recipients != null) {
209             for (int i = 0; i < recipients.size(); i++) {
210                 if (recipients.get(i) instanceof String) {
211                     if (((String) recipients.get(i)).length() > 0) {
212                         return recipients;
213                     }
214                 }
215             }
216         }
217         return null;
218     }
219 
220     /***
221      * Sends a document.
222      *
223      * @param recipient recipient
224      * @throws NotesException if send failed
225      */
226     private void send0(final String recipient) throws NotesException {
227         getDocument().send(recipient);
228     }
229 
230     /***
231      * Send a document.
232      *
233      * @param recipients list of recipients
234      * @throws NotesException if send failed
235      */
236     private void send0(final List recipients) throws NotesException {
237         getDocument().send(convertListToVector(recipients));
238     }
239 
240     /***
241      * {@inheritDoc}
242      * @see de.bea.domingo.DDocument#makeResponse(de.bea.domingo.DDocument)
243      */
244     public void makeResponse(final DDocument parent) {
245         if (!(parent instanceof BaseDocumentProxy)) {
246             throw newRuntimeException("Cannot make response", new ClassCastException(parent.getClass().getName()));
247         }
248         getFactory().preprocessMethod();
249         try {
250             final Document document = ((BaseDocumentProxy) parent).getDocument();
251             getDocument().makeResponse(document);
252         } catch (NotesException e) {
253             throw newRuntimeException("Cannot make response", e);
254         }
255     }
256 
257     /***
258      * {@inheritDoc}
259      * @see de.bea.domingo.DDocument#getParentDocument()
260      */
261     public DDocument getParentDocument() {
262         getFactory().preprocessMethod();
263         final String parentDocUnID = getParentDocumentUNID();
264         if (parentDocUnID == null || "".equals(parentDocUnID)) {
265             return null;
266         }
267         final DDatabase database = (DDatabase) getParent();
268         return database.getDocumentByUNID(parentDocUnID);
269     }
270 
271     /***
272      * {@inheritDoc}
273      * @see de.bea.domingo.DDocument#getParentDocumentUNID()
274      */
275     public String getParentDocumentUNID() {
276         getFactory().preprocessMethod();
277         try {
278             return getDocument().getParentDocumentUNID();
279         } catch (NotesException e) {
280             throw newRuntimeException("Cannot get UNID of parent document", e);
281         }
282     }
283 
284     /***
285      * {@inheritDoc}
286      * @see de.bea.domingo.DDocument#getResponses()
287      */
288     public Iterator getResponses() {
289         getFactory().preprocessMethod();
290         try {
291             final DocumentCollection docColl = getDocument().getResponses();
292             return new DocumentCollectionIterator(getFactory(), this.getParent(), docColl, getMonitor());
293         } catch (NotesException e) {
294             throw newRuntimeException("Cannot get responses", e);
295         }
296     }
297 
298     /***
299      * {@inheritDoc}
300      * @see de.bea.domingo.DDocument#setSaveMessageOnSend(boolean)
301      */
302     public void setSaveMessageOnSend(final boolean saveMessageOnSend) {
303         getFactory().preprocessMethod();
304         try {
305             getDocument().setSaveMessageOnSend(saveMessageOnSend);
306         } catch (NotesException e) {
307             throw newRuntimeException("Cannot set to save message on send", e);
308         }
309     }
310 
311     /***
312      * {@inheritDoc}
313      * @see de.bea.domingo.DDocument#setEncryptOnSend(boolean)
314      */
315     public void setEncryptOnSend(final boolean flag) {
316         getFactory().preprocessMethod();
317         try {
318             getDocument().setEncryptOnSend(flag);
319         } catch (NotesException e) {
320             throw newRuntimeException("Cannot set to encrypt mail on send.", e);
321         }
322     }
323 
324     /***
325      * {@inheritDoc}
326      * @see de.bea.domingo.DDocument#setSignOnSend(boolean)
327      */
328     public void setSignOnSend(final boolean flag) {
329         getFactory().preprocessMethod();
330         try {
331             getDocument().setSignOnSend(flag);
332         } catch (NotesException e) {
333             throw newRuntimeException("Cannot set to sign on send.", e);
334         }
335     }
336 
337     /***
338      * {@inheritDoc}
339      * @see de.bea.domingo.DDocument#sign()
340      */
341     public void sign() {
342         getFactory().preprocessMethod();
343         try {
344             getDocument().sign();
345         } catch (NotesException e) {
346             throw newRuntimeException("Cannot sign document.", e);
347         }
348     }
349 
350     /***
351      * {@inheritDoc}
352      * @see de.bea.domingo.DDocument#replaceHTML(java.lang.String, java.lang.String)
353      */
354     public void replaceHTML(final String name, final String value) {
355         getFactory().preprocessMethod();
356         try {
357             getDocument().removeItem(name);
358             MIMEEntity entity = getDocument().createMIMEEntity(name);
359             Stream stream = ((Session) getDSession().getNotesObject()).createStream();
360             stream.writeText(value);
361             entity.setContentFromText(stream, "text/html; charset=ISO-8859-1", MIMEEntity.ENC_NONE);
362 
363         } catch (NotesException e) {
364             throw newRuntimeException("Cannot replace HTML in item " + name, e);
365         }
366     }
367 
368     /***
369      * {@inheritDoc}
370      * @see de.bea.domingo.DDocument#getFTSearchScore()
371      */
372     public int getFTSearchScore() {
373         getFactory().preprocessMethod();
374         try {
375             return getDocument().getFTSearchScore();
376         } catch (NotesException e) {
377             throw newRuntimeException("Cannot get FTSearch scope.", e);
378         }
379     }
380 
381     /***
382      * {@inheritDoc}
383      * @see de.bea.domingo.DDocument#getParentView()
384      */
385     public DView getParentView() {
386         getFactory().preprocessMethod();
387         try {
388             final View parentView = getDocument().getParentView();
389             return ViewProxy.getInstance(getFactory(), this.getParentDatabase(), parentView, getMonitor());
390         } catch (NotesException e) {
391             throw newRuntimeException("Cannot put in folder", e);
392         }
393     }
394 
395     /***
396      * {@inheritDoc}
397      * @see de.bea.domingo.DDocument#getFolderReferences()
398      */
399     public List getFolderReferences() {
400         getFactory().preprocessMethod();
401         try {
402             return getDocument().getFolderReferences();
403         } catch (NotesException e) {
404             throw newRuntimeException("Cannot get folder references", e);
405         }
406     }
407 
408     /***
409      * {@inheritDoc}
410      * @see de.bea.domingo.DDocument#putInFolder(java.lang.String)
411      */
412     public void putInFolder(final String name) {
413         getFactory().preprocessMethod();
414         try {
415             getDocument().putInFolder(name);
416         } catch (NotesException e) {
417             throw newRuntimeException("Cannot put in folder", e);
418         }
419     }
420 
421     /***
422      * {@inheritDoc}
423      * @see de.bea.domingo.DDocument#putInFolder(java.lang.String, boolean)
424      */
425     public void putInFolder(final String name, final boolean create) {
426         getFactory().preprocessMethod();
427         try {
428             getDocument().putInFolder(name, create);
429         } catch (NotesException e) {
430             throw newRuntimeException("Cannot put in folder", e);
431         }
432     }
433 
434     /***
435      * {@inheritDoc}
436      * @see de.bea.domingo.DDocument#removeFromFolder(java.lang.String)
437      */
438     public void removeFromFolder(final String name) {
439         getFactory().preprocessMethod();
440         try {
441             getDocument().removeFromFolder(name);
442         } catch (NotesException e) {
443             throw newRuntimeException("Cannot remove from folder", e);
444         }
445     }
446 
447     /***
448      * {@inheritDoc}
449      * @see de.bea.domingo.DDocument#getURL()
450      */
451     public String getURL() {
452         getFactory().preprocessMethod();
453         try {
454             return getDocument().getURL();
455         } catch (NotesException e) {
456             throw newRuntimeException("Cannot get URL", e);
457         }
458     }
459 
460     /***
461      * {@inheritDoc}
462      * @see de.bea.domingo.DDocument#getNotesURL()
463      */
464     public String getNotesURL() {
465         getFactory().preprocessMethod();
466         try {
467             return getDocument().getNotesURL();
468         } catch (NotesException e) {
469             throw newRuntimeException("Cannot get Notes URL", e);
470         }
471     }
472 
473     /***
474      * {@inheritDoc}
475      * @see de.bea.domingo.DDocument#getHttpURL()
476      */
477     public String getHttpURL() {
478         getFactory().preprocessMethod();
479         try {
480             return getDocument().getHttpURL();
481         } catch (NotesException e) {
482             throw newRuntimeException("Cannot get Http URL", e);
483         }
484     }
485 }