예제 #1
0
        protected override DocumentPlug ConstructDocumentPlug(ExcelWorksheet schemaWorksheet, int startRow)
        {
            Pluglet rootPluglet = new Pluglet("X12", "GC root Node", PlugletType.Loop, null, 1, -1);

            DocumentPlug documentPlug = new DocumentPlug(rootPluglet, BusinessDomain.FlatFile);

            ReadMetadata(documentPlug, schemaWorksheet);

            string currentLoopName = string.Empty;
            string nextLoopName;

            IPluglet loopPluglet = null;
            IPluglet nextPluglet;
            int      minOccurs, maxOccurs;
            int      row = startRow;

            while ((nextPluglet = GetSegment(schemaWorksheet, ref row, out nextLoopName)) != null)
            {
                // In case of flat file, we do not have loops
                rootPluglet.Children.Add(nextPluglet);
                nextPluglet.Parent = rootPluglet;
            }

            return(documentPlug);
        }
        public IDocumentPlug GenerateDocumentPlug(Stream stream, string tradingPartnerName, int documentType, string direction, SpecCertFileType certFileType)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            DocumentPlug documentPlug = null;

            using (ExcelPackage pck = new ExcelPackage(stream))
            {
                ExcelWorkbook workBook = pck.Workbook;
                // TODO: Add more validation
                ExcelWorksheet schemaWorksheet;

                string worksheetName = string.Format("{0} {1} Spec Worksheet", documentType, direction);

                // Special case for 'super spec'
                if (string.IsNullOrWhiteSpace(tradingPartnerName) == false &&
                    string.Equals(tradingPartnerName, "Super Spec", StringComparison.InvariantCultureIgnoreCase) == true)
                {
                    worksheetName = "Segment & Element List";

                    if (workBook.Worksheets[worksheetName] == null)
                    {
                        worksheetName = "Specification Worksheet";
                    }
                }

                schemaWorksheet = workBook.Worksheets[worksheetName];

                if (schemaWorksheet == null)
                {
                    AddValidationResult(ResultType.Error, -1, "N/A", string.Format("'{0}' worksheet doesn't exist", worksheetName));
                    return(null);
                }

                InitializeColumnIndexes(direction);
                int row = GetStartRow(schemaWorksheet);
                documentPlug = ConstructDocumentPlug(schemaWorksheet, row);

                // TODO: Try to get source contingency row here instead of specifying -1
                foreach (int contingencyRow in PendingContingencies.Keys)
                {
                    AddValidationResult(ResultType.Error, -1, "N/A", string.Format("Unresolved contingency. Row '{0}' does not exist", contingencyRow));
                }
            }

            return(documentPlug);
        }
예제 #3
0
        private void ReadMetadata(DocumentPlug documentPlug, ExcelWorksheet schemaWorksheet)
        {
            int    row;
            string cellValue;
            int    delimiterCount = 0;

            for (row = 1; row < schemaWorksheet.Dimension.End.Row && delimiterCount < 2; ++row)
            {
                // column 1 has name 'Element Segment' or 'Segment Delimiter'
                // Column 3 has delimiter
                cellValue = GCExcelReaderHelper.ReadCell(schemaWorksheet, row, 1);
                if (cellValue == null)
                {
                    continue;
                }

                if (string.Compare("Element Delimiter", cellValue, true) == 0)
                {
                    cellValue = GCExcelReaderHelper.ReadCell(schemaWorksheet, row, 3);
                    // delimiter can be more than 1 char,
                    // split the string by ' ', convert each string to int
                    documentPlug.ElementDelimiters = GetDelimiters(cellValue);

                    if (documentPlug.ElementDelimiters == null)
                    {
                        AddValidationResult(ResultType.Error, row, GCExcelReaderHelper.GetColumnIndex(3), string.Format("'{0}' is invalid element delimiter", cellValue));
                    }

                    delimiterCount++;
                }
                else
                if (string.Compare("Segment Delimiter", cellValue, true) == 0)
                {
                    cellValue = GCExcelReaderHelper.ReadCell(schemaWorksheet, row, 3);
                    documentPlug.SegmentDelimiters = GetDelimiters(cellValue);

                    if (documentPlug.SegmentDelimiters == null)
                    {
                        AddValidationResult(ResultType.Error, row, GCExcelReaderHelper.GetColumnIndex(3), string.Format("'{0}' is invalid segment delimiter", cellValue));
                    }

                    delimiterCount++;
                }
            }
        }
        protected override DocumentPlug ConstructDocumentPlug(ExcelWorksheet schemaWorksheet, int startRow)
        {
            ReadMetadata(schemaWorksheet);

            Pluglet rootPluglet = new Pluglet(
                new PlugletInput()
            {
                Name            = RootNodeName,
                Definition      = "GC root node",
                Type            = PlugletType.Loop,
                Parent          = null,
                IsTagSameAsName = true,                             // for XML spec cert tag is always same as name
            });

            DocumentPlug documentPlug = new DocumentPlug(rootPluglet, BusinessDomain.Xml);

            // Assumption: Element names are not repeated on teh subsequent lines

            IPluglet loopPluglet = null;
            IPluglet nextPluglet;
            int      minOccurs, maxOccurs;
            int      row = startRow;

            // This loop is for level-1 nodes
            try
            {
                while ((nextPluglet = GetNextNode(schemaWorksheet, 1, ref row)) != null)
                {
                    rootPluglet.Children.Add(nextPluglet);
                    nextPluglet.Parent = rootPluglet;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Row: {0}, Error: {1}", row, ex.Message));
            }

            return(documentPlug);
        }
        protected override DocumentPlug ConstructDocumentPlug(ExcelWorksheet schemaWorksheet, int startRow)
        {
            Pluglet rootPluglet = new Pluglet("X12", "GC root Node", PlugletType.Loop, null, 1, -1);

            DocumentPlug documentPlug = new DocumentPlug(rootPluglet, BusinessDomain.X12);

            if (string.IsNullOrWhiteSpace(schemaWorksheet.Name) == false)
            {
                int    documentType;
                string strDocumentType = schemaWorksheet.Name.Substring(0, schemaWorksheet.Name.IndexOf(" "));
                if (int.TryParse(strDocumentType, out documentType) == true)
                {
                    documentPlug.DocumentType = documentType;
                }
            }

            string        currentLoopName = string.Empty;
            string        nextLoopName;
            string        loopName;
            List <string> intermediateLoops = new List <string>();

            IPluglet loopPluglet = null;
            IPluglet nextPluglet;
            IPluglet loopParent;
            int      minOccurs, maxOccurs;
            int      row = startRow;
            int      current, next;

            loopPluglet = rootPluglet;
            while ((nextPluglet = GetSegment(schemaWorksheet, ref row, out nextLoopName)) != null)
            {
                // In case of flat file, we do not have loops
                if (string.IsNullOrEmpty(nextLoopName))
                {
                    rootPluglet.Children.Add(nextPluglet);
                    nextPluglet.Parent = rootPluglet;
                    loopPluglet        = rootPluglet;
                    intermediateLoops.Clear();
                    currentLoopName = nextLoopName;
                }
                else
                {
                    // Check if at least one intermediate loop name is different in next loop
                    if (string.Compare(currentLoopName, nextLoopName) != 0)
                    {
                        loopName = nextLoopName;

                        string[] nextLoops = nextLoopName.Split(new string[] { "->" }, StringSplitOptions.None);

                        // Find first non-matching loop name between intermediateLoops and nextLoops
                        current = next = 0;
                        while (current < intermediateLoops.Count && next < nextLoops.Length)
                        {
                            if (string.Equals(intermediateLoops[current], nextLoops[next], StringComparison.OrdinalIgnoreCase) == false)
                            {
                                break;
                            }
                            current++;
                            next++;
                        }

                        // Get loopParent from current intermediate loops
                        for (int i = intermediateLoops.Count - 1; i >= current; i--)
                        {
                            loopPluglet = loopPluglet.Parent;
                        }

                        loopParent = loopPluglet;

                        // Remove all non-matching intermediate loops
                        int loopsCount = intermediateLoops.Count;
                        for (int i = current; i < loopsCount; i++)
                        {
                            intermediateLoops.RemoveAt(current);
                        }

                        // Add new intermediate loops
                        for (int j = next; j < nextLoops.Length; j++)
                        {
                            // TODO: Any criteria for setting min and max occurs for loop?
                            minOccurs = 1;
                            maxOccurs = 100;

                            // create new loop
                            loopPluglet = new Pluglet(nextLoops[j] + "Loop", "Loop Node", PlugletType.Loop, loopParent, minOccurs, maxOccurs);
                            intermediateLoops.Add(nextLoops[j]);
                            loopParent = loopPluglet;
                        }

                        currentLoopName = nextLoopName;
                    }

                    loopPluglet.Children.Add(nextPluglet);
                    nextPluglet.Parent = loopPluglet;
                }
            }

            return(documentPlug);
        }
예제 #6
0
        // static string RepositoryRoot  = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\..\");


        public IDocumentPlug generateDocumentPlug(BusinessDomain domain)
        {
            IDocumentPlug plug = new DocumentPlug(rootPluglet, domain);

            return(plug);
        }