/// <summary>
        ///     Copies the element to another content class by creating a new element and copying the attribute values to it.
        ///     Make sure to set the language variant in the target project into which the element should be copied, first.
        /// </summary>
        /// <param name="contentClass"> target content class, into which the element should be copied </param>
        /// <returns> the created copy </returns>
        /// <remarks>
        ///     <list type="bullet">
        ///         <item>
        ///             <description>Override this method, if you need to set other values than the direct attributes of the element (e.g. setting text values of TextHtml elements)</description>
        ///         </item>
        ///         <item>
        ///             <description>
        ///                 The target content class is only modified on the server, thus the content class object does not contain the newly created element.
        ///                 If you need an updated version of the content class, you have to retrieve it again with
        ///                 <code>new ContentClass(Project, Guid);</code>
        ///             </description>
        ///         </item>
        ///     </list>
        /// </remarks>
        public IContentClassElement CopyToContentClass(IContentClass contentClass)
        {
            var newContentClassElement = CreateElement(contentClass, Type);
            var assign = new AttributeAssignment();

            assign.AssignAllRedDotAttributesForLanguage(this, newContentClassElement,
                                                        Project.LanguageVariants.Current.Abbreviation);

            var node = (XmlElement)newContentClassElement.XmlElement.Clone();

            node.Attributes.RemoveNamedItem("guid");
            string creationString = GetSaveString(node);

            // <summary>
            // RQL for creating an element from a content class.
            // Two parameters:
            // 1. Content class guid
            // 2. Element to create, make sure it contains an attribute "action" with the value "save"!
            // </summary>
            const string CREATE_ELEMENT = @"<TEMPLATE guid=""{0}"">{1}</TEMPLATE>";

            XmlDocument rqlResult =
                contentClass.Project.ExecuteRQL(string.Format(CREATE_ELEMENT, contentClass.Guid.ToRQLString(),
                                                              creationString));
            var resultElementNode = (XmlElement)rqlResult.GetElementsByTagName("ELEMENT")[0];

            if (resultElementNode == null)
            {
                throw new SmartAPIException(Session.ServerLogin,
                                            string.Format("Error during creation of element {0}", this));
            }
            newContentClassElement.Guid = resultElementNode.GetGuid();

            return(newContentClassElement);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Copy selected elements from this content class to another target content class.
        /// </summary>
        /// <param name="targetCC"> Target content class to copy the elements to </param>
        /// <param name="elementNames"> Names of the elements to copy </param>
        public void CopyElementsToContentClass(IContentClass targetCC, params string[] elementNames)
        {
            if (elementNames == null || elementNames.Length == 0)
            {
                return;
            }

            var createdElements = new Dictionary <string, IContentClassElement>();

            using (new LanguageContext(Project))
            {
                var assign = new AttributeAssignment();
                foreach (var languageVariant in Project.LanguageVariants)
                {
                    ILanguageVariant targetLanguageVariant =
                        targetCC.Project.LanguageVariants[languageVariant.Abbreviation];
                    foreach (var curElementName in elementNames)
                    {
                        IContentClassElement curTargetContentClassElement;
                        languageVariant.Select();
                        var curSourceContentClassElement = Elements[curElementName];
                        if (createdElements.TryGetValue(curElementName, out curTargetContentClassElement))
                        {
                            targetLanguageVariant.Select();
                            assign.AssignAllRedDotAttributesForLanguage(curSourceContentClassElement,
                                                                        curTargetContentClassElement,
                                                                        targetLanguageVariant.Abbreviation);
                            curTargetContentClassElement.CommitInCurrentLanguage();
                        }
                        else
                        {
                            targetLanguageVariant.Select();
                            curTargetContentClassElement = curSourceContentClassElement.CopyToContentClass(targetCC);
                            createdElements.Add(curElementName, curTargetContentClassElement);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Copy selected elements from this content class to another target content class.
        /// </summary>
        /// <param name="targetCC"> Target content class to copy the elements to </param>
        /// <param name="elementNames"> Names of the elements to copy </param>
        public void CopyElementsToContentClass(IContentClass targetCC, params string[] elementNames)
        {
            if (elementNames == null || elementNames.Length == 0)
            {
                return;
            }

            var createdElements = new Dictionary<string, IContentClassElement>();
            using (new LanguageContext(Project))
            {
                var assign = new AttributeAssignment();
                foreach (var languageVariant in Project.LanguageVariants)
                {
                    ILanguageVariant targetLanguageVariant =
                        targetCC.Project.LanguageVariants[languageVariant.Abbreviation];
                    foreach (var curElementName in elementNames)
                    {
                        IContentClassElement curTargetContentClassElement;
                        languageVariant.Select();
                        var curSourceContentClassElement = Elements[curElementName];
                        if (createdElements.TryGetValue(curElementName, out curTargetContentClassElement))
                        {
                            targetLanguageVariant.Select();
                            assign.AssignAllRedDotAttributesForLanguage(curSourceContentClassElement,
                                                                        curTargetContentClassElement,
                                                                        targetLanguageVariant.Abbreviation);
                            curTargetContentClassElement.CommitInCurrentLanguage();
                        }
                        else
                        {
                            targetLanguageVariant.Select();
                            curTargetContentClassElement = curSourceContentClassElement.CopyToContentClass(targetCC);
                            createdElements.Add(curElementName, curTargetContentClassElement);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Copies the element to another content class by creating a new element and copying the attribute values to it.
        ///     Make sure to set the language variant in the target project into which the element should be copied, first.
        /// </summary>
        /// <param name="contentClass"> target content class, into which the element should be copied </param>
        /// <returns> the created copy </returns>
        /// <remarks>
        ///     <list type="bullet">
        ///         <item>
        ///             <description>Override this method, if you need to set other values than the direct attributes of the element (e.g. setting text values of TextHtml elements)</description>
        ///         </item>
        ///         <item>
        ///             <description>
        ///                 The target content class is only modified on the server, thus the content class object does not contain the newly created element.
        ///                 If you need an updated version of the content class, you have to retrieve it again with
        ///                 <code>new ContentClass(Project, Guid);</code>
        ///             </description>
        ///         </item>
        ///     </list>
        /// </remarks>
        public IContentClassElement CopyToContentClass(IContentClass contentClass)
        {
            var newContentClassElement = CreateElement(contentClass, Type);
            var assign = new AttributeAssignment();
            assign.AssignAllRedDotAttributesForLanguage(this, newContentClassElement,
                                                        Project.LanguageVariants.Current.Abbreviation);

            var node = (XmlElement) newContentClassElement.XmlElement.Clone();
            node.Attributes.RemoveNamedItem("guid");
            string creationString = GetSaveString(node);

            // <summary>
            // RQL for creating an element from a content class.
            // Two parameters:
            // 1. Content class guid
            // 2. Element to create, make sure it contains an attribute "action" with the value "save"!
            // </summary>
            const string CREATE_ELEMENT = @"<TEMPLATE guid=""{0}"">{1}</TEMPLATE>";

            XmlDocument rqlResult =
                contentClass.Project.ExecuteRQL(string.Format(CREATE_ELEMENT, contentClass.Guid.ToRQLString(),
                                                              creationString));
            var resultElementNode = (XmlElement) rqlResult.GetElementsByTagName("ELEMENT")[0];
            if (resultElementNode == null)
            {
                throw new SmartAPIException(Session.ServerLogin,
                                            string.Format("Error during creation of element {0}", this));
            }
            newContentClassElement.Guid = resultElementNode.GetGuid();

            return newContentClassElement;
        }