private static List <queryContainer> transformLinkEntitiesToQueries(CrmServiceClient service, SchemaEntities listOfEntities_DS, DataCollection <LinkEntity> linkEntities, queryContainer upper)
        {
            List <queryContainer> result = new List <queryContainer>();

            foreach (LinkEntity le in linkEntities)
            {
                queryContainer current = new queryContainer
                {
                    isRoot = false,
                    masterEntityLogicalName        = upper.entityLogicalName,
                    entityLogicalName              = le.LinkToEntityName,
                    ExequteAsSeparateLinkedQueries = upper.ExequteAsSeparateLinkedQueries,
                    CollectAllReferences           = upper.CollectAllReferences
                };

                SchemaField attr = GlobalHelper.getFieldFromSchema(listOfEntities_DS, le.LinkToEntityName, le.LinkToAttributeName);
                if (ReferenceEquals(attr, null))
                {
                    throw new Exception("Entity '" + le.LinkToEntityName + "' and its attribute '" + le.LinkToEntityName + "' are absent in source schema file");
                }
                string attrType = attr.primaryKey ? "pk" : attr.type;

                //n:1   entity : linkedEntity
                if (attrType == "pk")
                {
                    current.RelationShipType = relationShipType.Lookup;
                    //recurse to first level
                    FilterExpression filter = new FilterExpression(LogicalOperator.And);
                    filter.AddFilter(le.LinkCriteria);

                    ////move to crm helprer to query
                    //ConditionExpression cond = new ConditionExpression(attr.name, ConditionOperator.Equal, upper.references[le.LinkToAttributeName] - is null);

                    current.expression = transformLinkToQueryExpression(le);
                    //update filter
                    current.expression.Criteria = filter;

                    //add a column to get lookup in resultset
                    upper.expression.ColumnSet.AddColumn(le.LinkFromAttributeName);

                    current.masterEntityLogicalName = le.LinkFromEntityName;
                    current.entityLogicalName       = le.LinkToEntityName;
                    current.masterEntityLookUpName  = le.LinkFromAttributeName;
                    current.primaryKeyName          = le.LinkToAttributeName;

                    result.Add(transformToSeparateQueries(service, listOfEntities_DS, current));
                }
                //1:n   entity : linkedEntity
                else
                {
                    current.RelationShipType       = relationShipType.Child;
                    current.masterEntityLookUpName = attr.name;

                    QueryExpression linkEntityQuiery = new QueryExpression(le.LinkToEntityName);
                    ////move to crm helprer to query
                    //ConditionExpression cond = new ConditionExpression(attr.name, ConditionOperator.Equal, current.masterEntityLookUpID);
                    FilterExpression filter = new FilterExpression(LogicalOperator.And);
                    //filter.AddCondition(cond);
                    linkEntityQuiery.ColumnSet = le.Columns;
                    filter.AddFilter(le.LinkCriteria);
                    linkEntityQuiery.Criteria      = filter;
                    linkEntityQuiery.NoLock        = true;
                    linkEntityQuiery.ExtensionData = le.ExtensionData;

                    current.expression = linkEntityQuiery;
                    if (!ReferenceEquals(le.LinkEntities, null) && le.LinkEntities.Count > 0)
                    {
                        //recurse to itself
                        List <queryContainer> subQueries = transformLinkEntitiesToQueries(service, listOfEntities_DS, le.LinkEntities, current);
                        if (!ReferenceEquals(subQueries, null) && subQueries.Count > 0)
                        {
                            result.AddRange(subQueries);
                        }
                    }
                    result.Add(current);
                }
            }

            return(result);
        }
Esempio n. 2
0
        internal static void replaceGuids(string sorceFilePath, string destinationFilePath, string regexPattern = "", string dictionaryRegexPattern = "")
        {
            if (!File.Exists(sorceFilePath))
            {
                throw new Exception("Source file not found");
            }

            StreamReader sr   = new StreamReader(sorceFilePath);
            string       text = sr.ReadToEnd();

            sr.Close();

            string pattern  = GlobalHelper.isValidString(regexPattern) ? regexPattern : patternDefault;
            string dpattern = GlobalHelper.isValidString(dictionaryRegexPattern) ? regexPattern : dictionaryPatternDefault;


            StringBuilder sbNew = new StringBuilder();

            Dictionary <string, string> dicGuid = new Dictionary <string, string>();


            foreach (Match m in Regex.Matches(text, dpattern))
            {
                string match = m.Groups[1].Value;
                if (!dicGuid.ContainsKey(match))
                {
                    string newGuid = String.Empty;
                    newGuid = Guid.NewGuid().ToString();
                    dicGuid.Add(match, newGuid);
                }
            }


            int lastStart = 0;

            foreach (Match m in Regex.Matches(text, pattern))
            {
                string match   = m.Value;
                string newGuid = String.Empty;
                if (dicGuid.ContainsKey(match))
                {
                    newGuid = dicGuid[match];
                }
                else
                {
                    // do not replace this GUID is coming from outside!
                    newGuid = match;
                }
                sbNew.Append(text.Substring(lastStart, m.Index - lastStart));
                sbNew.Append(newGuid);
                lastStart = m.Index + m.Length;
            }

            sbNew.Append(text.Substring(lastStart));

            StreamWriter sw = new StreamWriter(destinationFilePath, false, Encoding.UTF8);

            sw.Write(sbNew.ToString());
            sw.Flush();
            sw.Close();
        }