public Type GetClassType(string recordType)
        {
            Type type = null;

            if (recordType == ObjectType.AssemblyQualifiedName)
            {
                type = ObjectType;
            }
            else
            {
                var fieldMetadata = GetFieldMetadata(ObjectType.AssemblyQualifiedName);
                foreach (var metadata in fieldMetadata.Where(m => m.FieldType == RecordFieldType.Enumerable))
                {
                    if (((EnumerableFieldMetadata)metadata).EnumeratedTypeQualifiedName == recordType)
                    {
                        var propertyName = metadata.SchemaName;
                        if (ObjectTypeMaps != null && ObjectTypeMaps.ContainsKey(propertyName))
                        {
                            type = ObjectTypeMaps[propertyName];
                        }
                        else
                        {
                            type = ObjectType.GetProperty(propertyName).PropertyType.GetGenericArguments()[0];
                        }

                        break;
                    }
                    else
                    {
                        var propertyValue = ObjectToEnter.GetPropertyValue(metadata.SchemaName);
                        if (propertyValue != null)
                        {
                            var enumerable = ((IEnumerable)propertyValue);
                            foreach (var item in enumerable)
                            {
                                var instanceType = item.GetType();
                                if (instanceType.AssemblyQualifiedName == recordType)
                                {
                                    type = instanceType;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (type == null)
            {
                type = Type.GetType(recordType);
            }

            return(type);
        }
        private IEnumerable <IRecord> GetPropertyObjectsAsRecords(string linkedEntityLookup)
        {
            var propertyValue = ObjectToEnter.GetPropertyValue(linkedEntityLookup);

            if (propertyValue == null)
            {
                return(new IRecord[0]);
            }
            var enumerable = ((IEnumerable)propertyValue);
            var objectList = new List <ObjectRecord>();

            foreach (var item in enumerable)
            {
                objectList.Add(new ObjectRecord(item));
            }
            return(objectList);
        }
        internal override RecordEntryFormViewModel GetLoadRowViewModel(string subGridName, RecordEntryViewModelBase parentForm, Action <IRecord> onSave, Action onCancel)
        {
            var propertyInfo = ObjectToEnter.GetType().GetProperty(subGridName);

            if (propertyInfo.GetCustomAttribute <FormEntry>() != null)
            {
                //lets start a dialog to add it on complete
                var fieldMetadata = (EnumerableFieldMetadata)ObjectRecordService.GetFieldMetadata(propertyInfo.Name, ObjectToEnter.GetType().AssemblyQualifiedName);
                var newRecord     = (ObjectRecord)ObjectRecordService.NewRecord(fieldMetadata.EnumeratedTypeQualifiedName);
                var newObject     = newRecord.Instance;
                var recordService = new ObjectRecordService(newObject, ObjectRecordService.LookupService, ObjectRecordService.OptionSetLimitedValues, ObjectRecordService, subGridName, parentForm.ApplicationController);
                var viewModel     = new ObjectEntryViewModel(
                    () => onSave(new ObjectRecord(newObject)),
                    onCancel,
                    newObject, new FormController(recordService, new ObjectFormService(newObject, recordService), parentForm.FormController.ApplicationController), parentForm, subGridName, parentForm.OnlyValidate);
                return(viewModel);
                //ideally could hide the parent dialog temporarily and load this one
            }
            //if the object specifies use a form then use the form/dialog
            else
            {
                return(null);
            }
        }
        public override bool AllowGridFieldEditEdit(string fieldName)
        {
            var propertyInfo = ObjectToEnter.GetType().GetProperty(fieldName);

            return(propertyInfo == null || !propertyInfo.PropertyType.GenericTypeArguments.Any() || propertyInfo.PropertyType.GenericTypeArguments[0].GetCustomAttribute <DoNotAllowGridEdit>() == null);
        }
        public override FormMetadata GetFormMetadata(string recordType, IRecordService recordService = null)
        {
            if (_formMetadata == null)
            {
                var formSections = new List <FormFieldSection>();

                var type             = ObjectToEnter.GetType();
                var propertyMetadata = ObjectRecordService.GetFieldMetadata(type.AssemblyQualifiedName);

                var standardFieldSectionName = type.GetDisplayName();

                var fieldSections = type.GetCustomAttributes <Group>();
                var otherSections = new Dictionary <string, List <FormFieldMetadata> >();
                foreach (var section in fieldSections)
                {
                    var functions = new List <CustomFormFunction>();
                    if (section.SelectAll)
                    {
                        functions.Add(new CustomFormFunction("SELECTALL", "SELECT ALL", (re) =>
                        {
                            var entryForm = re as RecordEntryFormViewModel;
                            if (entryForm != null)
                            {
                                var thisSection   = entryForm.GetFieldSection(section.Name);
                                var booleanFields = thisSection.Fields.Where(f => f is BooleanFieldViewModel).Cast <BooleanFieldViewModel>();
                                var turnOff       = booleanFields.All(b => b.Value);
                                foreach (var field in booleanFields)
                                {
                                    field.Value = !turnOff;
                                }
                            }
                        }));
                    }
                    otherSections[section.Name] = new List <FormFieldMetadata>();
                    var newSection = new FormFieldSection(section.Name, otherSections[section.Name], section.DisplayLayout, section.Order, customFunctions: functions);
                    formSections.Add(newSection);
                }

                foreach (var property in propertyMetadata.Where(m => m.Readable || m.Writeable))
                {
                    var propinfo       = ObjectRecordService.GetPropertyInfo(property.SchemaName, type.AssemblyQualifiedName);
                    var groupAttribute = propinfo.GetCustomAttribute <Group>();

                    string enumerableType = null;
                    if (property is EnumerableFieldMetadata)
                    {
                        enumerableType = ((EnumerableFieldMetadata)property).EnumeratedTypeQualifiedName;
                    }

                    var displayLabel = property.FieldType != RecordFieldType.Enumerable ||
                                       groupAttribute != null;

                    var fieldMetadata = new PersistentFormField(property.SchemaName, enumerableType, displayLabel);
                    fieldMetadata.Order = property.Order;

                    string sectionName = null;
                    if (groupAttribute != null)
                    {
                        sectionName = groupAttribute.Name;
                    }
                    else if (property.FieldType == RecordFieldType.Enumerable)
                    {
                        sectionName = property.DisplayName;
                    }
                    else
                    {
                        sectionName = standardFieldSectionName;
                    }

                    var order = 0;
                    if (groupAttribute != null)
                    {
                        order = groupAttribute.Order;
                    }
                    else if (property.FieldType == RecordFieldType.Enumerable)
                    {
                        order = 200000;
                    }
                    else
                    {
                        order = 1;
                    }

                    var displayLayout = groupAttribute != null
                        ? groupAttribute.DisplayLayout
                        : Group.DisplayLayoutEnum.VerticalList;

                    if (!otherSections.ContainsKey(sectionName))
                    {
                        otherSections[sectionName] = new List <FormFieldMetadata>();
                        var newSection = new FormFieldSection(sectionName, otherSections[sectionName], displayLayout, order);
                        formSections.Add(newSection);
                    }
                    otherSections[sectionName].Add(fieldMetadata);
                }
                foreach (var section in formSections)
                {
                    var fields = section.FormFields;
                    if (section.DisplayLayout == Group.DisplayLayoutEnum.HorizontalInputOnly)
                    {
                        foreach (var field in fields)
                        {
                            field.DisplayLabel = false;
                        }
                    }
                    if (fields.Count() == 1)
                    {
                        var field   = fields.First();
                        var fieldMt = ObjectRecordService.GetFieldMetadata(field.FieldName, type.AssemblyQualifiedName);
                        if (fieldMt is EnumerableFieldMetadata ||
                            fieldMt is MemoFieldMetadata ||
                            (fieldMt is StringFieldMetadata && fieldMt.IsMultiline()))
                        {
                            field.DisplayLabel = false;
                        }
                    }
                }
                formSections  = formSections.OrderBy(s => s.Order).ToList();
                _formMetadata = new FormMetadata(formSections);
            }
            return(_formMetadata);
        }