| |||||||||||||||||
Source File | Conditionals | Statements | Methods | TOTAL | |||||||||||||
Model\AttributeModel\AttributeMappingHandler.cs | 0,0 % | 0,0 % | 0,0 % | 0,0 % |
|
1 | #region Copyright | |
2 | /* | |
3 | ShowX. Maps business objects into presentation layer. | |
4 | Copyright (C) 2005 Jesus Diaz. | |
5 | ||
6 | This library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | This library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
17 | License along with this library; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | ||
20 | For project details see: http://showx.sourceforge.net | |
21 | */ | |
22 | #endregion | |
23 | ||
24 | using System; | |
25 | using System.Reflection; | |
26 | using System.Collections; | |
27 | using ShowX.Model; | |
28 | using log4net; | |
29 | ||
30 | namespace ShowX.Model.AttributeModel | |
31 | { | |
32 | /// <summary> | |
33 | /// Class AttributeMappingHandler provides useful methods to work with attribute | |
34 | /// mapping, by implementing the <see cref="IMappingHandler"/> interface. Brings | |
35 | /// a concrete implementation to work with property mapping, when we are using | |
36 | /// the attribute mapping model. | |
37 | /// </summary> | |
38 | public class AttributeMappingHandler : IMappingHandler | |
39 | { | |
40 | private static readonly ILog log = LogManager.GetLogger( | |
41 | typeof(AttributeMappingHandler)); | |
42 | ||
43 | #region Private methods | |
44 | ||
45 | /// <summary> | |
46 | /// Helper method, used to select a certain set of properties from a | |
47 | /// given type | |
48 | /// </summary> | |
49 | /// <param name="t">Type to query</param> | |
50 | /// <param name="attr">Attribute that decorate the properties searched</param> | |
51 | /// <returns>An arrayList with the propertie found, or empty if any property | |
52 | /// match query</returns> | |
53 | 0 | private PropertyInfo[] GetPropertiesWithAttribute(Type t, Type attr) |
54 | { | |
55 | ArrayList result = new ArrayList(); | |
56 | ||
57 | foreach (PropertyInfo pInfo in t.GetProperties()) { | |
58 | ||
59 | if (pInfo.GetCustomAttributes(attr,true).Length != 0) | |
60 | ||
61 | result.Add(pInfo); | |
62 | } | |
63 | ||
64 | return result.ToArray(typeof(PropertyInfo)) as PropertyInfo[]; | |
65 | } | |
66 | ||
67 | ||
68 | #endregion | |
69 | ||
70 | #region IMappingHandler methods | |
71 | ||
72 | /// <summary> | |
73 | /// Get the property marked as the Primary key of the object, for a given type. | |
74 | /// </summary> | |
75 | /// <param name="t">Type to get PK property.</param> | |
76 | /// <returns>A PropertyInfo instance, representing the PK property.</returns> | |
77 | 0 | public PropertyInfo GetPK(Type t) |
78 | { | |
79 | PropertyInfo pInfo = GetPropertiesWithAttribute(t,typeof(XPK))[0]; | |
80 | ||
81 | if (pInfo == null) { | |
82 | ||
83 | string errorMsg = "DBPK property not defined"; | |
84 | ||
85 | log.Fatal(errorMsg); | |
86 | throw new ModelException(errorMsg); | |
87 | } | |
88 | ||
89 | return pInfo; | |
90 | } | |
91 | ||
92 | /// <summary> | |
93 | /// Get a property marked as foreign key of the object, for a given type. | |
94 | /// </summary> | |
95 | /// <param name="t">Type to get FK property.</param> | |
96 | /// <param name="attrName">Name of the searched property.</param> | |
97 | /// <returns>A PropertyInfo instance, representing the FK property.</returns> | |
98 | 0 | public PropertyInfo GetFK(Type t, string attrName) |
99 | { | |
100 | PropertyInfo result = null; | |
101 | ||
102 | foreach (PropertyInfo pInfo in GetPropertiesWithAttribute | |
103 | (t,typeof(XFK))) { | |
104 | ||
105 | if (pInfo.Name == attrName) { | |
106 | ||
107 | result = pInfo; | |
108 | break; | |
109 | } | |
110 | } | |
111 | ||
112 | if (result == null) { | |
113 | ||
114 | string errorMsg = string.Format( | |
115 | "XFK property with name '{0}' not found",attrName ); | |
116 | ||
117 | log.Fatal(errorMsg); | |
118 | throw new ModelException(errorMsg); | |
119 | } | |
120 | ||
121 | return result; | |
122 | } | |
123 | ||
124 | /// <summary> | |
125 | /// Get an ShowX property of the object, for a given type. | |
126 | /// </summary> | |
127 | /// <param name="t">Type to get the ShowX property.</param> | |
128 | /// <param name="fieldName">Name of the searched property.</param> | |
129 | /// <returns>A PropertyInfo instance, representing the ShowX property. | |
130 | /// </returns> | |
131 | 0 | public PropertyInfo GetShowXPropertyByName(Type t, string fieldName) |
132 | { | |
133 | foreach (PropertyInfo pInfo in t.GetProperties()) { | |
134 | ||
135 | if (pInfo.Name == fieldName) | |
136 | return pInfo; | |
137 | } | |
138 | ||
139 | return null; | |
140 | } | |
141 | ||
142 | /// <summary> | |
143 | /// Retrieve the showX mapper property attached to this Property. Mapper | |
144 | /// property should be one of <see cref="XAttr"/>, | |
145 | /// <see cref="XPK"/> | |
146 | /// or <see cref="XFK"/>. | |
147 | /// </summary> | |
148 | /// <param name="pInfo">Property to query for query for mapper property.</param> | |
149 | /// <returns>The property map attached to this Property.</returns> | |
150 | 0 | public IPropertyMap GetPropertyMap (PropertyInfo pInfo) |
151 | { | |
152 | IPropertyMap result = null; | |
153 | ||
154 | object[] attrProps = pInfo.GetCustomAttributes(typeof(XAttr),true); | |
155 | ||
156 | if (attrProps.Length > 0) return attrProps[0] as XAttr; | |
157 | ||
158 | attrProps = pInfo.GetCustomAttributes(typeof(XFK),true); | |
159 | ||
160 | if (attrProps.Length > 0) return attrProps[0] as XFK; | |
161 | ||
162 | attrProps = pInfo.GetCustomAttributes(typeof(XPK),true); | |
163 | ||
164 | if (attrProps.Length > 0) return attrProps[0] as XPK; | |
165 | ||
166 | attrProps = pInfo.GetCustomAttributes(typeof(XPercent),true); | |
167 | ||
168 | if (attrProps.Length > 0) return attrProps[0] as XPercent; | |
169 | ||
170 | if (result == null) { | |
171 | ||
172 | string errorMsg = string.Format( | |
173 | "No property map associated to method {0} of type {1},", | |
174 | pInfo.Name,pInfo.DeclaringType.Name); | |
175 | ||
176 | log.Fatal(errorMsg); | |
177 | throw new ModelException(errorMsg); | |
178 | } | |
179 | ||
180 | return result; | |
181 | } | |
182 | ||
183 | /// <summary> | |
184 | /// Get all ShowX Properties marked of the object, for a given type (not | |
185 | /// counting primary key property). | |
186 | /// </summary> | |
187 | /// <param name="t">Type to get ShowX properties.</param> | |
188 | /// <returns>An ArrayList of PropertyInfo instances.</returns> | |
189 | 0 | public ArrayList GetAllShowXProperties (Type t) |
190 | { | |
191 | ArrayList result = new ArrayList(); | |
192 | ||
193 | foreach (PropertyInfo pInfo in t.GetProperties()) { | |
194 | ||
195 | if (((pInfo.GetCustomAttributes(typeof(XAttr),true).Length != 0) || | |
196 | ((pInfo.GetCustomAttributes(typeof(XPercent),true).Length != 0))) | |
197 | && (pInfo.GetCustomAttributes(typeof(XPK),true).Length == 0)) | |
198 | ||
199 | result.Add(pInfo); | |
200 | } | |
201 | ||
202 | return result; | |
203 | } | |
204 | ||
205 | /// <summary> | |
206 | /// Get the validators associated to a certain property. | |
207 | /// </summary> | |
208 | /// <param name="pInfo">PropertyInfo to get validators.</param> | |
209 | /// <returns>An ArrayList instance, populated with instance of | |
210 | /// XValidate class.</returns> | |
211 | 0 | public ArrayList GetValidators(PropertyInfo pInfo) |
212 | { | |
213 | XValidate[] validators = pInfo | |
214 | .GetCustomAttributes(typeof(XValidate),true) as XValidate[]; | |
215 | ||
216 | ArrayList result = new ArrayList(validators); | |
217 | ||
218 | foreach(IValidationMap val in validators) { | |
219 | ||
220 | val.PropertyName = pInfo.Name; | |
221 | val.Type = pInfo.DeclaringType; | |
222 | } | |
223 | ||
224 | return result; | |
225 | } | |
226 | ||
227 | /// <summary> | |
228 | /// Get the views collection for a given type (Mock). | |
229 | /// <remarks>Views are not supported by the attribute model.</remarks> | |
230 | /// </summary> | |
231 | /// <param name="t">Type to get ShowX view collection.</param> | |
232 | /// <returns>A view collection instance.</returns> | |
233 | 0 | public IViewCollection GetViews(Type t) |
234 | { | |
235 | return null; | |
236 | } | |
237 | ||
238 | #endregion | |
239 | } | |
240 | } | |
241 |
|