Exemplo n.º 1
0
        private static object GetDataListItem(Crate crate, int index)
        {
            var tableData = crate.ManifestType.Id == (int)MT.StandardTableData ? crate.Get <StandardTableDataCM>() : null;

            if (tableData != null)
            {
                //why?? why just skip header and return first row?
                return(tableData.FirstRowHeaders ? tableData.Table[index + 1] : tableData.Table[index]);
            }
            return(Fr8ReflectionHelper.FindFirstArray(crate.Get())[index]);
        }
Exemplo n.º 2
0
        private static FieldDTO FindField(OperationalStateCM operationalState, Crate crate, string fieldKey)
        {
            object searchArea;
            //let's check if we are in a loop
            //and this is a loop data?
            //check if this crate is loop related
            var loopState = operationalState.CallStack.FirstOrDefault(x =>
            {
                if (x.LocalData?.Type == "Loop")
                {
                    var loopStatus = x.LocalData.ReadAs <OperationalStateCM.LoopStatus>();

                    if (loopStatus != null && loopStatus.CrateManifest.CrateDescriptions[0].Label == crate.Label && loopStatus.CrateManifest.CrateDescriptions[0].ManifestType == crate.ManifestType.Type)
                    {
                        return(true);
                    }
                }

                return(false);
            });

            if (loopState != null) //this is a loop related data request
            {
                searchArea = GetDataListItem(crate, loopState.LocalData.ReadAs <OperationalStateCM.LoopStatus>().Index);
            }
            else
            {
                //hmmm this is a regular data request
                //lets search in complete crate
                searchArea = crate;
                //if we have a StandardTableDataCM and we are not in the loop and crate has Headers - we should search next row
                if (crate.IsOfType <StandardTableDataCM>())
                {
                    var tableCrate = crate.Get <StandardTableDataCM>();
                    if (tableCrate.FirstRowHeaders && tableCrate.Table.Count > 1)
                    {
                        //TODO it is weird to get just first row of table data while searching for a field
                        //note: GetDataListItem function skips header
                        TableRowDTO row = GetDataListItem(crate, 0) as TableRowDTO;
                        if (row != null)
                        {
                            return(row.Row.FirstOrDefault(a => a.Cell.Key == fieldKey)?.Cell);
                        }
                    }
                }
            }

            if (searchArea is Crate)
            {
                if (((Crate)searchArea).IsKnownManifest)
                {
                    searchArea = ((Crate)searchArea).Get();
                }
                else
                {
                    return(null);
                }
            }

            //we should find first related field and return
            var fields     = Fr8ReflectionHelper.FindFieldsRecursive(searchArea);
            var fieldMatch = fields.FirstOrDefault(f => f.Key == fieldKey);

            //let's return first match
            return(fieldMatch);
        }
        public static IEnumerable <FieldDTO> FindFieldsRecursive(object obj)
        {
            var result = new List <FieldDTO>();

            if (obj == null)
            {
                return(result);
            }
            var fieldDTO = obj as FieldDTO;

            if (fieldDTO != null)
            {
                result.Add(fieldDTO);
                return(result);
            }
            var type = obj.GetType();

            if (IsPrimitiveType(type))
            {
                return(result);
            }
            var list = obj as IEnumerable;

            if (list != null)
            {
                foreach (var element in list)
                {
                    result.AddRange(FindFieldsRecursive(element));
                }
                return(result);
            }


            var propsToIgnore = new HashSet <string>();
            var manifestType  = typeof(Manifest);
            var members       = Fr8ReflectionHelper.GetMembers(type);

            // ingore properties from Manifest base class
            if (manifestType.IsAssignableFrom(type))
            {
                foreach (var prop in Fr8ReflectionHelper.GetMembers(manifestType))
                {
                    propsToIgnore.Add(prop.Name);
                }
            }

            foreach (var memberAccessor in members)
            {
                if (propsToIgnore.Contains(memberAccessor.Name))
                {
                    continue;
                }

                if (IsPrimitiveType(memberAccessor.MemberType))
                {
                    result.Add(new FieldDTO(memberAccessor.Name, memberAccessor.GetValue(obj)?.ToString()));
                }
                else
                {
                    result.AddRange(FindFieldsRecursive(memberAccessor.GetValue(obj)));
                }
            }

            return(result);
        }