private static void AddAvailableValuesFromFileToList(string absoluteFile, List <string> stringsToReturn, bool shouldAppendFileName)
        {
            if (System.IO.File.Exists(absoluteFile))
            {
                try
                {
                    string toAppend = "";
                    if (shouldAppendFileName)
                    {
                        // Eventually we want to make this relative to the container, not just the folename
                        toAppend = " in " + FileManager.RemovePath(absoluteFile);
                    }

                    RuntimeCsvRepresentation rcr =
                        CsvFileManager.CsvDeserializeToRuntime(absoluteFile);

                    rcr.RemoveHeaderWhitespaceAndDetermineIfRequired();

                    int requiredIndex = -1;

                    for (int i = 0; i < rcr.Headers.Length; i++)
                    {
                        if (rcr.Headers[i].IsRequired)
                        {
                            requiredIndex = i;
                            break;
                        }
                    }

                    if (requiredIndex != -1)
                    {
                        foreach (string[] record in rcr.Records)
                        {
                            string possibleValue = record[requiredIndex];
                            if (!string.IsNullOrEmpty(possibleValue))
                            {
                                if (shouldAppendFileName)
                                {
                                    stringsToReturn.Add(possibleValue + toAppend);
                                }
                                else
                                {
                                    stringsToReturn.Add(possibleValue);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    // do nothing for now...
                }
            }
        }
        private static object GetCsvEntryByRequiredKey(string requiredKey, RuntimeCsvRepresentation rcr)
        {
            rcr.RemoveHeaderWhitespaceAndDetermineIfRequired();

            int requiredIndex = rcr.GetRequiredIndex();

            int startingRow;
            int count;

            GetStartingAndCount(rcr, requiredIndex, requiredKey, out startingRow, out count);
            CsvEntry csvEntry = null;

            if (startingRow != -1)
            {
                csvEntry = new CsvEntry();

                csvEntry.RuntimeCsvRepresentation = rcr;
                csvEntry.Count      = count;
                csvEntry.StartIndex = startingRow;
            }

            return(csvEntry);
        }
示例#3
0
        private static object GetCsvEntryByRequiredKey(string requiredKey, RuntimeCsvRepresentation rcr)
        {
            rcr.RemoveHeaderWhitespaceAndDetermineIfRequired();

            int requiredIndex = rcr.GetRequiredIndex();

            int startingRow;
            int count;
            GetStartingAndCount(rcr, requiredIndex, requiredKey, out startingRow, out count);
            CsvEntry csvEntry = null;

            if (startingRow != -1)
            {
                csvEntry = new CsvEntry();

                csvEntry.RuntimeCsvRepresentation = rcr;
                csvEntry.Count = count;
                csvEntry.StartIndex = startingRow;
            }

            return csvEntry;
        }
示例#4
0
        public static object GetValueFromCsv(string rowValue, string variableName, string csvType, IElement element, ElementRuntime elementRuntime)
        {
            // We should check the csvType (which might be a class already).
            // If we don't get anything back then we should try stripping off
            // the extension and path
            object foundValue = null;


            ReferencedFileSave rfs = ObjectFinder.Self.GetFirstCsvUsingClass(csvType, element);

            if (rfs == null && FileManager.GetExtension(csvType) == "csv")
            {
                string strippedType = FileManager.RemovePath(FileManager.RemoveExtension(csvType));

                rfs = ObjectFinder.Self.GetFirstCsvUsingClass(strippedType, element);
            }

            if (rfs != null)
            {
                RuntimeCsvRepresentation rcr = null;

                // This could be global content or it could be a RFS in the element
                if (element.ContainsRecursively(rfs))
                {
                    var loadedFile = elementRuntime.LoadReferencedFileSave(rfs, true, element);
                    rcr = loadedFile.RuntimeObject as RuntimeCsvRepresentation;
                }
                else
                {
                    var loadedFile =
                        GluxManager.GlobalContentFilesRuntime.LoadReferencedFileSave(rfs, true, element);
                    // Load this thing from global content
                    rcr = loadedFile.RuntimeObject as
                          RuntimeCsvRepresentation;
                }
                if (rcr.GetRequiredIndex() == -1)
                {
                    rcr.RemoveHeaderWhitespaceAndDetermineIfRequired();
                }
                int indexOfColumn = 0;
                for (int i = 0; i < rcr.Headers.Length; i++)
                {
                    if (rcr.Headers[i].Name == variableName)
                    {
                        indexOfColumn = i;
                        break;
                    }
                }

                if (rcr != null)
                {
                    // Right now I'm writing this to only support dictionaries.
                    int      indexOfRequired = rcr.GetRequiredIndex();
                    string[] matchingRecord  = null;

                    foreach (string[] record in rcr.Records)
                    {
                        try
                        {
                            if (record.Length > indexOfRequired &&
                                record[indexOfRequired] == rowValue)
                            {
                                if (indexOfColumn >= record.Length)
                                {
                                    int m = 3;
                                }
                                foundValue = record[indexOfColumn];

                                foundValue = ConvertValueToType(foundValue, rcr.Headers[indexOfColumn].OriginalText);


                                break;
                            }
                        }
                        catch
                        {
                            int m = 3;
                        }
                    }
                }
            }

            // We need to find a RFS either in this IElement or in GlobalContentFiles that match this name
            return(foundValue);
        }