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.HashMap;
26 import java.util.Map;
27
28 import de.bea.domingo.DDatabase;
29 import de.bea.domingo.DDocument;
30 import de.bea.domingo.DNotesException;
31 import de.bea.domingo.DNotesFactory;
32 import de.bea.domingo.DSession;
33 import de.bea.domingo.DViewEntry;
34
35 /***
36 * Abstract base class for concrete databases.
37 *
38 * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede</a>
39 */
40 public abstract class BaseDatabase {
41
42 /*** Reference to the notes database. */
43 private DDatabase mDatabase;
44
45 /*** Reference to the mapping factory. */
46 private MapperFactory mapperFactory = MapperFactory.newInstance();
47
48 private Map persistence = new HashMap();
49
50 /***
51 * Constructor.
52 *
53 * @param session an existing domingo session
54 * @param location location of database.
55 * @throws DNotesException if the uri is invalid or the database cannot be
56 * opened
57 */
58 public BaseDatabase(final DSession session, final NotesLocation location) throws DNotesException {
59 initDatabase(session, location);
60 registerMappers();
61 }
62
63 /***
64 * Constructor.
65 *
66 * @param locationUri URI of location of database.
67 * @throws DNotesException if the uri is invalid or the database cannot be
68 * opened
69 */
70 public BaseDatabase(final String locationUri) throws DNotesException {
71 this(NotesLocation.getInstance(locationUri));
72 }
73
74 /***
75 * Constructor.
76 *
77 * @param location location of database.
78 * @throws DNotesException if the uri is invalid or the database cannot be
79 * opened
80 */
81 public BaseDatabase(final NotesLocation location) throws DNotesException {
82 final DSession session = getSession(location);
83 initDatabase(session, location);
84 registerMappers();
85 }
86
87 /***
88 * Constructor.
89 *
90 * @param database a notes database
91 * @throws DNotesException if the database is invalid or cannot be opened
92 */
93 public BaseDatabase(final DDatabase database) throws DNotesException {
94 mDatabase = database;
95 registerMappers();
96 }
97
98 /***
99 * Creates and returns a new domingo session for a given location.
100 *
101 * @param location location of database.
102 * @return domingo session
103 * @throws DNotesException if the uri is invalid or the database cannot be
104 * opened
105 */
106 protected final DSession getSession(final NotesLocation location) throws DNotesException {
107 final DNotesFactory factory = DNotesFactory.getInstance();
108 if (location.isIIOP()) {
109 final String host = location.getHost();
110 final String user = location.getUsername();
111 final String passwd = location.getPassword();
112 return factory.getSession(host, user, passwd);
113 } else if (location.isLocal()) {
114 return factory.getSession();
115 } else {
116 throw new DNotesException("Invalid notes uri: " + location);
117 }
118 }
119
120 /***
121 * Initializes the database.
122 */
123 private void initDatabase(final DSession session, final NotesLocation location) throws DNotesException {
124 final String path = location.getPath();
125 if (location.isIIOP()) {
126 mDatabase = session.getDatabase("", path);
127 } else {
128 final String server = location.getServer();
129 mDatabase = session.getDatabase(server, path);
130 }
131 }
132
133 /***
134 * Implemented by derived class; must register all mappers.
135 *
136 * @throws MapperRegistrationException if an error occurred during
137 * registering a mapper
138 */
139 protected abstract void registerMappers() throws MapperRegistrationException;
140
141 /***
142 * Registers a mapper in the mapper factory.
143 *
144 * @param mapperClass the class of the new mapper
145 * @throws MapperRegistrationException if an error occurred during
146 * registering a mapper
147 */
148 protected final void register(final Class mapperClass) throws MapperRegistrationException {
149 mapperFactory.registerMapper(mapperClass);
150 }
151
152 /***
153 * Returns the database specified by the current location.
154 *
155 * @return Notes database at current location
156 */
157 protected final DDatabase getDatabase() {
158 return mDatabase;
159 }
160
161 /***
162 * Creates a new instance of a business object with a given class.
163 *
164 * @param clazz class of business object
165 * @return new business object
166 */
167 public final Object create(final Class clazz) {
168 DMapper instanceMapper = mapperFactory.getInstanceMapper(clazz);
169 return instanceMapper.newInstance();
170 }
171
172 /***
173 * Saves a business object.
174 *
175 * @param object business object to save
176 * @throws MappingException if an error occurred during mapping
177 */
178 public final void save(final Object object) throws MappingException {
179 String universalId = (String) persistence.get(object);
180 DDocument document = null;
181 if (universalId != null) {
182 document = mDatabase.getDocumentByUNID(universalId);
183 }
184 if (document == null) {
185 document = mDatabase.createDocument();
186 }
187 DMapper mapper = mapperFactory.getInstanceMapper(object.getClass());
188 mapper.map(object, document);
189 document.save(true, false);
190 }
191
192 /***
193 * Maps a business object to a domingo document.
194 *
195 * @param object the business object
196 * @param document the domingo document to map to
197 * @throws MappingException if an error occurred during mapping
198 *
199 * @see de.bea.domingo.map.Mapper#map(java.lang.Object,
200 * de.bea.domingo.DDocument)
201 */
202 public final void map(final Object object, final DDocument document) throws MappingException {
203 DMapper mapper = mapperFactory.getInstanceMapper(object.getClass());
204 mapper.map(object, document);
205 }
206
207 /***
208 * Maps a domingo document to a business object.
209 *
210 * @param document the domingo document to map
211 * @param object the business object
212 * @throws MappingException if an error occurred during mapping
213 *
214 * @see de.bea.domingo.map.Mapper#map(de.bea.domingo.DDocument,
215 * java.lang.Object)
216 */
217 public final void map(final DDocument document, final Object object) throws MappingException {
218 DMapper mapper = mapperFactory.getInstanceMapper(object.getClass());
219 mapper.map(document, object);
220 }
221
222 /***
223 * Maps a Domingo ViewEntry to a business object.
224 *
225 * @param viewEntry the domingo ViewEntry
226 * @param object the business object
227 * @throws MappingException if an error occurred during mapping
228 *
229 * @see de.bea.domingo.map.DMapper#map(DViewEntry, Object)
230 */
231 public final void map(final DViewEntry viewEntry, final Object object) throws MappingException {
232 DMapper mapper = mapperFactory.getDigestMapper(object.getClass());
233 mapper.map(viewEntry, object);
234 }
235
236 /***
237 * The title of a database.
238 *
239 * @return database title.
240 *
241 * @see DDatabase#getTitle()
242 */
243 public final String getTitle() {
244 return mDatabase.getTitle();
245 }
246 }