Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.ActiveRecordSupport / Scaffold / Utils / CommonOperationUtils.cs
blobd734c992bed9449e7247bf4732cf38c3416a47c4
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.ActiveRecordSupport.Scaffold
17 using System;
18 using System.Collections.Generic;
19 using System.Reflection;
20 using System.Collections;
21 using Castle.ActiveRecord;
22 using Castle.Components.Binder;
23 using Castle.MonoRail.Framework;
25 public abstract class CommonOperationUtils
27 internal static object[] FindAll(Type type)
29 return FindAll(type, null);
32 internal static object[] FindAll(Type type, String customWhere)
34 MethodInfo findAll = type.GetMethod("FindAll",
35 BindingFlags.Static | BindingFlags.Public, null, new Type[0], null);
37 object[] items = null;
39 if (findAll != null)
41 items = (object[])findAll.Invoke(null, null);
43 else
45 IList list = ActiveRecordMediator.FindAll(type);
47 items = new object[list.Count];
49 list.CopyTo(items, 0);
52 return items;
55 internal static void SaveInstance(object instance, IController controller,
56 ArrayList errors, ref IDictionary prop2Validation, bool create)
58 bool isValid = true;
60 Type genType = typeof(ActiveRecordValidationBase<>).MakeGenericType(instance.GetType());
62 if (genType.IsAssignableFrom(instance.GetType()))
64 // System.Type.EmptyTypes added because otherwise you get an AmbiguousMatchException for
65 // {Boolean IsValid()} and {Boolean IsValid(Castle.Components.Validator.RunWhen)}
66 MethodInfo isValidMethod = instance.GetType().GetMethod("IsValid", System.Type.EmptyTypes);
68 isValid = (bool)isValidMethod.Invoke(instance, null);
70 if (!isValid)
72 MethodInfo getValidationErrorMessages = instance.GetType().GetMethod("get_ValidationErrorMessages");
73 MethodInfo getPropertiesValidationErrorMessage = instance.GetType().GetMethod("get_PropertiesValidationErrorMessage");
75 errors.AddRange((ICollection)getValidationErrorMessages.Invoke(instance, null));
76 prop2Validation = (IDictionary)getPropertiesValidationErrorMessage.Invoke(instance, null);
79 else if (instance is ActiveRecordValidationBase)
81 ActiveRecordValidationBase instanceBase = instance as ActiveRecordValidationBase;
83 isValid = instanceBase.IsValid();
85 if (!isValid)
87 errors.AddRange(instanceBase.ValidationErrorMessages);
88 prop2Validation = instanceBase.PropertiesValidationErrorMessage;
92 if (isValid)
94 if (create)
96 ActiveRecordMediator.Create(instance);
98 else
100 ActiveRecordMediator.Update(instance);
105 internal static object ReadPkFromParams(IRequest request, PropertyInfo keyProperty)
107 String id = request.Params["id"];
109 if (id == null)
111 throw new ScaffoldException("Can't edit without the proper id");
114 bool conversionSuceeded;
116 return new DefaultConverter().Convert(keyProperty.PropertyType, id, out conversionSuceeded);
119 internal static object ReadPkFromParams(IDictionary<string, object> customParams, IRequest request, PropertyInfo keyProperty)
121 if (customParams.ContainsKey("id"))
123 bool conversionSuceeded;
125 object id = customParams["id"];
127 return new DefaultConverter().Convert(keyProperty.PropertyType, id, out conversionSuceeded);
130 return ReadPkFromParams(request, keyProperty);