Ejemplo n.º 1
0
 public IEnumerable <TerritoryPart> GetAllProvinces(TerritoryPart country)
 {
     return(GetTerritoryChildren(new TerritoryQueryContext {
         TerritoryAdministrativeType = TerritoryAdministrativeType.Province,
         ForTerritory = country
     }));
 }
Ejemplo n.º 2
0
 public IEnumerable <TerritoryPart> GetAllCities(TerritoryPart parent)
 {
     return(GetTerritoryChildren(new TerritoryQueryContext {
         TerritoryAdministrativeType = TerritoryAdministrativeType.City,
         ForTerritory = parent
     }));
 }
 void RemoveChildren(RemoveContentContext context, TerritoryPart part)
 {
     // Only remove first level of children, because they will remove their children
     foreach (var item in part.Children)
     {
         _contentManager.Remove(item);
     }
 }
Ejemplo n.º 4
0
        public void AssignInternalRecord(TerritoryPart territory, int id)
        {
            var internalRecord = _territoriesRepositoryService.GetTerritoryInternal(id);

            if (internalRecord == null)
            {
                throw new ArgumentException(nameof(id), T("No TerritoryInternalRecord exists with the id provided (\"{0}\")", id).Text);
            }
            AssignInternalRecord(territory, internalRecord);
        }
 public AddressConfigurationTerritoryViewModel(
     TerritoryPart part,
     IEnumerable <int> countries,
     IEnumerable <int> provinces,
     IEnumerable <int> cities,
     AddressConfigurationTerritoryViewModel parent)
     : this(part, countries, provinces, cities)
 {
     Parent = parent;
 }
 /// <summary>
 /// This method verifies that neither the passed territory is valid. In case it's not, it will
 /// throw the corresponding exception.
 /// </summary>
 /// <param name="territory">The TerritoryPart argument to validate.</param>
 /// <param name="name">The name of the argument being validated.</param>
 /// <exception cref="ArgumentNullException">Throws an ArgumentNullException if the TerritoryPart
 /// argument is null.</exception>
 /// <exception cref="ArgumentException">Throws an ArgumentException if the TerritoryPart
 /// argument has a null underlying record.</exception>
 public static void ValidateArgument(TerritoryPart territory, string name)
 {
     if (territory == null)
     {
         throw new ArgumentNullException(name);
     }
     if (territory.Record == null)
     {
         throw new ArgumentException(T("Part record cannot be null.").Text, name);
     }
 }
Ejemplo n.º 7
0
 public IEnumerable <TerritoryPart> GetAllCities(
     AddressRecordType addressRecordType, TerritoryPart parent,
     string nameQuery, int maxOptions = 20)
 {
     return(GetTerritoryChildren(new TerritoryQueryContext {
         AddressRecordType = addressRecordType,
         TerritoryAdministrativeType = TerritoryAdministrativeType.City,
         Filter = nameQuery,
         ForTerritory = parent
     }));
 }
Ejemplo n.º 8
0
        public void AssignParent(TerritoryPart territory, TerritoryPart parent)
        {
            TerritoriesUtilities.ValidateArgument(territory, nameof(territory));
            TerritoriesUtilities.ValidateArgument(parent, nameof(parent));

            // verify parent != territory
            if (parent.Record.Id == territory.Record.Id)
            {
                throw new InvalidOperationException(T("The parent and child territories cannot be the same.").Text);
            }

            // verify type
            if (territory.ContentItem.ContentType != parent.ContentItem.ContentType)
            {
                var territoryTypeText = territory.ContentItem
                                        .TypeDefinition.DisplayName;
                var parentTypeText = parent.ContentItem
                                     .TypeDefinition.DisplayName;
                throw new ArrayTypeMismatchException(
                          T("The ContentType for the Territory ({0}) does not match the ContentType for the parent ({1})",
                            territoryTypeText, parentTypeText).Text);
            }
            // verify hierarchies.
            if (territory.Record.Hierarchy == null)
            {
                throw new ArgumentException(T("The hierarchy for the Territory must not be null.").Text, nameof(territory));
            }
            if (parent.Record.Hierarchy == null)
            {
                throw new ArgumentException(T("The hierarchy for the Territory must not be null.").Text, nameof(parent));
            }
            if (parent.Record.Hierarchy.Id != territory.Record.Hierarchy.Id)
            {
                throw new ArrayTypeMismatchException(T("The two territories must belong to the same hierarchy.").Text);
            }

            // verify that the assignment would not create a cycle
            var recordCheck = parent.Record;

            while (recordCheck.ParentTerritory != null)
            {
                if (recordCheck.ParentTerritory.Id == territory.Record.Id)
                {
                    throw new InvalidOperationException(T("The parent territory cannot be a leaf of the child.").Text);
                }
                recordCheck = recordCheck.ParentTerritory;
            }

            // finally move
            territory.Record.ParentTerritory = parent.Record;
        }
        public IEnumerable <TerritoryPart> GetAllProvinces(
            AddressRecordType addressRecordType, TerritoryPart country, TerritoryPart city)
        {
            var allProvinces = GetAllProvinces(addressRecordType, country);

            if (city == null)
            {
                return(allProvinces);
            }
            // only provinces that contain the city
            return(allProvinces
                   .Where(pr => GetChildren(pr, _settingsService.SelectedCityIds)
                          .Any(tp => tp.Record.TerritoryInternalRecord.Id == city.Record.TerritoryInternalRecord.Id)));
        }
Ejemplo n.º 10
0
 public void AddTerritory(TerritoryPart territory, TerritoryHierarchyPart hierarchy)
 {
     TerritoriesUtilities.ValidateArgument(territory, nameof(territory));
     TerritoriesUtilities.ValidateArgument(hierarchy, nameof(hierarchy));
     // check that types are correct
     if (territory.ContentItem.ContentType != hierarchy.TerritoryType)
     {
         var territoryTypeText = territory.ContentItem
                                 .TypeDefinition.DisplayName;
         var hierarchyTerritoryTypeText = _contentDefinitionManager
                                          .GetTypeDefinition(hierarchy.TerritoryType).DisplayName;
         throw new ArrayTypeMismatchException(
                   T("The ContentType for the Territory ({0}) does not match the expected TerritoryType for the hierarchy ({1})",
                     territoryTypeText, hierarchyTerritoryTypeText).Text);
     }
     // The territory may come from a different hierarchy
     if (territory.Record.Hierarchy != null &&
         territory.Record.Hierarchy.Id != hierarchy.Record.Id)
     {
         // Verify that the TerritoryInternalRecords in the territory or its children can be moved there
         var internalRecords = new List <int>();
         if (territory.Record.TerritoryInternalRecord != null)
         {
             internalRecords.Add(territory.Record.TerritoryInternalRecord.Id);
         }
         if (territory.Record.Children != null)
         {
             internalRecords.AddRange(territory
                                      .Record
                                      .Children
                                      .Where(tpr => tpr.TerritoryInternalRecord != null)
                                      .Select(tpr => tpr.TerritoryInternalRecord.Id));
         }
         if (internalRecords.Any())
         {
             if (hierarchy.Record
                 .Territories
                 .Select(tpr => tpr.TerritoryInternalRecord.Id)
                 .Any(tir => internalRecords.Contains(tir)))
             {
                 throw new TerritoryInternalDuplicateException(T("The territory being moved is already assigned in the current hierarchy."));
             }
         }
     }
     // remove parent: This method always puts the territory at the root level of the hierarchy
     territory.Record.ParentTerritory = null;
     // set hierarchy and also set the hierarchy for all children: we need to move all levels of children,
     // and record.Children only contains the first level.
     AssignHierarchyToChildren(territory.Record, hierarchy.Record);
 }
        void LazyLoadHandlers(TerritoryPart part)
        {
            part.ChildrenField.Loader(() => {
                if (part.Record.Children != null && part.Record.Children.Any())
                {
                    return(_contentManager
                           .GetMany <ContentItem>(part.Record.Children.Select(tpr => tpr.ContentItemRecord.Id),
                                                  VersionOptions.Latest, QueryHints.Empty));
                }
                else
                {
                    return(Enumerable.Empty <ContentItem>());
                }
            });

            part.HierarchyField.Loader(() => {
                if (part.Record.Hierarchy != null)
                {
                    return(_contentManager
                           .Get <ContentItem>(part.Record.Hierarchy.Id,
                                              VersionOptions.Latest, QueryHints.Empty));
                }
                else
                {
                    return(null);
                }
            });

            part.ParentField.Loader(() => {
                if (part.Record.ParentTerritory != null)
                {
                    return(_contentManager
                           .Get <ContentItem>(part.Record.ParentTerritory.Id,
                                              VersionOptions.Latest, QueryHints.Empty));
                }
                else
                {
                    return(null);
                }
            });

            part.AllChildrenCountField.Loader(() => {
                if (part.Record != null)
                {
                    return(CountChildren(part.Record));
                }
                return(0);
            });
        }
Ejemplo n.º 12
0
        private TerritoryHierarchyTreeNode MakeANode(TerritoryPart territoryPart)
        {
            var metadata       = _contentManager.GetItemMetadata(territoryPart.ContentItem);
            var requestContext = _workContextAccessor.GetContext().HttpContext.Request.RequestContext;

            return(new TerritoryHierarchyTreeNode {
                Id = territoryPart.ContentItem.Id,
                TerritoryItem = territoryPart.ContentItem,
                ParentId = territoryPart.Record.ParentTerritory == null ? 0 : territoryPart.Record.ParentTerritory.Id,
                EditUrl = _routeCollection.GetVirtualPath(requestContext, metadata.EditorRouteValues).VirtualPath,
                DisplayText = metadata.DisplayText +
                              (!territoryPart.ContentItem.IsPublished() ? T(" (draft)").Text : string.Empty) +
                              ((territoryPart.Record.TerritoryInternalRecord == null) ? T(" (requires identity)").Text : string.Empty)
            });
        }
Ejemplo n.º 13
0
        private void UpdateTerritoryPosition(
            TerritoryPart territoryPart, TerritoryHierarchyPart hierarchyPart, TerritoryPart parentPart = null)
        {
            var context = new UpdateContentContext(territoryPart.ContentItem);

            _handlers.Invoke(handler => handler.Updating(context), Logger);
            if (parentPart == null)
            {
                _territoriesHierarchyService.AddTerritory(territoryPart, hierarchyPart); // move to root
            }
            else
            {
                _territoriesHierarchyService.AssignParent(territoryPart, parentPart);
            }
            _handlers.Invoke(handler => handler.Updated(context), Logger);
        }
Ejemplo n.º 14
0
 private void Invalidate(TerritoryPart territory)
 {
     if (territory != null)
     {
         var hId = territory.Hierarchy?.Id;
         // if the territory belongs the selected hierarchy or one
         // of its localizations
         var localizations = _addressConfigurationSettingsService.ShippingCountriesHierarchies;
         var hierarchyOk   = hId.HasValue && localizations.Any() &&
                             localizations.Select(thp => thp.Id).Contains(hId.Value);
         if (hierarchyOk)
         {
             Invalidate();
         }
     }
 }
 private bool SubValidation(IEnumerable <TerritoryPart> list, TerritoryPart item)
 {
     if (list == null || !list.Any())
     {
         return(false);
     }
     if (item == null)
     {
         return(false);
     }
     if (!list.Any(c => c.Id == item.Id))
     {
         return(false);
     }
     return(true);
 }
        public IEnumerable <TerritoryPart> GetAllCities(
            AddressRecordType addressRecordType, TerritoryPart parent)
        {
            var root = parent;

            // make sure root we'll use belongs to hierarchy
            if (parent.HierarchyPart.Id != ConfiguredHierarchy.Id)
            {
                root = SingleTerritory(parent.Record.TerritoryInternalRecord.Id);
            }
            if (root == null)
            {
                // if the root is not valid for the hierarchy, we cannot return
                // any province.
                return(Enumerable.Empty <TerritoryPart>());
            }
            var        allChildrens = GetAllChildrenParts(root.Children.AsPart <TerritoryPart>());
            List <int> cityIds;
            var        citiesQuery = _territoriesService
                                     // Query for territories in hierarchy
                                     .GetTerritoriesQuery(ConfiguredHierarchy)
                                     // only those marked as province
                                     .Where(tpr => _settingsService
                                            .SelectedCityIds.Contains(tpr.TerritoryInternalRecord.Id));

            if (addressRecordType == AddressRecordType.ShippingAddress)
            {
                cityIds = citiesQuery
                          // only those marked for the given record type
                          .Join <TerritoryAddressTypePartRecord>()
                          .Where(tatpr => tatpr.Shipping)
                          .List()
                          .Select(tp => tp.Id).ToList();
            }
            else
            {
                cityIds = citiesQuery
                          // only those marked for the given record type
                          .Join <TerritoryAddressTypePartRecord>()
                          .Where(tatpr => tatpr.Billing)
                          .List()
                          .Select(tp => tp.Id).ToList();
            }

            return(allChildrens
                   .Where(tp => cityIds.Contains(tp.Id)));
        }
Ejemplo n.º 17
0
        public IContentQuery <TerritoryPart, TerritoryPartRecord> GetTerritoriesQuery(
            TerritoryHierarchyPart hierarchyPart, TerritoryPart territoryPart, VersionOptions versionOptions)
        {
            var baseQuery = GetTerritoriesQuery(hierarchyPart, versionOptions)
                            .WithQueryHints(new QueryHints().ExpandRecords("TerritoryPartRecord"));

            if (territoryPart == null)
            {
                return(baseQuery
                       .Where(tpr => tpr.ParentTerritory == null));
            }
            else
            {
                return(baseQuery
                       .Where(tpr => tpr.ParentTerritory.Id == territoryPart.Record.Id));
            }
        }
        public IEnumerable <TerritoryPart> GetAllProvinces(TerritoryPart country)
        {
            var root = country;

            // make sure root we'll use belongs to hierarchy
            if (country.HierarchyPart.Id != ConfiguredHierarchy.Id)
            {
                root = SingleTerritory(country.Record.TerritoryInternalRecord.Id);
            }
            if (root == null)
            {
                // if the root is not valid for the hierarchy, we cannot return
                // any province.
                return(Enumerable.Empty <TerritoryPart>());
            }

            return(GetChildren(root, _settingsService.SelectedProvinceIds));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="selection">This is an array of int that represents a list of Ids
        /// for selected territories.</param>
        /// <returns></returns>
        private IEnumerable <TerritoryPart> GetChildren(TerritoryPart parent, int[] selection)
        {
            // we need root's children of all levels within hierarchy
            // that correspond to the territories that have been selected
            // based on the Ids.
            // Depth first recursion
            var allRecords      = GetAllChildrenRecords(parent.Record.Children);
            var selectedRecords = allRecords
                                  .Where(tpr => selection.Contains(tpr.TerritoryInternalRecord.Id));

            return(_contentManager
                   // GetMany will break if there are too many ids
                   .GetMany <TerritoryPart>(
                       selectedRecords.Select(r => r.Id),
                       // Consider eventually using the version from the hierarchy?
                       VersionOptions.Published,
                       QueryHints.Empty));
        }
Ejemplo n.º 20
0
 // Refactorized START
 public IEnumerable <TerritoryPart> GetAllProvinces(
     TerritoryPart country, TerritoryPart city)
 {
     if (city != null)
     {
         return(GetTerritoryParents(new TerritoryQueryContext {
             TerritoryAdministrativeType = TerritoryAdministrativeType.Province,
             ForTerritory = city
         }));
     }
     else
     {
         return(GetTerritoryChildren(new TerritoryQueryContext {
             TerritoryAdministrativeType = TerritoryAdministrativeType.Province,
             ForTerritory = country
         }));
     }
 }
        public IEnumerable <TerritoryPart> GetAllCities(TerritoryPart parent)
        {
            var root = parent;

            // make sure root we'll use belongs to hierarchy
            if (parent.HierarchyPart.Id != ConfiguredHierarchy.Id)
            {
                root = SingleTerritory(parent.Record.TerritoryInternalRecord.Id);
            }
            if (root == null)
            {
                // if the root is not valid for the hierarchy, we cannot return
                // any province.
                return(Enumerable.Empty <TerritoryPart>());
            }
            // usually parent will be either a country or a province, but it doesn't
            // really affect our code here.
            return(GetChildren(root, _settingsService.SelectedCityIds));
        }
Ejemplo n.º 22
0
        public void AssignInternalRecord(TerritoryPart territory, TerritoryInternalRecord internalRecord)
        {
            TerritoriesUtilities.ValidateArgument(territory, nameof(territory));
            if (internalRecord == null || _territoriesRepositoryService.GetTerritoryInternal(internalRecord.Id) == null)
            {
                throw new ArgumentNullException(nameof(internalRecord));
            }
            // check that the internal record does not exist yet in the same hierarchy
            var hierarchyRecord = territory.Record.Hierarchy;

            if (hierarchyRecord != null)
            {
                if (hierarchyRecord
                    .Territories
                    .Where(tpr => tpr.Id != territory.Record.Id)     // exclude current territory
                    .Select(tpr => tpr.TerritoryInternalRecord)
                    .Any(tir => tir.Id == internalRecord.Id))
                {
                    throw new TerritoryInternalDuplicateException(T("The selected territory is already assigned in the current hierarchy."));
                }
            }
            territory.Record.TerritoryInternalRecord = internalRecord;
        }
Ejemplo n.º 23
0
        public AddressConfigurationTerritoryViewModel(
            TerritoryPart part,
            IEnumerable <int> countries,
            IEnumerable <int> provinces,
            IEnumerable <int> cities,
            ITerritoryPartRecordService _territoryPartRecordService) : this()
        {
            _contentManager = part.ContentItem.ContentManager;

            Territory   = part;
            TerritoryId = part.Record.TerritoryInternalRecord.Id;

            var adminTypePart = part.As <TerritoryAdministrativeTypePart>();

            if (adminTypePart != null)
            {
                AdministrativeType = adminTypePart.AdministrativeType;
            }
            else
            {
                AdministrativeType = TerritoryAdministrativeType.None;
            }

            IsCountry  = AdministrativeType == TerritoryAdministrativeType.Country;
            IsProvince = AdministrativeType == TerritoryAdministrativeType.Province;
            IsCity     = AdministrativeType == TerritoryAdministrativeType.City;

            DisplayText = _contentManager
                          .GetItemMetadata(part)
                          .DisplayText;
            if (string.IsNullOrWhiteSpace(DisplayText))
            {
                DisplayText = part.Record.TerritoryInternalRecord.Name;
            }

            ChildrenCount = _territoryPartRecordService.GetTerritoriesChildCount(part); //part.Record.Children.Count();
        }
        static void PropertySetHandlers(
            InitializingContentContext context, TerritoryPart part)
        {
            part.ChildrenField.Setter(value => {
                var actualItems = value
                                  .Where(ci => ci.As <TerritoryPart>() != null);
                part.Record.Children = actualItems.Any() ?
                                       actualItems.Select(ci => ci.As <TerritoryPart>().Record).ToList() :
                                       new List <TerritoryPartRecord>();
                return(actualItems);
            });

            part.HierarchyField.Setter(hierarchy => {
                part.Record.Hierarchy = hierarchy.As <TerritoryHierarchyPart>().Record;
                return(hierarchy);
            });

            part.ParentField.Setter(parent => {
                part.Record.ParentTerritory = parent.As <TerritoryPart>().Record;
                return(parent);
            });

            //call the setters in case a value had already been set
            if (part.ChildrenField.Value != null)
            {
                part.ChildrenField.Value = part.ChildrenField.Value;
            }
            if (part.HierarchyField.Value != null)
            {
                part.HierarchyField.Value = part.HierarchyField.Value;
            }
            if (part.ParentField.Value != null)
            {
                part.ParentField.Value = part.ParentField.Value;
            }
        }
Ejemplo n.º 25
0
 public IContentQuery <TerritoryPart, TerritoryPartRecord> GetTerritoriesQuery(
     TerritoryHierarchyPart hierarchyPart, TerritoryPart territoryPart)
 {
     return(GetTerritoriesQuery(hierarchyPart, territoryPart, null));
 }
 void CleanupRecord(RemoveContentContext context, TerritoryPart part)
 {
     part.Record.Hierarchy               = null;
     part.Record.ParentTerritory         = null;
     part.Record.TerritoryInternalRecord = null;
 }