| |||||||||||||||||
Source File | Conditionals | Statements | Methods | TOTAL | |||||||||||||
Model\XmlModel\XmlMappingHandler.cs | 100,0 % | 81,3 % | 87,5 % | 86,7 % |
|
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.Collections; | |
26 | using System.Reflection; | |
27 | using ShowX.Config; | |
28 | using ShowX.Session; | |
29 | ||
30 | namespace ShowX.Model.XmlModel | |
31 | { | |
32 | /// <summary> | |
33 | /// Class XmlMappingHandler 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 XML file mapping model. | |
37 | /// </summary> | |
38 | public class XmlMappingHandler : IMappingHandler | |
39 | { | |
40 | /// <summary> | |
41 | /// Currens Xml session runtime. | |
42 | /// </summary> | |
43 | protected XmlSession session = null; | |
44 | ||
45 | /// <summary> | |
46 | /// Expose the Xml session runtime. | |
47 | /// </summary> | |
48 | private XmlSession Session | |
49 | { | |
50 | 5191 | get |
51 | { | |
52 | 5191 | if (session == null) session = Configuration.Session as XmlSession; |
53 | ||
54 | 5191 | return session; |
55 | } | |
56 | } | |
57 | ||
58 | #region IMappingHandler Members | |
59 | ||
60 | /// <summary> | |
61 | /// Get the property marked as the Primary key of the object, for a given type. | |
62 | /// </summary> | |
63 | /// <param name="t">Type to get PK property.</param> | |
64 | /// <returns>A PropertyInfo instance, representing the PK property.</returns> | |
65 | 190 | public PropertyInfo GetPK(Type t) |
66 | { | |
67 | 190 | return Session[t].IdentifierMap.PropertyInfo; |
68 | } | |
69 | ||
70 | /// <summary> | |
71 | /// Get a property marked as foreign key of the object, for a given type. | |
72 | /// </summary> | |
73 | /// <param name="t">Type to get FK property.</param> | |
74 | /// <param name="attrName">Name of the searched property.</param> | |
75 | /// <returns>A PropertyInfo instance, representing the FK property.</returns> | |
76 | 0 | public PropertyInfo GetFK(Type t, string attrName) |
77 | { | |
78 | object fk = Session[t].PropertyMaps[attrName as string]; | |
79 | ||
80 | return ((fk != null) && (fk is IForeignIdentifierMap)) | |
81 | ? (fk as XmlForeignIdentifierMap).PropertyInfo | |
82 | : null; | |
83 | } | |
84 | ||
85 | /// <summary> | |
86 | /// Get an ShowX property of the object, for a given type. | |
87 | /// </summary> | |
88 | /// <param name="t">Type to get the ShowX property.</param> | |
89 | /// <param name="fieldName">Name of the searched property.</param> | |
90 | /// <returns>A PropertyInfo instance, representing the ShowX property. | |
91 | /// </returns> | |
92 | 38 | public PropertyInfo GetShowXPropertyByName(Type t, string fieldName) |
93 | { | |
94 | 38 | return (Session[t].PropertyMaps[fieldName as string] |
95 | as XmlPropertyMap).PropertyInfo; | |
96 | } | |
97 | ||
98 | /// <summary> | |
99 | /// Retrieve the showX mapper property attached to this Property. | |
100 | /// </summary> | |
101 | /// <param name="pInfo">Property to query for query for mapper property.</param> | |
102 | /// <returns>The property map attached to this Property.</returns> | |
103 | 2434 | public IPropertyMap GetPropertyMap(PropertyInfo pInfo) |
104 | { | |
105 | //Especial treatment if we are looking for identifier propery | |
106 | 2434 | if(pInfo.Equals(Session[pInfo.ReflectedType].IdentifierMap.PropertyInfo)) |
107 | 320 | return Session[pInfo.ReflectedType].IdentifierMap; |
108 | ||
109 | //Search in the other registered properties. | |
110 | 2114 | foreach (XmlPropertyMap pm in Session[pInfo.ReflectedType] |
111 | .PropertyMaps.Values) { | |
112 | ||
113 | 8393 | if (pInfo.Equals(pm.PropertyInfo)) |
114 | 2114 | return pm; |
115 | } | |
116 | ||
117 | 0 | return null; |
118 | } | |
119 | ||
120 | /// <summary> | |
121 | /// Get all ShowX Properties marked of the object, for a given type (not | |
122 | /// counting primary key property). | |
123 | /// </summary> | |
124 | /// <param name="t">Type to get ShowX properties.</param> | |
125 | /// <returns>An ArrayList of PropertyInfo instances.</returns> | |
126 | 60 | public ArrayList GetAllShowXProperties(Type t) |
127 | { | |
128 | 60 | return Session[t].PropertyList; |
129 | } | |
130 | ||
131 | /// <summary> | |
132 | /// Get the validators associated to a certain property. | |
133 | /// </summary> | |
134 | /// <param name="pInfo">PropertyInfo to get validators.</param> | |
135 | /// <returns>An ArrayList instance, populated with instance of | |
136 | /// XValidate class.</returns> | |
137 | 141 | public ArrayList GetValidators(PropertyInfo pInfo) |
138 | { | |
139 | 141 | return (GetPropertyMap(pInfo) as XmlPropertyMap).Validators; |
140 | } | |
141 | ||
142 | /// <summary> | |
143 | /// Get the views collection for a given type. | |
144 | /// </summary> | |
145 | /// <param name="t">Type to get ShowX view collection.</param> | |
146 | /// <returns>A view collection instance.</returns> | |
147 | 35 | public IViewCollection GetViews(Type t) |
148 | { | |
149 | 35 | return Session[t].Views; |
150 | } | |
151 | ||
152 | #endregion | |
153 | } | |
154 | } | |
155 |
|