Ejemplo n.º 1
0
		public override string Import(string containerId, 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);
			
			var taxCategory = systemValues.SingleOrDefault(x => x.Name == "TaxCategory") != null ? systemValues.Single(x => x.Name == "TaxCategory").Value : null;
			var categoryId = systemValues.SingleOrDefault(x => x.Name == "CategoryId") != null ? systemValues.Single(x => x.Name == "CategoryId").Value : null;
			var itemCode = systemValues.SingleOrDefault(x => x.Name == "Code") != null ? systemValues.Single(x => x.Name == "Code").Value : null;
			var availability = systemValues.SingleOrDefault(x => x.Name == "AvailabilityRule") != null ? systemValues.Single(x => x.Name == "AvailabilityRule").Value : null;
			if (availability != null)
			{
				var number = (int)((AvailabilityRule)Enum.Parse(typeof(AvailabilityRule), availability));
				systemValues.SingleOrDefault(x => x.Name == "AvailabilityRule").Value = number.ToString();
			}


			switch (action)
			{
				case ImportAction.Insert:
					if (_repository.Items.Where(item => item.CatalogId == containerId && item.Code == itemCode).FirstOrDefault() != null)
					{
						_error = string.Format("Item with the code {0} already exist", itemCode);
					}
					else
					{
						var addItem = SetupItem(null, containerId, propertySetId, systemValues, customValues, _repository, taxCategory);
						_repository.Add(addItem);

						_error = SetupCategoryRelation(categoryId, containerId, _repository, addItem);
					}
					break;
				case ImportAction.InsertAndReplace:
					if (itemCode != null)
					{
						var originalItem = _repository.Items.Where(i => i.CatalogId == containerId && i.Code == itemCode).Expand(x => x.CategoryItemRelations).FirstOrDefault();
						if (originalItem != null)
						{
							originalItem = SetupItem(originalItem, containerId, propertySetId, systemValues, customValues, _repository, taxCategory);
							_repository.Update(originalItem);
							if (originalItem.CategoryItemRelations.All(rel => rel.CategoryId != categoryId))
								_error = SetupCategoryRelation(categoryId, containerId, _repository, originalItem);
						}
						else
						{
							var newItem = SetupItem(null, containerId, propertySetId, systemValues, customValues, _repository, taxCategory);
							_repository.Add(newItem);
							_error = SetupCategoryRelation(categoryId, containerId, _repository, newItem);
						}

					}
					break;
				case ImportAction.Update:
					if (itemCode != null)
					{
						var origItem = _repository.Items.FirstOrDefault(i => i.CatalogId == containerId && i.Code == itemCode);
						if (origItem != null)
						{
							SetupItem(origItem, containerId, propertySetId, systemValues, customValues, _repository, taxCategory);
							_repository.Update(origItem);
						}
					}
					break;
				case ImportAction.Delete:
					if (itemCode != null)
					{
						var deleteItem = _repository.Items.Where(i => i.CatalogId == containerId && i.Code == itemCode).SingleOrDefault();
						if (deleteItem != null)
							_repository.Remove(deleteItem);
					}
					break;
			}
			return _error;
		}
		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);
			var itemCode = systemValues.SingleOrDefault(y => y.Name == "Code") != null ? systemValues.Single(y => y.Name == "Code").Value : null;
			var parentCategoryCode = systemValues.SingleOrDefault(val => val.Name == "ParentCategoryId") != null ? systemValues.Single(val => val.Name == "ParentCategoryId").Value : null;
			var parentCategory = _repository.Categories.Where(cat => cat.CatalogId == catalogId && cat.Code == parentCategoryCode).FirstOrDefault();

			//if action is not delete and parent category code provided is not null and there is no category with the code in repository return error
			if (action != ImportAction.Delete && !string.IsNullOrEmpty(parentCategoryCode) && parentCategory == null)
			{
				_error = string.Format("Parent category with the code {0} does not exist", parentCategoryCode);
			}
			else
			{
				switch (action)
				{
					case ImportAction.Insert:
						//if there is already category with the same code - return error
						if (_repository.Categories.Where(cat => cat.CatalogId == catalogId && cat.Code == itemCode).FirstOrDefault() != null)
						{
							_error = string.Format("Category with the code {0} already exist", itemCode);
						}
						else
						{
							var addItem = InitializeItem(null, systemValues);
							addItem.ParentCategoryId = !string.IsNullOrEmpty(parentCategoryCode) ? parentCategory.CategoryId : null;
							addItem.CatalogId = catalogId;
							addItem.PropertySetId = propertySetId;
							_repository.Add(addItem);
							var propSet =
								_repository.PropertySets.Expand("PropertySetProperties/Property/PropertyValues")
								           .Where(x => x.PropertySetId == propertySetId)
								           .SingleOrDefault();
							if (propSet != null)
							{
								var resProps = InitializeProperties(propSet.PropertySetProperties.ToArray(), customValues, addItem.CategoryId);
								resProps.ForEach(_repository.Add);
							}
						}
						break;
					case ImportAction.InsertAndReplace:
						if (itemCode != null)
						{
							var originalItem =
								_repository.Categories.Where(cat => cat.CatalogId == catalogId && cat.Code == itemCode).FirstOrDefault();
							if (originalItem != null)
							{
								InitializeItem((Category)originalItem, systemValues);
								originalItem.ParentCategoryId = !string.IsNullOrEmpty(parentCategoryCode) ? parentCategory.CategoryId : null;
								_repository.Update(originalItem);
							}
							else
							{
								var addItem = InitializeItem(null, systemValues);
								addItem.ParentCategoryId = !string.IsNullOrEmpty(parentCategoryCode) ? parentCategory.CategoryId : null;
								addItem.CatalogId = catalogId;
								addItem.PropertySetId = propertySetId;
								_repository.Add(addItem);
								var propSet =
									_repository.PropertySets.Expand("PropertySetProperties/Property/PropertyValues")
											   .Where(x => x.PropertySetId == propertySetId)
											   .SingleOrDefault();
								if (propSet != null)
								{
									var resProps = InitializeProperties(propSet.PropertySetProperties.ToArray(), customValues, addItem.CategoryId);
									resProps.ForEach(_repository.Add);
								}
							}
						}
						break;
					case ImportAction.Update:
						var itemU = systemValues.FirstOrDefault(y => y.Name == "Code");
						if (itemU != null)
						{
							var origItem = _repository.Categories.Where(x => x.CategoryId == itemU.Value).SingleOrDefault();
							if (origItem != null)
							{
								InitializeItem((Category) origItem, systemValues);
								_repository.Update(origItem);
							}
						}
						break;
					case ImportAction.Delete:
						var deleteItem = _repository.Categories.Where(cat => cat.CatalogId == catalogId && cat.Code == itemCode).SingleOrDefault();
						if (deleteItem != null)
							_repository.Remove(deleteItem);
						break;
				}
			}
			return _error;
		}