private void UpdateOptionSet(OptionSetDTO optionSet, int languageId, KatrinEntities context)
        {
            OptionSet coptionSet = context.OptionSets.First(c => c.OptionSetId == optionSet.OptionSetId);

            coptionSet.Name = optionSet.Name;
            context.OptionSets.Attach(coptionSet);
            DeleteOptionRelation(optionSet, languageId, context);
            AddOptionRelation(optionSet, languageId, context);
        }
 private void AddOptionSet(OptionSetDTO optionSet, int languageId, KatrinEntities context)
 {
     context.OptionSets.Add(new OptionSet
     {
         IsCustomizable = optionSet.IsCustomizable ?? true,
         Name           = optionSet.Name,
         OptionSetId    = optionSet.OptionSetId
     });
     AddOptionRelation(optionSet, languageId, context);
 }
 public List <OptionSetDTO> GetOptionSetById(Guid optionSetId, int languageId)
 {
     using (var context = KatrinEntities.CreateInstance())
     {
         context.Configuration.ProxyCreationEnabled = false;
         //var query = context.OptionSets
         //    .FetchMany(r=>r.AttributePicklistValues);
         var optionSetQuery = from o in context.OptionSets
                              join p in context.AttributePicklistValues.OrderBy(c => c.DisplayOrder)
                              on o.OptionSetId equals p.OptionSetId into pickupList
                              join local in context.LocalizedLabels.Where(c => c.LanguageId == languageId)
                              on o.OptionSetId equals local.ObjectId into locals
                              where o.OptionSetId == optionSetId
                              select new
         {
             optionset  = o,
             pList      = pickupList,
             localLabls = locals
         };
         return(optionSetQuery.ToList().Select(r =>
         {
             var item = new OptionSetDTO();
             var description = r.localLabls.Where(c => c.ObjectColumnName == "Description").FirstOrDefault();
             item.Description = description != null ? description.Label : "";
             var displayName = r.localLabls.Where(c => c.ObjectColumnName == "DisplayName").FirstOrDefault();
             item.DisplayName = displayName != null ? displayName.Label : "";
             item.IsCustomizable = r.optionset.IsCustomizable;
             item.Name = r.optionset.Name;
             item.OptionSetId = r.optionset.OptionSetId;
             item.OptionList = new List <OptionDTO>();
             r.pList.ToList().ForEach(p =>
             {
                 var option = new OptionDTO();
                 var itemDescription = context.LocalizedLabels.SingleOrDefault(c => c.ObjectId == p.AttributePicklistValueId &&
                                                                               c.ObjectColumnName == "Description" &&
                                                                               c.LanguageId == languageId);
                 option.Description = itemDescription != null ? itemDescription.Label : "";
                 var itemDisplayName = context.LocalizedLabels.Single(c => c.ObjectId == p.AttributePicklistValueId &&
                                                                      c.ObjectColumnName == "DisplayName" &&
                                                                      c.LanguageId == languageId);
                 option.DisplayName = itemDisplayName != null ? itemDisplayName.Label : "";
                 option.DisplayOrder = p.DisplayOrder;
                 option.OptionId = p.AttributePicklistValueId;
                 option.Value = p.Value;
                 item.OptionList.Add(option);
             });
             return item;
         }
                                               ).ToList());
     }
 }
 public void SaveOptionSet(OptionSetDTO optionSet, int languageId, bool isAdd)
 {
     using (var context = KatrinEntities.CreateInstance())
     {
         context.Configuration.ProxyCreationEnabled = false;
         if (isAdd)
         {
             AddOptionSet(optionSet, languageId, context);
         }
         else
         {
             UpdateOptionSet(optionSet, languageId, context);
         }
         context.SaveChanges();
     }
 }
        private void DeleteOptionRelation(OptionSetDTO optionSet, int languageId, KatrinEntities context)
        {
            var optionLocals = context.LocalizedLabels.Where(c => c.ObjectId == optionSet.OptionSetId &&
                                                             c.LanguageId == languageId).ToList();

            optionLocals.ForEach(l => context.LocalizedLabels.Remove(l));
            var pickUpList = context.AttributePicklistValues.Where(c => c.OptionSetId == optionSet.OptionSetId).ToList();

            pickUpList.ForEach(p =>
            {
                var locals = context.LocalizedLabels.Where(c => c.ObjectId == p.AttributePicklistValueId &&
                                                           c.LanguageId == languageId).ToList();
                locals.ForEach(l => context.LocalizedLabels.Remove(l));
                context.AttributePicklistValues.Remove(p);
            });
        }
 private void AddOptionRelation(OptionSetDTO optionSet, int languageId, KatrinEntities context)
 {
     context.LocalizedLabels.Add(new LocalizedLabel
     {
         LocalizedLabelId = Guid.NewGuid(),
         Label            = optionSet.Description,
         LanguageId       = languageId,
         ObjectColumnName = "Description",
         ObjectId         = optionSet.OptionSetId
     });
     context.LocalizedLabels.Add(new LocalizedLabel
     {
         LocalizedLabelId = Guid.NewGuid(),
         Label            = optionSet.DisplayName,
         LanguageId       = languageId,
         ObjectColumnName = "DisplayName",
         ObjectId         = optionSet.OptionSetId
     });
     foreach (OptionDTO option in optionSet.OptionList)
     {
         context.AttributePicklistValues.Add(new AttributePicklistValue
         {
             AttributePicklistValueId = option.OptionId,
             DisplayOrder             = option.DisplayOrder,
             OptionSetId = optionSet.OptionSetId,
             Value       = option.Value
         });
         context.LocalizedLabels.Add(new LocalizedLabel
         {
             LocalizedLabelId = Guid.NewGuid(),
             Label            = option.Description,
             LanguageId       = languageId,
             ObjectColumnName = "Description",
             ObjectId         = option.OptionId
         });
         context.LocalizedLabels.Add(new LocalizedLabel
         {
             LocalizedLabelId = Guid.NewGuid(),
             Label            = option.DisplayName,
             LanguageId       = languageId,
             ObjectColumnName = "DisplayName",
             ObjectId         = option.OptionId
         });
     }
 }
        protected override void OnViewSet()
        {
            base.OnViewSet();
            var metadataService = MetadataProvider.CreateServiceClient();

            View.SaveOptionSet += SaveOptionSet;
            int langId = System.Globalization.CultureInfo.CurrentCulture.LCID;

            entity = WorkingMode == EntityDetailWorkingMode.Add?
                     new OptionSetDTO() : metadataService.GetOptionSetById(optionSetId, langId).First();
            if (WorkingMode == EntityDetailWorkingMode.Add)
            {
                entity.OptionSetId = optionSetId;
                entity.OptionList  = new List <OptionDTO>();
            }
            List <OptionSetDTO> list = new List <OptionSetDTO>();

            list.Add(entity);
            View.Bind(list);
        }