18 using erminas.SmartAPI.CMS.Project;
19 using erminas.SmartAPI.CMS.Project.ContentClasses;
20 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
24 namespace erminas.SmartAPI.CMS.Converter
27 internal class ElementReferenceConverter : IAttributeConverter<IContentClassElement>
31 Guid projectGuid, ccGuid, elementGuid;
32 if (!element.TryGetGuid(
"eltprojectguid", out projectGuid) ||
33 !element.TryGetGuid(
"elttemplateguid", out ccGuid) ||
34 !element.TryGetGuid(
"eltelementguid", out elementGuid))
39 string langId = element.GetAttributeValue(
"eltlanguagevariantid");
44 return contentClass.
Elements.GetByGuid(elementGuid);
47 public bool IsReadOnly
52 public void WriteTo(
IProjectObject parent, IXmlReadWriteWrapper element, RedDotAttribute attribute,
61 CheckReadOnly(parent.
Project, attribute);
65 SetValuesToNull(element);
69 if (AreFromTheSameServer(parent, value))
71 SetValuesFromSameServer(element, value);
75 SetValuesFromOtherServer(parent, element, value);
83 private void CheckReadOnly(
IProject projectOfTarget, RedDotAttribute attribute)
88 string.Format(
"Writing to attribute {0} is forbidden", attribute.Description));
95 return ConverterHelper.GetEquivalentContentClassElementFromOtherProject(value, project);
98 private static void SetValuesFromOtherServer(
IProjectObject parent, IXmlReadWriteWrapper element,
103 var referencedElement = GetReferencedElement(parent, value);
105 SetValuesFromSameServer(element, referencedElement);
106 }
catch (Exception e)
110 "Can't find project/content class/element {0}/{1}/{2} on server",
115 private static void SetValuesFromSameServer(IXmlReadWriteWrapper element,
IContentClassElement value)
118 element.SetAttributeValue(
"eltelementguid", value.
Guid.ToRQLString());
119 element.SetAttributeValue(
"elttemplateguid", value.
ContentClass.
Guid.ToRQLString());
120 element.SetAttributeValue(
"eltprojectguid", value.
Project.
Guid.ToRQLString());
123 private static void SetValuesToNull(IXmlReadWriteWrapper element)
126 element.SetAttributeValue(
"eltelementguid", null);
127 element.SetAttributeValue(
"elttemplateguid", null);
128 element.SetAttributeValue(
"eltprojectguid", null);