17 using System.Globalization;
18 using System.Security;
20 using erminas.SmartAPI.Exceptions;
21 using erminas.SmartAPI.Utils;
23 namespace erminas.SmartAPI.CMS.Project.ContentClasses.Elements
35 void CommitInCurrentLanguage();
37 void CommitInLanguage(
string languageAbbreviation);
46 new string Name {
get;
set; }
84 internal abstract class ContentClassElement : LanguageDependentPartialRedDotProjectObject, IContentClassElement
86 private string _originalName;
88 protected ContentClassElement(
IContentClass contentClass, XmlElement xmlElement)
89 : base(contentClass.Project, xmlElement)
92 ContentClass = contentClass;
97 public new string Name {
get {
return base.Name; }
set { base.Name = value; } }
107 public virtual void CommitInCurrentLanguage()
109 CommitInLanguage(Project.LanguageVariants.Current.Abbreviation);
112 public virtual void CommitInLanguage(
string abbreviation)
116 const string COMMIT_ELEMENT =
"<TEMPLATE><ELEMENTS>{0}</ELEMENTS></TEMPLATE>";
120 var node = GetElementForLanguage(abbreviation).MergedElement;
121 if (Name != _originalName)
123 node.SetAttributeValue(
"eltname", SecurityElement.Escape(Name));
125 XmlDocument rqlResult =
126 ContentClass.Project.ExecuteRQL(
string.Format(COMMIT_ELEMENT, GetSaveString(node)),
130 var resultElement = (XmlElement) rqlResult.GetElementsByTagName(
"ELEMENT")[0];
131 string tmpGuidStr = resultElement.Attributes[
"guid"].Value;
132 var newGuid =
new Guid(tmpGuidStr);
133 if (!newGuid.Equals(Guid))
135 throw new SmartAPIException(Session.ServerLogin,
"Unexpected guid in return value");
139 }
catch (Exception e)
142 string.Format(
"Could not save changes to {0}",
this), e);
145 if (Name != _originalName)
147 _originalName = Name;
148 ContentClass.Elements.InvalidateCache();
152 public IContentClass ContentClass {
get;
set; }
174 public IContentClassElement CopyToContentClass(IContentClass contentClass)
176 var newContentClassElement = CreateElement(contentClass, Type);
177 var assign =
new AttributeAssignment();
178 assign.AssignAllRedDotAttributesForLanguage(
this, newContentClassElement,
179 Project.LanguageVariants.Current.Abbreviation);
181 var node = (XmlElement) newContentClassElement.XmlElement.Clone();
182 node.Attributes.RemoveNamedItem(
"guid");
183 string creationString = GetSaveString(node);
191 const string CREATE_ELEMENT =
@"<TEMPLATE guid=""{0}"">{1}</TEMPLATE>";
193 XmlDocument rqlResult =
194 contentClass.Project.ExecuteRQL(
string.Format(CREATE_ELEMENT, contentClass.Guid.ToRQLString(),
196 var resultElementNode = (XmlElement) rqlResult.GetElementsByTagName(
"ELEMENT")[0];
197 if (resultElementNode == null)
200 string.Format(
"Error during creation of element {0}",
this));
202 newContentClassElement.Guid = resultElementNode.GetGuid();
204 return newContentClassElement;
214 protected override void LoadWholeObject()
219 protected override XmlElement RetrieveWholeObject()
221 return GetRQLRepresentation(Project, Guid);
224 internal static IContentClassElement CreateElement(IContentClass contentClass, Guid elementGuid)
226 var xmlElement = GetRQLRepresentation(contentClass.Project, elementGuid);
227 return CreateElement(contentClass, xmlElement);
236 internal static ContentClassElement CreateElement(IContentClass contentClass, XmlElement xmlElement)
238 var type = (
ElementType)
int.Parse(xmlElement.GetAttributeValue(
"elttype"));
242 return new DatabaseContent(contentClass, xmlElement);
244 return new TextHtml(contentClass, xmlElement);
246 return new TextAscii(contentClass, xmlElement);
249 return new StandardFieldText(contentClass, xmlElement);
251 return new StandardFieldNumeric(contentClass, xmlElement);
253 return new StandardFieldDate(contentClass, xmlElement);
255 return new StandardFieldTime(contentClass, xmlElement);
257 return new StandardFieldUserDefined(contentClass, xmlElement);
259 return new StandardFieldEmail(contentClass, xmlElement);
261 return new StandardFieldURL(contentClass, xmlElement);
263 return new Headline(contentClass, xmlElement);
265 return new Background(contentClass, xmlElement);
267 return new Image(contentClass, xmlElement);
269 return new Media(contentClass, xmlElement);
271 return new ListEntry(contentClass, xmlElement);
273 return new Transfer(contentClass, xmlElement);
275 return new IVW(contentClass, xmlElement);
277 return new OptionList(contentClass, xmlElement);
279 return new Attribute(contentClass, xmlElement);
281 return new Info(contentClass, xmlElement);
283 return new Browse(contentClass, xmlElement);
285 return new Area(contentClass, xmlElement);
287 return new ImageAnchor(contentClass, xmlElement);
289 return new TextAnchor(contentClass, xmlElement);
291 return new Container(contentClass, xmlElement);
293 return new Frame(contentClass, xmlElement);
295 return new SiteMap(contentClass, xmlElement);
297 return new HitList(contentClass, xmlElement);
299 return new List(contentClass, xmlElement);
301 return new ProjectContent(contentClass, xmlElement);
302 case ElementType.ConditionRedDotLiveOrDeliveryServer:
303 return new DeliveryServerConstraint(contentClass, xmlElement);
305 throw new ArgumentException(
"unknown element type: " + type);
315 private static ContentClassElement CreateElement(IContentClass contentClass,
ElementType elementType)
317 var doc =
new XmlDocument();
318 XmlElement element = doc.CreateElement(
"ELEMENT");
319 XmlAttribute typeAttr = doc.CreateAttribute(
"elttype");
320 XmlAttribute guidAttr = doc.CreateAttribute(
"guid");
321 typeAttr.Value = ((int) elementType).ToString(CultureInfo.InvariantCulture);
322 guidAttr.Value =
new Guid().ToRQLString();
323 element.Attributes.Append(typeAttr);
324 element.Attributes.Append(guidAttr);
326 return CreateElement(contentClass, element);
329 private static XmlElement GetRQLRepresentation(IProject project, Guid ccElementGuid)
331 const string LOAD_ELEMENT =
@"<TEMPLATE><ELEMENT action=""load"" guid=""{0}""/></TEMPLATE>";
332 return project.ExecuteRQL(LOAD_ELEMENT.RQLFormat(ccElementGuid)).GetSingleElement(
"ELEMENT");
335 private void LoadXml()
337 _name = _xmlElement.GetAttributeValue(
"eltname");
338 Type = (
ElementType) _xmlElement.GetIntAttributeValue(
"elttype").GetValueOrDefault();