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 SrcFileConverter : IAttributeConverter<IFile>
28 private const string ELTSRC =
"eltsrc";
29 private const string ELTFOLDERGUID =
"eltfolderguid";
30 private const string ELTSRCSUBDIRGUID =
"eltsrcsubdirguid";
32 public IFile ConvertFrom(
IProjectObject parent, XmlElement element, RedDotAttribute attribute)
39 var fileName = element.GetAttributeValue(ELTSRC);
41 if (
string.IsNullOrEmpty(fileName))
46 var folder = GetFolder(parent, element);
48 return new File(folder, fileName);
51 public bool IsReadOnly
56 public void WriteTo(
IProjectObject parent, IXmlReadWriteWrapper element, RedDotAttribute attribute,
IFile value)
69 if (ConverterHelper.AreFromTheSameProject(parent, value))
71 SetFromSameProject(parent, element, value);
77 SetValuesFromAssetManagerFolder(parent, element, value);
82 SetValuesFromTopLevelFolder(parent, element, value, ownFolder);
87 private static void ClearFile(IXmlReadWriteWrapper element)
89 element.SetAttributeValue(ELTSRCSUBDIRGUID, null);
90 element.SetAttributeValue(ELTSRC, null);
96 if (!element.TryGetGuid(ELTSRCSUBDIRGUID, out folderGuid))
98 folderGuid = element.GetGuid(ELTFOLDERGUID);
108 if (ownTopLevelFolder == null)
111 string.Format(
"No asset folder with name {0} found in project {1}",
114 return ownTopLevelFolder;
118 IXmlReadWriteWrapper writeElement)
124 string.Format(
"No file with name {0} found in folder {1} of project {2}",
128 writeElement.SetAttributeValue(ELTSRC, ownFile.Name);
131 private static void SetFromSameProject(
IProjectObject parent, IXmlReadWriteWrapper element,
IFile value)
133 var folderGuid = element.GetGuid(ELTFOLDERGUID);
135 var topLevelFolder = value.
Folder;
138 var assetFolder = (IAssetManagerFolder) value.
Folder;
139 if (assetFolder.IsSubFolder)
141 topLevelFolder = assetFolder.ParentFolder;
144 if (topLevelFolder.Guid != folderGuid)
148 "Cannot set src file '{0}', because it isn't in the current folder branch '{1}/'",
153 element.SetAttributeValue(ELTSRCSUBDIRGUID,
154 topLevelFolder.Guid == folderGuid ?
"undefined" : value.
Folder.
Guid.ToRQLString());
156 element.SetAttributeValue(ELTSRC, value.
Name);
159 private static void SetValuesFromAssetManagerFolder(
IProjectObject parent, IXmlReadWriteWrapper element,
162 var assetFolder = (IAssetManagerFolder) value.
Folder;
163 var ownFolder = GetTopLevelFolder(parent, assetFolder);
165 if (assetFolder.IsSubFolder)
167 SetValuesFromSubFolder(parent, element, value, ownFolder);
171 SetValuesFromTopLevelFolder(parent, element, value, ownFolder);
175 private static void SetValuesFromSubFolder(
IProjectObject parent, IXmlReadWriteWrapper writeElement,
IFile value,
176 IAssetManagerFolder ownFolder)
179 writeElement.SetAttributeValue(ELTSRCSUBDIRGUID, ownFolder.
Guid.ToRQLString());
180 SetFilename(parent, value, ownFolder, writeElement);
183 private static void SetValuesFromTopLevelFolder(
IProjectObject parent, IXmlReadWriteWrapper element,
IFile value,
186 var folderGuid = element.GetGuid(ELTFOLDERGUID);
187 element.SetAttributeValue(ELTSRCSUBDIRGUID,
188 ownFolder.
Guid == folderGuid ?
"undefined" : ownFolder.
Guid.ToRQLString());
190 SetFilename(parent, value, ownFolder, element);