public AssociationGroupEditViewModel(AssociationGroup item, IAppConfigRepository appConfigRepository, IEnumerable<object> selectedNames)
        {
            InnerItem = item;

            _selectedNames = selectedNames.Select(x => (string)x).Where(x => x != item.Name).ToList();

            NameSetting = appConfigRepository.Settings.Where(s => s.Name == "AssociationGroupTypes")
                                  .Expand(s => s.SettingValues)
                                  .FirstOrDefault();
            if (NameSetting != null)
            {
                var view = CollectionViewSource.GetDefaultView(NameSetting.SettingValues);
                view.Filter = FilterItems;
                view.Refresh();
            }
        }
		public AssociationGroupViewModel(
			IViewModelsFactory<IAssociationViewModel> vmFactory, 
			ICatalogEntityFactory entityFactory, 
			AssociationGroup item, 
			IItemViewModel parent)
		{
			InnerItem = item;
			_vmFactory = vmFactory;
			_entityFactory = entityFactory;
			_parent = parent;

			ItemAddCommand = new DelegateCommand(RaiseItemAddInteractionRequest);
			ItemEditCommand = new DelegateCommand<Association>(RaiseItemEditInteractionRequest, x => x != null);
			ItemRemoveCommand = new DelegateCommand<Association>(RaiseItemRemoveInteractionRequest, x => x != null);

			CommonConfirmRequest = new InteractionRequest<Confirmation>();
		}
		public void Can_create_associationGroup_with_association()
		{
			var repository = GetRepository();
			var items = new Item[] { };
			CreateFullGraphCatalog(repository, ref items, "testcatalog");

			//Refresh
			RefreshRepository(ref repository);
			var itemId = items[0].ItemId;
			var innerItem = repository.Items.Expand(x => x.AssociationGroups).Where(x => x.ItemId == itemId).Single();

			// create AssociationGroup
			var ag = new AssociationGroup()
			{
				Name = "name"
			};

			innerItem.AssociationGroups.Add(ag);

			// create Association
			itemId = items[1].ItemId;
			var a = new Association
				{
					AssociationType = "optional",
					//CatalogItem = repository.Items.Where(x => x.ItemId == itemId).Single() //USING THIS WORKS ONLY ON EF directly
					ItemId = itemId
				};
			ag.Associations.Add(a);

			// save
			repository.UnitOfWork.Commit();

			//RefreshClient
			RefreshRepository(ref repository);

			innerItem = repository.Items.Expand("AssociationGroups/Associations").Where(x => x.ItemId == innerItem.ItemId).Single();
			ag = innerItem.AssociationGroups.Single(x => x.AssociationGroupId == ag.AssociationGroupId);

			Assert.NotNull(innerItem);
			Assert.True(innerItem.AssociationGroups.Any());
			Assert.True(innerItem.AssociationGroups.Any(x => x.AssociationGroupId == ag.AssociationGroupId));
			Assert.True(ag.Associations.Any());
			Assert.True(ag.Associations.Any(x => x.AssociationId == a.AssociationId));
		}
		public void Can_delete_catalog_with_cascade()
		{
			var repository = GetRepository();

			var items = new Item[] { };
			const string catalogId = "testCatalog";

			CreateFullGraphCatalog(repository, ref items, catalogId);

			// create an AssociationGroup
			var group = new AssociationGroup
			{
				Name = "Name",
				ItemId = items[0].ItemId
			};
			repository.Add(group);

			var groupItem = new Association
			{
				AssociationType = "Required",
				AssociationGroupId = group.AssociationGroupId,
				ItemId = items[1].ItemId
			};
			repository.Add(groupItem);
			repository.UnitOfWork.Commit();

			RefreshRepository(ref repository);

			//var innerItem = repository.Catalogs.Where(x => x.CatalogId == catalogId).Single();
			var innerItem = repository.Catalogs.OfType<Catalog>()
				.Where(x => x.CatalogId == catalogId)
				.Expand(x => x.CatalogLanguages)
				.Expand("PropertySets/PropertySetProperties/Property/PropertyValues")
				.SingleOrDefault();

			repository.Remove(innerItem);
			repository.UnitOfWork.Commit();

			var associationCount = repository.Associations.Count();
			var itemCount = repository.Items.Count();
			Assert.Equal(0, associationCount);
			Assert.Equal(0, itemCount);
		}
		public override string Import(string catalogId, string propertySetId, ImportItem[] systemValues, ImportItem[] customValues, IRepository repository)
		{
			var _error = string.Empty;
			_repository = (ICatalogRepository)repository;

			var action = GetAction(systemValues.First(x => x.Name == "Action").Value);
			
			switch (action)
			{
				case ImportAction.Insert:
					var group = systemValues.First(y => y.Name == "GroupName").Value;
					var target = systemValues.First(y => y.Name == "TargetId").Value;
					var source = systemValues.First(y => y.Name == "ItemId").Value;
					
					var sourceItems = _repository.Items.Where(x => x.ItemId == source || x.Code == source).ToList();
					
					Item sourceItem;

					//aa: below condition checks if more than 1 item found - catalogId should be provided and item of the catalog selected, otherwise return error
					if (sourceItems.Count() > 1)
					{
						var sourceCatName = systemValues.First(y => y.Name == "SourceCatalogId").Value;
						if (!string.IsNullOrEmpty(sourceCatName))
							catalogId = _repository.Catalogs.Where(cat => cat.Name == sourceCatName).First().CatalogId;

						if (!string.IsNullOrEmpty(catalogId))
							sourceItem = sourceItems.FirstOrDefault(x => x.CatalogId == catalogId);
						else
						{
							var catNames = string.Empty;
							sourceItems.ForEach(x => catNames = catNames + x.CatalogId + ", ");
							_error = string.Format(notUniqueSourceError, source, catNames);
							return _error;
						}
					}
					//aa: if 1 item found set it to sourceItem and go further
					else
					{
						sourceItem = sourceItems.FirstOrDefault();
					}

					var targetItems = _repository.Items.Where(x => x.ItemId == target || x.Code == target).ToList();
					
					Item targetItem;

					//aa: below condition checks if more than 1 item found - catalogId should be provided and item of the catalog selected, otherwise return error
					if (targetItems.Count() > 1)
					{
						var targetCatName = systemValues.First(y => y.Name == "TargetCatalogId").Value;
						if (!string.IsNullOrEmpty(targetCatName))
							catalogId = _repository.Catalogs.Where(cat => cat.Name == targetCatName).First().CatalogId;

						if (!string.IsNullOrEmpty(catalogId))
							targetItem = targetItems.FirstOrDefault(x => x.CatalogId == catalogId);
						else
						{
							var catNames = string.Empty;
							targetItems.ForEach(x => catNames = catNames + x.CatalogId + ", ");
							_error = string.Format(notUniqueTargetError, target, catNames);
							return _error;
						}
					}
					//aa: if 1 item found set it to sourceItem and go further
					else
					{
						targetItem = targetItems.FirstOrDefault();
					}
					
					if (!string.IsNullOrEmpty(group) && targetItem != null && sourceItem != null)
					{
						var associationGroup = _repository.Associations.Where(x => x.AssociationGroup.Name == group && x.ItemId == targetItem.ItemId).SingleOrDefault();
						string groupId;
						if (associationGroup == null)
						{
							var addGroup = new AssociationGroup() { ItemId = targetItem.ItemId, Name = group };
							_repository.Add(addGroup);
							groupId = addGroup.AssociationGroupId;
						}
						else
						{
							groupId = associationGroup.AssociationGroupId;
						}

						var addItem = InitializeItem(null, systemValues);
						((Association)addItem).AssociationGroupId = groupId;
						((Association)addItem).ItemId = sourceItem.ItemId;
						
						_repository.Add(addItem);
					}
					else
					{
						_error = "Not all required data provided";
					}
					break;
				case ImportAction.InsertAndReplace:
					var itemR = systemValues.FirstOrDefault(y => y.Name == "AssociationId");
					if (itemR != null)
					{
						var originalItem = _repository.Associations.Where(x => x.ItemId == itemR.Value).SingleOrDefault();
						if (originalItem != null)
							repository.Remove(originalItem);
					}
					var replaceItem = InitializeItem(null, systemValues);
					repository.Add(replaceItem);
					break;
				case ImportAction.Update:
					var itemU = systemValues.FirstOrDefault(y => y.Name == "AssociationId");
					if (itemU != null)
					{
						var origItem = _repository.Associations.Where(x => x.ItemId == itemU.Value).SingleOrDefault();
						if (origItem != null)
						{
							InitializeItem(origItem, systemValues);
							_repository.Update(origItem);
						}
					}
					break;
				case ImportAction.Delete:
					var itemD = systemValues.FirstOrDefault(y => y.Name == "AssociationId");
					if (itemD != null)
					{
						var deleteItem = _repository.Associations.Where(x => x.ItemId == itemD.Value).SingleOrDefault();
						if (deleteItem != null)
							_repository.Remove(deleteItem);
					}
					break;
			}
			return _error;
		}