using Microsoft.AspNetCore.Mvc; using Raven.Client.Documents; using Raven.Client.Documents.Session; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zero.Commerce.Entities; using zero.Core; using zero.Core.Api; using zero.Core.Attributes; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Utils; namespace zero.Debug.Controllers { public partial class MigrationController : Controller { [HttpGet] public async Task CreateBlueprints() { IList apps; using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { apps = await session.Query().ToListAsync(); } async Task Handle() where T : IZeroIdEntity { return await CreateBlueprintsForAll(apps); } return Json(new { //Applications = await Handle(), //Categories = await Handle(), //Channels = await Handle(), //Countries = await Handle(), //Currencies = await Handle(), //Customers = await Handle(), //Languages = await Handle(), //MailTemplates = await Handle(), //Manufacturers = await Handle(), //Media = await Handle(), //MediaFolders = await Handle(), //NumberTemplates = await Handle(), //OrderDetailStates = await Handle(), //Orders = await Handle(), //Pages = await Handle(), //ProductProperties = await Handle(), //Products = await Handle(), //RecycleBin = await Handle(), ShippingOptions = await Handle(), //SpaceContents = await Handle(), //TaxRates = await Handle(), //Translations = await Handle(), //UserRoles = await Handle(), //Users = await Handle(), }); } async Task> CreateBlueprintsForAll(IList apps) where T : IZeroIdEntity { HashSet ids = new HashSet(); IList items; using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { session.Advanced.MaxNumberOfRequestsPerSession = 10000; items = await session.Query().ToListAsync(); foreach (T item in items) { HashSet variants = ExtractBlueprints(item, apps); if (variants.Count > 0) { ids.Add("blueprint." + item.Id); foreach (IZeroIdEntity variant in variants) { ((IZeroEntity)variant).BlueprintId = item.Id; // find all references List> references = ObjectTraverser.FindAttribute(variant); foreach (ObjectTraverser.Result reference in references) { object value = reference.Property.GetValue(reference.Parent, null); //if (String.IsNullOrWhiteSpace(id) || id.StartsWith(NEW_ID)) //{ // item.Property.SetValue(item.Parent, item.Item.Length.HasValue ? Raven.Id(item.Item.Length.Value) : Raven.Id()); //} } //await session.StoreAsync(variant); ids.Add(variant.Id); } } } //await session.SaveChangesAsync(); } return ids; } HashSet ExtractBlueprints(T model, IList apps) where T : IZeroIdEntity { HashSet newModels = new HashSet(); IAppAwareEntity appAwareEntity = model as IAppAwareEntity; IZeroEntity zeroEntity = model as IZeroEntity; if (appAwareEntity != null && appAwareEntity.AppId == "shared") { foreach (IApplication app in apps) { IZeroEntity newSpecificEntity = zeroEntity.Clone(); newSpecificEntity.Id = null; ((IAppAwareEntity)newSpecificEntity).AppId = app.Id; newModels.Add(newSpecificEntity); } } return newModels; } } }