예제 #1
0
        private ActionResult CreateTerritoryPost(
            string typeName, int hierarchyId, string returnUrl, Action <ContentItem> conditionallyPublish)
        {
            return(ExecuteTerritoryPost(new TerritoryExecutionContext {
                HierarchyItem = _contentManager.Get(hierarchyId, VersionOptions.Latest),
                TerritoryItem = _contentManager.New(typeName),
                Message = TerritoriesUtilities.Creation401TerritoryMessage,
                AdditionalPermissions = new Permission[] { Orchard.Core.Contents.Permissions.EditContent },
                ExecutionAction = item => {
                    _contentManager.Create(item, VersionOptions.Draft);

                    // We can and should tell the drivers for the TerritoryPart what is the hierarchy we are
                    // creating territories for:
                    item.As <TerritoryPart>().CreationHierarchy = _contentManager
                                                                  .Get(hierarchyId, VersionOptions.Latest)?.As <TerritoryHierarchyPart>();
                    var model = _contentManager.UpdateEditor(item, this);

                    if (!ModelState.IsValid)
                    {
                        _transactionManager.Cancel();
                        return View(model.Hierarchy(_contentManager.Get(hierarchyId, VersionOptions.Latest)));
                    }

                    var territoryPart = item.As <TerritoryPart>();
                    var hierachyPart = _contentManager.Get <TerritoryHierarchyPart>(hierarchyId, VersionOptions.Latest);

                    _territoriesHierarchyService.AddTerritory(territoryPart, hierachyPart);

                    conditionallyPublish(item);

                    _notifier.Information(string.IsNullOrWhiteSpace(item.TypeDefinition.DisplayName)
                        ? T("Your content has been created.")
                        : T("Your {0} has been created.", item.TypeDefinition.DisplayName));

                    return this.RedirectLocal(returnUrl, () =>
                                              RedirectToAction("EditTerritory",
                                                               new RouteValueDictionary {
                        { "Id", item.Id }
                    }));
                }
            }));
        }
        protected override void Importing(TerritoryPart part, ImportContentContext context)
        {
            // Set stuff in the record based off what is being imported
            if (context.Data.Element(part.PartDefinition.Name) == null)
            {
                return;
            }

            var hierarchyIdentity = context.Attribute(part.PartDefinition.Name, "HierarchyId");

            if (hierarchyIdentity == null)
            {
                part.Record.Hierarchy = null;
            }
            else
            {
                var ci        = context.GetItemFromSession(hierarchyIdentity);
                var hierarchy = ci.As <TerritoryHierarchyPart>();
                _territoriesHierarchyService.AddTerritory(part, hierarchy);
            }

            var parentIdentity = context.Attribute(part.PartDefinition.Name, "ParentTerritoryId");

            if (parentIdentity == null)
            {
                part.Record.ParentTerritory = null;
            }
            else
            {
                var ci     = context.GetItemFromSession(parentIdentity.ToString());
                var parent = ci.As <TerritoryPart>();
                _territoriesHierarchyService.AssignParent(part, parent);
            }


            var internalIdentity = context.Attribute(part.PartDefinition.Name, "TerritoryInternalRecordId");

            if (internalIdentity == null)
            {
                part.Record.TerritoryInternalRecord = null;
            }
            else
            {
                _territoriesHierarchyService.AssignInternalRecord(part, internalIdentity.ToString());
            }
        }
        private void AddSampleTerritoriesData(out List <IContent> hierarchies, out List <IContent> territories)
        {
            hierarchies = AddSampleHierarchiesData();
            territories = new List <IContent>();
            var hierarchiesArray = hierarchies.ToArray();

            for (int i = 0; i < hierarchiesArray.Length; i++)
            {
                var currentHierarchy = hierarchiesArray[i].As <TerritoryHierarchyPart>();
                var territoryType    = currentHierarchy.TerritoryType;
                // add i+1 territories
                for (int j = 0; j < i + 1; j++)
                {
                    var territory = _contentManager
                                    .Create <TerritoryPart>(territoryType, i % 2 == 0 ? VersionOptions.Published : VersionOptions.Draft);
                    _territoriesHierarchyService.AddTerritory(territory, currentHierarchy);
                    territories.Add(territory);
                }
            }
            _transactionManager.RequireNew();
        }
예제 #4
0
        public void AddTerritoryThrowsTheExpectedArgumentNullExceptions()
        {
            // Check the expected ArgumentNullExceptions for AddTerritory(TerritoryPart, TerritoryHierarchyPart):
            // 1. territory is null
            var territory = (TerritoryPart)null;
            var hierarchy = _contentManager.Create <TerritoryHierarchyPart>("HierarchyType0");

            Assert.Throws <ArgumentNullException>(() => _territoriesHierarchyService.AddTerritory(territory, hierarchy));
            // 2. territory.Record is null
            territory        = _contentManager.Create <TerritoryPart>("TerritoryType0");
            territory.Record = null;
            Assert.Throws <ArgumentException>(() => _territoriesHierarchyService.AddTerritory(territory, hierarchy));
            // 3. hierarchy is null
            territory = _contentManager.Create <TerritoryPart>("TerritoryType0");
            hierarchy = null;
            Assert.Throws <ArgumentNullException>(() => _territoriesHierarchyService.AddTerritory(territory, hierarchy));
            // 4. hierarchy.Record is null
            hierarchy        = _contentManager.Create <TerritoryHierarchyPart>("HierarchyType0");
            hierarchy.Record = null;
            Assert.Throws <ArgumentException>(() => _territoriesHierarchyService.AddTerritory(territory, hierarchy));

            // Sanity check: the following should succeed
            territory = _contentManager.Create <TerritoryPart>("TerritoryType0");
            hierarchy = _contentManager.Create <TerritoryHierarchyPart>("HierarchyType0");
            _territoriesHierarchyService.AddTerritory(territory, hierarchy);
            Assert.That(territory.Record.Hierarchy.Id, Is.EqualTo(hierarchy.Record.Id));
        }