private long AddStyle(IUnitOfWork db, DateTime when, long?by)
        {
            var notEmptyLinkedStyles = LinkedStyles
                                       .Where(s => !String.IsNullOrEmpty(s.LinkedStyleString))
                                       .ToList();

            StyleViewModel.SetDefaultImage(ImageSet.Images);

            var dbStyle = new Style
            {
                StyleID       = StyleId,
                Type          = (int)StyleTypes.References,
                Name          = Name,
                DropShipperId = DSHelper.DefaultPAId,
                Image         = ImageSet.GetMainImageUrl(),
                //AdditionalImages = ImageSet.GetAdditionalImagesUrl(),

                ItemTypeId = (int)ItemType.Pajama,

                CreateDate = when,
                CreatedBy  = by
            };

            db.Styles.Add(dbStyle);
            db.Commit();

            StyleViewModel.UpdateImages(db, dbStyle.Id, ImageSet.Images, when, by);

            db.StyleReferences.UpdateStyleReferencesForStyle(dbStyle.Id,
                                                             notEmptyLinkedStyles,
                                                             when,
                                                             by);

            var styleItemToLinkedItems = RebuildStyleItems(db, dbStyle, notEmptyLinkedStyles);

            db.StyleItems.UpdateStyleItemsForStyle(dbStyle.Id,
                                                   styleItemToLinkedItems.Select(si => si.StyleItem).ToList(),
                                                   when,
                                                   by);

            var styleItemReferences = BuildStyleItemReferences(db, dbStyle.Id, styleItemToLinkedItems);

            db.StyleItemReferences.UpdateStyleItemReferencesForStyle(dbStyle.Id,
                                                                     styleItemReferences,
                                                                     when,
                                                                     by);

            UpdateStyleFeatures(db, dbStyle.Id, LinkedStyles.Select(s => s.LinkedStyleId).ToList(), when, by);

            return(dbStyle.Id);
        }
        public List <ValidationResult> Validate(IUnitOfWork db)
        {
            var result = new List <ValidationResult>();

            if (String.IsNullOrEmpty(StyleId))
            {
                result.Add(new ValidationResult("Style Id is empty"));
            }
            else
            {
                var existStyles = db.Styles.GetAllAsDto().Where(s => s.StyleID == StyleId).ToList();
                if (existStyles.Count > 1 || (existStyles.Count == 1 && existStyles[0].Id != Id))
                {
                    result.Add(new ValidationResult("Specified StyleId already exist"));
                }
            }

            var notEmptyLinkedStyles = LinkedStyles.Where(s => !String.IsNullOrEmpty(s.LinkedStyleString)).ToList();

            if (!notEmptyLinkedStyles.Any())
            {
                result.Add(new ValidationResult("At least the one linked style should be specified"));
            }
            else
            {
                var linkedStyleStringList = notEmptyLinkedStyles.Select(s => s.LinkedStyleString).ToList();
                var dbStyles = db.Styles.GetAllAsDto()
                               .Where(s => linkedStyleStringList.Contains(s.StyleID) &&
                                      !s.Deleted)
                               .ToList();

                foreach (var linkedStyle in notEmptyLinkedStyles)
                {
                    var existDbStyle = dbStyles.FirstOrDefault(s => s.StyleID == linkedStyle.LinkedStyleString);
                    if (existDbStyle == null)
                    {
                        result.Add(new ValidationResult(String.Format("Specified StyleId: \"{0}\" is not found in the system", linkedStyle.LinkedStyleString)));
                    }
                    else
                    {
                        linkedStyle.LinkedStyleId = existDbStyle.Id;
                    }
                }
            }

            return(result);
        }
        private void UpdateStyle(IUnitOfWork db, Style style, DateTime when, long?by)
        {
            var notEmptyLinkedStyles = LinkedStyles
                                       .Where(s => !String.IsNullOrEmpty(s.LinkedStyleString))
                                       .ToList();

            StyleViewModel.SetDefaultImage(ImageSet.Images);

            style.StyleID = StyleId;
            style.Name    = Name;

            style.Image = ImageSet.GetMainImageUrl();
            //style.AdditionalImages = ImageSet.GetAdditionalImagesUrl();

            style.UpdateDate = when;
            style.UpdatedBy  = by;

            db.Commit();

            StyleViewModel.UpdateImages(db, style.Id, ImageSet.Images, when, by);

            db.StyleReferences.UpdateStyleReferencesForStyle(style.Id,
                                                             notEmptyLinkedStyles,
                                                             when,
                                                             by);

            var styleItemToLinkedItems = RebuildStyleItems(db, style, notEmptyLinkedStyles);

            db.StyleItems.UpdateStyleItemsForStyle(style.Id,
                                                   styleItemToLinkedItems.Select(si => si.StyleItem).ToList(),
                                                   when,
                                                   by);

            var styleItemReferences = BuildStyleItemReferences(db, style.Id, styleItemToLinkedItems);

            db.StyleItemReferences.UpdateStyleItemReferencesForStyle(style.Id,
                                                                     styleItemReferences,
                                                                     when,
                                                                     by);
        }