-
Notifications
You must be signed in to change notification settings - Fork 2
/
DictionaryDao.cs
287 lines (276 loc) · 14.7 KB
/
DictionaryDao.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) 2004-2010 Azavea, Inc.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Xml;
using Azavea.Open.Common.Collections;
using Azavea.Open.DAO.Criteria;
using Azavea.Open.Common;
using Azavea.Open.DAO.Exceptions;
namespace Azavea.Open.DAO
{
/// <summary>
/// A FastDAO that returns Dictionaries instead of objects. This is a more
/// basic DAO that is useful in cases where you don't actually care much about
/// the object (I.E. are just going to drop it into XML or JSON anyway or
/// something like that).
/// </summary>
public class DictionaryDao : FastDAO<CheckedDictionary<string, object>>
{
/// <summary>
/// This allows you to specify the config name and the section in the config file
/// used to get the database config info.
/// </summary>
/// <param name="mappedType">The type name we're mapping. Since this class doesn't
/// actually instantiate it, it can just be the key to the
/// config file and doesn't have to be a real class.</param>
/// <param name="configName">Name used to get the configuration.</param>
/// <param name="sectionName">Name of the section within the config file.</param>
public DictionaryDao(string mappedType, string configName, string sectionName)
: this(mappedType, Config.GetConfig(configName), sectionName, null)
{
}
/// <summary>
/// This allows you to specify the config name and the section in the config file
/// used to get the database config info.
/// </summary>
/// <param name="mappedType">The type name we're mapping. Since this class doesn't
/// actually instantiate it, it can just be the key to the
/// config file and doesn't have to be a real class.</param>
/// <param name="configName">Name used to get the configuration.</param>
/// <param name="sectionName">Name of the section within the config file.</param>
/// <param name="decryptionDelegate">The method to call to decrypt passwords or
/// other encrypted connection info. May be null.</param>
public DictionaryDao(string mappedType, string configName, string sectionName,
ConnectionInfoDecryptionDelegate decryptionDelegate)
: this(mappedType, Config.GetConfig(configName), sectionName, decryptionDelegate)
{
}
/// <summary>
/// This allows you to give the config object and the section in the config
/// used to get the database config info.
/// </summary>
/// <param name="mappedType">The type name we're mapping. Since this class doesn't
/// actually instantiate it, it can just be the key to the
/// config file and doesn't have to be a real class.</param>
/// <param name="config">Configuration object loaded somewhere else.</param>
/// <param name="sectionName">Name of the section within the config.</param>
public DictionaryDao(string mappedType, Config config, string sectionName) :
this(mappedType, config, sectionName, null)
{
}
/// <summary>
/// This allows you to give the config object and the section in the config
/// used to get the database config info.
/// </summary>
/// <param name="mappedType">The type name we're mapping. Since this class doesn't
/// actually instantiate it, it can just be the key to the
/// config file and doesn't have to be a real class.</param>
/// <param name="config">Configuration object loaded somewhere else.</param>
/// <param name="sectionName">Name of the section within the config.</param>
/// <param name="decryptionDelegate">The method to call to decrypt passwords or
/// other encrypted connection info. May be null.</param>
public DictionaryDao(string mappedType, Config config, string sectionName,
ConnectionInfoDecryptionDelegate decryptionDelegate) :
this(mappedType, ConnectionDescriptor.LoadFromConfig(config, sectionName, decryptionDelegate),
config.GetParameterWithSubstitution(sectionName, "MAPPING", false))
{
}
/// <summary>
/// This allows you to specify the data source connection and the mapping file.
/// </summary>
/// <param name="mappedType">The type name we're mapping. Since this class doesn't
/// actually instantiate it, it can just be the key to the
/// config file and doesn't have to be a real class.</param>
/// <param name="connDesc">Data source Connection information.</param>
/// <param name="mappingFileName">Filename (with path) to the mapping file.</param>
public DictionaryDao(string mappedType, IConnectionDescriptor connDesc, string mappingFileName) :
this(connDesc, ParseHibernateConfig(mappedType, mappingFileName))
{
}
/// <summary>
/// If you already have the data source connection and the mapping file, you may use
/// this constructor.
/// </summary>
/// <param name="connDesc">Data source Connection information.</param>
/// <param name="mapping">ClassMapping describing the class to be mapped and
/// the table to map it to.</param>
public DictionaryDao(IConnectionDescriptor connDesc, ClassMapping mapping)
: base(connDesc, mapping)
{
}
/// <summary>
/// This version of this method looks for a matching string, not a matching type.
/// TODO: Refactor FastDAO to look for the object type name rather than trying to
/// parse the type out of the mapping file. Then we don't need the other
/// version, and this can be moved to FastDAO and made protected.
/// </summary>
/// <param name="desiredType">The type we're loading a mapping for.</param>
/// <param name="fileName">XML File containing an NHibernate configuration.</param>
/// <returns>The class mapping for the desired type. If unable to find it, an
/// exception is thrown, so you may safely assume this will never return
/// null.</returns>
private static ClassMapping ParseHibernateConfig(string desiredType, string fileName)
{
ClassMapping retVal = null;
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList list = doc.GetElementsByTagName("class");
foreach (XmlNode node in list)
{
string thisNodeType = node.Attributes["name"].Value;
if (desiredType.Equals(thisNodeType))
{
// NOTE: The boolean parameters here are not compatible with FastDAO.
retVal = new ClassMapping(node, false, true);
break;
}
}
if (retVal == null)
{
throw new BadDaoConfigurationException("Type " + desiredType +
" does not appear to be mapped in file " + fileName);
}
return retVal;
}
/// <summary>
/// This method takes property/field values from the data object
/// and puts them into the dictionary 'putEmHere', keyed by the column names.
/// </summary>
/// <param name="dataObject">An implementation-specific data object.</param>
/// <param name="putEmHere">A dictionary to insert values into. Values will be objects
/// (which may be null) keyed by column names. The values
/// will be cast to the appropriate type for the db, if necessary.</param>
/// <param name="classMap">The class map for the object.</param>
/// <param name="idFields">If true, we copy the ID fields.</param>
/// <param name="dataFields">If true, we copy the non-ID fields.</param>
protected override void GetFieldValues(CheckedDictionary<string, object> dataObject,
IDictionary<string, object> putEmHere, ClassMapping classMap, bool idFields, bool dataFields)
{
IDictionary<string, string> fieldMapping;
if (idFields)
{
if (dataFields)
{
fieldMapping = classMap.AllDataColsByObjAttrs;
}
else
{
fieldMapping = classMap.IdDataColsByObjAttrs;
}
}
else
{
if (dataFields)
{
fieldMapping = classMap.NonIdDataColsByObjAttrs;
}
else
{
throw new ArgumentNullException("dataFields", "GetFieldValues called for no fields.");
}
}
foreach (KeyValuePair<string, string> kvp in fieldMapping)
{
string propertyName = kvp.Key;
string columnName = kvp.Value;
// The value not being present is also treated as null.
object colValue = dataObject.ContainsKey(propertyName) ? dataObject[propertyName] : null;
if (colValue != null)
{
// All attr names are in the collection, but they have null values
// if a type wasn't specified in the mapping.
Type desiredType = _classMap.DataColTypesByObjAttr[propertyName];
if (desiredType != null)
{
colValue = _dataAccessLayer.CoerceType(desiredType, colValue);
}
}
putEmHere[columnName] = colValue;
}
}
/// <summary>
/// This object gets a value off the data object based on the
/// name of the field/property.
/// </summary>
/// <param name="dataObject">The object to get a value off of.</param>
/// <param name="fieldName">The name of the field/property to get the value of.</param>
/// <returns>The value.</returns>
public override object GetValueFromObject(CheckedDictionary<string, object> dataObject,
string fieldName)
{
return dataObject[fieldName];
}
/// <summary>
/// Given an object and a (data source) column name
/// set the given memberValue onto the object's property.
///
/// Since the DictionaryDAO is probably used only for reading,
/// it will tolerate the same column mapped to more than one property
/// (normal FastDAO will not work if you try that).
/// </summary>
/// <param name="dataObj">Object to set the value upon.</param>
/// <param name="classMap">Object's mapping.</param>
/// <param name="colName">Name of the column we got the value from.</param>
/// <param name="memberValue">Value to set on the field or property.</param>
protected override void SetValueOnObject(CheckedDictionary<string, object> dataObj,
ClassMapping classMap, string colName, object memberValue)
{
foreach (KeyValuePair<string, string> kvp in classMap.AllDataColsByObjAttrs)
{
if (kvp.Value.Equals(colName))
{
// Don't coerce nulls.
if (memberValue != null)
{
// We'll go ahead and coerce the type, although
// the db "should" give it to us as the mapped type.
// Not all data sources do however, so this is the best
// we can do.
Type desiredType = classMap.DataColTypesByObjAttr[classMap.AllObjAttrsByDataCol[colName]];
if (desiredType != null)
{
memberValue = _dataAccessLayer.CoerceType(desiredType, memberValue);
}
}
dataObj[kvp.Key] = memberValue;
}
}
}
/// <summary>
/// Populates a criteria with a bunch of EqualExpressions, one for each
/// ID on the data object.
/// </summary>
/// <param name="dataObject">The object we're concerned with.</param>
/// <param name="crit">Criteria to add equals expressions to.</param>
/// <param name="classMap">The mapping of the object to the data source.</param>
protected override void PopulateIDCriteria(CheckedDictionary<string, object> dataObject,
DaoCriteria crit, ClassMapping classMap)
{
foreach (KeyValuePair<string, string> kvp in classMap.IdDataColsByObjAttrs)
{
crit.Expressions.Add(new EqualExpression(kvp.Key, dataObject[kvp.Key]));
}
}
}
}