Ejemplo n.º 1
0
        private void CheckMissingMandatoryElements(IPluglet currentPluglet, IDocumentFragment currentDocumentFragment)
        {
            foreach (IPluglet childPluglet in currentPluglet.Children)
            {
                if (childPluglet.IsMandatory == false)
                {
                    continue;
                }

                bool childExist = false;
                foreach (IDocumentFragment childDocumentFragment in currentDocumentFragment.Children)
                {
                    if (childDocumentFragment.Pluglet.Tag == childPluglet.Tag)
                    {
                        childExist = true;
                        break;
                    }
                }

                EdiErrorType errorType = EdiErrorType.Error;
                if (childPluglet.IsIgnore)
                {
                    errorType = EdiErrorType.Warning;
                }

                if (childExist == false)
                {
                    Errors.AddSegmentError(childPluglet.Tag, X12ErrorCode.MandatorySegmentMissingCode
                                           , string.Format("{0} : {1}", X12ErrorCode.GetStandardSegmentErrorDescription(X12ErrorCode.MandatorySegmentMissingCode), childPluglet.Tag)
                                           , CurrentElementNumber, CurrentLinePayloadStart + TotalPayloadLength, CurrentLinePayloadEnd + TotalPayloadLength, errorType);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add new DocumentFragment based on it's xPath. If intermediate nodes are not available then this function create those nodes too.
        /// </summary>
        /// <param name="currentDocument"></param>
        /// <param name="newFragment"></param>
        public static void AddDocumentFragment(this DocumentFragment document, IDocumentFragment newFragment)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            if (newFragment == null)
            {
                throw new ArgumentNullException("newFragment");
            }

            ILogger logger = LoggerFactory.Logger;

            logger.Debug("DocumentFragmentExtensions.AddDocumentFragment", "Adding document fragment {0}", newFragment.Name);

            DocumentFragment parentFragment = document.AddIntermediateDocumentFragment(newFragment.Pluglet.Path);

            if (parentFragment.Children == null)
            {
                parentFragment.Children = new List <IDocumentFragment>();
            }
            parentFragment.Children.Add(newFragment);
            ((DocumentFragment)newFragment).Parent = parentFragment;

            logger.Debug("DocumentFragmentExtensions.AddDocumentFragment", "Added document fragment {0}", newFragment.Name);
        }
Ejemplo n.º 3
0
        public void Requeue()
        {
            lock (syncRoot)
            {
                IDocumentFragment <Documents.Queue> result = bucket.MutateIn <Documents.Queue>(Id)
                                                             .Remove(q => q.FetchedAt)
                                                             .Execute();

                reQueued = result.Success;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get the existence result for a fragement of type <typeparamref name="TContent"/> from a document of type <typeparamref name="TDocument"/>,
        /// using a given lambda expression path.
        /// </summary>
        /// <typeparam name="TDocument">Type of the parent document.</typeparam>
        /// <typeparam name="TContent">Type of the subdocument.</typeparam>
        /// <param name="result"><see cref="IDocumentFragment{TDocument}"/> where the the subdocument lookup was returned.</param>
        /// <param name="path">Lambda expression path that navigates to the subdocument from the parent document.
        /// This must be a path that was provided originally to the <see cref="ILookupInBuilder{TDocument}"/>.</param>
        /// <returns>True if the subdocument exists.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="result"/> or <paramref name="path"/> is null.</exception>
        public static bool Exists <TDocument, TContent>(this IDocumentFragment <TDocument> result, Expression <Func <TDocument, TContent> > path)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            return(result.Exists(ParsePath(result as ITypeSerializerProvider, path)));
        }
Ejemplo n.º 5
0
        public static T AttachTo <T>(this T e, IDocumentFragment c)
            where T : INodeConvertible <IHTMLElement>
        {
            // X:\jsc.svn\examples\javascript\Avalon\Test\TestShadowTextBox\TestShadowTextBox\ApplicationCanvas.cs
            // X:\jsc.svn\core\ScriptCoreLib.Avalon\ScriptCoreLib.Avalon\JavaScript\BCLImplementation\System\Windows\Controls\TextBox.cs

            if (e != null)
            {
                c.appendChild(e.AsNode());
            }

            return(e);
        }
Ejemplo n.º 6
0
        public IDocumentFragment SelectSegment(SegmentPath segmentPath, string value)
        {
            IDocumentFragment segmentDocumentFragment = null;

            // If SegmentPath is conditional group then replace value with conditional value
            if (string.IsNullOrWhiteSpace(segmentPath.Value) == false)
            {
                value = segmentPath.Value;
            }

            if (CurrentSegments.TryGetValue(segmentPath.Path, out segmentDocumentFragment) == false)
            {
                List <IDocumentFragment> unusedSegmentsList;
                if (UnusedSegments.TryGetValue(segmentPath.Path, out unusedSegmentsList) == false)
                {
                    PopulateUnusedSegments(segmentPath);
                }

                List <IDocumentFragment> documentFragments;
                if (UnusedSegments.TryGetValue(segmentPath.Path, out documentFragments))
                {
                    if (documentFragments.Count > 0)
                    {
                        if (string.IsNullOrWhiteSpace(value) == false && documentFragments.Count != 1)
                        {
                            foreach (IDocumentFragment documentFragment in documentFragments)
                            {
                                string segmentValue = documentFragment.GetDataSegmentValue(segmentPath.DataSegmentName);
                                if (string.Equals(segmentValue, value, StringComparison.OrdinalIgnoreCase))
                                {
                                    segmentDocumentFragment = documentFragment;
                                }
                            }
                        }
                        else
                        {
                            segmentDocumentFragment = documentFragments[0];
                        }

                        if (segmentDocumentFragment != null)
                        {
                            documentFragments.Remove(segmentDocumentFragment);
                            CurrentSegments.Add(segmentPath.Path, segmentDocumentFragment);
                        }
                    }
                }
            }

            return(segmentDocumentFragment);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Create plug based on the fatpipeDocument (instance tree).
        /// Generated plug will only have pluglets referred in instance tree.
        /// Generated plug will have repeated pluglets in case of loops.
        /// </summary>
        /// <param name="documentFragment"></param>
        /// <returns></returns>
        public static Pluglet GeneratePluglet(this IDocumentFragment documentFragment)
        {
            Pluglet pluglet = documentFragment.Pluglet.Clone(false);

            // Check if instance tree has any childrens, if not then copy all childrens from existingPluglet
            if (documentFragment.Children != null)
            {
                foreach (IDocumentFragment child in documentFragment.Children)
                {
                    pluglet.Children.Add(child.GeneratePluglet());
                }
            }

            return(pluglet);
        }
        public override string GetJobParameter(string id, string name)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            IDocumentFragment <Documents.Job> result = bucket.LookupIn <Documents.Job>(id)
                                                       .Get($"parameters.{name}")
                                                       .Execute();

            return(result.Success ? result.Content($"parameters.{name}").ToString() : null);
        }
Ejemplo n.º 9
0
        public static string GetDataSegmentValue(this IDocumentFragment documentFragment, string dataSegmentName)
        {
            string segmentValue = null;

            if (documentFragment != null && documentFragment.Children != null)
            {
                foreach (DocumentFragment child in documentFragment.Children)
                {
                    if (string.Equals(child.Pluglet.Tag, dataSegmentName, StringComparison.OrdinalIgnoreCase))
                    {
                        segmentValue = child.Value;
                        break;
                    }
                }
            }

            return(segmentValue);
        }
Ejemplo n.º 10
0
        private static IElement GetInnerMostElement(this IDocumentFragment fragment)
        {
            if (fragment.ChildElementCount != 1)
            {
                throw new InvalidOperationException("The provided HTML code did not result in any element.");
            }

            var element = default(IElement);
            var child   = fragment.FirstElementChild;

            do
            {
                element = child;
                child   = element.FirstElementChild;
            }while (child != null);

            return(element);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Count all segments in a given document
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        public static int CountAllChilds(this IDocumentFragment document)
        {
            if (document.Pluglet.PlugletType == PlugletType.Segment)
            {
                return(1);
            }

            if (document.Children == null)
            {
                return(0);
            }

            int count = 0;

            foreach (IDocumentFragment child in document.Children)
            {
                count += child.CountAllChilds();
            }

            return(count);
        }
Ejemplo n.º 12
0
        public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
        {
            lock (syncLock)
            {
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    int invisibilityTimeoutEpoch = DateTime.UtcNow.Add(invisibilityTimeout.Negate()).ToEpoch();
                    logger.Trace("Looking for any jobs from the queue");

                    using (new CouchbaseDistributedLock(DISTRIBUTED_LOCK_KEY, defaultLockTimeout, storage))
                    {
                        using (IBucket bucket = storage.Client.OpenBucket(storage.Options.DefaultBucket))
                        {
                            BucketContext   context = new BucketContext(bucket);
                            Documents.Queue data    = context.Query <Documents.Queue>()
                                                      .Where(q => q.DocumentType == DocumentTypes.Queue && queues.Contains(q.Name) && (N1QlFunctions.IsMissing(q.FetchedAt) || q.FetchedAt < invisibilityTimeoutEpoch))
                                                      .OrderBy(q => q.CreatedOn)
                                                      .FirstOrDefault();

                            if (data != null)
                            {
                                IDocumentFragment <Documents.Queue> result = bucket.MutateIn <Documents.Queue>(data.Id)
                                                                             .Upsert(q => q.FetchedAt, DateTime.UtcNow.ToEpoch(), true)
                                                                             .Execute();

                                if (result.Success)
                                {
                                    logger.Trace($"Found job {data.JobId} from the queue {data.Name}");
                                    return(new FetchedJob(storage, data));
                                }
                            }
                        }
                    }

                    logger.Trace($"Unable to find any jobs in the queue. Will check the queue for jobs in {storage.Options.QueuePollInterval.TotalSeconds} seconds");
                    cancellationToken.WaitHandle.WaitOne(storage.Options.QueuePollInterval);
                }
            }
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Move to given xPath fragment (leaf as well as non-leaf)
        /// </summary>
        /// <param name="rootFragment"></param>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public static IDocumentFragment MoveTo(this IDocumentFragment rootFragment, string xPath)
        {
            if (rootFragment == null)
            {
                return(null);
            }

            IDocumentFragment documentFragment = null;

            string[] pathElements = xPath.Split(new string[] { rootFragment.Pluglet.PathSeperator }, StringSplitOptions.None);

            // Check if root element and 1st element in path match
            if (string.Equals(pathElements[0], rootFragment.Name, StringComparison.OrdinalIgnoreCase) == false)
            {
                throw new PlugDataModelException(
                          string.Format("Error while traversing path {0}. First element in path {1} does not match with root element {2} of document",
                                        xPath, pathElements[0], rootFragment.Name));
            }

            IDocumentFragment prevFragment = rootFragment;

            for (int i = 1; i < pathElements.Length; i++)
            {
                documentFragment = prevFragment.Children.FirstOrDefault(
                    f => string.Equals(f.Name, pathElements[i], StringComparison.OrdinalIgnoreCase));

                if (documentFragment == null)
                {
                    break;
                }

                prevFragment = documentFragment;
            }

            return(documentFragment);
        }
Ejemplo n.º 14
0
        private void ReadSegmentsWithIdDataTypeChildrens(Dictionary <string, List <IDocumentFragment> > contingencyOccurances, IDocumentFragment documentFragment)
        {
            if (documentFragment == null ||
                documentFragment.Pluglet == null ||
                documentFragment.Pluglet.PlugletType == PlugletType.Data ||
                documentFragment.Pluglet.PlugletType == PlugletType.Unknown)
            {
                return;
            }

            bool currentSegmentAdded = false;

            if (documentFragment.Children != null && documentFragment.Children.Count > 0)
            {
                foreach (IDocumentFragment child in documentFragment.Children)
                {
                    if (child.Pluglet != null)
                    {
                        if (child.Pluglet.PlugletType == PlugletType.Data)
                        {
                            if (child.Pluglet.DataType is X12_IdDataType && child.Pluglet.IsMandatory == true && currentSegmentAdded == false)
                            {
                                List <IDocumentFragment> contingencies;
                                if (contingencyOccurances.TryGetValue(documentFragment.Pluglet.Path, out contingencies) == false)
                                {
                                    contingencies = new List <IDocumentFragment>();
                                    contingencyOccurances.Add(documentFragment.Pluglet.Path, contingencies);
                                }
                                contingencies.Add(documentFragment);

                                currentSegmentAdded = true;
                            }
                        }
                        else
                        {
                            ReadSegmentsWithIdDataTypeChildrens(contingencyOccurances, child);
                        }
                    }
                }
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Read xml file and construct IFatpipeDocument.
        /// Xml file reader will traverse Xml files and for each element
        /// match it with current pluglet. If match fails then it tries to
        /// find matching pluglet (similar to X12).
        /// </summary>
        /// <returns></returns>
        public IFatpipeDocument ReadFile(Stream xmlFileStream, IDocumentPlug documentPlug)
        {
            if (xmlFileStream == null)
            {
                throw new ArgumentNullException("xmlFileStream", "Xml file stream cannot be null");
            }

            if (documentPlug == null)
            {
                throw new ArgumentNullException("documentPlug", "Document plug cannot be null");
            }

            string location = "XmlFileReader.ReadFile";

            Logger.Debug(location, "Start");

            BeautifiedOriginalPayload = string.Empty;
            CurrentElementNumber      = 0;
            TotalPayloadLength        = 0;
            CurrentLinePayloadStart   = 0;
            CurrentLinePayloadEnd     = 0;
            CurrentLevel = 0;
            Stopwatch sw = new Stopwatch();

            sw.Start();

            errors = new InterchangeErrors();

            // Since xml file doesn't have concept of ST/SE, ans we want to use InterchangeErrors for reporting purpose
            // create dummy transaction set details
            Errors.AddTransactionSetDetails(1, "", "", true);

            IPluglet currentPluglet = documentPlug.RootPluglet;

            currentPluglet.ResetCurrentOccurances();
            currentPluglet.InitializeStartSegmentList();

            FatpipeDocumentInst = new FatpipeDocument();
            FatpipeDocumentInst.DocumentPlug = documentPlug;
            FatpipeDocumentInst.RootFragment = currentPluglet.ConstructDocumentFragment(null, null);

            IDocumentFragment currentDocumentFragment = FatpipeDocumentInst.RootFragment;
            IDocumentFragment newDocumentFragment     = null;

            bool isLeafNode = false;

            try
            {
                XmlTextReader xmlReader = new XmlTextReader(xmlFileStream);

                // If some element doesn't match document plutlet then stop
                // TODO: Should we try to match other elements? If yes, which pluglet to start with?
                // Also we need to ignore this entire element
                bool stopProcessing = false;

                while (xmlReader.Read())
                {
                    switch (xmlReader.NodeType)
                    {
                    case XmlNodeType.Element:
                        isLeafNode = false;
                        AddStartElementToPayload(xmlReader.Name, xmlReader.IsEmptyElement);

                        if (xmlReader.IsEmptyElement)
                        {
                            break;
                        }

                        if (stopProcessing == false)
                        {
                            currentDocumentFragment = ConstructNewDocumentFragment(xmlReader.Name, currentPluglet, currentDocumentFragment);
                        }

                        // If some element doesn't match document plutlet then stop
                        // TODO: Should we try to match other elements? If yes, which pluglet to start with?
                        // Also we need to ignore this entire element
                        if (currentDocumentFragment == null)
                        {
                            stopProcessing = true;
                        }
                        else
                        {
                            currentPluglet = currentDocumentFragment.Pluglet;
                        }

                        CurrentLevel++;
                        CurrentElementNumber++;
                        break;

                    case XmlNodeType.Text:
                        isLeafNode = true;
                        AddValueToPayload(xmlReader.Value);
                        if (stopProcessing == false)
                        {
                            currentDocumentFragment.Value = xmlReader.Value;
                        }
                        // Assumption: Leaf nodes are on same line and non-leaf nodes are 1-per line (separate line for start and end element)
                        // If this assumption is wrong then we can construct the xml in string format to match fulfill above assumption
                        // and then Ux can use this string version of xml to highlight the errors.
                        CurrentElementNumber--;     // Decrement since leaf level elements are one line, so we need to increment element on endElement only.
                        break;

                    case XmlNodeType.EndElement:
                        CurrentLevel--;
                        AddEndElementToPayload(xmlReader.Name, isLeafNode);
                        if (stopProcessing == false)
                        {
                            // Check if all mandatory segments were present
                            CheckMissingMandatoryElements(currentPluglet, currentDocumentFragment);
                            currentDocumentFragment = currentDocumentFragment.Parent;
                            currentPluglet          = currentPluglet.Parent;
                        }

                        CurrentElementNumber++;
                        isLeafNode = false;
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (XmlException xmlException)
            {
                // TODO: Pass start and end postition
                Errors.AddGenericError(currentPluglet == null ? "N/A" : currentPluglet.Name, X12ErrorCode.UnexpectedSegmentCode,
                                       string.Format("Error parsing XML document: {0}", xmlException.Message),
                                       CurrentElementNumber / 2, CurrentLinePayloadStart + TotalPayloadLength, CurrentLinePayloadEnd + TotalPayloadLength);
            }
            catch (Exception exception)
            {
                // TODO: Pass start and end postition (for all Errors.Add* calls) in this file.
                Errors.AddGenericError(currentPluglet == null ? "N/A" : currentPluglet.Name, X12ErrorCode.UnexpectedSegmentCode,
                                       "Internal error occurred, please contact Maarg",
                                       CurrentElementNumber / 2, CurrentLinePayloadStart + TotalPayloadLength, CurrentLinePayloadEnd + TotalPayloadLength);
                Logger.Error(location, EventId.XmlReaderUnhandledException, "Error occured during xml file processing: {0}", exception.ToString());
            }

            FatpipeDocumentInst.BeautifiedOriginalPayloadBody = BeautifiedOriginalPayload;

            sw.Stop();
            Logger.Debug(location, "Stop - Elapsed time {0} ms", sw.ElapsedMilliseconds);

            return(FatpipeDocumentInst);
        }
Ejemplo n.º 16
0
        private IDocumentFragment ConstructNewDocumentFragment(string elementName, IPluglet currentPluglet, IDocumentFragment currentDocumentFragment)
        {
            IDocumentFragment newDocumentPluglet = null;

            IPluglet nextPluglet = null;

            // Special case for root pluglet
            if (CurrentElementNumber == 0)
            {
                nextPluglet = currentPluglet;

                string rootNodeName = elementName;
                int    pos          = elementName.IndexOf(":");
                if (pos != -1)
                {
                    rootNodeName = elementName.Substring(pos + 1);
                }

                if (string.Equals(nextPluglet.Tag, rootNodeName, StringComparison.InvariantCultureIgnoreCase) == false)
                {
                    Errors.AddSegmentError(elementName, -1,
                                           string.Format("Invalid root node name. Expected: {0}, Actual {1}", nextPluglet.Tag, rootNodeName), CurrentElementNumber,
                                           CurrentLinePayloadStart + TotalPayloadLength, CurrentLinePayloadEnd + TotalPayloadLength, EdiErrorType.Error);
                }
            }
            else
            {
                foreach (IPluglet childPluglet in currentPluglet.Children)
                {
                    if (string.Equals(childPluglet.Tag, elementName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        nextPluglet = childPluglet;
                        break;
                    }
                }
            }

            if (nextPluglet == null)
            {
                Errors.AddSegmentError(elementName, X12ErrorCode.UnrecognizedSegmentIDCode,
                                       X12ErrorCode.GetStandardSegmentErrorDescription(X12ErrorCode.UnrecognizedSegmentIDCode), CurrentElementNumber,
                                       CurrentLinePayloadStart + TotalPayloadLength, CurrentLinePayloadEnd + TotalPayloadLength, EdiErrorType.Error);
                // TODO: Should we add 'Unrecognized segment' pluglet here?
            }
            else
            {
                newDocumentPluglet = new DocumentFragment()
                {
                    Parent  = currentDocumentFragment,
                    Pluglet = nextPluglet,
                    Value   = elementName,
                };

                if (currentDocumentFragment.Children == null)
                {
                    ((DocumentFragment)currentDocumentFragment).Children = new List <IDocumentFragment>();
                }

                currentDocumentFragment.Children.Add(newDocumentPluglet);
            }

            return(newDocumentPluglet);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Construct EDI representation of this fragment
        /// </summary>
        /// <param name="ediDelimiters"></param>
        /// <param name="internalSegment"></param>
        /// <returns></returns>
        public string ToEDI(Delimiters ediDelimiters, bool internalSegment)
        {
            if (Children == null)
            {
                return(string.Empty);
            }

            StringBuilder segment = new StringBuilder();

            string componentDelimiter = ((char)ediDelimiters.ComponentSeperator).ToString();
            string fieldDelimiter     = ((char)ediDelimiters.FieldSeperator).ToString();
            string delimiter          = internalSegment == true ? componentDelimiter : fieldDelimiter;

            // construct segment only for parent with at least 1 leaf node
            if (Children.Any(c => c.Pluglet.PlugletType == PlugletType.Data))
            {
                if (internalSegment == false)
                {
                    segment.AppendFormat("{0}", Pluglet.Tag);
                }

                foreach (IDocumentFragment child in Children)
                {
                    if (child.Pluglet.PlugletType == PlugletType.Data)
                    {
                        if (segment.Length != 0 || internalSegment == false)
                        {
                            segment.AppendFormat("{0}{1}", delimiter, child.Value);
                        }
                        else
                        {
                            segment.AppendFormat("{0}", child.Value);
                        }
                    }
                    else
                    {
                        segment.AppendFormat("{0}{1}", fieldDelimiter, child.ToEDI(ediDelimiters, true));
                    }
                }
            }
            else
            {
                string childSegment;

                StringBuilder segmentB = new StringBuilder(3);
                segmentB.Append((char)ediDelimiters.SegmentDelimiter);
                if (ediDelimiters.SegmentDelimiterSuffix1 != -1)
                {
                    segmentB.Append((char)ediDelimiters.SegmentDelimiterSuffix1);
                }
                if (ediDelimiters.SegmentDelimiterSuffix2 != -1)
                {
                    segmentB.Append((char)ediDelimiters.SegmentDelimiterSuffix2);
                }
                string segmentDelimiter = segmentB.ToString();


                foreach (IDocumentFragment child in Children)
                {
                    childSegment = child.ToEDI(ediDelimiters, false);
                    segment.AppendFormat("{0}{1}"
                                         , string.IsNullOrEmpty(segment.ToString()) ? string.Empty : segmentDelimiter
                                         , childSegment);
                }

                IDocumentFragment firstChild = Children[0];
                // Special case for ading SE/GE/IEA segments
                switch (firstChild.Pluglet.Tag)
                {
                case "ST":
                    string txnSetNumber = "";
                    int    segmentCount = 0;
                    if (firstChild.Children != null && firstChild.Children.Count > 1)
                    {
                        txnSetNumber = firstChild.Children[1].Value;
                    }
                    // Set segmentCount to child count of X12_00401_850
                    segmentCount = this.CountAllChilds() + 1;     // +1 for SE segment
                    //segment.AppendFormat("{0}SE{1}{2}{3}{4}", segmentDelimiter, fieldDelimiter, segmentCount, fieldDelimiter, txnSetNumber);

                    //segment.AppendFormat("{0}GE{1}1{2}1", segmentDelimiter, fieldDelimiter, fieldDelimiter);
                    //segment.AppendFormat("{0}IEA{1}1{2}{3}", segmentDelimiter, fieldDelimiter, fieldDelimiter, "NNNN");

                    break;
                }
            }

            return(segment.ToString());
        }
Ejemplo n.º 18
0
 public JsDocumentFragment(IDocumentFragment baseObject, ISvgScriptEngine engine)
     : base(baseObject, engine)
 {
 }
Ejemplo n.º 19
0
        private void ValidateContingencies()
        {
            // Assumption: If id data segment is not present then this function will not report error
            // as we must have already reported that data segment as missing segment earlier
            Dictionary <string, List <IDocumentFragment> > contingencyOccurances = new Dictionary <string, List <IDocumentFragment> >();

            ReadSegmentsWithIdDataTypeChildrens(contingencyOccurances, FatpipeDocumentInst.RootFragment);

            foreach (string path in contingencyOccurances.Keys)
            {
                // Select pluglet for current path
                // Selecting pluglet from the 1st documentFragment as pluglet will be
                // same for all documentFragments with same path
                IDocumentFragment documentFragment = contingencyOccurances[path][0];
                IPluglet          pluglet          = documentFragment.Pluglet;

                // Pluglet will point to segment, process all child with data type as X12_IdDataType
                // Filter out non-mandatory data type pluglets
                foreach (Pluglet child in pluglet.Children)
                {
                    if (child.PlugletType == PlugletType.Data && child.DataType is X12_IdDataType && child.IsMandatory == true)
                    {
                        List <string> presentValues = GetAllPresentValues(contingencyOccurances[path], child);

                        X12_IdDataType dataType = child.DataType as X12_IdDataType;
                        foreach (string allowedValue in dataType.AllowedValues.Keys)
                        {
                            Contingency contingencies = dataType.GetContingencies(allowedValue);

                            // TODO: Use Ignore flag at id value level

                            // If Id value does not have any contingency then segment with that value must exist
                            if (contingencies == null || contingencies.ContingencyValues.Count == 0)
                            {
                                if (presentValues.Contains(allowedValue) == false && dataType.IsOptionalValue(allowedValue) == false)
                                {
                                    Errors.AddSegmentError(pluglet.Tag, X12ErrorCode.DeMandatoryIdValueMissingCode
                                                           , string.Format("{0} : {1}", X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeMandatoryIdValueMissingCode), allowedValue)
                                                           , documentFragment.SequenceNumber, documentFragment.StartOffset, documentFragment.EndOffset, EdiErrorType.Error);
                                }
                            }
                            // If Id value has contingencies of type Enumeration then segment with that value or any contingency value must exist
                            else if (contingencies.Type == ContingencyType.Enumeration)
                            {
                                bool valuePresent = presentValues.Contains(allowedValue);
                                if (valuePresent == false)
                                {
                                    foreach (string alternateValue in contingencies.ContingencyValues)
                                    {
                                        valuePresent = presentValues.Contains(alternateValue);
                                        if (valuePresent)
                                        {
                                            break;
                                        }
                                    }
                                }

                                if (valuePresent == false)
                                {
                                    Errors.AddSegmentError(pluglet.Tag, X12ErrorCode.DeMandatoryIdValueOrAlternativeValueMissingCode
                                                           , string.Format("{0} : {1}", X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeMandatoryIdValueOrAlternativeValueMissingCode), allowedValue)
                                                           , documentFragment.SequenceNumber, documentFragment.StartOffset, documentFragment.EndOffset, EdiErrorType.Error);
                                }
                            }
                            // If contingency type is cross segment then either both values must exist or both values missing
                            else if (contingencies.Type == ContingencyType.CrossSegment)
                            {
                                // TODO: handle all values in contingencies.ContingencyValues
                                string xPath = contingencies.ContingencyValues[0];
                                bool   currentValuePresent      = presentValues.Contains(allowedValue);
                                bool   crossSegmentValuePresent = IsCrossSegmentValuePresent(contingencyOccurances, xPath, pluglet.PathSeperator);

                                if (currentValuePresent != crossSegmentValuePresent)
                                {
                                    Errors.AddSegmentError(pluglet.Tag, X12ErrorCode.DeCrossSegmentIdValueOccurancesDoesNotMatch
                                                           , string.Format("{0} : {1} {2}", X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeCrossSegmentIdValueOccurancesDoesNotMatch), allowedValue, xPath)
                                                           , documentFragment.SequenceNumber, documentFragment.StartOffset, documentFragment.EndOffset, EdiErrorType.Error);
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 20
0
        static void GetDisplay <T>(IDocumentFragment <T> fragment, string path)
        {
            string format = "Path='{0}' Value='{1}'";

            Console.WriteLine(format, path, fragment.Content(path));
        }
Ejemplo n.º 21
0
        protected override IProviderResponse <TEntityFragment> MapResponse <TEntityFragment>(IDocumentFragment <TSvcFragmentEntity> cbSvcResult, string fragmentKey)
        {
            if (cbSvcResult != null)
            {
                int fragmentIndex;

                dynamic data = (Int32.TryParse(fragmentKey, out fragmentIndex)) ? cbSvcResult.Content <TEntityFragment>(fragmentIndex) : cbSvcResult.Content <TEntityFragment>(fragmentKey);

                if (data == null && typeof(TEntityFragment) == typeof(bool))
                {
                    data = cbSvcResult.Exists(fragmentKey);
                }

                return(new ProviderResponse <TEntityFragment>()
                {
                    Data = data,
                    Success = cbSvcResult.Success,
                    Message = cbSvcResult.Message,
                    ResponseStatus = cbSvcResult.Status.ToString("f"),
                    Exception = cbSvcResult.Exception,
                    Version = cbSvcResult.Cas
                });
            }

            return(new ProviderResponse <TEntityFragment>()
            {
                Success = false,
                Message = string.Empty,
                ResponseStatus = CustomResponseErrors.CBSvcMappingError.ToString("f")
            });
        }
        /// <summary>
        /// Construct DocumentFragment from IPluglet for non-leaf elements
        /// </summary>
        /// <param name="pluglet"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static DocumentFragment ConstructDocumentFragment(this IPluglet pluglet, IDocumentFragment parent, string value)
        {
            if (pluglet == null)
            {
                throw new ArgumentNullException("pluglet");
            }

            ++pluglet.CurrentOccurrences;

            DocumentFragment documentFragment = new DocumentFragment()
            {
                Pluglet = pluglet,
                Parent  = parent,
                Value   = value,
            };

            return(documentFragment);
        }
Ejemplo n.º 23
0
        private static List <BizRuleValidationResult> ValidateBizRules(BizRuleSet bizRuleSet, List <DocumentPlugValidationInfo> documentPlugValidationInfoList)
        {
            if (bizRuleSet == null)
            {
                throw new ArgumentNullException("bizRuleSet", "bizRuleSet cannot be null");
            }

            if (documentPlugValidationInfoList == null || documentPlugValidationInfoList.Count <= 1)
            {
                throw new ArgumentNullException("documentPlugValidationInfoList", "documentPlugValidationInfoList cannot be null and should have > 1 entries");
            }

            List <BizRuleValidationResult> bizRuleValidationResults = new List <BizRuleValidationResult>();

            List <RuleSegments> ruleSegmentsList = new List <RuleSegments>();

            foreach (int messageDomainId in bizRuleSet.MessageDomainIds)
            {
                RuleSegments ruleSegments = null;
                foreach (DocumentPlugValidationInfo documentPlugValidationInfo in documentPlugValidationInfoList)
                {
                    if (documentPlugValidationInfo.DocumentPlug != null && documentPlugValidationInfo.DocumentPlug.DocumentType == messageDomainId)
                    {
                        ruleSegments = new RuleSegments(documentPlugValidationInfo.FatpipeDocument, documentPlugValidationInfo.SpecCertName);
                        break;
                    }
                }
                ruleSegmentsList.Add(ruleSegments);
            }

            // Conditional groups - first get all BizRuleValidationResult for all groups
            // then for each group check if at least one is successful, in case
            // of success, mark others as success?
            Dictionary <string, List <BizRuleValidationResult> > conditionalRulesResult = new Dictionary <string, List <BizRuleValidationResult> >();
            string segmentValue;

            foreach (string firstSegmentPath in bizRuleSet.BizRules.Keys)
            {
                // TODO: Handle segmentPath being null/empty

                List <BizRule> bizRules = bizRuleSet.BizRules[firstSegmentPath];

                bool loopHasValues = true;
                int  loopOccurance = 0;
                while (loopHasValues)
                {
                    foreach (BizRule bizRule in bizRules)
                    {
                        string valueToMatch = null;
                        BizRuleValidationResult bizRuleValidationResult = new BizRuleValidationResult();
                        bizRuleValidationResult.RuleName = bizRule.Name;
                        bizRuleValidationResult.Type     = ResultType.Success;
                        bizRuleValidationResult.RuleInfo = new List <BizRuleInfo>();

                        int  i = 0;
                        bool isRuleSuccessful = true;
                        foreach (SegmentPath segmentPath in bizRule.SegmentPaths)
                        {
                            if (string.IsNullOrWhiteSpace(segmentPath.OriginalPath))
                            {
                                i++;
                                continue;
                            }
                            BizRuleInfo bizRuleInfo = new BizRuleInfo()
                            {
                                SegmentPath = segmentPath.OriginalPath,
                            };

                            if (ruleSegmentsList[i] != null)
                            {
                                bizRuleInfo.FileName = ruleSegmentsList[i].CertName;

                                IDocumentFragment documentFragment = ruleSegmentsList[i].SelectSegment(segmentPath, valueToMatch);
                                if (documentFragment != null)
                                {
                                    segmentValue = documentFragment.GetDataSegmentValue(segmentPath.DataSegmentName);
                                    if (string.IsNullOrWhiteSpace(valueToMatch) || string.IsNullOrWhiteSpace(segmentPath.Value) == false)
                                    {
                                        valueToMatch = segmentValue;
                                    }
                                    else
                                    if (string.Equals(valueToMatch, segmentValue, StringComparison.OrdinalIgnoreCase) == false)
                                    {
                                        isRuleSuccessful = false;
                                    }
                                    bizRuleInfo.Value = segmentValue;
                                }
                                else if (loopOccurance != 0 && i == 0)
                                {
                                    loopHasValues = false;
                                    break;
                                }
                            }
                            else
                            {
                                bizRuleInfo.FileName = "Not provided";
                            }

                            if (string.IsNullOrWhiteSpace(bizRuleInfo.Value))
                            {
                                bizRuleInfo.Value = "<Not Valued>";
                                isRuleSuccessful  = false;
                            }

                            bizRuleValidationResult.RuleInfo.Add(bizRuleInfo);

                            i++;
                        }

                        if (loopHasValues)
                        {
                            // TODO: Conditional mandatory will impact setting type to error.
                            if (isRuleSuccessful == false)
                            {
                                bizRuleValidationResult.Type = bizRule.Type == BizRuleType.Optional ? ResultType.Warning : ResultType.Error;
                            }


                            if (string.IsNullOrWhiteSpace(bizRule.ConditionalGroupName) == false)
                            {
                                List <BizRuleValidationResult> bizRuleResults;
                                if (conditionalRulesResult.TryGetValue(bizRule.ConditionalGroupName, out bizRuleResults) == false)
                                {
                                    bizRuleResults = new List <BizRuleValidationResult>();
                                    conditionalRulesResult.Add(bizRule.ConditionalGroupName, bizRuleResults);
                                }
                                bizRuleResults.Add(bizRuleValidationResult);
                            }

                            bizRuleValidationResults.Add(bizRuleValidationResult);
                        }
                    }

                    foreach (RuleSegments ruleSegments in ruleSegmentsList)
                    {
                        ruleSegments.MoveCurrentToUsed();
                    }

                    loopOccurance++;
                }
            }

            if (conditionalRulesResult.Count > 0)
            {
                foreach (List <BizRuleValidationResult> bizRuleResults in conditionalRulesResult.Values)
                {
                    if (bizRuleResults.Any(bizRuleResult => bizRuleResult.Type == ResultType.Success))
                    {
                        foreach (BizRuleValidationResult bizRuleResult in bizRuleResults)
                        {
                            bizRuleResult.Type = ResultType.Success;
                        }
                    }
                }
            }

            return(bizRuleValidationResults);
        }
        private void Acquire(TimeSpan timeout)
        {
            logger.Trace($"Trying to acquire lock for {resource} within {timeout.TotalSeconds} seconds");

            System.Diagnostics.Stopwatch acquireStart = new System.Diagnostics.Stopwatch();
            acquireStart.Start();

            string id = $"{resource}:{DocumentTypes.Lock}".GenerateHash();

            while (string.IsNullOrEmpty(resourceId))
            {
                // default ttl for lock document
                TimeSpan ttl = DateTime.UtcNow.Add(timeout).AddMinutes(1).TimeOfDay;

                // read the document
                IDocumentResult <Lock> document = bucket.GetDocument <Lock>(id);

                // false means the document does not exists got ahead and create
                if (document.Success == false)
                {
                    Lock @lock = new Lock
                    {
                        Id       = id,
                        Name     = resource,
                        ExpireOn = DateTime.UtcNow.Add(timeout).ToEpoch()
                    };

                    IOperationResult <Lock> result = bucket.Insert(@lock.Id, @lock, ttl);
                    if (result.Success)
                    {
                        resourceId = id;
                        break;
                    }
                }
                else if (document.Content != null)
                {
                    if (document.Content.ExpireOn < DateTime.UtcNow.ToEpoch())
                    {
                        IDocumentFragment <Lock> result = bucket.MutateIn <Lock>(id)
                                                          .WithCas(document.Document.Cas)
                                                          .WithExpiry(ttl)
                                                          .Upsert(l => l.ExpireOn, DateTime.UtcNow.Add(timeout).ToEpoch(), false)
                                                          .Execute();

                        if (result.Success)
                        {
                            resourceId = id;
                            break;
                        }
                    }
                }

                // check the timeout
                if (acquireStart.ElapsedMilliseconds > timeout.TotalMilliseconds)
                {
                    throw new CouchbaseDistributedLockException($"Could not place a lock on the resource '{resource}': Lock timeout.");
                }

                // sleep for 2000 millisecond
                logger.Trace($"Unable to acquire lock for {resource}. Will check try after 2 seconds");
                System.Threading.Thread.Sleep(2000);
            }

            logger.Trace($"Acquired lock for {resource} in {acquireStart.Elapsed.TotalSeconds} seconds");
        }
Ejemplo n.º 25
0
        public static IFatpipeDocument Transform(IFatpipeDocument sourceDocument, ITransformPlug transformPlug)
        {
            if (sourceDocument == null)
            {
                throw new ArgumentNullException("sourceDocument");
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            FatpipeDocument targetDocument = new FatpipeDocument();

            targetDocument.RootFragment = transformPlug.TargetDocument.RootPluglet.ConstructDocumentFragment(null, null);;

            //TODO: Handle loops
            foreach (ITransformGroup transformGroup in transformPlug.Facets)
            {
                Logger.Debug("DocumentTransformer.Transform", "Processing {0} tranformGroup", transformGroup.Name);

                foreach (ITransformLink transformLink in transformGroup.Links)
                {
                    Logger.Debug("DocumentTransformer.Transform", "Processing Link#-{0}: {1} [{2}] => {3} [{4}]"
                                 , transformLink.Name
                                 , transformLink.Source.ReferenceType, transformLink.Source.Name
                                 , transformLink.Target.ReferenceType, transformLink.Target.Name);

                    //TODO: Handle all kind of transformation
                    if (transformLink.Source.ReferenceType == ReferenceType.Document &&
                        transformLink.Target.ReferenceType == ReferenceType.Document)
                    {
                        // Traverse source path
                        IDocumentFragment sourceFragment = sourceDocument.RootFragment.MoveTo(transformLink.Source.Name);
                        if (sourceFragment != null)
                        {
                            IPluglet targetPluglet = targetDocument.RootFragment.Pluglet.MoveTo(transformLink.Target.Name);

                            if (targetPluglet != null)
                            {
                                Logger.Debug("DocumentTransformer.Transform", "Source = {0}, Target = {1}", sourceFragment.Name, targetPluglet.Tag);

                                Dictionary <string, string> attributes = new Dictionary <string, string>();
                                foreach (IPluglet attr in targetPluglet.Attributes)
                                {
                                    string attributeName = attr.Name;
                                    attributeName.Remove(0, 1);
                                    attributeName.Remove(attributeName.Length, 1);
                                    attributes.Add(attributeName, attributeName);
                                }

                                bool   isAttribute = false;
                                string name        = transformLink.Target.Name.Substring(
                                    transformLink.Target.Name.LastIndexOf(targetPluglet.PathSeperator) + targetPluglet.PathSeperator.Length);
                                // Check if this transformation point to attribute or leaf node
                                if (targetPluglet.Attributes != null && attributes.ContainsKey(name))
                                {
                                    isAttribute = true;
                                }

                                if (isAttribute == false)
                                {
                                    IDocumentFragment newFragment = targetPluglet.ConstructDocumentFragment(null, sourceFragment.Value);
                                    ((DocumentFragment)targetDocument.RootFragment).AddDocumentFragment(newFragment);
                                }
                                else
                                {
                                    ((DocumentFragment)targetDocument.RootFragment).AddDocumentFragment(transformLink.Target.Name, sourceFragment.Value);
                                }
                            }
                            else
                            {
                                string error = string.Format("Link#-{0}: {1} path not found in target tree", transformLink.Name, transformLink.Target.Name);
                                if (targetDocument.Errors == null)
                                {
                                    targetDocument.Errors = new List <string>();
                                }
                                targetDocument.Errors.Add(error);

                                Logger.Error("DocumentTransformer.Transform", EventId.DocTransformerNoMapping, error);
                            }
                        }
                    }
                    else
                    {
                        Logger.Debug("DocumentTransformer.Transform", "Ignoring Link#-{0}", transformLink.Name);
                    }
                }

                //TODO: Handle transformGroup.Formulas
            }

            sw.Stop();
            Logger.Debug("DocumentTransformer.Transform", "Stop. Elapsed time {0} ms", sw.ElapsedMilliseconds);

            return(targetDocument);
        }