public virtual string Dump() { var sb = new StringBuilder(); ActiveRecordModel model = ActiveRecordModel.GetModel(typeof(T)); sb.Append(model.Type.Name); sb.Append(" { "); if (model.PrimaryKey != null) { sb.AppendFormat("{0} = '{1}'; ", model.PrimaryKey.Property.Name, model.PrimaryKey.Property.GetValue(this, null)); } DumpProperties(sb, this, null, model.Properties); foreach (NestedModel nm in model.Components) { object n = nm.Property.GetValue(this, null); if (n == null) { sb.AppendFormat("{0}.* = (null); ", nm.Property.Name); } else { DumpProperties(sb, n, nm, nm.Model.Properties); } } sb.Append("}"); return(sb.ToString()); }
public void LazyForClass() { ActiveRecordModelBuilder builder = new ActiveRecordModelBuilder(); builder.Create(typeof(ClassWithAnyAttribute)); ActiveRecordModel model = builder.Create(typeof(LazyClass)); Assert.IsNotNull(model); SemanticVerifierVisitor semanticVisitor = new SemanticVerifierVisitor(builder.Models); semanticVisitor.VisitNode(model); XmlGenerationVisitor xmlVisitor = new XmlGenerationVisitor(); xmlVisitor.CreateXml(model); String xml = xmlVisitor.Xml; String expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + "<hibernate-mapping xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-mapping-2.0\">\r\n" + " <class name=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.LazyClass, Castle.ActiveRecord.Framework.Internal.Tests\" table=\"LazyClass\" lazy=\"true\">\r\n" + " <id name=\"Id\" access=\"property\" column=\"Id\" type=\"Int32\" unsaved-value=\"0\">\r\n" + " <generator class=\"native\">\r\n" + " </generator>\r\n" + " </id>\r\n" + " <many-to-one name=\"Clazz\" access=\"property\" class=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.ClassWithAnyAttribute, Castle.ActiveRecord.Framework.Internal.Tests\" column=\"Clazz\" />\r\n" + " </class>\r\n" + "</hibernate-mapping>\r\n"; Assert.AreEqual(expected, xml); }
private IList ObtainListableProperties(ActiveRecordModel model) { var properties = new ArrayList(); ObtainPKProperty(); if (model.Parent != null) { properties.AddRange(ObtainListableProperties(model.Parent)); } foreach (var propModel in model.Properties) { if (IsNotSupported(propModel.Property.PropertyType)) { continue; } properties.Add(propModel.Property); } foreach (var prop in model.NotMappedProperties) { if (IsNotSupported(prop.PropertyType)) { continue; } properties.Add(prop); } return(properties); }
public void JoinedSubClassUse() { ActiveRecordModelBuilder builder = new ActiveRecordModelBuilder(); builder.Create(typeof(ClassJoinedSubClassA)); ActiveRecordModel model = builder.Create(typeof(ClassJoinedSubClassParent)); Assert.IsNotNull(model); String xml = Process(builder, model); String expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + "<hibernate-mapping xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-mapping-2.0\">\r\n" + " <class name=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.ClassJoinedSubClassParent, Castle.ActiveRecord.Framework.Internal.Tests\" table=\"disctable\" >\r\n" + " <id name=\"Id\" access=\"property\" column=\"Id\" type=\"Int32\" unsaved-value=\"0\">\r\n" + " <generator class=\"native\">\r\n" + " </generator>\r\n" + " </id>\r\n" + " <property name=\"Name\" access=\"property\" column=\"Name\" type=\"String\" />\r\n" + " <joined-subclass name=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.ClassJoinedSubClassA, Castle.ActiveRecord.Framework.Internal.Tests\" table=\"disctablea\" >\r\n" + " <key column=\"AId\" />\r\n" + " <property name=\"Age\" access=\"property\" column=\"Age\" type=\"Int32\" />\r\n" + " </joined-subclass>\r\n" + " </class>\r\n" + "</hibernate-mapping>\r\n"; Assert.AreEqual(expected, xml); }
public String LinkToEdit(ActiveRecordModel model, bool useModelName, String text, object key, IDictionary attributes) { var targetAction = "edit" + (useModelName ? model.Type.Name : ""); return(LinkTo(targetAction, key, text, attributes)); }
public void HasManyInfering() { ActiveRecordModelBuilder builder = new ActiveRecordModelBuilder(); ActiveRecordModel model = builder.Create(typeof(Category)); Assert.IsNotNull(model); String xml = Process(builder, model); String expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + "<hibernate-mapping xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-mapping-2.0\">\r\n" + " <class name=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.Category, Castle.ActiveRecord.Framework.Internal.Tests\" table=\"Category\" >\r\n" + " <id name=\"Id\" access=\"property\" column=\"Id\" type=\"Int32\" unsaved-value=\"0\">\r\n" + " <generator class=\"native\">\r\n" + " </generator>\r\n" + " </id>\r\n" + " <property name=\"Name\" access=\"property\" column=\"Name\" type=\"String\" />\r\n" + " <many-to-one name=\"Parent\" access=\"property\" class=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.Category, Castle.ActiveRecord.Framework.Internal.Tests\" column=\"parent_id\" />\r\n" + " <bag name=\"SubCategories\" access=\"property\" table=\"Category\" >\r\n" + " <key column=\"parent_id\" />\r\n" + " <one-to-many class=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.Category, Castle.ActiveRecord.Framework.Internal.Tests\" />\r\n" + " </bag>\r\n" + " </class>\r\n" + "</hibernate-mapping>\r\n"; Assert.AreEqual(expected, xml); }
private object ObtainPrimaryKeyValue(ActiveRecordModel model, CompositeNode node, String prefix, out PrimaryKeyModel pkModel) { pkModel = ObtainPrimaryKey(model); var pkPropName = pkModel.Property.Name; var idNode = node.GetChildNode(pkPropName); if (idNode == null) { return(null); } if (idNode != null && idNode.NodeType != NodeType.Leaf) { throw new BindingException("Expecting leaf node to contain id for ActiveRecord class. " + "Prefix: {0} PK Property Name: {1}", prefix, pkPropName); } var lNode = (LeafNode)idNode; if (lNode == null) { throw new BindingException("ARDataBinder autoload failed as element {0} " + "doesn't have a primary key {1} value", prefix, pkPropName); } bool conversionSuc; return(Converter.Convert(pkModel.Property.PropertyType, lNode.ValueType, lNode.Value, out conversionSuc)); }
public void HasManyWithExplicitInfo() { ActiveRecordModelBuilder builder = new ActiveRecordModelBuilder(); ActiveRecordModel model = builder.Create(typeof(HasManyClassA)); Assert.IsNotNull(model); String xml = Process(builder, model); String expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + "<hibernate-mapping xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-mapping-2.0\">\r\n" + " <class name=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.HasManyClassA, Castle.ActiveRecord.Framework.Internal.Tests\" table=\"HasManyClassA\" >\r\n" + " <id name=\"Id\" access=\"property\" column=\"Id\" type=\"Int32\" unsaved-value=\"0\">\r\n" + " <generator class=\"native\">\r\n" + " </generator>\r\n" + " </id>\r\n" + " <bag name=\"Items\" access=\"property\" table=\"ClassATable\" inverse=\"true\" >\r\n" + " <key column=\"keycol\" />\r\n" + " <one-to-many class=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.ClassA, Castle.ActiveRecord.Framework.Internal.Tests\" />\r\n" + " </bag>\r\n" + " </class>\r\n" + "</hibernate-mapping>\r\n"; Assert.AreEqual(expected, xml); }
public void SimpleClassWithMappedFieldAndNonDefaultAccess() { ActiveRecordModelBuilder builder = new ActiveRecordModelBuilder(); ActiveRecordModel model = builder.Create(typeof(ClassWithMappedField)); Assert.IsNotNull(model); SemanticVerifierVisitor semanticVisitor = new SemanticVerifierVisitor(builder.Models); semanticVisitor.VisitNode(model); XmlGenerationVisitor xmlVisitor = new XmlGenerationVisitor(); xmlVisitor.CreateXml(model); String xml = xmlVisitor.Xml; String expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + "<hibernate-mapping xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-mapping-2.0\">\r\n" + " <class name=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.ClassWithMappedField, Castle.ActiveRecord.Framework.Internal.Tests\" table=\"ClassWithMappedField\" >\r\n" + " <id name=\"Id\" access=\"nosetter.camelcase-underscore\" column=\"Id\" type=\"Int32\" unsaved-value=\"0\">\r\n" + " <generator class=\"native\">\r\n" + " </generator>\r\n" + " </id>\r\n" + " <property name=\"name1\" access=\"field\" column=\"MyCustomName\" type=\"String\" />\r\n" + " <property name=\"Value\" access=\"CustomAccess\" column=\"Value\" type=\"Int32\" />\r\n" + " </class>\r\n" + "</hibernate-mapping>\r\n"; Assert.AreEqual(expected, xml); }
public String CreateControl(ActiveRecordModel model, PropertyModel propertyModel, object instance) { stringBuilder.Length = 0; PropertyInfo prop = propertyModel.Property; // Skip non standard properties if (!prop.CanWrite || !prop.CanRead) { return(String.Empty); } // Skip indexers if (prop.GetIndexParameters().Length != 0) { return(String.Empty); } object value = null; if (instance != null) { if (model.IsNestedType) { instance = model2nestedInstance[model]; } if (instance != null) { value = prop.GetValue(instance, null); } } String propName = null; if (model.IsNestedType) { propName = String.Format("{0}.{1}", model.Type.Name, prop.Name); } else { propName = prop.Name; } if (prop.PropertyType == typeof(DateTime)) { stringBuilder.Append(LabelFor(propName + "day", prop.Name + ": ")); } else { stringBuilder.Append(LabelFor(propName, prop.Name + ": ")); } PropertyAttribute propAtt = propertyModel.PropertyAtt; RenderAppropriateControl(model, prop.PropertyType, propName, prop, value, propAtt.Unique, propAtt.NotNull, propAtt.ColumnType, propAtt.Length); return(stringBuilder.ToString()); }
public void ImportsGeneration() { ActiveRecordModelBuilder builder = new ActiveRecordModelBuilder(); ActiveRecordModel model = builder.Create(typeof(ImportClass)); Assert.IsNotNull(model); string xml = Process(builder, model); string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + "<hibernate-mapping auto-import=\"true\" default-lazy=\"false\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-mapping-2.2\">\r\n" + " <import class=\"Castle.ActiveRecord.Framework.Internal.Tests.ImportClassRow, Castle.ActiveRecord.Framework.Internal.Tests\" rename=\"ImportClassRow\" />\r\n" + " <class name=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.ImportClass, Castle.ActiveRecord.Framework.Internal.Tests\" table=\"ImportClass\" lazy=\"false\">\r\n" + " <id name=\"Id\" access=\"property\" column=\"Id\" type=\"Int32\" unsaved-value=\"0\">\r\n" + " <generator class=\"native\">\r\n" + " </generator>\r\n" + " </id>\r\n" + " <property name=\"Name\" access=\"property\" type=\"String\">\r\n" + " <column name=\"Name\" />\r\n" + " </property>\r\n" + " </class>\r\n" + "</hibernate-mapping>\r\n"; Assert.AreEqual(expected, xml); }
/// <summary> /// Maps the ActiveRecord class to the table information from Aptify /// </summary> /// <param name="model">ActiveRecord class model</param> public override void VisitModel(ActiveRecordModel model) { if (model.ActiveRecordAtt == null) { return; } // Find the Aptify entity that maps to this ActiveRecord model AptifyEntityMetadata entity = this.entities.FirstOrDefault( e => e.Tables.Any(t => t.Name == model.ActiveRecordAtt.Table)); if (entity == null) { // The mapping of the table name is incorrect throw new InvalidOperationException( string.Format("'{0}' is mapped to table '{1}', which is not in an entity in Aptify", model.Type.FullName, model.ActiveRecordAtt.Table)); } // Find the table within the entity AptifyTableMetadata table = entity.Tables.First(t => t.Name == model.ActiveRecordAtt.Table); // Insert this mapping into the mappings this.mappings.Add(model.Type, table); base.VisitModel(model); }
public String CreateControl(ActiveRecordModel model, String prefix, PropertyInfo prop, object instance) { stringBuilder.Length = 0; // Skip non standard properties if (!prop.CanWrite || !prop.CanRead) { return(String.Empty); } // Skip indexers if (prop.GetIndexParameters().Length != 0) { return(String.Empty); } var propName = CreatePropName(model, prefix, prop.Name); if (prop.PropertyType == typeof(DateTime)) { stringBuilder.Append(LabelFor(propName + "day", TextHelper.PascalCaseToWord(prop.Name) + ": ")); } else { stringBuilder.Append(LabelFor(propName, TextHelper.PascalCaseToWord(prop.Name) + ": ")); } RenderAppropriateControl(model, prop.PropertyType, propName, prop, null, false, false, null, 0); return(stringBuilder.ToString()); }
private void RenderAppropriateControl(ActiveRecordModel model, Type propType, string propName, PropertyInfo property, object value, bool unique, bool notNull, String columnType, int length) { IDictionary htmlAttributes = new Hashtable(); htmlAttributes["validators"] = PopulateCustomValidators( notNull ? "blank" : "bok", propType, property, model.Validators); if (propType == typeof(String)) { if (String.Compare("stringclob", columnType, true) == 0) { stringBuilder.AppendFormat(TextArea(propName, 30, 3, (String)value)); } else if (length != 0) { stringBuilder.AppendFormat(InputText(propName, (String)value, length, length, htmlAttributes)); } else { stringBuilder.AppendFormat(InputText(propName, (String)value, htmlAttributes)); } } else if (propType == typeof(Int16) || propType == typeof(Int32) || propType == typeof(Int64)) { stringBuilder.AppendFormat(InputText(propName, value.ToString(), 10, 4, htmlAttributes)); } else if (propType == typeof(Single) || propType == typeof(Double)) { stringBuilder.AppendFormat(InputText(propName, value.ToString(), htmlAttributes)); } else if (propType == typeof(DateTime)) { stringBuilder.AppendFormat(DateTime(propName, (DateTime)value, htmlAttributes)); } else if (propType == typeof(bool)) { // stringBuilder.AppendFormat( InputCheckbox(propName, "true", (bool) value) ); stringBuilder.Append(Select(propName)); stringBuilder.Append(CreateOptionsFromPrimitiveArray(new bool[] { true, false }, value.ToString())); stringBuilder.Append(EndSelect()); } else if (propType == typeof(Enum)) { // TODO: Support flags as well String[] names = System.Enum.GetNames(propType); IList options = new ArrayList(); foreach (String name in names) { options.Add(String.Format("{0} {1}\r\n", InputRadio(propName, name), LabelFor(name, name))); } stringBuilder.AppendFormat(BuildUnorderedList(options)); } }
public String CreateControl(ActiveRecordModel model, String prefix, FieldModel fieldModel, object instance) { stringBuilder.Length = 0; var fieldInfo = fieldModel.Field; var propName = CreatePropName(model, prefix, fieldInfo.Name); if (fieldInfo.FieldType == typeof(DateTime)) { stringBuilder.Append(LabelFor(propName + "day", TextHelper.PascalCaseToWord(fieldInfo.Name) + ": ")); } else { stringBuilder.Append(LabelFor(propName, TextHelper.PascalCaseToWord(fieldInfo.Name) + ": ")); } var propAtt = fieldModel.FieldAtt; RenderAppropriateControl(model, fieldInfo.FieldType, propName, null, null, propAtt.Unique, propAtt.NotNull, propAtt.ColumnType, propAtt.Length); return(stringBuilder.ToString()); }
public void DiscriminatorUse() { XmlGenerationVisitor xmlVisitor = new XmlGenerationVisitor(); xmlVisitor.CreateXml(ActiveRecordModel.GetModel(typeof(PersistedRule))); String xml = xmlVisitor.Xml; String expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + "<hibernate-mapping auto-import=\"true\" default-lazy=\"false\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-mapping-2.2\">\r\n" + " <class name=\"Castle.ActiveRecord.Tests.Model.RulesModel.PersistedRule, Castle.ActiveRecord.Tests\" table=\"PersistedRule\" discriminator-value=\"0\" lazy=\"false\">\r\n" + " <id name=\"Id\" access=\"property\" column=\"Id\" type=\"Int32\" unsaved-value=\"0\">\r\n" + " <generator class=\"native\">\r\n" + " </generator>\r\n" + " </id>\r\n" + " <discriminator column=\"discriminator\" />\r\n" + " <property name=\"Count\" access=\"property\" type=\"Int32\">\r\n" + " <column name=\"Count\" />\r\n" + " </property>\r\n" + " <subclass name=\"Castle.ActiveRecord.Tests.Model.RulesModel.WorkDaysRules, Castle.ActiveRecord.Tests\" discriminator-value=\"2\" lazy=\"false\">\r\n" + " <property name=\"Name\" access=\"property\" type=\"String\">\r\n" + " <column name=\"Name\" />\r\n" + " </property>\r\n" + " <property name=\"Days\" access=\"property\" type=\"Int32\">\r\n" + " <column name=\"Days\" />\r\n" + " </property>\r\n" + " </subclass>\r\n" + " </class>\r\n" + "</hibernate-mapping>\r\n";; Assert.AreEqual(expected, xml); }
private void CreateMappedInstances(object instance, PropertyInfo prop, PrimaryKeyModel keyModel, ActiveRecordModel otherModel, DataBindContext context) { object container = InitializeRelationPropertyIfNull(instance, prop); // TODO: Support any kind of key String paramName = String.Format("{0}.{1}", prop.Name, keyModel.Property.Name); String[] values = context.ParamList.GetValues(paramName); int[] ids = (int[])ConvertUtils.Convert(typeof(int[]), values, paramName, null, context.ParamList); if (ids != null) { foreach (int id in ids) { object item = Activator.CreateInstance(otherModel.Type); keyModel.Property.SetValue(item, id, EmptyArg); AddToContainer(container, item); } } }
public override bool Perform(object instance, object fieldValue) { ActiveRecordValidationBase arInstance = (ActiveRecordValidationBase)instance; ActiveRecordModel model = ActiveRecordBase.GetModel(arInstance.GetType()); while (model != null) { if (model.Ids.Count != 0) { _pkModel = model.Ids[0] as PrimaryKeyModel; } model = model.Parent; } if (_pkModel == null) { throw new ValidationFailure("We couldn't find the primary key for " + arInstance.GetType().FullName + " so we can't ensure the uniqueness of any field. Validatior failed"); } _fieldValue = fieldValue; return((bool)arInstance.Execute(new NHibernateDelegate(CheckUniqueness))); }
public void SimpleCaseWithKeyAndProperties() { ActiveRecordModelBuilder builder = new ActiveRecordModelBuilder(); ActiveRecordModel model = builder.Create(typeof(ClassA)); Assert.IsNotNull(model); SemanticVerifierVisitor semanticVisitor = new SemanticVerifierVisitor(builder.Models); semanticVisitor.VisitNode(model); XmlGenerationVisitor xmlVisitor = new XmlGenerationVisitor(); xmlVisitor.CreateXml(model); String xml = xmlVisitor.Xml; String expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" + "<hibernate-mapping xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-mapping-2.0\">\r\n" + " <class name=\"Castle.ActiveRecord.Framework.Internal.Tests.Model.ClassA, Castle.ActiveRecord.Framework.Internal.Tests\" table=\"ClassA\" >\r\n" + " <id name=\"Id\" access=\"property\" column=\"Id\" type=\"Int32\" unsaved-value=\"0\">\r\n" + " <generator class=\"native\">\r\n" + " </generator>\r\n" + " </id>\r\n" + " <property name=\"Name1\" access=\"property\" column=\"Name1\" type=\"String\" insert=\"false\" update=\"false\" />\r\n" + " <property name=\"Name2\" access=\"property\" column=\"Name2\" type=\"String\" unsaved-value=\"hammett\" />\r\n" + " <property name=\"Name3\" access=\"property\" column=\"Name3\" type=\"String\" not-null=\"true\" unique=\"true\" />\r\n" + " <property name=\"Text\" access=\"property\" column=\"Text\" type=\"StringClob\" />\r\n" + " </class>\r\n" + "</hibernate-mapping>\r\n"; Assert.AreEqual(expected, xml); }
private static PrimaryKeyModel ObtainPrimaryKey(ActiveRecordModel model) { if (model.IsJoinedSubClass || model.IsDiscriminatorSubClass) { return(ObtainPrimaryKey(model.Parent)); } return(model.PrimaryKey); }
protected internal static bool Exists(Type t, object pk, ICriterion criteria) { PrimaryKeyModel pkModel = ActiveRecordModel.GetModel(t).PrimaryKey; string pkField = pkModel.Property.Name; object o = FindFirst(t, Expression.And(criteria, Expression.Not(Expression.Eq(pkField, pk)))); return(o != null); }
/// <summary> /// Initializes a new instance of the <see cref="CountQuery"/> class. /// </summary> /// <param name="targetType">The target type.</param> /// <param name="filter">The filter.</param> /// <param name="parameters">The parameters.</param> public CountQuery(Type targetType, string filter, params object[] parameters) : base(targetType, " WHERE " + filter, parameters) { ActiveRecordBase.EnsureInitialized(targetType); var model = ActiveRecordModel.GetModel(targetType); string typeName = model.UseAutoImport ? targetType.Name : targetType.FullName; Query = "SELECT COUNT(*) FROM " + typeName + Query; }
internal static IEnumerable GetNestedPropertiesToValidate(object instance) { Type type = instance.GetType(); ActiveRecordModel me = ActiveRecordModel.GetModel(type); foreach (NestedModel component in me.Components) { yield return(component.Property); } }
public void ArIssue274_CanConnectGenericJoinedBase() { ActiveRecordStarter.Initialize(GetConfigSource(), typeof(GenBaseJoinedClass <>), typeof(GenIntermediateClass <>), typeof(GenGrandsonClass)); Assert.That(ActiveRecordModel.GetModel(typeof(GenBaseJoinedClass <>)), Is.EqualTo(ActiveRecordModel.GetModel(typeof(GenGrandsonClass)).Parent)); }
private void RenderAppropriateControl(ActiveRecordModel model, Type propType, string propName, PropertyInfo property, object value, bool unique, bool notNull, String columnType, int length) { IDictionary htmlAttributes = new Hashtable(); if (propType == typeof(String)) { if (String.Compare("stringclob", columnType, true) == 0) { stringBuilder.AppendFormat(TextArea(propName)); } else { if (length > 0) { htmlAttributes["maxlength"] = length.ToString(); } stringBuilder.AppendFormat(TextField(propName, htmlAttributes)); } } else if (propType == typeof(Int16) || propType == typeof(Int32) || propType == typeof(Int64)) { stringBuilder.AppendFormat(NumberField(propName, htmlAttributes)); } else if (propType == typeof(Single) || propType == typeof(Double) || propType == typeof(Decimal)) { stringBuilder.AppendFormat(NumberField(propName, htmlAttributes)); } else if (propType == typeof(DateTime)) { stringBuilder.AppendFormat(Select(propName + "month", Months, htmlAttributes)); stringBuilder.AppendFormat(Select(propName + "day", Days, htmlAttributes)); stringBuilder.AppendFormat(Select(propName + "year", Years, htmlAttributes)); } else if (propType == typeof(bool)) { stringBuilder.Append(CheckboxField(propName)); } else if (propType == typeof(Enum)) { // TODO: Support flags as well var names = Enum.GetNames(propType); IList options = new ArrayList(); foreach (var name in names) { options.Add(String.Format("{0} {1}\r\n", RadioField(propName, name), LabelFor(name, TextHelper.PascalCaseToWord(name)))); } } }
private object LoadActiveRecord(Type type, object pk, ARFetchAttribute attr, ActiveRecordModel model) { object instance = null; if (pk != null && !String.Empty.Equals(pk)) { var pkModel = ObtainPrimaryKey(model); var pkType = pkModel.Property.PropertyType; bool conversionSucceeded; var convertedPk = converter.Convert(pkType, pk.GetType(), pk, out conversionSucceeded); if (!conversionSucceeded) { throw new MonoRailException("ARFetcher could not convert PK {0} to type {1}", pk, pkType); } if (string.IsNullOrEmpty(attr.Eager)) { // simple load instance = ActiveRecordMediator.FindByPrimaryKey(type, convertedPk, attr.Required); } else { // load using eager fetching of lazy collections var criteria = DetachedCriteria.For(type); criteria.Add(Expression.Eq(pkModel.Property.Name, convertedPk)); foreach (var associationToEagerFetch in attr.Eager.Split(',')) { var clean = associationToEagerFetch.Trim(); if (clean.Length == 0) { continue; } criteria.SetFetchMode(clean, FetchMode.Eager); } var result = (object[])ActiveRecordMediator.FindAll(type, criteria); if (result.Length > 0) { instance = result[0]; } } } if (instance == null && attr.Create) { instance = Activator.CreateInstance(type); } return(instance); }
/// <summary> /// Perform the check that the property value is unqiue in the table /// </summary> /// <param name="instance"></param> /// <param name="fieldValue"></param> /// <returns><c>true</c> if the field is OK</returns> public override bool IsValid(object instance, object fieldValue) { Type instanceType = instance.GetType(); ActiveRecordModel model = ActiveRecordBase.GetModel(instance.GetType()); while (model != null) { if (model.PrimaryKey != null) { pkModel = model.PrimaryKey; } model = model.Parent; } if (pkModel == null) { throw new ValidationFailure("We couldn't find the primary key for " + instanceType.FullName + " so we can't ensure the uniqueness of any field. Validatior failed"); } IsUniqueValidator.fieldValue = fieldValue; SessionScope scope = null; FlushMode? originalMode = null; if (SessionScope.Current == null /*|| * SessionScope.Current.ScopeType != SessionScopeType.Transactional*/) { scope = new SessionScope(); } else { originalMode = ActiveRecordBase.holder.CreateSession(instanceType).FlushMode; ActiveRecordBase.holder.CreateSession(instanceType).FlushMode = FlushMode.Never; } try { return((bool)ActiveRecordMediator.Execute(instanceType, CheckUniqueness, instance)); } finally { if (scope != null) { scope.Dispose(); } if (originalMode != null) { ActiveRecordBase.holder.CreateSession(instanceType).FlushMode = originalMode ?? FlushMode.Commit; } } }
private ActiveRecordModel GetARModel() { ActiveRecordModel model = ActiveRecordModel.GetModel(modelType); if (model == null) { throw new ScaffoldException("Specified type isn't an ActiveRecord type or the ActiveRecord " + "framework wasn't started properly. Did you forget about the Initialize method?"); } return(model); }
public void CanConnectGrandChildren() { ActiveRecordStarter.Initialize(GetConfigSource(), typeof(ClassDiscriminatorA), typeof(DiscriminatorGrandchild), typeof(ClassDiscriminatorParent)); Assert.AreEqual( ActiveRecordModel.GetModel(typeof(ClassDiscriminatorA)), ActiveRecordModel.GetModel(typeof(DiscriminatorGrandchild)).Parent ); }
private ActiveRecordModel GetARModel() { var foundModel = ActiveRecordModel.GetModel(modelType); if (foundModel == null) { throw new ScaffoldException("Specified type is not an ActiveRecord type or the ActiveRecord " + "framework was not started properly. Did you forget to invoke ActiveRecordStarter.Initialize() ?"); } return(foundModel); }
private string Process(ActiveRecordModelBuilder builder, ActiveRecordModel model) { GraphConnectorVisitor connectorVisitor = new GraphConnectorVisitor(builder.Models); connectorVisitor.VisitNode(model); SemanticVerifierVisitor semanticVisitor = new SemanticVerifierVisitor(builder.Models); semanticVisitor.VisitNode(model); XmlGenerationVisitor xmlVisitor = new XmlGenerationVisitor(); xmlVisitor.CreateXml(model); return xmlVisitor.Xml; }