Esempio n. 1
0
        private IEnumerable <PicklistOption> GetSelections()
        {
            if (RecordType == null)
            {
                return(new PicklistOption[0]);
            }

            //need to generate a list of all join optionds
            //n:N, 1:N or N:1

            var options      = new List <PicklistOption>();
            var lookupFields = RecordService.GetFieldMetadata(RecordType)
                               .Where(f => f.FieldType == Record.Metadata.RecordFieldType.Lookup || f.FieldType == Record.Metadata.RecordFieldType.Customer)
                               .ToArray();

            //okay these need appending for each target type
            foreach (var lookupField in lookupFields)
            {
                var targetTypes = RecordService.GetLookupTargetType(lookupField.SchemaName, RecordType);
                if (!string.IsNullOrWhiteSpace(targetTypes))
                {
                    var relationshipLabel = lookupField.DisplayName;
                    var split             = targetTypes.Split(',');
                    var isMultiple        = split.Count() > 1;
                    foreach (var target in split)
                    {
                        var key    = $"n1:{lookupField.SchemaName}:{target}";
                        var label  = relationshipLabel + (isMultiple ? ($" ({RecordService.GetDisplayName(target)})") : null);
                        var option = new PicklistOption(key, label);
                        options.Add(option);
                    }
                }
            }

            var manyToManyRelationships = RecordService.GetManyToManyRelationships(RecordType).ToArray();

            foreach (var manyToMany in manyToManyRelationships)
            {
                if (manyToMany.RecordType1 == RecordType)
                {
                    var key    = $"nn:{manyToMany.IntersectEntityName}:{manyToMany.Entity1IntersectAttribute}:{manyToMany.Entity2IntersectAttribute}:{manyToMany.RecordType2}";
                    var label  = manyToMany.RecordType2UseCustomLabel ? manyToMany.RecordType2CustomLabel : RecordService.GetDisplayName(manyToMany.RecordType2);
                    var option = new PicklistOption(key, label);
                    options.Add(option);
                }
                if (manyToMany.RecordType2 == RecordType)
                {
                    var key    = $"nn:{manyToMany.IntersectEntityName}:{manyToMany.Entity2IntersectAttribute}:{manyToMany.Entity1IntersectAttribute}:{manyToMany.RecordType1}";
                    var label  = manyToMany.RecordType1UseCustomLabel ? manyToMany.RecordType1CustomLabel : RecordService.GetDisplayName(manyToMany.RecordType1);
                    var option = new PicklistOption(key, label);
                    options.Add(option);
                }
            }

            var oneToManyRelationships = RecordService.GetOneToManyRelationships(RecordType).ToArray();

            foreach (var oneToMany in oneToManyRelationships)
            {
                if (RecordService.FieldExists(oneToMany.ReferencingAttribute, oneToMany.ReferencingEntity))
                {
                    var key    = $"1n:{oneToMany.ReferencingEntity}:{oneToMany.ReferencingAttribute}";
                    var label  = $"{RecordService.GetDisplayName(oneToMany.ReferencingEntity)} ({RecordService.GetFieldLabel(oneToMany.ReferencingAttribute, oneToMany.ReferencingEntity)})";
                    var option = new PicklistOption(key, label);
                    options.Add(option);
                }
            }

            options.Sort((p1, p2) => p1.CompareTo(p2));
            options = options.Distinct().ToList();
            return(options);
        }