19 using erminas.SmartAPI.CMS.Project;
20 using erminas.SmartAPI.CMS.Project.Folder;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
24 namespace erminas.SmartAPI.CMS.Converter
26 internal class ExampleFileConverter : IAttributeConverter<IFile>
28 private const string ELTSRC =
"eltrdexample";
29 private const string ELTFOLDERGUID =
"eltfolderguid";
30 private const string ELTSRCSUBDIRGUID =
"eltrdexamplesubdirguid";
32 public IFile ConvertFrom(
IProjectObject parent, XmlElement readElement, RedDotAttribute attribute)
39 var fileName = readElement.GetAttributeValue(ELTSRC);
41 if (
string.IsNullOrEmpty(fileName))
46 var folder = GetFolder(parent, readElement);
48 return new File(folder, fileName);
51 public bool IsReadOnly
56 public void WriteTo(
IProjectObject parent, IXmlReadWriteWrapper element, RedDotAttribute attribute,
IFile value)
69 if (!element.IsAttributeSet(parent, ELTFOLDERGUID))
73 "Cannot set an example file ({0}) without an active folder in element {1}",
77 if (ConverterHelper.AreFromTheSameProject(parent, value))
79 SetFromSameProject(parent, element, value);
85 SetValuesFromAssetManagerFolder(parent, element, value);
90 SetValuesFromTopLevelFolder(parent, element, value, ownFolder);
95 private static void ClearFile(IXmlReadWriteWrapper element)
97 element.SetAttributeValue(ELTSRCSUBDIRGUID, null);
98 element.SetAttributeValue(ELTSRC, null);
104 if (!readElement.TryGetGuid(ELTSRCSUBDIRGUID, out folderGuid))
106 folderGuid = readElement.GetGuid(ELTFOLDERGUID);
113 private static void SetFilename(
IProjectObject parent,
IFile value, IXmlReadWriteWrapper element,
120 string.Format(
"No file with name {0} found in folder {1} of project {2}",
123 element.SetAttributeValue(ELTSRC, value.
Name);
126 private static void SetFromSameProject(
IProjectObject parent, IXmlReadWriteWrapper element,
IFile value)
128 var folderGuid = element.GetGuid(ELTFOLDERGUID);
129 var topLevelFolder = value.
Folder;
133 if (assetFolder.IsSubFolder)
135 topLevelFolder = assetFolder.ParentFolder;
139 if (topLevelFolder.Guid != folderGuid)
143 "Cannot set sample file '{0}', because it isn't in the current folder branch '{1}/'",
148 element.SetAttributeValue(ELTSRCSUBDIRGUID,
151 element.SetAttributeValue(ELTSRC, value.
Name);
154 private static void SetValuesFromAssetManagerFolder(
IProjectObject parent, IXmlReadWriteWrapper element,
158 var folderGuid = element.GetGuid(ELTFOLDERGUID);
159 if (assetFolder.IsSubFolder)
166 "Setting a example file from a subfolder into a non asset manager folder is not possible"));
168 if (folder.Name != assetFolder.ParentFolder.Name)
172 "Example file not from subfolder of the currently active folder is not allowed (active folder: {0}, file folder {1})",
176 var ownSubFolder = folder.SubFolders.GetByName(value.
Folder.
Name);
177 element.SetAttributeValue(ELTSRCSUBDIRGUID, ownSubFolder.Guid.ToRQLString());
178 SetFilename(parent, value, element, ownSubFolder);
186 string.Format(
"Missing folder {1} in project {0}", value.
Folder.
Name,
189 SetValuesFromTopLevelFolder(parent, element, value, ownFolder);
193 private static void SetValuesFromTopLevelFolder(
IProjectObject parent, IXmlReadWriteWrapper element,
IFile value,
196 var folderGuid = element.GetGuid(ELTFOLDERGUID);
198 if (folderGuid != ownFolder.
Guid)
202 "Example file not from the folder or one of its subfolders is not allowed"));
205 element.SetAttributeValue(ELTSRCSUBDIRGUID, ownFolder.
Guid.ToRQLString());
206 SetFilename(parent, value, element, ownFolder);