示例#1
0
        public virtual void AddVersion(ItemDefinition itemDefinition, VersionUri baseVersion, CallContext context)
        {
            if (DisableSerialization)
            {
                return;
            }

            Assert.ArgumentNotNull(itemDefinition, "itemDefinition");

            var sourceItem = GetSourceFromIdIfIncluded(itemDefinition);

            if (sourceItem == null)
            {
                return;                                 // not an included item
            }
            // we make a clone of the item so that we can insert a new version on it
            var versionAddProxy = new ProxyItem(sourceItem);

            // determine what the next version number should be in the current language
            // (highest number currently present + 1)
            var newVersionNumber = 1 + versionAddProxy.Versions
                                   .Where(v => v.Language.Equals(baseVersion.Language.CultureInfo))
                                   .Select(v => v.VersionNumber)
                                   .DefaultIfEmpty()
                                   .Max();

            IItemVersion newVersion;

            // if the base version is 0 or less, that means add a blank version (per SC DP behavior). If 1 or more we should copy all fields on that version into the new version.
            if (baseVersion.Version.Number > 0)
            {
                newVersion = versionAddProxy.Versions.FirstOrDefault(v => v.Language.Equals(baseVersion.Language.CultureInfo) && v.VersionNumber.Equals(baseVersion.Version.Number));

                // the new version may not exist if we are using language fallback and adding a new version. If that's the case we should create a blank version, as that's what Sitecore does.
                if (newVersion != null)
                {
                    newVersion = new ProxyItemVersion(newVersion)
                    {
                        VersionNumber = newVersionNumber
                    }
                }
                ;                                                                                                           // creating a new proxyversion essentially clones the existing version
                else
                {
                    newVersion = new ProxyItemVersion(baseVersion.Language.CultureInfo, newVersionNumber);
                }
            }
            else
            {
                newVersion = new ProxyItemVersion(baseVersion.Language.CultureInfo, newVersionNumber);
            }

            // inject the new version we created into the proxy item
            var newVersions = versionAddProxy.Versions.Concat(new[] { newVersion }).ToArray();

            versionAddProxy.Versions = newVersions;

            // flush to serialization data store
            SerializeItemIfIncluded(versionAddProxy, "Version Added");
        }
示例#2
0
        public virtual void AddVersion(ItemDefinition itemDefinition, VersionUri baseVersion, CallContext context)
        {
            if (DisableSerialization)
            {
                return;
            }

            Assert.ArgumentNotNull(itemDefinition, "itemDefinition");

            var sourceItem = GetSourceFromIdIfIncluded(itemDefinition);

            if (sourceItem == null)
            {
                return;                                 // not an included item
            }
            // if the source item came from the database (which we're somewhat hackily determining by type name)
            // then because (a) DB cache is disabled and (b) the Sitecore data provider went first,
            // the source item ALREADY contains the version we added. So all we have to do is update the serialized version.
            if (sourceItem is ItemData)
            {
                SerializeItemIfIncluded(sourceItem, "Version Added");
                return;
            }

            // on the other hand if the source item did not come from the database - e.g. transparent sync,
            // and the item did not exist in the database, the sourceItem will be a YAML file on disk.
            // in this case nobody has 'gone first' with adding the version, so we have to manually add it like
            // Sitecore would to a database item.

            // we make a clone of the item so that we can insert a new version on it
            var versionAddProxy = new ProxyItem(sourceItem);

            // determine what the next version number should be in the current language
            // (highest number currently present + 1)
            var newVersionNumber = 1 + versionAddProxy.Versions
                                   .Where(v => v.Language.Equals(baseVersion.Language.CultureInfo))
                                   .Select(v => v.VersionNumber)
                                   .DefaultIfEmpty()
                                   .Max();

            IItemVersion newVersion;

            // if the base version is 0 or less, that means add a blank version (per SC DP behavior). If 1 or more we should copy all fields on that version into the new version.
            if (baseVersion.Version.Number > 0)
            {
                newVersion = versionAddProxy.Versions.FirstOrDefault(v => v.Language.Equals(baseVersion.Language.CultureInfo) && v.VersionNumber.Equals(baseVersion.Version.Number));

                // the new version may not exist if we are using language fallback and adding a new version. If that's the case we should create a blank version, as that's what Sitecore does.
                if (newVersion != null)
                {
                    newVersion = new ProxyItemVersion(newVersion)
                    {
                        VersionNumber = newVersionNumber
                    }
                }
                ;                                                                                                           // creating a new proxyversion essentially clones the existing version
                else
                {
                    newVersion = new ProxyItemVersion(baseVersion.Language.CultureInfo, newVersionNumber);
                }
            }
            else
            {
                newVersion = new ProxyItemVersion(baseVersion.Language.CultureInfo, newVersionNumber);
            }

            // inject the new version we created into the proxy item
            var newVersions = versionAddProxy.Versions.Concat(new[] { newVersion }).ToArray();

            versionAddProxy.Versions = newVersions;

            // flush to serialization data store
            SerializeItemIfIncluded(versionAddProxy, "Version Added");
        }