SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
XslFileConverter.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Linq;
18 using System.Xml;
19 using erminas.SmartAPI.CMS.Project;
20 using erminas.SmartAPI.CMS.Project.Folder;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 
24 namespace erminas.SmartAPI.CMS.Converter
25 {
26  internal class XslFileConverter : IAttributeConverter<IFile>
27  {
28  private const string ELTXSLFILE = "eltxslfile";
29  private const string ELTFOLDERGUID = "eltfolderguid";
30 
31  public IFile ConvertFrom(IProjectObject parent, XmlElement element, RedDotAttribute attribute)
32  {
33  ConverterHelper.EnsureValidProjectObject(parent);
34 
35  Guid folderGuid;
36  if (!element.TryGetGuid(ELTFOLDERGUID, out folderGuid) || !element.IsAttributeSet(parent, ELTXSLFILE))
37  {
38  return null;
39  }
40 
41  var folder = parent.Project.Folders.GetByGuid(folderGuid);
42  return new File(folder, element.GetAttributeValue(ELTXSLFILE));
43  }
44 
45  public bool IsReadOnly
46  {
47  get { return false; }
48  }
49 
50  public void WriteTo(IProjectObject parent, IXmlReadWriteWrapper element, RedDotAttribute attribute, IFile value)
51  {
52  ConverterHelper.EnsureValidProjectObject(parent);
53 
54  ConverterHelper.CheckReadOnly(this, parent, attribute);
55 
56  if (value == null)
57  {
58  SetEmptyValues(element);
59  return;
60  }
61 
62  if (ConverterHelper.AreFromTheSameProject(parent, value))
63  {
64  SetValuesFromSameProject(element, value);
65  return;
66  }
67 
68  SetValuesFromDifferentProjects(parent, element, attribute, value);
69  }
70 
71  private static void SetEmptyValues(IXmlReadWriteWrapper element)
72  {
73  element.SetAttributeValue(ELTFOLDERGUID, null);
74  element.SetAttributeValue(ELTXSLFILE, null);
75  }
76 
77  private static void SetValuesFromDifferentProjects(IProjectObject parent, IXmlReadWriteWrapper element,
78  RedDotAttribute attribute, IFile value)
79  {
80  IFolder ownFolder;
81  if (!parent.Project.Folders.TryGetByName(value.Folder.Name, out ownFolder))
82  {
83  throw new SmartAPIException(parent.Session.ServerLogin,
84  string.Format("No such folder {0} in project {1} for setting of {2}",
85  value.Folder.Name, parent.Project, attribute.Description));
86  }
87 
88  var ownFile = ownFolder.Files.GetByNamePattern(value.Name).SingleOrDefault();
89  if (ownFile == null)
90  {
91  throw new SmartAPIException(parent.Session.ServerLogin,
92  string.Format(
93  "No such file {3} in folder {0} in project {1} for setting of {2}",
94  value.Folder.Name, parent.Project, attribute.Description, value.Name));
95  }
96 
97  element.SetAttributeValue(ELTFOLDERGUID, ownFolder.Guid.ToRQLString());
98  element.SetAttributeValue(ELTXSLFILE, ownFile.Name);
99  }
100 
101  private static void SetValuesFromSameProject(IXmlReadWriteWrapper element, IFile value)
102  {
103  element.SetAttributeValue(ELTFOLDERGUID, value.Folder.Guid.ToRQLString());
104  element.SetAttributeValue(ELTXSLFILE, value.Name);
105  }
106  }
107 }