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.http;
24  
25  import java.io.InputStream;
26  import java.io.Reader;
27  import java.util.ArrayList;
28  import java.util.Calendar;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.TimeZone;
32  
33  import de.bea.domingo.DBase;
34  import de.bea.domingo.DDateRange;
35  import de.bea.domingo.DDocument;
36  import de.bea.domingo.DItem;
37  import de.bea.domingo.DNotesMonitor;
38  import de.bea.domingo.util.GregorianDateRange;
39  
40  /***
41   * Simple implementation of an item in a Notes document.
42   *
43   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
44   */
45  public final class ItemHttp extends BaseHttp implements DItem {
46  
47      /*** Size on disk in bytes of a number value. */
48      private static final int NUMBER_VALUE_SIZE = 8; // TODO check for correct value size
49  
50      /*** Size on disk in bytes of a calendar value. */
51      private static final int CALENDAR_VALUE_SIZE = 8; // TODO check for correct value size
52  
53      /*** Size on disk in bytes of a null value. */
54      private static final int NULL_VALUE_SIZE = 4; // TODO check for correct value size
55  
56      /*** serial version ID for serialization. */
57      private static final long serialVersionUID = 963751252547033826L;
58  
59      private String fName;
60  
61      private List fValues;
62  
63      private boolean fAuthors;
64  
65      private boolean fReaders;
66  
67      private boolean fNames;
68  
69      private boolean fIsProtected;
70  
71      private boolean fIsSummary;
72  
73      /***
74       * Constructor.
75       *
76       * @param theFactory the controlling factory
77       * @param theParent the document that contains the item
78       * @param theName the item name
79       * @param monitor the monitor that handles logging
80       */
81      public ItemHttp(final NotesHttpFactory theFactory, final DBase theParent, final String theName,
82              final DNotesMonitor monitor) {
83          this(theFactory, theParent, theName, null, monitor);
84      }
85  
86      /***
87       * Constructor.
88       *
89       * @param theFactory the controlling factory
90       * @param theParent the document that contains the item
91       * @param theName the item name
92       * @param theValues the values
93       * @param monitor the monitor that handles logging
94       */
95      public ItemHttp(final NotesHttpFactory theFactory, final DBase theParent, final String theName,
96              final List theValues, final DNotesMonitor monitor) {
97          super(theFactory, theParent, monitor);
98          fName = theName;
99          fValues = theValues;
100     }
101 
102     /*** {@inheritDoc}
103      * @see de.bea.domingo.DItem#getValues()
104      */
105     public List getValues() {
106         return fValues;
107     }
108 
109     /*** {@inheritDoc}
110      * @see de.bea.domingo.DItem#setValues(java.util.List)
111      */
112     public void setValues(final List theValues) {
113         fValues = theValues;
114     }
115 
116     /*** {@inheritDoc}
117      * @see de.bea.domingo.DItem#getValueString()
118      */
119     public String getValueString() {
120         return fValues.get(0).toString();
121     }
122 
123     /*** {@inheritDoc}
124      * @see de.bea.domingo.DItem#getValueInteger()
125      */
126     public Integer getValueInteger() {
127         Object value = getValue(0);
128         if (value == null) {
129             return null;
130         } else if (value instanceof Integer) {
131             return (Integer) value;
132         } else if (value instanceof Number) {
133             return new Integer(((Number) value).intValue());
134         }
135         return null;
136     }
137 
138     /*** {@inheritDoc}
139      * @see de.bea.domingo.DItem#getValueDouble()
140      */
141     public Double getValueDouble() {
142         Object value = getValue(0);
143         if (value == null) {
144             return null;
145         } else if (value instanceof Double) {
146             return (Double) value;
147         } else if (value instanceof Number) {
148             return new Double(((Number) value).doubleValue());
149         }
150         return null;
151     }
152 
153     /*** {@inheritDoc}
154      * @see de.bea.domingo.DItem#getValueDateTime()
155      */
156     public Calendar getValueDateTime() {
157         Object value = getValue(0);
158         if (value == null) {
159             return null;
160         } else if (value instanceof Calendar) {
161             return (Calendar) value;
162         }
163         return null;
164     }
165 
166     /*** {@inheritDoc}
167      * @see de.bea.domingo.DItem#getValueDateRange()
168      */
169     public DDateRange getValueDateRange() {
170         Object from = getValue(0);
171         if (from == null || !(from instanceof Calendar)) {
172             return null;
173         }
174         Object to = getValue(1);
175         if (to == null || !(to instanceof Calendar)) {
176             return null;
177         }
178         return new GregorianDateRange((Calendar) from, (Calendar) to);
179     }
180 
181     /*** {@inheritDoc}
182      * @see de.bea.domingo.DItem#setValueString(java.lang.String)
183      */
184     public void setValueString(final String value) {
185         fValues = new ArrayList(1);
186         fValues.set(0, value);
187     }
188 
189     /*** {@inheritDoc}
190      * @see de.bea.domingo.DItem#setValueInteger(int)
191      */
192     public void setValueInteger(final int i) {
193         fValues = new ArrayList(1);
194         fValues.set(0, new Integer(i));
195     }
196 
197     /*** {@inheritDoc}
198      * @see de.bea.domingo.DItem#setValueInteger(java.lang.Integer)
199      */
200     public void setValueInteger(final Integer i) {
201         fValues = new ArrayList(1);
202         fValues.set(0, i);
203     }
204 
205     /*** {@inheritDoc}
206      * @see de.bea.domingo.DItem#setValueDouble(double)
207      */
208     public void setValueDouble(final double d) {
209         fValues = new ArrayList(1);
210         fValues.set(0, new Double(d));
211     }
212 
213     /*** {@inheritDoc}
214      * @see de.bea.domingo.DItem#setValueDouble(java.lang.Double)
215      */
216     public void setValueDouble(final Double d) {
217         fValues = new ArrayList(1);
218         fValues.set(0, d);
219     }
220 
221     /*** {@inheritDoc}
222      * @see de.bea.domingo.DItem#setValueDateTime(java.util.Calendar)
223      */
224     public void setValueDateTime(final Calendar calendar) {
225         fValues = new ArrayList(1);
226         fValues.set(0, calendar);
227     }
228 
229     /*** {@inheritDoc}
230      * @see de.bea.domingo.DItem#setValueDateRange(de.bea.domingo.DDateRange)
231      */
232     public void setValueDateRange(final DDateRange dateRange) {
233         fValues = new ArrayList(2);
234         fValues.set(0, dateRange.getFrom());
235         fValues.set(0, dateRange.getTo());
236     }
237 
238     /*** {@inheritDoc}
239      * @see de.bea.domingo.DItem#setValueDateRange(java.util.Calendar, java.util.Calendar)
240      */
241     public void setValueDateRange(final Calendar calendar1, final Calendar calendar2) {
242         fValues = new ArrayList(2);
243         fValues.set(0, calendar1);
244         fValues.set(0, calendar2);
245     }
246 
247     /*** {@inheritDoc}
248      * @see de.bea.domingo.DItem#appendToTextList(java.lang.String)
249      */
250     public void appendToTextList(final String value) {
251         fValues.add(value);
252     }
253 
254     /*** {@inheritDoc}
255      * @see de.bea.domingo.DItem#appendToTextList(java.util.List)
256      */
257     public void appendToTextList(final List theValues) {
258         fValues.addAll(theValues);
259     }
260 
261     /*** {@inheritDoc}
262      * @see de.bea.domingo.DItem#containsValue(java.lang.String)
263      */
264     public boolean containsValue(final String value) {
265         Iterator i = fValues.iterator();
266         while (i.hasNext()) {
267             if (i.next().equals(value)) {
268                 return true;
269             }
270         }
271         return false;
272     }
273 
274     /*** {@inheritDoc}
275      * @see de.bea.domingo.DItem#containsValue(java.lang.Integer)
276      */
277     public boolean containsValue(final Integer value) {
278         return containsValueIntern(value);
279     }
280 
281     /*** {@inheritDoc}
282      * @see de.bea.domingo.DItem#containsValue(int)
283      */
284     public boolean containsValue(final int value) {
285         return containsValueIntern(new Integer(value));
286     }
287 
288     /*** {@inheritDoc}
289      * @see de.bea.domingo.DItem#containsValue(java.lang.Double)
290      */
291     public boolean containsValue(final Double value) {
292         return containsValueIntern(value);
293     }
294 
295     /*** {@inheritDoc}
296      * @see de.bea.domingo.DItem#containsValue(double)
297      */
298     public boolean containsValue(final double value) {
299         return containsValueIntern(new Double(value));
300     }
301 
302     /*** {@inheritDoc}
303      * @see de.bea.domingo.DItem#containsValue(java.util.Calendar)
304      */
305     public boolean containsValue(final Calendar value) {
306         return containsValueIntern(value);
307     }
308 
309     /*** {@inheritDoc}
310      * @see de.bea.domingo.DItem#isSummary()
311      */
312     public boolean isSummary() {
313         return fIsSummary;
314     }
315 
316     /*** {@inheritDoc}
317      * @see de.bea.domingo.DItem#setSummary(boolean)
318      */
319     public void setSummary(final boolean flag) {
320         fIsSummary = flag;
321     }
322 
323     /*** {@inheritDoc}
324      * @see de.bea.domingo.DItem#isReaders()
325      */
326     public boolean isReaders() {
327         return fReaders;
328     }
329 
330     /*** {@inheritDoc}
331      * @see de.bea.domingo.DItem#isNames()
332      */
333     public boolean isNames() {
334         return fNames;
335     }
336 
337     /*** {@inheritDoc}
338      * @see de.bea.domingo.DItem#setNames(boolean)
339      */
340     public void setNames(final boolean flag) {
341         fNames = flag;
342     }
343 
344     /*** {@inheritDoc}
345      * @see de.bea.domingo.DItem#setReaders(boolean)
346      */
347     public void setReaders(final boolean flag) {
348         fReaders = flag;
349     }
350 
351     /*** {@inheritDoc}
352      * @see de.bea.domingo.DItem#isAuthors()
353      */
354     public boolean isAuthors() {
355         return fAuthors;
356     }
357 
358     /*** {@inheritDoc}
359      * @see de.bea.domingo.DItem#isProtected()
360      */
361     public boolean isProtected() {
362         return fIsProtected;
363     }
364 
365     /*** {@inheritDoc}
366      * @see de.bea.domingo.DItem#setAuthors(boolean)
367      */
368     public void setAuthors(final boolean flag) {
369         fAuthors = flag;
370     }
371 
372     /*** {@inheritDoc}
373      * @see de.bea.domingo.DItem#setProtected(boolean)
374      */
375     public void setProtected(final boolean flag) {
376         fIsProtected = flag;
377     }
378 
379     /*** {@inheritDoc}
380      * @see de.bea.domingo.DItem#getSize()
381      */
382     public int getSize() {
383         int size = 0;
384         Iterator i = fValues.iterator();
385         while (i.hasNext()) {
386             size += getSize(i.next());
387         }
388         return size;
389     }
390 
391     /*** {@inheritDoc}
392      * @see de.bea.domingo.DBaseItem#getName()
393      */
394     public String getName() {
395         return fName;
396     }
397 
398     /*** {@inheritDoc}
399      * @see de.bea.domingo.DBaseItem#remove()
400      */
401     public void remove() {
402         ((DocumentHttp) getParent()).removeItem(this.fName);
403     }
404 
405     /*** {@inheritDoc}
406      * @see java.lang.Object#toString()
407      */
408     public String toString() {
409         return fName + "=" + fValues;
410     }
411 
412     /*** {@inheritDoc}
413      * Returns the n-th value of exists, else <code>null</code>.
414      *
415      * @param i value index
416      * @return the n-th value of exists, else <code>null</code>
417      */
418     private Object getValue(final int i) {
419         try {
420             return fValues.get(i);
421         } catch (IndexOutOfBoundsException e) {
422             getMonitor().warn("Cannot access value " + i + " of item " + fName);
423         }
424         return null;
425     }
426 
427     /*** {@inheritDoc}
428      * Checks if the item contains a specific value, independent of its time.
429      *
430      * @param value any possible value
431      * @return <code>true</code> if the item contains the value, else <code>false</code>
432      */
433     private boolean containsValueIntern(final Object value) {
434         Iterator i = fValues.iterator();
435         while (i.hasNext()) {
436             if (i.next().equals(value)) {
437                 return true;
438             }
439         }
440         return false;
441     }
442 
443     /*** {@inheritDoc}
444      * Returns the size on disk of a single value.
445      *
446      * @param object any possible value
447      * @return size size on disk of the value
448      */
449     private int getSize(final Object object) {
450         if (object == null) {
451             return NULL_VALUE_SIZE;
452         } else if (object instanceof Calendar) {
453             return CALENDAR_VALUE_SIZE;
454         } else if (object instanceof String) {
455             return ((String) object).length();
456         } else if (object instanceof Number) {
457             return NUMBER_VALUE_SIZE;
458         }
459         return NULL_VALUE_SIZE;
460     }
461 
462     /***
463      * {@inheritDoc}
464      * @see de.bea.domingo.DItem#abstractText(int, boolean, boolean)
465      */
466     public String abstractText(final int maxlen, final boolean dropVowels, final boolean userDict) {
467         throw new UnsupportedOperationException("not supported in Http Item");
468     }
469 
470     /***
471      * {@inheritDoc}
472      * @see de.bea.domingo.DItem#copyItemToDocument(de.bea.domingo.DDocument)
473      */
474     public DItem copyItemToDocument(final DDocument document) {
475         throw new UnsupportedOperationException("not supported in Http Item");
476     }
477 
478     /***
479      * {@inheritDoc}
480      * @see de.bea.domingo.DItem#copyItemToDocument(de.bea.domingo.DDocument, java.lang.String)
481      */
482     public DItem copyItemToDocument(final DDocument document, final String newName) {
483         throw new UnsupportedOperationException("not supported in Http Item");
484     }
485 
486     /***
487      * {@inheritDoc}
488      * @see de.bea.domingo.DItem#getLastModified()
489      */
490     public Calendar getLastModified() {
491         throw new UnsupportedOperationException("not supported in Http Item");
492     }
493 
494     /***
495      * {@inheritDoc}
496      * @see de.bea.domingo.DItem#getText()
497      */
498     public String getText() {
499         throw new UnsupportedOperationException("not supported in Http Item");
500     }
501 
502     /***
503      * {@inheritDoc}
504      * @see de.bea.domingo.DItem#getText(int)
505      */
506     public String getText(final int maxLen) {
507         throw new UnsupportedOperationException("not supported in Http Item");
508     }
509 
510     /***
511      * {@inheritDoc}
512      * @see de.bea.domingo.DItem#getType()
513      */
514     public int getType() {
515         throw new UnsupportedOperationException("not supported in Http Item");
516     }
517 
518     /***
519      * {@inheritDoc}
520      * @see de.bea.domingo.DItem#getValueLength()
521      */
522     public int getValueLength() {
523         throw new UnsupportedOperationException("not supported in Http Item");
524     }
525 
526     /***
527      * {@inheritDoc}
528      * @see de.bea.domingo.DItem#setValueCustomData(java.lang.String, java.lang.Object)
529      */
530     public void setValueCustomData(final String type, final Object obj) {
531         throw new UnsupportedOperationException("not supported in Http Item");
532     }
533 
534     /***
535      * {@inheritDoc}
536      * @see de.bea.domingo.DItem#setValueCustomData(java.lang.Object)
537      */
538     public void setValueCustomData(final Object obj) {
539         throw new UnsupportedOperationException("not supported in Http Item");
540     }
541 
542     /***
543      * {@inheritDoc}
544      * @see de.bea.domingo.DItem#setValueCustomDataBytes(java.lang.String, byte[])
545      */
546     public void setValueCustomDataBytes(final String type, final byte[] bytes) {
547         throw new UnsupportedOperationException("not supported in Http Item");
548     }
549 
550     /***
551      * {@inheritDoc}
552      * @see de.bea.domingo.DItem#getValueCustomData(java.lang.String)
553      */
554     public Object getValueCustomData(final String type) {
555         throw new UnsupportedOperationException("not supported in Http Item");
556     }
557 
558     /***
559      * {@inheritDoc}
560      * @see de.bea.domingo.DItem#getValueCustomData()
561      */
562     public Object getValueCustomData() {
563         throw new UnsupportedOperationException("not supported in Http Item");
564     }
565 
566     /***
567      * {@inheritDoc}
568      * @see de.bea.domingo.DItem#getValueCustomDataBytes(java.lang.String)
569      */
570     public byte[] getValueCustomDataBytes(final String type) {
571         throw new UnsupportedOperationException("not supported in Http Item");
572     }
573 
574     /***
575      * {@inheritDoc}
576      * @see de.bea.domingo.DItem#isEncrypted()
577      */
578     public boolean isEncrypted() {
579         throw new UnsupportedOperationException("not supported in Http Item");
580     }
581 
582     /***
583      * {@inheritDoc}
584      * @see de.bea.domingo.DItem#setEncrypted(boolean)
585      */
586     public void setEncrypted(final boolean flag) {
587         throw new UnsupportedOperationException("not supported in Http Item");
588     }
589 
590     /***
591      * {@inheritDoc}
592      * @see de.bea.domingo.DItem#isSaveToDisk()
593      */
594     public boolean isSaveToDisk() {
595         throw new UnsupportedOperationException("not supported in Http Item");
596     }
597 
598     /***
599      * {@inheritDoc}
600      * @see de.bea.domingo.DItem#setSaveToDisk(boolean)
601      */
602     public void setSaveToDisk(final boolean flag) {
603         throw new UnsupportedOperationException("not supported in Http Item");
604     }
605 
606     /***
607      * {@inheritDoc}
608      * @see de.bea.domingo.DItem#isSigned()
609      */
610     public boolean isSigned() {
611         throw new UnsupportedOperationException("not supported in Http Item");
612     }
613 
614     /***
615      * {@inheritDoc}
616      * @see de.bea.domingo.DItem#setSigned(boolean)
617      */
618     public void setSigned(final boolean flag) {
619         throw new UnsupportedOperationException("not supported in Http Item");
620     }
621 
622     /***
623       * {@inheritDoc}
624      * @see de.bea.domingo.DItem#getReader()
625      */
626     public Reader getReader() {
627         throw new UnsupportedOperationException("not supported in Http Item");
628     }
629 
630     /***
631      * {@inheritDoc}
632      * @see de.bea.domingo.DItem#getInputStream()
633      */
634     public InputStream getInputStream() {
635         throw new UnsupportedOperationException("not supported in Http Item");
636     }
637 
638     /***
639      * {@inheritDoc}
640      * @see de.bea.domingo.DItem#setValueDateTime(java.util.TimeZone)
641      */
642     public void setValueDateTime(final TimeZone timezone) {
643         throw new UnsupportedOperationException("not supported in Http Item");
644     }
645 }