Ejemplo n.º 1
0
        //�Private�Methods�(7)�

        bool ChangeOrCreateEntities(
            List <EntityData> entityDatas, ICollection <Sku> blanks, Stack <EntityDataGridView.ChangeItem> undoHistory, bool createNewEntityDatas)
        {
            bool?copySchemati    = null;
            var  affectedTaxAtts = new DoubleKeyDictionary <TaxonomyInfo, Attribute, bool>();
            var  nodesToReorderAttributeRanksIn = new HashSet <TaxonomyInfo>();

            var changeId = Guid.NewGuid();

            //List<string> columnsToRefresh= new List<string>();

            if (OldValues.Att != null && !entityDatas.Any())
            {
                foreach (var sku in blanks)
                {
                    affectedTaxAtts[sku.Taxonomy, OldValues.Att] = true;
                }
            }

            // Update existing Entities
            //bool entityDataAddedToRefreshList = false;
            foreach (EntityData entityData in entityDatas)
            {
                var sku       = entityData.EntityInfo.Sku;
                var attribute = entityData.Attribute;

                if (AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache.ContainsKeys(attribute, sku))
                {
                    AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache[attribute].Remove(sku);
                }
                if (NewValues.Att != null)
                {
                    if (!AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache.ContainsKey(NewValues.Att))
                    {
                        AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache.Add(NewValues.Att, sku, new List <EntityData>());
                    }
                    if (AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache.ContainsKeys(NewValues.Att, sku))
                    {
                        AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache[NewValues.Att].Remove(sku);
                    }
                }

                if (Delete && entityData.Active)
                {
                    entityData.Active        = false;
                    entityData.DeletedRemark = AryaTools.Instance.RemarkID;
                    if (undoHistory != null)
                    {
                        undoHistory.Push(new EntityDataGridView.ChangeItem(entityData, null, changeId));
                    }
                    continue;
                }

                //if (attribute.AttributeType != null && attribute.AttributeType.Equals(Attribute.AttributeTypeCalculated))
                //    return false;

                affectedTaxAtts[sku.Taxonomy, attribute] = true;

                // Make the existing EntityInfo Inactive
                if (RunTool != Tool.None)
                {
                    switch (RunTool)
                    {
                    case Tool.SliceInPlace:
                        SliceEntities(entityData, undoHistory, changeId, true);
                        break;

                    case Tool.SliceIntoNewAttributes:
                        SliceEntities(entityData, undoHistory, changeId, false);
                        break;

                    case Tool.ConvertToDecimal:
                        ConvertToDecimal(entityData, undoHistory, changeId);
                        break;

                    case Tool.ConvertToUom:
                        ConvertToUnitOfMeaure(entityData, undoHistory, changeId);
                        break;


                    case Tool.ExtractCountOfValues:
                    case Tool.ExtractMaximumValue:
                    case Tool.ExtractMinimumValue:
                        if (!_processedSkus.Contains(sku))
                        {
                            ExtractAggregate(sku, attribute.AttributeName, undoHistory, changeId);
                            _processedSkus.Add(sku);
                        }
                        break;
                    }
                    //This block must return right here.
                }

                else if (entityData.Active == false)
                {
                    entityData.Active = true;
                    entityData.Value  = NewValues.Val ?? entityData.Value;
                    entityData.Uom    = NewValues.FieldUomDelete ? null : NewValues.Uom ?? entityData.Uom;
                    entityData.Field1 = NewValues.Field1Delete ? null : NewValues.Field1 ?? entityData.Field1;
                    entityData.Field2 = NewValues.Field2Delete ? null : NewValues.Field2 ?? entityData.Field2;
                    entityData.Field3 = NewValues.Field3Delete ? null : NewValues.Field3 ?? entityData.Field3;

                    //Reserved for Base unit of measure and Status/PT Flag
                    //entityData.Field4 = NewValues.Field4Delete ? null : NewValues.Field4 ?? entityData.Field3;
                    //entityData.Field5 = NewValues.Field5Delete ? null : NewValues.Field5 ?? entityData.Field3;
                    LastEntityDataCreated = entityData;
                }
                else
                {
                    // Create a new entityData and add it to the entityInfo
                    entityData.Active = false;
                    var newEntityData = new EntityData
                    {
                        Attribute = NewValues.Att ?? attribute,
                        Value     = NewValues.Val ?? entityData.Value,
                        Uom       = NewValues.FieldUomDelete ? null : NewValues.Uom ?? entityData.Uom,
                        Field1    = NewValues.Field1Delete ? null : NewValues.Field1 ?? entityData.Field1,
                        Field2    = NewValues.Field2Delete ? null : NewValues.Field2 ?? entityData.Field2,
                        Field3    = NewValues.Field3Delete ? null : NewValues.Field3 ?? entityData.Field3,
                        Field4    = NewValues.Field4Delete ? null : NewValues.Field4 ?? entityData.Field4

                                    //Reserved for Base unit of measure and Status/PT Flag
                                    //Field4 = BaseUnitConvertion.GetBaseUnitValueAsString(NewValues.Val ?? entityData.Value, NewValues.Uom ?? entityData.Uom)  ,
                                    //Field5orStatus = NewValues.Field5 ?? entityData.Field5OrStatus
                    };

                    if (!entityData.Sku.Project.EntityField5IsStatus)
                    {
                        newEntityData.Field5 = NewValues.Field5 ?? entityData.Field5;
                    }

                    entityData.EntityInfo.EntityDatas.Add(newEntityData);
                    LastEntityDataCreated = newEntityData;

                    // Add these two to change history for Undo
                    if (undoHistory != null)
                    {
                        undoHistory.Push(new EntityDataGridView.ChangeItem(entityData, newEntityData, changeId));
                    }
                }
            }

            if (RunTool != Tool.None)
            {
                //This block must return right here.
                switch (RunTool)
                {
                case Tool.ConvertToDecimal:
                case Tool.SliceInPlace:
                    return(false);

                case Tool.ExtractCountOfValues:
                case Tool.ExtractMaximumValue:
                case Tool.ExtractMinimumValue:
                case Tool.SliceIntoNewAttributes:
                    return(true);
                }
            }

            if (NewValues.Att != null)
            {
                foreach (DoubleKeyPairValue <TaxonomyInfo, Attribute, bool> taxAtt in affectedTaxAtts)
                {
                    var taxonomy     = taxAtt.Key1;
                    var oldAttribute = taxAtt.Key2;

                    var oldIsMapped =
                        SchemaAttribute.GetValue(taxonomy, oldAttribute, SchemaAttribute.SchemaAttributeIsMapped) != null;
                    var newIsMapped =
                        SchemaAttribute.GetValue(taxonomy, NewValues.Att, SchemaAttribute.SchemaAttributeIsMapped) != null;

                    if (oldIsMapped && !newIsMapped)
                    {
                        copySchemati = CopySchemati(copySchemati);

                        if ((bool)copySchemati)
                        {
                            var newSchemaData =
                                taxonomy.SchemaInfos.FirstOrDefault(si => si.Attribute.Equals(oldAttribute)).
                                SchemaData;
                            SchemaAttribute.SetValue(taxonomy, NewValues.Att, true, null, newSchemaData);

                            SchemaAttribute.SecondarySchemati.ForEach(
                                schematus =>
                            {
                                object newValue = SchemaAttribute.GetValue(taxonomy, oldAttribute, schematus);
                                if (newValue != null)
                                {
                                    SchemaAttribute.SetValue(taxonomy, NewValues.Att, true, schematus, newValue);
                                }
                            });
                            //SchemaAttribute.UnmapNodeAttribute(taxonomy, oldAttribute);
                        }
                    }

                    /*
                     * This part is obsolete unless required
                     */

                    //var oldTaxAtt =
                    //    taxonomy.TaxonomyAttributes.FirstOrDefault(
                    //        ta => ta.Attribute.Equals(oldAttribute) && (ta.DisplayOrder > 0 || ta.NavigationalOrder > 0));
                    //var newTaxAtt =
                    //    taxonomy.TaxonomyAttributes.FirstOrDefault(
                    //        ta =>
                    //        ta.Attribute.Equals(NewValues.Att) && (ta.DisplayOrder > 0 || ta.NavigationalOrder > 0));
                    //if (oldTaxAtt != null && newTaxAtt == null)
                    //{
                    //    moveAttributeRanks = MoveAttributeRanks(moveAttributeRanks);
                    //    if ((bool)(moveAttributeRanks))
                    //    {
                    //        taxonomy.InsertNavigationOrder(NewValues.Att, oldTaxAtt.NavigationalOrder, false);
                    //        taxonomy.InsertDisplayOrder(NewValues.Att, oldTaxAtt.DisplayOrder, false);
                    //        nodesToReorderAttributeRanksIn.Add(taxonomy);
                    //    }
                    //}
                }
            }

            //nodesToReorderAttributeRanksIn.ForEach(tax => tax.TryReorderAttributeOrders(true));

            if (!createNewEntityDatas || blanks.Count == 0)
            {
                return((copySchemati != null && (bool)copySchemati) || nodesToReorderAttributeRanksIn.Count > 0);
            }

            // Create new Entities
            Attribute att = NewValues.Att ??
                            (OldValues.Att ?? Attribute.GetAttributeFromName(OldValues.AttributeName, true));

            foreach (Sku sku in blanks)
            {
                if (!AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache.ContainsKey(att))
                {
                    AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache.Add(att, sku, new List <EntityData>());
                }
                if (AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache.ContainsKeys(att, sku))
                {
                    AryaTools.Instance.InstanceData.Dc.SkuAttributeValueCache[att].Remove(sku);
                }

                var currentSku    = sku;
                var newEntityData = new EntityData
                {
                    Attribute = att,
                    Value     = NewValues.Val ?? string.Empty,
                    Uom       = NewValues.Uom,
                    Field1    = NewValues.Field1,
                    Field2    = NewValues.Field2,
                    Field3    = NewValues.Field3,
                    //Field4 = BaseUnitConvertion.GetBaseUnitValueAsString(NewValues.Val ?? string.Empty, NewValues.Uom)
                    //Field5orStatus = NewValues.Field5
                };
                currentSku.EntityInfos.Add(new EntityInfo {
                    EntityDatas = { newEntityData }
                });
                LastEntityDataCreated = newEntityData;

                // Add this to change history for Undo
                if (undoHistory != null)
                {
                    undoHistory.Push(new EntityDataGridView.ChangeItem(null, newEntityData, changeId));
                }
            }

            return(nodesToReorderAttributeRanksIn.Count > 0);
        }
Ejemplo n.º 2
0
        private void WriteTaxonomyToFile(TaxonomyInfo taxonomy, int taxonomyMaxDepth)
        {
            var ignoreT1taxonomy = taxonomy.ToString();

            if (IgnoreT1Taxonomy)
            {
                ignoreT1taxonomy = taxonomy.ToString().Substring(taxonomy.ToString().IndexOf('>') + 1);
            }

            if (string.IsNullOrEmpty(taxonomy.ToString()))
            {
                return;
            }

            if (_processedTaxonomies.Contains(taxonomy))
            {
                return;
            }
            _processedTaxonomies.Add(taxonomy);

            if (!LeafNodesOnly || taxonomy.HasSkus || ExportAllSchemaNodes)
            {
                StatusMessage = string.Format("{1}{0}Reading Node", Environment.NewLine, taxonomy);

                List <Attribute> atts;

                if (ExportSuperSchema && !taxonomy.IsLeafNode)
                {
                    var schemaInfos = new List <SchemaInfo>();
                    schemaInfos.AddRange(taxonomy.SchemaInfos.Where(si => si.SchemaDatas.Any(sd => sd.Active)).ToList());
                    if (ExportSuperSchema)
                    {
                        foreach (var leaf in taxonomy.AllLeafChildren)
                        {
                            schemaInfos.AddRange(
                                leaf.SchemaInfos.Where(si => si.SchemaDatas.Any(sd => sd.Active)).ToList());
                        }
                    }
                    var schemaAttributes =
                        schemaInfos.Where(p => p.SchemaData.InSchema)
                        .Select(p => new { p.Attribute, p.SchemaData })
                        .ToList();

                    atts = (from att in schemaAttributes
                            group att by att.Attribute
                            into grp
                            let minRank = grp.Min(p => GetRank(p.SchemaData, SortOrder.OrderbyDisplayNavigation))
                                          orderby minRank
                                          select grp.Key).Distinct().ToList();
                }
                else
                {
                    atts = (from si in AryaTools.Instance.InstanceData.Dc.SchemaInfos
                            where si.TaxonomyID == taxonomy.ID
                            let sd = si.SchemaDatas.FirstOrDefault(sd => sd.Active)
                                     where sd != null
                                     let inSchema = sd.InSchema
                                                    where inSchema
                                                    let navRank = sd == null || sd.NavigationOrder == 0 ? decimal.MaxValue : sd.NavigationOrder
                                                                  let dispRank = sd == null || sd.DisplayOrder == 0 ? decimal.MaxValue : sd.DisplayOrder
                                                                                 orderby navRank, dispRank
                            select si.Attribute).ToList();

                    var currentTaxonomyString = ignoreT1taxonomy;

                    var possibleMissingAttributes = (from si in AryaTools.Instance.InstanceData.Dc.SchemaInfos
                                                     where si.TaxonomyID == taxonomy.ID
                                                     let sd = si.SchemaDatas.FirstOrDefault(sd => sd.Active)
                                                              where sd != null && !sd.InSchema && (sd.NavigationOrder > 0 || sd.DisplayOrder > 0)
                                                              select si.Attribute.AttributeName).ToList()
                                                    .Select(p => currentTaxonomyString + "\t" + p)
                                                    .ToList();

                    _possibleMissingInSchemaAttributes.AddRange(possibleMissingAttributes);
                }

                var isLeafNode = taxonomy.IsLeafNode;

                //if a node has no schema, just skip it.
                if ((atts.Count == 0 && ExportAllSchemaNodes) || (LeafNodesOnly && isLeafNode == false))
                {
                    StatusMessage = string.Format("{1}{0}, Fetching Child Nodes", Environment.NewLine, taxonomy);
                    var taxonomies =
                        taxonomy.ChildTaxonomyDatas.Where(td => td.Active)
                        .Select(td => td.TaxonomyInfo)
                        .OrderBy(tax => tax.ToString());

                    foreach (var tax in taxonomies)
                    {
                        WriteTaxonomyToFile(tax, taxonomyMaxDepth);
                    }
                    CurrentProgress++;
                    return;
                }

                var taxonomyString = ignoreT1taxonomy;
                var taxParts       = ignoreT1taxonomy.Split(new[] { TaxonomyInfo.Delimiter }, StringSplitOptions.None);
                var taxonomyParts  = new string[taxonomyMaxDepth];

                _taxFile.Write("{1}{0}", _delimiter, taxonomyString);
                for (var i = 0; i < taxonomyMaxDepth; i++)
                {
                    taxonomyParts[i] = i < taxParts.Length ? taxParts[i].Trim() : string.Empty;
                    _taxFile.Write(taxonomyParts[i] + "\t");
                }

                // write to taxonomy file
                // _taxFile.WriteLine("{9}{0}{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}{0}{8}", _delimiter, taxonomyString);

                WriteTaxMetaValue(taxonomy);

                _schemaFile.WriteLine("{1}{0}", _delimiter, taxonomyString);
                foreach (var attribute in atts)
                {
                    _schemaFile.Write("{1}{0}", _delimiter, taxonomyString);
                    for (var i = 0; i < taxonomyMaxDepth; i++)
                    {
                        taxonomyParts[i] = i < taxParts.Length ? taxParts[i].Trim() : string.Empty;
                        _schemaFile.Write(taxonomyParts[i] + "\t");
                    }

                    _schemaFile.Write("{1}{0}{2}", _delimiter,
                                      (taxonomy.NodeType == "Derived") ? "Cross List" : TaxonomyInfo.NodeTypeRegular, attribute);

                    foreach (var schematus in _allSchemati)
                    {
                        object value = null;

                        if (schematus.AttributeType == SchemaAttribute.SchemaAttributeType.FillRate)
                        {
                            if (_fillRates.Count > 0)
                            {
                                var data =
                                    _fillRates.FirstOrDefault(
                                        p =>
                                        p.Item1 == schematus.FillRateSchemaAttribute.GetFilterName() &&
                                        p.Item2 == taxonomy.ID && p.Item3 == attribute.ID);

                                value = data == null ? "0" : data.Item4.ToString(CultureInfo.InvariantCulture);
                            }
                            else
                            {
                                value = SchemaAttribute.GetValue(taxonomy, attribute, schematus);
                            }
                        }
                        else
                        {
                            if (ExportSuperSchema && !isLeafNode)
                            {
                                if (schematus.AttributeType == SchemaAttribute.SchemaAttributeType.Meta)
                                {
                                    if (schematus.MetaSchemaAttribute.AttributeName.Equals("Sample Values",
                                                                                           StringComparison.OrdinalIgnoreCase) &&
                                        schematus.MetaSchemaAttribute.AttributeName.Equals("Restricted UOM",
                                                                                           StringComparison.OrdinalIgnoreCase))
                                    {
                                        value = string.Empty;
                                    }
                                }
                                else
                                {
                                    var allChildren = taxonomy.AllLeafChildren.ToList();
                                    allChildren.Add(taxonomy);

                                    var attribute2 = attribute;
                                    var schematus1 = schematus;
                                    var bestValue  = (from child in allChildren
                                                      let childValue = SchemaAttribute.GetValue(child, attribute2, schematus1)
                                                                       where childValue != null
                                                                       group child by childValue
                                                                       into grp
                                                                       orderby grp.Count() descending
                                                                       select grp.Key).FirstOrDefault();

                                    value = bestValue;
                                }
                            }
                            else
                            {
                                value = SchemaAttribute.GetValue(taxonomy, attribute, schematus);
                            }
                        }

                        value = value ?? string.Empty;

                        if ((schematus.PrimarySchemaAttribute == "Navigation Order" ||
                             schematus.PrimarySchemaAttribute == "Display Order"))
                        {
                            decimal result;
                            if (Decimal.TryParse(value.ToString(), out result) && result > 0)
                            {
                                value = string.Format("{0:0.##}", value);
                            }
                        }
                        else if (schematus.AttributeType == SchemaAttribute.SchemaAttributeType.Meta &&
                                 schematus.MetaSchemaAttribute.AttributeName == Resources.SchemaEnrichmentImageAttributeName &&
                                 !String.IsNullOrEmpty(value.ToString()))
                        {
                            using (var dc = new AryaDbDataContext(AryaTools.Instance.InstanceData.CurrentProject.ID, AryaTools.Instance.InstanceData.CurrentUser.ID))
                            {
                                var imageMgr = new ImageManager(dc, AryaTools.Instance.InstanceData.CurrentProject.ID)
                                {
                                    RemoteImageGuid = value.ToString()
                                };
                                if (DownloadAssets)
                                {
                                    imageMgr.LocalDirectory = _downloadDir.ToString();
                                    imageMgr.DownloadImage(taxonomy.ID, attribute.ID);
                                    value = imageMgr.LocalImageName;
                                }
                                else
                                {
                                    value = imageMgr.RemoteImageUrl;
                                }
                            }
                        }

                        var value2 = value.ToString();

                        if (value2.Length > 0)
                        {
                            _schemaFile.Write(_delimiter
                                              + value2.Replace("\n", string.Empty).Replace("\r", string.Empty));
                        }
                        else
                        {
                            _schemaFile.Write(_delimiter + value2);
                        }
                    }
                    _schemaFile.WriteLine();

                    var schemaData =
                        attribute.SchemaInfos.Where(p => p.TaxonomyID == taxonomy.ID && p.SchemaData != null)
                        .Select(p => p.SchemaData)
                        .FirstOrDefault();

                    if (schemaData != null)
                    {
                        var attribute1 = attribute;

                        foreach (var lov in schemaData.SchemaInfo.ListOfValues.Where(lov => lov.Active))
                        {
                            for (var j = 0; j < taxonomyMaxDepth; j++)
                            {
                                taxonomyParts[j] = j < taxParts.Length ? taxParts[j].Trim() : string.Empty;
                                _lovFile.Write(taxonomyParts[j] + "\t");
                            }
                            //_lovFile.WriteLine(
                            //    "{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}{0}{7}{0}{8}{0}{9}{0}{10}{0}{11}{0}{12}{0}{13}",
                            //    _delimiter, taxParts[0], taxParts[1], taxParts[2], taxParts[3], taxParts[4], taxParts[5],
                            //    taxParts[6], taxParts[7], (taxonomy.NodeType == "Derived") ? "Cross List" : TaxonomyInfo.NodeTypeRegular,
                            //    attribute1.AttributeName,
                            //    schemaData.NavigationOrder == 0
                            //        ? string.Empty
                            //        : string.Format("{0:0.##}", schemaData.NavigationOrder),
                            //    schemaData.DisplayOrder == 0
                            //        ? string.Empty
                            //        : string.Format("{0:0.##}", schemaData.DisplayOrder), lov.Value);

                            _lovFile.Write("{1}{0}{2}{0}{3}{0}{4}{0}{5}", _delimiter,
                                           (taxonomy.NodeType == "Derived") ? "Cross List" : TaxonomyInfo.NodeTypeRegular,
                                           attribute1.AttributeName,
                                           schemaData.NavigationOrder == 0
                                    ? string.Empty
                                    : string.Format("{0:0.##}", schemaData.NavigationOrder),
                                           schemaData.DisplayOrder == 0
                                    ? string.Empty
                                    : string.Format("{0:0.##}", schemaData.DisplayOrder), lov.Value);

                            if (ExportEnrichments)
                            {
                                using (
                                    var dc =
                                        new AryaDbDataContext(AryaTools.Instance.InstanceData.CurrentProject.ID,
                                                              AryaTools.Instance.InstanceData.CurrentUser.ID))
                                {
                                    var imageMgr = new ImageManager(dc, AryaTools.Instance.InstanceData.CurrentProject.ID)
                                    {
                                        ImageSku = dc.Skus.FirstOrDefault(s => s.ItemID == lov.EnrichmentImage)
                                    };
                                    string lovImage;
                                    if (DownloadAssets)
                                    {
                                        imageMgr.LocalDirectory = _downloadDir.ToString();
                                        imageMgr.DownloadImage(taxonomy.ID, attribute.ID, lov.ID);
                                        lovImage = imageMgr.LocalImageName;
                                    }
                                    else
                                    {
                                        lovImage = imageMgr.RemoteImageUrl;
                                    }

                                    _lovFile.Write("{0}{1}{0}{2}", _delimiter, lovImage, lov.EnrichmentCopy);
                                }
                            }

                            _lovFile.WriteLine();
                        }
                    }
                }
            }

            StatusMessage = string.Format("{1}{0}, Fetching Child Nodes", Environment.NewLine, taxonomy);
            var taxs =
                taxonomy.ChildTaxonomyDatas.Where(td => td.Active)
                .Select(td => td.TaxonomyInfo)
                .OrderBy(tax => tax.ToString());

            foreach (var tax in taxs)
            {
                WriteTaxonomyToFile(tax, taxonomyMaxDepth);
            }
            CurrentProgress++;
        }