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.map;
24  
25  import java.util.Calendar;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Map.Entry;
30  
31  import de.bea.domingo.DDocument;
32  
33  /***
34   * Class defining and performing a mapping of a map of attributes to a document.
35   *
36   * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede</a>
37   */
38  public abstract class MapMapper extends BaseDMapper {
39  
40      /***
41       * Constructor.
42       */
43      public MapMapper() {
44          super();
45      }
46  
47      /***
48       * {@inheritDoc}
49       *
50       * @see de.bea.domingo.map.Mapper#map(de.bea.domingo.DDocument, java.lang.Object)
51       */
52      public final void map(final DDocument document, final Object object) throws MappingException {
53          // TODO map all items into a HashSet
54      }
55  
56      /***
57       * {@inheritDoc}
58       *
59       * @see de.bea.domingo.map.Mapper#map(java.lang.Object, de.bea.domingo.DDocument)
60       */
61      public final void map(final Object object, final DDocument document) throws MappingException {
62          Map map = getMap(object);
63          if (map == null) {
64              return;
65          }
66          Iterator iterator = map.entrySet().iterator();
67          while (iterator.hasNext()) {
68              Entry entry = (Entry) iterator.next();
69              setValue(document, (String) entry.getKey(), entry.getValue());
70          }
71      }
72  
73      /***
74       * derived classes implement this method and return the map that should be mapped.
75       *
76       * @param object the base object
77       * @return map
78       */
79      protected abstract Map getMap(final Object object);
80  
81      /***
82       * Must be implemented by derived classes to select allowed names.
83       *
84       * @param name the name of a value to check
85       * @return <code>true</code> if the header name is allowed, else <code>false</code>
86       */
87      protected abstract boolean isNameAllowed(final String name);
88  
89      /***
90       * Sets a single value in the document.
91       *
92       * Allowed objects are of type <code>String</code>,
93       * <code>Calendar</code>, <code>Integer</code>, <code>Double</code>
94       * or <code>List</code>, where if the object is a <code>List</code>, all
95       * values in the list must be of one of the other types.
96       *
97       * @param name the name of the value
98       * @param value the value
99       * @param document the Notes document
100      * @throws MappingException if an error occurred during mapping
101      */
102     private void setValue(final DDocument document, final String name, final Object value) throws MappingException {
103         if (name == null) {
104             return;
105         }
106         if (value == null) {
107             document.replaceItemValue(name, "");
108             return;
109         }
110         Class clazz = value.getClass();
111         if (value instanceof String && String.class.isAssignableFrom(clazz)) {
112             document.replaceItemValue(name, (String) value);
113         } else if (value instanceof Calendar && Calendar.class.isAssignableFrom(clazz)) {
114             document.replaceItemValue(name, (Calendar) value);
115         } else if (value instanceof Double && Number.class.isAssignableFrom(clazz)) {
116             document.replaceItemValue(name, (Double) value);
117         } else if (value instanceof Integer && Number.class.isAssignableFrom(clazz)) {
118             document.replaceItemValue(name, (Integer) value);
119         } else if (value instanceof List) { // TODO test behavior with mixed type in list
120             document.replaceItemValue(name, (List) value);
121         } else {
122             throw new MappingException("Invalid datatype: " + clazz.getName());
123         }
124 
125     }
126 }