private void WriteSharedField(
            [NotNull] ID itemId,
            [NotNull] FieldChange change,
            DateTime now,
            bool fieldsAreEmpty)
        {
            var row = fieldsAreEmpty
        ? null
        : SharedFields[itemId].Where(r => r.FieldID == change.FieldID).FirstOrDefault();

            if (row == null)
            {
                SharedFields[itemId].Add(
                    new FieldsRow
                {
                    ItemID  = itemId,
                    FieldID = change.FieldID,
                    Value   = change.Value
                });
            }
            else
            {
                row.Value = change.Value;
            }
        }
        protected override void WriteUnversionedField(ID itemId, FieldChange change, DateTime now, bool fieldsAreEmpty)
        {
            string sql;

            if (!fieldsAreEmpty)
            {
                sql = "\r\nDECLARE\r\n  vString {0}UnversionedFields{1}.{0}Value{1}%Type;\r\n                BEGIN\r\n                vString :={2}value{3};\r\nMERGE INTO {0}UnversionedFields{1} F\r\nUSING (\r\n  SELECT {2}itemId{3} IId, \r\n         {2}fieldId{3} FId,\r\n         {2}language{3} L\r\n  FROM DUAL) S\r\nON (F.{0}ItemId{1} = S.IId AND F.{0}FieldId{1} = S.FId\r\n   AND (F.{0}Language{1} = S.L OR (F.{0}Language{1} IS NULL AND S.L IS NULL)))\r\nWHEN MATCHED THEN UPDATE\r\n    SET F.{0}Value{1} = vString,\r\n        F.{0}Updated{1} = {2}now{3}\r\nWHEN NOT MATCHED THEN INSERT\r\n      (F.{0}ItemId{1}, F.{0}Language{1}, F.{0}FieldId{1}, F.{0}Value{1}, F.{0}Created{1}, F.{0}Updated{1})\r\n    VALUES \r\n      ({2}itemId{3}, {2}language{3}, {2}fieldId{3}, vString, {2}now{3}, {2}now{3});\r\nEND;";
            }
            else
            {
                sql = " INSERT INTO {0}UnversionedFields{1} (   {0}ItemId{1}, {0}Language{1}, {0}FieldId{1}, {0}Value{1}, {0}Created{1}, {0}Updated{1} ) VALUES (   {2}itemId{3}, {2}language{3}, {2}fieldId{3}, {2}value{3}, {2}now{3}, {2}now{3} )";
            }
            BigString bigString = (change.Value != string.Empty) ? new BigString(change.Value) : new BigString("__#!$No value$!#__");

            base.Api.Execute(sql, new object[]
            {
                "itemId",
                itemId,
                "language",
                change.Language,
                "fieldId",
                change.FieldID,
                "value",
                bigString,
                "now",
                now
            });
        }
Beispiel #3
0
        private FieldChange CreateFieldChange(JToken current, JToken previous, JToken original)
        {
            var result = new FieldChange
            {
                TheChange = Builder.Parent + @"'s " + Builder.CurrentProperty.Name,
                Details   = new FieldDetail
                {
                    Current =
                        new ChangeValue {
                        Value = current == null ? String.Empty : current.ToString(Formatting.None)
                    },
                    Original =
                        new ChangeValue {
                        Value = original == null ? String.Empty : original.ToString(Formatting.None)
                    }
                }
            };

            if (Builder.HasPrevious)
            {
                result.Details.Previous.Value = previous == null ? String.Empty : previous.ToString(Formatting.None);
            }

            return(result);
        }
        private void WriteVersionedField(
            [NotNull] ID itemId,
            [NotNull] FieldChange change,
            DateTime now,
            bool fieldsAreEmpty)
        {
            var row = fieldsAreEmpty
        ? null
        : VersionedFields[itemId].Where(
                r =>
                (r.Language == change.Language.ToString()) && (r.Version == change.Version.ToInt32()) &&
                (r.FieldID == change.FieldID)).FirstOrDefault();

            if (row == null)
            {
                VersionedFields[itemId].Add(
                    new FieldsRow
                {
                    ItemID   = itemId,
                    Language = change.Language.ToString(),
                    Version  = change.Version.ToInt32(),
                    FieldID  = change.FieldID,
                    Value    = change.Value
                });
            }
            else
            {
                row.Value = change.Value;
            }
        }
Beispiel #5
0
        private void SaveField(ID itemId, FieldChange fieldChange)
        {
            if (!IsTemplateOnWhiteList(fieldChange.Definition.Template.ID))
            {
                return;
            }
            if (IsFieldOnBlackList(fieldChange.FieldID))
            {
                return;
            }

            if (fieldChange.Definition.IsShared)
            {
                SaveSharedField(itemId, fieldChange);
                return;
            }

            if (fieldChange.Definition.IsUnversioned)
            {
                SaveUnversionedField(itemId, fieldChange);
                return;
            }

            if (fieldChange.Definition.IsVersioned)
            {
                SaveVersionedField(itemId, fieldChange);
            }

            // Ok to fall through, if definition is "unknown" type.
        }
        private static IDefinitionField GetDefinitionField(FieldChange fieldChange, Product product)
        {
            var definition      = ProductDefinition.SingleOrDefault(x => x.Name == product.ProductDefinition.Name);
            var definitionField = definition.GetDefinitionFields().SingleOrDefault(x => x.Name == fieldChange.Definition.Name);

            return(definitionField);
        }
Beispiel #7
0
        private void UpdateCategoryProducts(FieldChange fieldChange, Category category)
        {
            var productRepository = ObjectFactory.Instance.Resolve <IRepository <Product> >();

            var selectedSitecoreProductIds = fieldChange.Value.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            var products = productRepository.Select(new ProductsByGuidsQuery(selectedSitecoreProductIds.Select(Guid.Parse).ToList()));

            var relationsToRemove = category.CategoryProductRelations.Where(x => !products.Contains(x.Product)).ToList();

            var productsToAdd = new List <Product>();

            foreach (var product in products)
            {
                if (category.CategoryProductRelations.Any(x => x.Product == product))
                {
                    continue;
                }

                productsToAdd.Add(product);
            }

            foreach (var categoryProductRelation in relationsToRemove)
            {
                category.CategoryProductRelations.Remove(categoryProductRelation);
            }

            foreach (var product in productsToAdd)
            {
                category.AddProduct(product, 0);
            }
        }
        private void RemoveField([NotNull] ID itemId, [NotNull] FieldChange change)
        {
            var sharing = GetSharingType(change);

            if ((sharing == DefaultFieldSharing.SharingType.Versioned) ||
                (sharing == DefaultFieldSharing.SharingType.Unknown))
            {
                VersionedFields[itemId].Delete(
                    r =>
                    (r.Language == change.Language.ToString()) && (r.Version == change.Version.ToInt32()) &&
                    (r.FieldID == change.FieldID));
            }

            if ((sharing == DefaultFieldSharing.SharingType.Shared) || (sharing == DefaultFieldSharing.SharingType.Unknown))
            {
                SharedFields[itemId].Delete(r => r.FieldID == change.FieldID);
            }

            if ((sharing == DefaultFieldSharing.SharingType.Unversioned) ||
                (sharing == DefaultFieldSharing.SharingType.Unknown))
            {
                UnversionedFields[itemId].Delete(
                    r => (r.Language == change.Language.ToString()) && (r.FieldID == change.FieldID));
            }
        }
        public void OnPostUpdate(PostUpdateEvent updateEvent)
        {
            var entity = updateEvent.Entity as IDomainObject;

            // Мы умеет трекать только объекты реализующие IDomainObject, иначе далее будем падать на получении Id.
            if (entity == null || !TrackerMain.NeedTrace(entity))
            {
                return;
            }

            //FIXME добавлено чтобы не дублировались записи. Потому что от Nhibernate приходит по 2 события на один объект. Если это удастся починить, то этот код не нужен.
            if (changes.Any(hce => hce.EntityId == entity.Id && NHibernateProxyHelper.GuessClass(entity).Name == hce.EntityClassName))
            {
                return;
            }

            var fields = Enumerable.Range(0, updateEvent.State.Length)
                         .Select(i => FieldChange.CheckChange(i, updateEvent))
                         .Where(x => x != null)
                         .ToList();

            if (fields.Count > 0)
            {
                changes.Add(new ChangedEntity(EntityChangeOperation.Change, updateEvent.Entity, fields));
            }
        }
Beispiel #10
0
        public void SetCommonValues(FieldChange change, BLModel.Alternate.Long.Airing currentAsset, BLModel.Alternate.Long.Airing previousAsset, BLModel.Alternate.Long.Airing originalAsset)
        {
            change.Details.Original.By = originalAsset.ReleasedBy;
            change.Details.Original.On = originalAsset.ReleasedOn;

            SetCommonValues(change, currentAsset, originalAsset);
        }
        private void UpdateSelectedCategories(FieldChange fieldChange, Product product)
        {
            var selectedSitecoreCategoryIds = fieldChange.Value.Split('|');
            var categoryRepository          = ObjectFactory.Instance.Resolve <IRepository <Category> >();

            var selectedCategories = categoryRepository.Select(
                x => selectedSitecoreCategoryIds.Select(Guid.Parse).Contains(x.Guid));

            var existingRelations = CategoryProductRelation.Find(x => x.Product.ProductId == product.ProductId)
                                    .Select(x => x.Category.CategoryId).ToList();

            // Removing not selected categories
            var categoryIdsToBeRemoved = existingRelations.Except(selectedCategories.Select(x => x.CategoryId)).ToList();
            var categoriesToBeRemoved  = Category.Find(x => categoryIdsToBeRemoved.Contains(x.CategoryId)).ToList();

            foreach (var categoryToBeRemoved in categoriesToBeRemoved)
            {
                _loggingService.Debug <ProductTemplatesBuilder>("Removing category " + categoryToBeRemoved.Name);
                product.RemoveCategory(categoryToBeRemoved);
            }

            // Adding new selected categories
            var categoriesToBeAdded = selectedCategories.Where(x => !existingRelations.Contains(x.CategoryId)).ToList();

            foreach (var categoryToBeAdded in categoriesToBeAdded)
            {
                _loggingService.Debug <ProductTemplatesBuilder>("Adding category " + categoryToBeAdded.Name);
                categoryToBeAdded.AddProduct(product, 0);
            }
        }
Beispiel #12
0
        public static Dictionary <string, FieldChange> StoreFieldChanges(Sitecore.Data.Items.ItemChanges itemChanges)
        {
            var fields = new Dictionary <string, FieldChange>();

            foreach (Sitecore.Data.Items.FieldChange fieldChange in itemChanges.FieldChanges)
            {
                var fieldChangeRecord = new FieldChange
                {
                    FieldId  = fieldChange.FieldID.ToString(),
                    NewValue = fieldChange.Value,
                    OldValue = fieldChange.OriginalValue
                };

                // empty fields lack a "Definition" and require a placeholder name
                if (fieldChange.Definition != null)
                {
                    fieldChangeRecord.FieldName = fieldChange.Definition.Name;
                }
                else
                {
                    fieldChangeRecord.FieldName = $"Undefined field ({fieldChangeRecord.FieldId})";
                }

                if (!(IsDefaultItemField(fieldChangeRecord.FieldName) && IgnoreDefaultItemFields))
                {
                    fields.Add(fieldChangeRecord.FieldName, fieldChangeRecord);
                }
            }

            return(fields);
        }
Beispiel #13
0
        private static string GetImageId(FieldChange fieldChange, Item item)
        {
            ImageField imageField = new Field(fieldChange.FieldID, item);

            return(imageField.MediaID.ToString() == "{00000000-0000-0000-0000-000000000000}"
                                ? null
                                : imageField.MediaID.ToString());
        }
 public static FieldChangeReturnDTO ToChangeReturnDTO(this FieldChange fieldChange)
 {
     return(new FieldChangeReturnDTO
     {
         ChangedField = fieldChange.FieldName,
         NewValue = fieldChange.NewValue,
         OldValue = fieldChange.OldValue
     });
 }
Beispiel #15
0
        private static string GetFieldSpecificValueForDropDownList(FieldChange fieldChange)
        {
            if (string.IsNullOrEmpty(fieldChange.Value))
            {
                return(fieldChange.Value);
            }

            return(Database.GetDatabase(SitecoreConstants.SitecoreMasterDatabaseName).GetItem(ID.Parse(fieldChange.Value)).Name);
        }
        private void UpdateDisplayName(FieldChange fieldChange, ProductCatalog catalog)
        {
            var description = catalog.GetDescription(fieldChange.Language.CultureInfo.ToString());

            if (description != null)
            {
                description.DisplayName = fieldChange.Value;
            }
        }
Beispiel #17
0
        protected override bool ShouldApplyChangeToDataStore(FieldChange change)
        {
            if (change.Definition != null && change.Definition.Name == "Enriched Image")
            {
                return(true);
            }

            return(false);
        }
Beispiel #18
0
        private string GetImageValue(FieldChange fieldChange, Item item)
        {
            var        fieldId = fieldChange.FieldID;
            ImageField field   = new Field(fieldId, item);

            return(field.MediaID.ToString() == "{00000000-0000-0000-0000-000000000000}"
                                ? null
                                : field.MediaID.ToString());
        }
        private void UpdateLongDescription(FieldChange fieldChange, Product product)
        {
            var description = product.GetDescription(fieldChange.Language.CultureInfo.ToString());

            if (description != null)
            {
                description.LongDescription = fieldChange.Value;
            }
        }
Beispiel #20
0
        private void RemoveVersionedField(ID itemId, FieldChange fieldChange)
        {
            EnsureItemEntry(itemId.Guid, _versionedFields);
            var fieldData = _versionedFields[itemId.Guid];
            var key       = fieldChange.Language.CultureInfo.Name + ";" + fieldChange.Version.Number;

            RemoveFieldDataForKey(fieldChange, fieldData, key);

            _persistor.DeleteVersionedField(itemId.Guid, fieldChange.FieldID.Guid, fieldChange.Language.CultureInfo.Name, fieldChange.Version.Number);
        }
Beispiel #21
0
        private bool FieldChangeNotNeedToBeHided(FieldChange fieldChange)
        {
            var restrictedToShowPropertyAttribute = new RestrictedHistoryProperty();

            var persistentClassType = OrmConfig.NhConfig.ClassMappings
                                      .Where(mc => mc.MappedClass.Name == fieldChange.Entity.EntityClassName)
                                      .Select(mc => mc.MappedClass).FirstOrDefault();

            return(!persistentClassType?.GetProperty(fieldChange.Path)?.GetCustomAttributes(false).Contains(restrictedToShowPropertyAttribute) ?? false);
        }
        private static ProductDefinitionField GetDefinitionField(FieldChange fieldChange, Product productVariant)
        {
            var definition      = ProductDefinition.SingleOrDefault(x => x.Name == productVariant.ProductDefinition.Name);
            var definitionField =
                definition.GetDefinitionFields()
                .Cast <ProductDefinitionField>()
                .SingleOrDefault(x => x.Name == fieldChange.Definition.Name && x.IsVariantProperty);

            return(definitionField);
        }
        private DefaultFieldSharing.SharingType GetSharingType([NotNull] FieldChange change)
        {
            var definition = change.Definition;

            if (definition == null)
            {
                return(DefaultFieldSharing.Sharing[change.FieldID]);
            }

            return(GetSharingType(definition));
        }
Beispiel #24
0
            public static bool IsUnversioned(FieldChange change)
            {
                var def = change.Definition;

                if (def == null)
                {
                    return(false);
                }

                return(def.IsUnversioned && !def.IsShared);
            }
Beispiel #25
0
        private void UpdateDynamicCategoryProperty(FieldChange fieldChange, Category category, Item item)
        {
            var property = GetDynamicProperty(fieldChange, category);

            if (property == null)
            {
                throw new InvalidDataException(string.Format("Tried to modify category property with name: {0} that doesn't exists.", fieldChange.Definition.Key));
            }

            property.SetValue(this.GetFieldSpecificValue(fieldChange, item));
        }
Beispiel #26
0
 private static void RemoveFieldDataForKey(FieldChange fieldChange, Dictionary <string, Dictionary <Guid, string> > fieldData, string key)
 {
     if (fieldData.ContainsKey(key))
     {
         var fieldDataForKey = fieldData[key];
         if (fieldDataForKey.ContainsKey(fieldChange.FieldID.Guid))
         {
             fieldDataForKey.Remove(fieldChange.FieldID.Guid);
         }
     }
 }
Beispiel #27
0
        private static void AddKeyedValue(FieldChange fieldChange, Dictionary <string, Dictionary <Guid, string> > values, string key)
        {
            if (!values.ContainsKey(key))
            {
                values[key] = new Dictionary <Guid, string>();
            }

            var fieldEntry = values[key];

            fieldEntry[fieldChange.FieldID.Guid] = fieldChange.Value;
        }
        private bool IsDynamicField(FieldChange fieldChange, Product productVariant)
        {
            if (!fieldChange.Definition.ID.IsUCommerceDynamicField())
            {
                return(false);
            }

            var definitionField = GetDefinitionField(fieldChange, productVariant);

            return(definitionField != null);
        }
Beispiel #29
0
        private void UpdateStoreValueFor(FieldChange fieldChange, Category category, Item item)
        {
            if (this.ValueDidNotChangeFor(fieldChange))
            {
                return;
            }
            if (this.FieldBelongsToStatistics(fieldChange))
            {
                return;
            }

            if (fieldChange.FieldID == FieldIds.Category.DisplayOnSiteFieldId)
            {
                category.DisplayOnSite = (fieldChange.Value == "1");
            }
            else if (fieldChange.FieldID == FieldIds.Category.ImageFieldId)
            {
                category.ImageMediaId = GetImageValue(fieldChange, item);
            }
            else if (fieldChange.FieldID == FieldIds.Category.NameFieldId)
            {
                UpdateCategoryName(fieldChange.Value, category);
            }
            else if (fieldChange.FieldID == FieldIds.Category.DescriptionFieldId)
            {
                UpdateCategoryDescription(fieldChange, category);
            }
            else if (fieldChange.FieldID == FieldIds.Category.DisplayNameFieldId)
            {
                UpdateCategoryDisplayName(fieldChange, category);
            }
            else if (fieldChange.FieldID == FieldIds.Category.ProductsInCategory)
            {
                UpdateCategoryProducts(fieldChange, category);
            }
            else if (fieldChange.FieldID == FieldIds.Category.ParentCatalogIdFieldId)
            {
                return;
            }
            else if (fieldChange.FieldID == FieldIds.Category.CategoryIdFieldId)
            {
                return;
            }
            else if (fieldChange.Definition.Template.ID == _dynamicCategoryTemplate.GetTemplateId(category.Definition.Id))
            {
                UpdateDynamicCategoryProperty(fieldChange, category, item);
            }
            else
            {
                _loggingService.Debug <ProductCategoryTemplateBuilder>(
                    string.Format("Could not find property: {0} for category: {1}.", fieldChange.Definition.Key, category.Name));
            }
        }
Beispiel #30
0
        private static IProperty GetDynamicProperty(FieldChange fieldChange, Category category)
        {
            IProperty property;
            var       definition =
                DefinitionField.SingleOrDefault(
                    x => x.Name == fieldChange.Definition.Name && x.Definition.DefinitionType.DefinitionTypeId == 1);

            property = definition.Multilingual
                                ? category.GetProperty(fieldChange.Definition.Name, fieldChange.Language.CultureInfo.ToString())
                                : category.GetProperty(fieldChange.Definition.Name);
            return(property);
        }
			public static bool IsUnversioned(FieldChange change)
			{
				var def = change.Definition;
				if (def == null) return false;

				return def.IsUnversioned && !def.IsShared;
			}