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.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
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) {
120 document.replaceItemValue(name, (List) value);
121 } else {
122 throw new MappingException("Invalid datatype: " + clazz.getName());
123 }
124
125 }
126 }