Ejemplo n.º 1
0
        internal void PopulateChildrenRecursively <T>(BaseDto baseDto, RDO objectRdo, ObjectFieldsDepthLevel depthLevel)
        {
            foreach (var objectPropertyInfo in BaseDto.GetRelativityMultipleObjectPropertyInfos <T>())
            {
                var propertyInfo = objectPropertyInfo.Key;
                var theMultipleObjectAttribute = objectPropertyInfo.Value;

                Type childType = objectPropertyInfo.Value.ChildType;

                int[] childArtifactIds = objectRdo[objectPropertyInfo.Value.FieldGuid].GetValueAsMultipleObject <kCura.Relativity.Client.DTOs.Artifact>()
                                         .Select <kCura.Relativity.Client.DTOs.Artifact, int>(artifact => artifact.ArtifactID).ToArray();

                MethodInfo method = GetType().GetMethod("GetDTOs", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(new Type[] { childType });

                var allObjects = method.Invoke(this, new object[] { childArtifactIds, depthLevel }) as IEnumerable;

                var   listType   = typeof(List <>).MakeGenericType(theMultipleObjectAttribute.ChildType);
                IList returnList = (IList)Activator.CreateInstance(listType);

                foreach (var item in allObjects)
                {
                    returnList.Add(item);
                }

                propertyInfo.SetValue(baseDto, returnList);
            }

            foreach (var ObjectPropertyInfo in BaseDto.GetRelativitySingleObjectPropertyInfos <T>())
            {
                var propertyInfo = ObjectPropertyInfo.Key;

                Type objectType   = ObjectPropertyInfo.Value.ChildType;
                var  singleObject = Activator.CreateInstance(objectType);

                int childArtifactId = objectRdo[ObjectPropertyInfo.Value.FieldGuid].ValueAsSingleObject.ArtifactID;

                MethodInfo method = GetType().GetMethod("GetDTO", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(new Type[] { objectType });

                if (childArtifactId != 0)
                {
                    singleObject = method.Invoke(this, new object[] { childArtifactId, depthLevel });
                }

                propertyInfo.SetValue(baseDto, singleObject);
            }

            foreach (var childPropertyInfo in BaseDto.GetRelativityObjectChildrenListInfos <T>())
            {
                var propertyInfo      = childPropertyInfo.Key;
                var theChildAttribute = childPropertyInfo.Value;

                Type       childType = childPropertyInfo.Value.ChildType;
                MethodInfo method    = GetType().GetMethod("GetAllChildDTOs", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(new Type[] { childType });

                Guid parentFieldGuid = childType.GetRelativityObjectGuidForParentField();

                var allChildObjects = method.Invoke(this, new object[] { parentFieldGuid, baseDto.ArtifactId, depthLevel }) as IEnumerable;

                var   listType   = typeof(List <>).MakeGenericType(theChildAttribute.ChildType);
                IList returnList = (IList)Activator.CreateInstance(listType);

                foreach (var item in allChildObjects)
                {
                    returnList.Add(item);
                }

                propertyInfo.SetValue(baseDto, returnList);
            }

            foreach (var filePropertyInfo in baseDto.GetType().GetPublicProperties().Where(prop => prop.PropertyType == typeof(RelativityFile)))
            {
                var filePropertyValue = filePropertyInfo.GetValue(baseDto, null) as RelativityFile;

                if (filePropertyValue != null)
                {
                    filePropertyValue = GetFile(filePropertyValue.ArtifactTypeId, baseDto.ArtifactId);
                }

                filePropertyInfo.SetValue(baseDto, filePropertyValue);
            }
        }