/// <summary>
        /// Export entities from CRM to dictionary of blocks with entities
        /// </summary>
        /// <returns>Blocks with exported entities</returns>
        public ShuffleBlocks ExportFromCRM()
        {
            log.StartSection("ExportFromCRM");
            if (definition == null)
            {
                throw new ArgumentNullException("Definition", "Shuffle definition must be specified to export data");
            }
            var blocks = new ShuffleBlocks();

            ExistingSolutionVersions = null;
            if (ShuffleDefinition.Blocks.Items.Any(b => (b is DataBlock data && data.Export != null) || b is SolutionBlock sol && sol.Export != null))
            {
                stoponerror = ShuffleDefinition.StopOnError;
                timeout     = ShuffleDefinition.TimeoutSpecified ? ShuffleDefinition.Timeout : -1;
                double savedtimeout = -1;
                if (timeout > -1)
                {
                    savedtimeout = SetTimeout();
                }

                var totalBlocks  = ShuffleDefinition.Blocks.Items.Length;
                var currentBlock = 0;
                foreach (var block in ShuffleDefinition.Blocks.Items)
                {
                    currentBlock++;
                    SendStatus(totalBlocks, currentBlock, -1, -1);
                    if (block is DataBlock datablock)
                    {
                        var cExported = ExportDataBlock(blocks, datablock);
                        var name      = datablock.Name;
                        if (cExported != null)
                        {
                            if (blocks.ContainsKey(name))
                            {
                                SendLine($"Block already added: {name}");
                            }
                            else
                            {
                                blocks.Add(name, cExported);
                            }
                        }
                    }
                    else if (block is SolutionBlock solutionblock)
                    {
                        if (ExistingSolutionVersions == null)
                        {
                            GetCurrentVersions();
                        }
                        ExportSolutionBlock(solutionblock);
                    }
                }
                SendStatus(0, 0, 0, 0);
                if (savedtimeout > -1)
                {
                    ResetTimeout(savedtimeout);
                }
            }
            log.EndSection();
            return(blocks);
        }
Beispiel #2
0
        public ShuffleBlocks MergeFiles(Dictionary <string, XmlDocument> datas)
        {
            log.StartSection("Start MergeFiles");
            ShuffleBlocks blocks = new ShuffleBlocks();

            foreach (var dataKeyValuePair in datas)
            {
                var blockNameItemIdCombinationString = dataKeyValuePair.Key;
                var blockNameItemIdCombination       = blockNameItemIdCombinationString.Split('\\');
                var blockName = blockNameItemIdCombination[0];

                var block = Deserialize(dataKeyValuePair.Value);
                var blockEntityCollection = block.First().Value;

                if (!blocks.ContainsKey(blockName))
                {
                    blocks.Add(blockName, blockEntityCollection);
                }
                else
                {
                    blocks[blockName].Add(blockEntityCollection.First());
                }
            }

            log.EndSection();
            return(blocks);
        }
 private static void AddRelationFilter(ShuffleBlocks blocks, DataBlockRelation relation, XmlNode xEntity, ILoggable log)
 {
     if (blocks != null && blocks.Count > 0)
     {
         var block       = relation.Block;
         var attribute   = relation.Attribute;
         var pkattribute = relation.PKAttribute;
         var includenull = relation.IncludeNull;
         var ids         = new List <string>();
         var parentcoll  = blocks.ContainsKey(block) ? blocks[block] : null;
         if (parentcoll != null && parentcoll.Count > 0)
         {
             foreach (var parent in parentcoll)
             {
                 if (string.IsNullOrEmpty(pkattribute))
                 {
                     ids.Add(parent.Id.ToString());
                 }
                 else
                 {
                     ids.Add(parent.Property <EntityReference>(pkattribute, new EntityReference()).Id.ToString());
                 }
             }
         }
         else
         {
             // Adding temp guid to indicate "no matches", as ConditionOperator.In will fail if no values are given
             ids.Add(new Guid().ToString());
         }
         if (!includenull)
         {
             CintFetchXML.AppendFilter(xEntity, "and", attribute, "in", ids.ToArray());
         }
         else
         {
             var xFilter = CintFetchXML.AppendFilter(xEntity, "or");
             CintFetchXML.AppendCondition(xFilter, attribute, "null");
             CintFetchXML.AppendCondition(xFilter, attribute, "in", ids.ToArray());
         }
         log.Log("Adding relation condition for {0} in {1} values in {2}.{3}", attribute, ids.Count, block, pkattribute);
     }
 }
Beispiel #4
0
 private static void AddRelationFilter(IExecutionContainer container, ShuffleBlocks blocks, DataBlockRelation relation, XmlNode xEntity)
 {
     if (blocks != null && blocks.Count > 0)
     {
         var block       = relation.Block;
         var attribute   = relation.Attribute;
         var pkattribute = relation.PKAttribute;
         var includenull = relation.IncludeNull;
         var ids         = new List <string>();
         var parentcoll  = blocks.ContainsKey(block) ? blocks[block] : null;
         if (parentcoll != null && parentcoll.Entities.Count > 0)
         {
             foreach (var parent in parentcoll.Entities)
             {
                 if (string.IsNullOrEmpty(pkattribute))
                 {
                     ids.Add(parent.Id.ToString());
                 }
                 else
                 {
                     ids.Add(parent.GetAttribute(pkattribute, new EntityReference()).Id.ToString());
                 }
             }
         }
         else
         {
             // Adding temp guid to indicate "no matches", as ConditionOperator.In will fail if no values are given
             ids.Add(new Guid().ToString());
         }
         if (!includenull)
         {
             FetchXML.AppendFilter(xEntity, "and", attribute, "in", ids.ToArray());
         }
         else
         {
             var xFilter = FetchXML.AppendFilter(xEntity, "or");
             FetchXML.AppendCondition(xFilter, attribute, "null");
             FetchXML.AppendCondition(xFilter, attribute, "in", ids.ToArray());
         }
         container.Log($"Adding relation condition for {attribute} in {ids.Count} values in {block}.{pkattribute}");
     }
 }
Beispiel #5
0
        /// <summary>
        /// Import entities to CRM from dictionary of blocks
        /// </summary>
        /// <param name="blocks">Blocks with entities to import</param>
        /// <returns>Tuple with counters for: Created, Updated, Skipped and Failed records</returns>
        public Tuple <int, int, int, int, int, EntityReferenceCollection> ImportToCRM(ShuffleBlocks blocks)
        {
            log.StartSection("ImportToCRM");
            if (definition == null)
            {
                throw new ArgumentNullException("Definition", "Shuffle definition must be specified to import data");
            }

            int created = 0;
            int updated = 0;
            int skipped = 0;
            int deleted = 0;
            int failed  = 0;
            EntityReferenceCollection references = new EntityReferenceCollection();

            XmlNode xRoot   = CintXML.FindChild(definition, "ShuffleDefinition");
            XmlNode xBlocks = CintXML.FindChild(xRoot, "Blocks");

            if (xBlocks != null)
            {
                guidmap     = new Dictionary <Guid, Guid>();
                stoponerror = CintXML.GetBoolAttribute(xRoot, "StopOnError", false);
                timeout     = CintXML.GetIntAttribute(xRoot, "Timeout", -1);
                double savedtimeout = -1;
                if (timeout > -1)
                {
                    try
                    {
                        SendLine("Setting timeout: {0} minutes", timeout);
                        OrganizationServiceProxy orgsvcpxy = crmsvc.GetService <OrganizationServiceProxy>();
                        savedtimeout      = orgsvcpxy.Timeout.TotalMinutes;
                        orgsvcpxy.Timeout = new TimeSpan(0, timeout, 0);
                    }
                    catch (InvalidPluginExecutionException)
                    {   // Couldn't cast to correct service type, for some reason...
                        savedtimeout = -1;
                    }
                }

                int totalBlocks  = xBlocks.ChildNodes.Count;
                int currentBlock = 0;
                foreach (XmlNode xBlock in xBlocks.ChildNodes)
                {
                    currentBlock++;
                    SendStatus(totalBlocks, currentBlock, -1, -1);
                    if (xBlock.NodeType == XmlNodeType.Element)
                    {
                        switch (xBlock.Name)
                        {
                        case "DataBlock":
                            string name = CintXML.GetAttribute(xBlock, "Name");
                            if (!blocks.ContainsKey(name))
                            {
                                blocks.Add(name, new CintDynEntityCollection());
                            }
                            Tuple <int, int, int, int, int, EntityReferenceCollection> dataresult = ImportDataBlock(xBlock, blocks[name]);
                            created += dataresult.Item1;
                            updated += dataresult.Item2;
                            skipped += dataresult.Item3;
                            deleted += dataresult.Item4;
                            failed  += dataresult.Item5;
                            references.AddRange(dataresult.Item6);
                            break;

                        case "SolutionBlock":
                            var solutionresult = ImportSolutionBlock(xBlock);
                            switch (solutionresult)
                            {
                            case ItemImportResult.Created: created++; break;

                            case ItemImportResult.Updated: updated++; break;

                            case ItemImportResult.Skipped: skipped++; break;

                            case ItemImportResult.Failed: failed++; break;
                            }
                            break;
                        }
                    }
                }
                SendStatus(0, 0, 0, 0);
                if (savedtimeout > -1)
                {
                    OrganizationServiceProxy orgsvcpxy = crmsvc.GetService <OrganizationServiceProxy>();
                    orgsvcpxy.Timeout = new TimeSpan(0, (int)savedtimeout, 0);
                }
            }
            log.EndSection();
            return(new Tuple <int, int, int, int, int, EntityReferenceCollection>(created, updated, skipped, deleted, failed, references));
        }
Beispiel #6
0
        /// <summary>
        /// Export entities from CRM to dictionary of blocks with entities
        /// </summary>
        /// <returns>Blocks with exported entities</returns>
        public ShuffleBlocks ExportFromCRM()
        {
            log.StartSection("ExportFromCRM");
            if (definition == null)
            {
                throw new ArgumentNullException("Definition", "Shuffle definition must be specified to export data");
            }
            ShuffleBlocks blocks = new ShuffleBlocks();

            ExistingSolutionVersions = null;
            XmlNode xRoot   = CintXML.FindChild(definition, "ShuffleDefinition");
            XmlNode xBlocks = CintXML.FindChild(xRoot, "Blocks");

            if (xBlocks != null)
            {
                stoponerror = CintXML.GetBoolAttribute(xRoot, "StopOnError", false);
                timeout     = CintXML.GetIntAttribute(xRoot, "Timeout", -1);
                double savedtimeout = -1;
                if (timeout > -1)
                {
                    SendLine("Setting timeout: {0} minutes", timeout);
                    OrganizationServiceProxy orgsvcpxy = crmsvc.GetService <OrganizationServiceProxy>();
                    savedtimeout      = orgsvcpxy.Timeout.TotalMinutes;
                    orgsvcpxy.Timeout = new TimeSpan(0, timeout, 0);
                }

                int totalBlocks  = xBlocks.ChildNodes.Count;
                int currentBlock = 0;
                foreach (XmlNode xBlock in xBlocks.ChildNodes)
                {
                    currentBlock++;
                    SendStatus(totalBlocks, currentBlock, -1, -1);
                    if (xBlock.NodeType == XmlNodeType.Element)
                    {
                        switch (xBlock.Name)
                        {
                        case "DataBlock":
                            CintDynEntityCollection cExported = ExportDataBlock(blocks, xBlock);
                            string name = CintXML.GetAttribute(xBlock, "Name");
                            if (cExported != null)
                            {
                                if (blocks.ContainsKey(name))
                                {
                                    SendLine("Block already added: {0}", name);
                                }
                                else
                                {
                                    blocks.Add(name, cExported);
                                }
                            }
                            break;

                        case "SolutionBlock":
                            if (ExistingSolutionVersions == null)
                            {
                                GetCurrentVersions();
                            }
                            ExportSolutionBlock(xBlock);
                            break;
                        }
                    }
                }
                SendStatus(0, 0, 0, 0);
                if (savedtimeout > -1)
                {
                    OrganizationServiceProxy orgsvcpxy = crmsvc.GetService <OrganizationServiceProxy>();
                    orgsvcpxy.Timeout = new TimeSpan(0, (int)savedtimeout, 0);
                }
            }
            log.EndSection();
            return(blocks);
        }
        private void AddRelationFilter(ShuffleBlocks blocks, string entityName, DataBlockRelation relation, FilterExpression filter, ILoggable log)
        {
            log.StartSection(MethodBase.GetCurrentMethod().Name);
            if (blocks != null && blocks.Count > 0)
            {
                var block       = relation.Block;
                var attribute   = relation.Attribute;
                var pkattribute = relation.PKAttribute;
                var includenull = relation.IncludeNull;

                var type = GetAttributeType(attribute, entityName);

                var cond = new ConditionExpression
                {
                    AttributeName = attribute,
                    Operator      = Microsoft.Xrm.Sdk.Query.ConditionOperator.In
                };

                var ids        = new List <object>();
                var parentcoll = blocks.ContainsKey(block) ? blocks[block] : null;
                if (parentcoll != null && parentcoll.Count > 0)
                {
                    foreach (var parent in parentcoll)
                    {
                        if (string.IsNullOrEmpty(pkattribute))
                        {
                            if (type == AttributeTypeCode.String)
                            {
                                ids.Add(parent.Id.ToString());
                            }
                            else
                            {
                                ids.Add(parent.Id);
                            }
                        }
                        else if (type == AttributeTypeCode.String)
                        {
                            ids.Add(parent.Property <EntityReference>(pkattribute, new EntityReference()).Id.ToString());
                        }
                        else
                        {
                            ids.Add(parent.Property <EntityReference>(pkattribute, new EntityReference()).Id);
                        }
                    }
                }
                else
                {
                    // Adding temp guid to indicate "no matches", as ConditionOperator.In will fail if no values are given
                    ids.Add(new Guid());
                }
                cond.Values.AddRange(ids);
                if (!includenull)
                {
                    filter.AddCondition(cond);
                }
                else
                {
                    var orfilter = new FilterExpression(LogicalOperator.Or);
                    orfilter.AddCondition(attribute, Microsoft.Xrm.Sdk.Query.ConditionOperator.Null);
                    orfilter.AddCondition(cond);
                    filter.AddFilter(orfilter);
                }
                log.Log("Adding relation condition for {0} in {1} values in {2}.{3}", attribute, ids.Count, block, pkattribute);
            }
            log.EndSection();
        }
        /// <summary>
        /// Import entities to CRM from dictionary of blocks
        /// </summary>
        /// <param name="blocks">Blocks with entities to import</param>
        /// <returns>Tuple with counters for: Created, Updated, Skipped and Failed records</returns>
        public Tuple <int, int, int, int, int, EntityReferenceCollection> ImportToCRM(ShuffleBlocks blocks)
        {
            log.StartSection("ImportToCRM");
            if (definition == null)
            {
                throw new ArgumentNullException("Definition", "Shuffle definition must be specified to import data");
            }

            var created    = 0;
            var updated    = 0;
            var skipped    = 0;
            var deleted    = 0;
            var failed     = 0;
            var references = new EntityReferenceCollection();

            if (ShuffleDefinition.Blocks.Items.Any(b => (b is DataBlock data && data.Import != null) || b is SolutionBlock sol && sol.Import != null))
            {
                guidmap     = new Dictionary <Guid, Guid>();
                stoponerror = ShuffleDefinition.StopOnError;
                timeout     = ShuffleDefinition.TimeoutSpecified ? ShuffleDefinition.Timeout : -1;
                double savedtimeout = -1;
                if (timeout > -1)
                {
                    savedtimeout = SetTimeout();
                }

                var totalBlocks  = ShuffleDefinition.Blocks.Items.Length;
                var currentBlock = 0;
                foreach (var block in ShuffleDefinition.Blocks.Items)
                {
                    currentBlock++;
                    SendStatus(totalBlocks, currentBlock, -1, -1);
                    if (block is DataBlock datablock)
                    {
                        var name = datablock.Name;
                        if (!blocks.ContainsKey(name))
                        {
                            blocks.Add(name, new CintDynEntityCollection());
                        }
                        var dataresult = ImportDataBlock(datablock, blocks[name]);
                        created += dataresult.Item1;
                        updated += dataresult.Item2;
                        skipped += dataresult.Item3;
                        deleted += dataresult.Item4;
                        failed  += dataresult.Item5;
                        references.AddRange(dataresult.Item6);
                    }
                    else if (block is SolutionBlock solutionblock)
                    {
                        var solutionresult = ImportSolutionBlock(solutionblock);
                        switch (solutionresult)
                        {
                        case ItemImportResult.Created: created++; break;

                        case ItemImportResult.Updated: updated++; break;

                        case ItemImportResult.Skipped: skipped++; break;

                        case ItemImportResult.Failed: failed++; break;
                        }
                    }
                }
                SendStatus(0, 0, 0, 0);
                if (savedtimeout > -1)
                {
                    ResetTimeout(savedtimeout);
                }
            }
            log.EndSection();
            return(new Tuple <int, int, int, int, int, EntityReferenceCollection>(created, updated, skipped, deleted, failed, references));
        }