Ejemplo n.º 1
0
		public override string Import(string catalogId, string propertySetId, ImportItem[] systemValues, ImportItem[] customValues, IRepository repository)
		{
			var _error = string.Empty;
			_repository = (IAppConfigRepository)repository;

			var action = GetAction(systemValues.First(x => x.Name == "Action").Value);
			var type = systemValues.First(y => y.Name == "KeywordType");
			type.Value = ((int)Enum.Parse(typeof(SeoUrlKeywordTypes), type.Value)).ToString(CultureInfo.InvariantCulture);

			switch (action)
			{
				case ImportAction.Insert:
					
					var addItem = InitializeItem(null, systemValues);
					_repository.Add(addItem);
					break;
				case ImportAction.InsertAndReplace:
					var itemR = systemValues.FirstOrDefault(y => y.Name == "Language");
					var itemRKey = systemValues.FirstOrDefault(y => y.Name == "KeywordValue");
					var itemRType = Int32.Parse(systemValues.First(y => y.Name == "KeywordType").Value);
					if (itemR != null && itemRKey != null)
					{
						var originalItem = _repository.SeoUrlKeywords.Where(x => x.Language == itemR.Value && x.KeywordValue == itemRKey.Value && x.KeywordType == itemRType).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 == "Language");
					var itemUKey = systemValues.FirstOrDefault(y => y.Name == "KeywordValue");
					var itemUType = Int32.Parse(systemValues.First(y => y.Name == "KeywordType").Value);
					if (itemU != null)
					{
						var origItem = _repository.SeoUrlKeywords.Where(x => x.Language == itemU.Value && x.KeywordValue.Equals(itemUKey.Value, StringComparison.Ordinal) && x.KeywordType == itemUType).SingleOrDefault();
						if (origItem != null)
						{
							InitializeItem(origItem, systemValues);							
							_repository.Update(origItem);
						}
					}
					break;
				case ImportAction.Delete:
					var itemD = systemValues.First(y => y.Name == "Language");
					var itemDKey = systemValues.First(y => y.Name == "KeywordValue");
					var itemDType = Int32.Parse(systemValues.First(y => y.Name == "KeywordType").Value);
					if (itemD != null)
					{
						var deleteItem = _repository.SeoUrlKeywords.Where(x => x.Language == itemD.Value && x.KeywordValue == itemDKey.Value && x.KeywordType == itemDType).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 itemId = systemValues.First(y => y.Name == "ItemId").Value;
			var itemAssetId = systemValues.First(y => y.Name == "AssetId").Value;
			var assetType = systemValues.First(y => y.Name == "AssetType").Value;

			var originalItem = _repository.Items.Where(x => x.ItemId == itemId || x.Code == itemId).Expand(it => it.ItemAssets).FirstOrDefault();
			if (originalItem != null)
			{
				switch (action)
				{
					case ImportAction.Insert:

						if (originalItem.ItemAssets.All(x => x.AssetId != itemAssetId))
						{
							var addItem = InitializeItem(null, systemValues);

							addItem.AssetType = !string.IsNullOrEmpty(assetType)
								                    ? addItem.AssetType
								                    : imageTypes.Any(x => x.Equals(Path.GetExtension(addItem.AssetId).Replace(".", string.Empty)))
									                      ? imageType
														  : Path.GetExtension(addItem.AssetId).Replace(".", string.Empty);

							originalItem.ItemAssets.Add(addItem);
							_repository.Update(originalItem);
						}
						break;
					case ImportAction.Update:
						if (originalItem.ItemAssets.Any(x => x.AssetId == itemAssetId))
						{
							var asset = originalItem.ItemAssets.FirstOrDefault(x => x.AssetId == itemAssetId);
							if (asset != null)
							{
								InitializeItem(asset, systemValues);
								_repository.Update(originalItem);
							}
						}
						break;
					case ImportAction.Delete:
						if (originalItem.ItemAssets.Any(x => x.AssetId == itemAssetId))
						{
							var assetD = originalItem.ItemAssets.FirstOrDefault(x => x.AssetId == itemAssetId);
							originalItem.ItemAssets.Remove(assetD);
							_repository.Update(originalItem);
						}
						break;
				}
			}
			return _error;
		}
		public override string Import(string catalogId, string propertySetId, ImportItem[] systemValues, ImportItem[] customValues, IRepository repository)
		{
			var _error = string.Empty;
			_repository = (IAppConfigRepository)repository;

			var action = GetAction(systemValues.First(x => x.Name == "Action").Value);

			switch (action)
			{
				case ImportAction.Insert:
					var addItem = InitializeItem(null, systemValues);
					_repository.Add(addItem);
					
					break;
				case ImportAction.InsertAndReplace:
					var itemR = systemValues.FirstOrDefault(y => y.Name == "LanguageCode");
					var itemRKey = systemValues.FirstOrDefault(y => y.Name == "Name");
					if (itemR != null)
					{
						var originalItem = _repository.Localizations.Where(x => x.LanguageCode == itemR.Value && x.Name == itemRKey.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 == "LanguageCode");
					var itemUKey = systemValues.FirstOrDefault(y => y.Name == "Name");
					if (itemU != null)
					{
						var origItem = _repository.Localizations.Where(x => x.LanguageCode == itemU.Value && x.Name.Equals(itemUKey.Value, StringComparison.Ordinal)).SingleOrDefault();
						if (origItem != null)
						{
							InitializeItem(origItem, systemValues);							
							_repository.Update(origItem);
						}
					}
					break;
				case ImportAction.Delete:
					var itemD = systemValues.FirstOrDefault(y => y.Name == "LanguageCode");
					if (itemD != null)
					{
						var deleteItem = _repository.Localizations.Where(x => x.LanguageCode == itemD.Value).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);

			switch (action)
			{
				case ImportAction.Insert:
					var addItem = InitializeItem(null, systemValues);
					_repository.Add(addItem);
					
					break;
				case ImportAction.InsertAndReplace:
					var itemR = systemValues.FirstOrDefault(y => y.Name == "TaxCategoryId");
					if (itemR != null)
					{
						var originalItem = _repository.TaxCategories.Where(x => x.TaxCategoryId == 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 == "TaxCategoryId");
					if (itemU != null)
					{
						var origItem = _repository.TaxCategories.Where(x => x.TaxCategoryId == itemU.Value).SingleOrDefault();
						if (origItem != null)
						{
							InitializeItem(origItem, systemValues);
							_repository.Update(origItem);
						}
					}
					break;
				case ImportAction.Delete:
					var itemD = systemValues.FirstOrDefault(y => y.Name == "TaxCategoryId");
					if (itemD != null)
					{
						var deleteItem = _repository.TaxCategories.Where(x => x.TaxCategoryId == itemD.Value).SingleOrDefault();
						if (deleteItem != null)
							_repository.Remove(deleteItem);
					}
					break;
			}
			return _error;
		}
Ejemplo n.º 5
0
		public override string Import(string catalogId, string propertySetId, ImportItem[] systemValues, ImportItem[] customValues, IRepository repository)
		{
			var _error = string.Empty;
			_catalogRepository = (ICatalogRepository) repository;
			_repository = (IPricelistRepository) repository;

			var action = GetAction(systemValues.First(x => x.Name == "Action").Value);
			var itemId = systemValues.First(y => y.Name == "ItemId").Value;
			var catName = systemValues.First(y => y.Name == "CatalogId").Value;
			if (!string.IsNullOrEmpty(catName))
				catalogId = _catalogRepository.Catalogs.Where(cat => cat.Name == catName).First().CatalogId;

			//get item with the provided ItemId or item Code
			var originalItems = _catalogRepository.Items.Where(x => x.ItemId == itemId || x.Code == itemId).ToArray();
			Item originalItem;

			//if more than 1 item found and catalogId is not provided - return error, otherwise ok
			if (originalItems.Count() > 1)
			{
				if (!string.IsNullOrEmpty(catalogId))
					originalItem = originalItems.FirstOrDefault(x => x.CatalogId == catalogId);
				else
				{
					var catNames = string.Empty;
					originalItems.ToList().ForEach(x => catNames = catNames + x.CatalogId + ", ");
					_error = string.Format("Item with code - {0} has matches in catalogs: {1}. Specify catalog property.", itemId, catNames);
					return _error;
				}
			}
			else
			{
				originalItem = originalItems.FirstOrDefault();
			}
			
			//if item with the code found (not null) try to execute the import action otherwise return error
			if (originalItem != null)
			{
				switch (action)
				{
					case ImportAction.Insert:
						var addItem = InitializeItem(null, systemValues);
						//set price itemId to the found ItemId
						addItem.ItemId = originalItem.ItemId;
						_repository.Add(addItem);

						break;
					case ImportAction.InsertAndReplace:
						var qtyR = systemValues.FirstOrDefault(y => y.Name == "MinQuantity");
						var listIdR = systemValues.FirstOrDefault(y => y.Name == "PricelistId");
						if (qtyR != null && listIdR != null)
						{
							var qtyVal = int.Parse(qtyR.Value);
							var origItem = _repository.Prices.ToList().Where(x => x.ItemId == originalItem.ItemId && x.MinQuantity == qtyVal && x.PricelistId == listIdR.Value).SingleOrDefault();
							if (origItem != null)
							{
								InitializeItem(origItem, systemValues);
								//set price itemId to the found ItemId
								origItem.ItemId = originalItem.ItemId;
								_repository.Update(origItem);
							}
							else
							{
								var newItem = InitializeItem(null, systemValues);
								//set price itemId to the found ItemId
								newItem.ItemId = originalItem.ItemId;
								_repository.Add(newItem);
							}
						}
						break;
					case ImportAction.Update:
						var qty = systemValues.FirstOrDefault(y => y.Name == "MinQuantity");
						var listId = systemValues.FirstOrDefault(y => y.Name == "PricelistId");
						if (qty != null && listId != null)
						{
							var qtyVal = int.Parse(qty.Value);
							var origItem = _repository.Prices.ToList().Where(x => x.ItemId == originalItem.ItemId && x.MinQuantity == qtyVal && x.PricelistId == listId.Value).SingleOrDefault();
							if (origItem != null)
							{
								InitializeItem(origItem, systemValues);
								//set price itemId to the found ItemId
								origItem.ItemId = originalItem.ItemId;
								_repository.Update(origItem);
							}
						}
						break;
					case ImportAction.Delete:
						var qtyValue = systemValues.FirstOrDefault(y => y.Name == "MinQuantity");
						var pricelistId = systemValues.FirstOrDefault(y => y.Name == "PricelistId");
						if (qtyValue != null && pricelistId != null)
						{
							var qtyVal = int.Parse(qtyValue.Value);
							var deleteItem = _repository.Prices.ToList().Where(x => x.ItemId == originalItem.ItemId && x.MinQuantity == qtyVal && x.PricelistId == pricelistId.Value).SingleOrDefault();
							if (deleteItem != null)
								_repository.Remove(deleteItem);
						}
						break;
				}
			}
			else
			{
				_error = string.Format("Item with itemId/code - {0} not found", itemId);
			}
			return _error;
		}
Ejemplo n.º 6
0
		private static ImportItem[] MapColumns(IEnumerable<MappingItem> mappingItems, IReadOnlyDictionary<string, int> csvNamesAndIndexes, IList<string> csvValues, ImportResult result)
		{
			var csvNamesAndValues = new List<ImportItem>();

			if (mappingItems != null)
			{
				foreach (var mappingItem in mappingItems)
				{					
					var importItem = new ImportItem {Name = mappingItem.EntityColumnName, Locale = mappingItem.Locale};
					if (mappingItem.CsvColumnName != null && csvNamesAndIndexes.ContainsKey(mappingItem.CsvColumnName))
					{
						var columnIndex = csvNamesAndIndexes[mappingItem.CsvColumnName];

						if (csvValues[columnIndex] != null)
							importItem.Value = csvValues[columnIndex];

						if (!string.IsNullOrEmpty(mappingItem.StringFormat))
							importItem.Value = string.Format(mappingItem.StringFormat, importItem.Value);

						if (mappingItem.IsRequired && string.IsNullOrEmpty(importItem.Value))
						{
							if (result.Errors == null)
								result.Errors = new List<string>();
							result.Errors.Add(string.Format("Row: {0}, Property: {1}, Error: {2}", result.ProcessedRecordsCount + result.ErrorsCount, mappingItem.DisplayName, "The value for required property not provided"));							
							result.ErrorsCount++;
							return null;
						}
					}
					else if (mappingItem.CustomValue != null)
					{
						importItem.Value = mappingItem.CustomValue;

						if (!string.IsNullOrEmpty(mappingItem.StringFormat))
							importItem.Value = string.Format(mappingItem.StringFormat, importItem.Value);
					}

					csvNamesAndValues.Add(importItem);
				}
			}

			return csvNamesAndValues.ToArray();
		}
Ejemplo n.º 7
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;
		}
Ejemplo n.º 8
0
		private Item SetupItem(Item item, string containerId, string propertySetId, ImportItem[] systemValues, IEnumerable<ImportItem> customValues, ICatalogRepository repository, string taxCategory)
		{
			var retVal = (Item)InitializeItem(item, systemValues);
			retVal.CatalogId = containerId;
			var propSet = repository.PropertySets.Expand("PropertySetProperties/Property/PropertyValues").Where(x => x.PropertySetId == propertySetId).First();
			var resProps = InitializeProperties(propSet.PropertySetProperties.ToArray(), customValues, retVal.ItemId);
			retVal.ItemPropertyValues.Add(resProps);
			retVal.PropertySetId = propSet.PropertySetId;

			if (!string.IsNullOrEmpty(taxCategory))
			{
				var cat =
					repository.TaxCategories.Where(x => x.TaxCategoryId == taxCategory || x.Name == taxCategory).FirstOrDefault();
				retVal.TaxCategory = cat != null ? cat.TaxCategoryId : null;
			}

			if (!string.IsNullOrEmpty(systemValues.First(x => x.Name == "ItemAsset").Value))
			{
				var itemAsset = new ItemAsset
				{
					AssetType = "image",
					ItemId = retVal.ItemId,
					AssetId = systemValues.First(x => x.Name == "ItemAsset").Value,
					GroupName = "primaryimage"
				};
				retVal.ItemAssets.Add(itemAsset);
			}

			if (!string.IsNullOrEmpty(systemValues.First(x => x.Name == "EditorialReview").Value))
			{
				var editorial = new CatalogEntityFactory().CreateEntity<EditorialReview>();
				editorial.ReviewState = (int) ReviewState.Active;
				editorial.Content = systemValues.First(x => x.Name == "EditorialReview").Value;
				editorial.ItemId = retVal.ItemId;
				retVal.EditorialReviews.Add(editorial);
			}

			return retVal;
		}
		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;
		}
		public override string Import(string catalogId, string propertySetId, ImportItem[] systemValues, ImportItem[] customValues, IRepository repository)
		{
			var _error = string.Empty;
			_repository = (IOrderRepository)repository;

			var action = GetAction(systemValues.First(x => x.Name == "Action").Value);

			switch (action)
			{
				case ImportAction.Insert:
					var itemI = systemValues.FirstOrDefault(y => y.Name == "Code");

					if (itemI != null)
					{
						var originalItem = _repository.JurisdictionGroups.Where(x => x.Code == itemI.Value).FirstOrDefault();
						if (originalItem == null)
						{
							var addItem = InitializeItem(null, systemValues);							
							_repository.Add(addItem);
							originalItem = addItem;
						}

						var jurisdictionCode = systemValues.First(x => x.Name == "JurisdictionCode").Value;

						if (jurisdictionCode != null)
						{
							var jItem = _repository.Jurisdictions.Where(x => x.Code == jurisdictionCode).SingleOrDefault();
							if (jItem != null)
							{
								var relation = new JurisdictionRelation() { JurisdictionId = jItem.JurisdictionId, JurisdictionGroupId = originalItem.JurisdictionGroupId };
								_repository.Add(relation);
							}
						}
					}

					break;
				case ImportAction.InsertAndReplace:
					var itemR = systemValues.FirstOrDefault(y => y.Name == "Code");
					if (itemR != null)
					{
						var originalItem = _repository.JurisdictionGroups.Where(x => x.Code == 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 == "JurisdictionCode");
					if (itemU != null)
					{
						var origItem = _repository.JurisdictionGroups.Where(x => x.Code == itemU.Value).SingleOrDefault();
						if (origItem != null)
						{
							InitializeItem(origItem, systemValues);
							_repository.Update(origItem);
						}
					}
					break;
				case ImportAction.Delete:
					var itemD = systemValues.FirstOrDefault(y => y.Name == "JurisdictionCode");
					if (itemD != null)
					{
						var deleteItem = _repository.JurisdictionGroups.Where(x => x.Code == itemD.Value).SingleOrDefault();
						if (deleteItem != null)
							_repository.Remove(deleteItem);
					}
					break;
			}
			return _error;
		}
		public abstract string Import(string catalogId, string propertySetId, ImportItem[] systemValues,
		                              ImportItem[] customValues, IRepository repository);
		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;
		}
Ejemplo n.º 13
0
		public override string Import(string catalogId, string propertySetId, ImportItem[] systemValues, ImportItem[] customValues, IRepository repository)
		{
			var _error = string.Empty;
			_repository = (ITaxRepository)repository;

			var action = GetAction(systemValues.First(x => x.Name == "Action").Value);

			switch (action)
			{
				case ImportAction.Insert:
					var itemI = systemValues.FirstOrDefault(y => y.Name == "TaxId");
					if (itemI != null)
					{
						var originalItem = _repository.Taxes.Where(x => x.TaxId == itemI.Value);
						if (originalItem.Count() == 0)
						{
							var name = systemValues.FirstOrDefault(y => y.Name == "TaxName");
							var type = systemValues.FirstOrDefault(y => y.Name == "TaxType");
							var addItem = new Tax { TaxId = itemI.Value, Name = name.Value, TaxType = Int32.Parse(type.Value) };
							_repository.Add(addItem);
							_repository.UnitOfWork.Commit();
						}
					}
					
					var tax = _repository.Taxes.Where(x => x.TaxId == itemI.Value);
					if (tax.Count() > 0)
					{
						var t = tax.First();
						var value = InitializeItem(null, systemValues);
						t.TaxValues.Add(value);
					}
					break;
				case ImportAction.InsertAndReplace:
					var itemR = systemValues.FirstOrDefault(y => y.Name == "TaxId");
					if (itemR != null)
					{
						var originalItem = _repository.Taxes.Where(x => x.TaxId == 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 == "PriceId");
					//if (itemU != null)
					//{
					//	var origItem = _repository.Taxes.Where(x => x.TaxId == itemU.Value).SingleOrDefault();
					//	if (origItem != null && origItem is TaxValue)
					//	{
					//		InitializeItem((TaxValue)origItem, systemValues);
					//		_repository.Update(origItem);
					//	}
					//}
					break;
				case ImportAction.Delete:
					var itemD = systemValues.FirstOrDefault(y => y.Name == "TaxId");
					if (itemD != null)
					{
						var deleteItem = _repository.Taxes.Where(x => x.TaxId == itemD.Value).SingleOrDefault();
						if (deleteItem != null)
							_repository.Remove(deleteItem);
					}
					break;
			}
			return _error;
		}