private void AppendConnectionForChanges(string fieldName, string recordType, List <Action <RecordEntryViewModelBase> > onChanges, bool isOnLoad) { var lookupForAttributes = ObjectRecordService.GetPropertyInfo(fieldName, recordType) .GetCustomAttributes(typeof(ConnectionFor), true).Cast <ConnectionFor>(); foreach (var attribute in lookupForAttributes) { onChanges.Add( re => { var changedViewModel = re.GetFieldViewModel(fieldName); if (changedViewModel.ValueObject != null) { var matchingFields = re.FieldViewModels.Where(f => f.FieldName == attribute.Property); if (matchingFields.Any()) { var fieldViewModel = matchingFields.First(); if (!isOnLoad && fieldViewModel is LookupFieldViewModel) { var typedViewModel = (LookupFieldViewModel)fieldViewModel; typedViewModel.ConnectionForChanged(); } if (fieldViewModel is RecordTypeFieldViewModel) { var typedViewModel = (RecordTypeFieldViewModel)fieldViewModel; typedViewModel.ItemsSource = ObjectRecordService .GetPicklistKeyValues(fieldViewModel.FieldName, fieldViewModel.GetRecordType()) .Select(p => new RecordType(p.Key, p.Value)) .Where(rt => !rt.Value.IsNullOrWhiteSpace()) .OrderBy(rt => rt.Value) .ToArray(); } } } }); } }
private void AppendFieldForChanges(string fieldName, string recordType, List <Action <RecordEntryViewModelBase> > onChanges, bool clearValue = false) { var lookupForAttributes = ObjectRecordService.GetPropertyInfo(fieldName, recordType) .GetCustomAttributes(typeof(RecordFieldFor), true).Cast <RecordFieldFor>(); foreach (var attribute in lookupForAttributes) { onChanges.Add( re => re.StartNewAction(() => { var recordFieldViewModel = re.GetRecordFieldFieldViewModel(fieldName); if (recordFieldViewModel.Value != null) { var matchingFields = re.FieldViewModels.Where(f => f.FieldName == attribute.PropertyPaths.First()); foreach (var fieldViewModel in matchingFields.ToArray()) { var selectedFieldName = recordFieldViewModel.Value.Key; var selectedFieldRecordType = GetRecordTypeFor(recordFieldViewModel.FieldName, re); var lookupService = re.RecordService.GetLookupService(recordFieldViewModel.FieldName, recordFieldViewModel.GetRecordType(), null, recordFieldViewModel.RecordEntryViewModel.GetRecord()); if (lookupService != null) { //get the source field type var fieldType = lookupService.GetFieldType(selectedFieldName, selectedFieldRecordType); //get the section the target field is in and its field metadata var metadata = re.FormService.GetFormMetadata(re.GetRecordType(), ObjectRecordService); FormFieldMetadata fieldMetadata = null; string sectionName = null; foreach (var sectionMetadata in metadata.FormSections.Cast <FormFieldSection>()) { foreach (var field in sectionMetadata.FormFields) { if (field.FieldName == fieldViewModel.FieldName) { sectionName = sectionMetadata.SectionLabel; fieldMetadata = field; } } } if (!string.IsNullOrWhiteSpace(sectionName)) { var targetPropInfo = ObjectRecordService.GetPropertyType(attribute.PropertyPaths.First(), recordType); if (targetPropInfo.IsEnum) { //if target is an enum then filter the items source for the field type var picklistFieldViewModel = fieldViewModel as PicklistFieldViewModel; var picklistOptions = ObjectRecordService.GetPicklistKeyValues(picklistFieldViewModel.FieldName, picklistFieldViewModel.GetRecordType(), fieldType.ToString(), picklistFieldViewModel.RecordEntryViewModel.GetRecord()); if (clearValue) { picklistFieldViewModel.Value = null; } picklistFieldViewModel.ItemsSource = picklistOptions; } else { //otherwise is a field input dynamic for the field's tpye //now we need to create the view model for the target field as the correct type //okay now we need to replace the old field view model for this field var explicitTargetType = fieldType == RecordFieldType.Lookup || fieldType == RecordFieldType.Customer || fieldType == RecordFieldType.Owner ? lookupService.GetLookupTargetType(selectedFieldName, selectedFieldRecordType) : null; var explicitPicklistOptions = fieldType == RecordFieldType.Picklist || fieldType == RecordFieldType.Status || fieldType == RecordFieldType.State || fieldType == RecordFieldType.Integer ? lookupService.GetPicklistKeyValues(selectedFieldName, selectedFieldRecordType) : null; if (clearValue) { fieldViewModel.ValueObject = null; } var newFieldViewModel = fieldMetadata.CreateFieldViewModel(re.GetRecordType(), re.RecordService, re, re.ApplicationController, explicitFieldType: fieldType, explicitLookupTargetType: explicitTargetType, explicitPicklistOptions: explicitPicklistOptions); var section = re.FieldSections.First(s => s.SectionLabel == sectionName); var index = section.Fields.Count; for (var i = 0; i < section.Fields.Count; i++) { if (section.Fields[i].FieldName == fieldViewModel.FieldName) { index = i; break; } } re.DoOnMainThread(() => { if (section.Fields.Count > index) { section.Fields.RemoveAt(index); } if (clearValue) { fieldViewModel.ValueObject = null; } section.Fields.Insert(index, newFieldViewModel); re.RefreshVisibility(); }); } } } } } })); } }