Clover.NET coverage report - Coverage

Coverage timestamp: viernes, 12 de agosto de 2005 12:53:38 PM

File Stats: LOC: 426   Methods: 16
NCLOC: 249 Classes: 2
 
Source File Conditionals Statements Methods TOTAL
Web\WebControls\AdminElement.cs 50,0 % 51,7 % 68,8 % 52,7 %
coverage coverage
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 System.Web.UI;
28   using System.Web.UI.WebControls;
29   using System.ComponentModel;
30   using ShowX.Web.WebBuilders.ItemTemplates;
31   using ShowX.Model;
32   using ShowX.Config;
33  
34   namespace ShowX.Web.WebControls
35   {
36   /// <summary>
37   /// Represent the operations available to do with an adminElement control.
38   /// </summary>
39   public enum ElemOps {
40  
41   /// <summary>
42   /// Add element operation.
43   /// </summary>
44   Add,
45   /// <summary>
46   /// Query element operation.
47   /// </summary>
48   Query
49   }
50  
51   /// <summary>
52   /// AdminElementEventArgs define the argument information needed when the
53   /// AdminElement event occurs.
54   /// </summary>
55   public class AdminElementEventArgs : EventArgs
56   {
57   object addedElement;
58  
59   /// <summary>
60   /// Constructor.
61   /// </summary>
62   /// <param name="addedElement">Element added by the control.</param>
63 3 public AdminElementEventArgs(object addedElement)
64   {
65 3 this.addedElement = addedElement;
66   }
67  
68   /// <summary>
69   /// Expose the added element instance.
70   /// </summary>
71 0 public object AddedElement
72   {
73   get { return this.addedElement; }
74   }
75   }
76  
77   /// <summary>
78   /// Delegate to represent event to raise when queried results are obtained.
79   /// </summary>
80   public delegate void OnQueryResultEventHandler(ArrayList elems);
81  
82   /// <summary>
83   /// Delegate to represent event to raise when a new element is added via the
84   /// AdminElement control.
85   /// </summary>
86   public delegate void OnAddElemEventHandler(object sender, AdminElementEventArgs e);
87  
88   /// <summary>
89   /// AdminElement allow to add or search elements of a certain type.
90   /// </summary>
91   [DefaultProperty("Text"),
92   ToolboxData("<{0}:AdminElement runat=server></{0}:AdminElement>")]
93   public class AdminElement : System.Web.UI.WebControls.DataList
94   {
95   TableItemStyle childControlStyle;
96  
97   object context;
98  
99   #region Private methods
100  
101 4 private void execAdd(DataListCommandEventArgs e)
102   {
103 4 ArrayList values = new ArrayList();
104 4 bool validated = true;
105  
106 4 System.Web.UI.WebControls.Table table = e.Item.Controls[0] as
107   System.Web.UI.WebControls.Table;
108  
109 4 foreach (TableRow row in table.Rows) {
110  
111 0 if (row.Cells.Count != 2) continue;
112  
113 32 if (row.Cells[1].Controls[0] is TextField) {
114  
115 12 TextField tf = row.Cells[1].Controls[0] as TextField;
116  
117 12 if (tf.Validate(XContext)) {
118 9 values.Add(tf.InputText);
119   }
120   else
121 3 validated = false;
122  
123   }
124 20 else if (row.Cells[1].Controls[0] is
125   ShowX.Web.WebControls.CheckBoxList) {
126  
127 4 ShowX.Web.WebControls.CheckBoxList cblist = row.Cells[1]
128   .Controls[0] as ShowX.Web.WebControls.CheckBoxList;
129 4 ArrayList sel = new ArrayList();
130  
131 4 foreach (ListItem li in cblist.Items) {
132  
133 12 if (li.Selected)
134 8 sel.Add(Configuration.Session.MethodCaller
135   .InvokeMethodGetXByID(cblist.CollectionMap
136   .ContainedType,XContext,int.Parse(li.Value)));
137   }
138  
139 4 if (cblist.Validate(XContext))
140 3 values.Add(sel);
141   else
142 1 validated = false;
143   }
144 16 else if (row.Cells[1].Controls[0] is ShowX.Web.WebControls
145   .Calendar) {
146  
147 4 ShowX.Web.WebControls.Calendar calendar = row.Cells[1].Controls[0]
148   as ShowX.Web.WebControls.Calendar;
149  
150 4 if (calendar.Validate(XContext))
151 3 values.Add(calendar.SelectedDate);
152   else
153 1 validated = false;
154   }
155 12 else if (row.Cells[1].Controls[0] is CheckBox)
156 4 values.Add((row.Cells[1].Controls[0] as CheckBox).Checked);
157 8 else if (row.Cells[1].Controls[0] is ChangePassword) {
158  
159 4 ChangePassword cp = row.Cells[1].Controls[0] as ChangePassword;
160  
161 4 if (cp.Validate(XContext))
162 3 values.Add(cp.Password);
163   else
164 1 validated = false;
165   }
166 4 else if ((row.Cells[1].Controls.Count == 2)
167   && (row.Cells[1].Controls[0] is DropDownList)) {
168   //This is a template column with a drop down list
169  
170 0 DropDownList ddl = row.Cells[1].Controls[0] as DropDownList;
171 0 Literal fkPropertyName = row.Cells[1].Controls[1] as Literal;
172  
173 0 int selfkId = int.Parse(ddl.SelectedValue);
174  
175 0 PropertyInfo pInfo = Configuration.Session.MappingHandler
176   .GetFK(Type,fkPropertyName.Text);
177  
178 0 object fkInstance = Configuration.Session.MethodCaller
179   .InvokeMethodGetXByID(pInfo.PropertyType,XContext,selfkId);
180  
181 0 values.Add(fkInstance);
182   }
183   }
184  
185 4 if (validated) {
186  
187 3 object added = Configuration.Session.MethodCaller
188   .InvokeMethodAddX(Type,XContext,values.ToArray());
189  
190 3 if (OnAddElement != null)
191 3 OnAddElement(this,new AdminElementEventArgs(added));
192   }
193   }
194  
195 0 private void execSearch(DataListCommandEventArgs e)
196   {
197   ArrayList matchElems = Configuration.Session.MethodCaller
198   .InvokeMethodGetAllX(this.Type,XContext);
199  
200   ArrayList DBFields = Configuration.Session.MappingHandler
201   .GetAllShowXProperties(this.Type);
202   int DBFieldsIndex = 0;
203  
204   System.Web.UI.WebControls.Table table = e.Item.Controls[0] as
205   System.Web.UI.WebControls.Table;
206  
207   foreach (TableRow row in table.Rows) {
208  
209   if (row.Cells.Count != 2) continue;
210   if (DBFieldsIndex >= DBFields.Count) break;
211  
212   // XAttr dbAttr = (DBFields[DBFieldsIndex] as PropertyInfo)
213   // .GetCustomAttributes(typeof(ShowX.XAttr),true)[0] as XAttr;
214  
215   IPropertyMap dbAttr = Configuration.Session.MappingHandler
216   .GetPropertyMap(DBFields[DBFieldsIndex] as PropertyInfo);
217  
218   while (!dbAttr.EditOnInsert && (DBFieldsIndex < DBFields.Count)) {
219  
220   DBFieldsIndex++;
221  
222   // dbAttr = (DBFields[DBFieldsIndex] as PropertyInfo)
223   // .GetCustomAttributes(typeof(ShowX.XAttr),true)[0] as XAttr;
224  
225   dbAttr = Configuration.Session.MappingHandler
226   .GetPropertyMap(DBFields[DBFieldsIndex] as PropertyInfo);
227   }
228  
229   if (row.Cells[1].Controls[0] is TextField) {
230  
231   TextField tf = row.Cells[1].Controls[0] as TextField;
232  
233   if (tf.InputText != "") {
234  
235   matchElems = filterMatching(matchElems,tf.InputText,
236   DBFields[DBFieldsIndex] as PropertyInfo);
237   }
238   }
239   else if (row.Cells[1].Controls[0] is CheckBox) {
240   //values.Add((row.Cells[1].Controls[0] as CheckBox).Checked);
241   matchElems = filterMatching(matchElems,
242   (row.Cells[1].Controls[0] as CheckBox).Checked,
243   DBFields[DBFieldsIndex] as PropertyInfo);
244  
245   }
246   else if (row.Cells[1].Controls[0] is ChangePassword) {
247  
248   ChangePassword cp = row.Cells[1].Controls[0] as ChangePassword;
249  
250   // if (cp.Validate())
251   // values.Add(cp.Password);
252   // else
253   // validated = false;
254   }
255   else if ((row.Cells[1].Controls.Count == 2)
256   && (row.Cells[1].Controls[0] is DropDownList)) {
257   //This is a template column with a drop down list
258  
259   DropDownList ddl = row.Cells[1].Controls[0] as DropDownList;
260   Literal fkPropertyName = row.Cells[1].Controls[1] as Literal;
261  
262   int selfkId = int.Parse(ddl.SelectedValue);
263  
264   PropertyInfo pInfo = Configuration.Session.MappingHandler
265   .GetFK(Type,fkPropertyName.Text);
266  
267   object fkInstance = Configuration.Session.MethodCaller
268   .InvokeMethodGetXByID(pInfo.PropertyType,XContext,selfkId);
269  
270   //values.Add(fkInstance);
271   }
272  
273   DBFieldsIndex++;
274   }
275  
276   if (matchElems.Count != 0) {
277  
278   OnQueryResult(matchElems);
279   }
280   }
281  
282 0 private ArrayList filterMatching(ArrayList matchElems,string textToMatch,
283   PropertyInfo pInfo) {
284   ArrayList result = new ArrayList();
285  
286   foreach(Object o in matchElems) {
287  
288   string propResult = o.GetType().InvokeMember(
289   pInfo.Name,BindingFlags.GetProperty,null,o,null).ToString();
290  
291   if (propResult == textToMatch)
292   result.Add(o);
293   }
294  
295   return result;
296   }
297  
298 0 private ArrayList filterMatching(ArrayList matchElems,bool condToMatch,
299   PropertyInfo pInfo) {
300   ArrayList result = new ArrayList();
301  
302   foreach(Object o in matchElems) {
303  
304   bool propResult = Convert.ToBoolean(o.GetType().InvokeMember(
305   pInfo.Name,BindingFlags.GetProperty,null,o,null));
306  
307   if (propResult == condToMatch)
308   result.Add(o);
309   }
310  
311   return result;
312   }
313  
314  
315   #endregion
316  
317   /// <summary>
318   /// Reflex whether the context in wich occurs the operations over this
319   /// control.
320   /// </summary>
321   public object XContext
322   {
323 60 get
324   {
325 60 return this.context;
326   }
327 0 set { this.context = value; }
328   }
329  
330   /// <summary>
331   /// Event to to raise when queried results are obtained.
332   /// </summary>
333   public event OnQueryResultEventHandler OnQueryResult;
334  
335   /// <summary>
336   /// Event to to raise when an element is added.
337   /// </summary>
338   public event OnAddElemEventHandler OnAddElement;
339  
340   /// <summary>
341   /// Reflex the ShowX type assigned to this Control
342   /// </summary>
343   [Bindable(true), Category("Business"), DefaultValue("")]
344   public Type Type
345   {
346 78 get { return (Type)(ViewState["ShowXObjectBrowserType"]); }
347 6 set
348   {
349 6 ViewState["ShowXObjectBrowserType"] = value;
350 6 this.ChildControlsCreated = false;
351 6 this.CreateChildControls();
352   }
353   }
354  
355   /// <summary>
356   /// Reflex the operation assigned to this Control.
357   /// </summary>
358   [Bindable(true), Category("Business"), DefaultValue("")]
359   public ElemOps ElemOps
360   {
361 25 get { return (ElemOps)(ViewState["ShowXObjectElemOps"]); }
362 6 set { ViewState["ShowXObjectElemOps"] = value; }
363   }
364  
365   /// <summary>
366   /// Style assigned to child controls.
367   /// </summary>
368   [Bindable(true), Category("Style"), Browsable(true)]
369   [PersistenceMode(PersistenceMode.InnerProperty),
370   TemplateContainer(typeof(AdminElement))]
371   public TableItemStyle ChildControlStyle
372   {
373 275 get {
374 275 if (childControlStyle == null)
375 0 childControlStyle = new TableItemStyle();
376  
377 275 return childControlStyle;
378   }
379 25 set {
380 25 childControlStyle = value;
381   }
382   }
383  
384   /// <summary>
385   /// CreateChildControls create the columns of the Control.
386   /// </summary>
387 25 protected override void CreateChildControls()
388   {
389 0 if (Type == null || ChildControlsCreated) return;
390  
391 25 this.EditItemIndex = 0;
392  
393 25 switch (ElemOps) {
394  
395   case ElemOps.Add:
396  
397 25 this.EditItemTemplate = new AddItemTemplate(Type,this);
398 25 break;
399  
400   case ElemOps.Query:
401  
402 0 this.EditItemTemplate = new SearchItemTemplate(Type,this);
403 0 break;
404   }
405  
406 25 ChildControlsCreated = true;
407  
408 25 ArrayList datasource = new ArrayList();
409 25 datasource.Add(Configuration.Session.MethodCaller.InvokeMethodNewX(Type));
410 25 this.DataSource = datasource;
411 25 this.DataBind();
412  
413 25 this.ItemCommand += new DataListCommandEventHandler(dl_ItemCommand);
414   }
415  
416 4 private void dl_ItemCommand(object source, DataListCommandEventArgs e)
417   {
418 4 switch(e.CommandName) {
419  
420 1 case "Add": execAdd(e); break;
421 0 case "Search": execSearch(e); break;
422   }
423   }
424   }
425   }
426