SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
CategoryConverter.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.Xml;
18 using erminas.SmartAPI.CMS.Project;
19 using erminas.SmartAPI.CMS.Project.Keywords;
20 using erminas.SmartAPI.Exceptions;
21 using erminas.SmartAPI.Utils;
22 
23 namespace erminas.SmartAPI.CMS.Converter
24 {
25  internal class CategoryConverter : AbstractGuidElementConverter<ICategory>
26  {
27  private const string ARBITRARY_CATEGORY_VALUE = "-1";
28 
29  public override ICategory ConvertFrom(IProjectObject parent, XmlElement element, RedDotAttribute attribute)
30  {
31  var stringValue = element.GetAttributeValue(attribute.ElementName);
32 
33  if (string.IsNullOrEmpty(stringValue))
34  {
35  return null;
36  }
37 
38  return stringValue == "-1" ? null : base.ConvertFrom(parent, element, attribute);
39  }
40 
41  public override void WriteTo(IProjectObject parent, IXmlReadWriteWrapper element, RedDotAttribute attribute,
42  ICategory value)
43  {
44  CheckReadOnly(parent.Project, attribute);
45 
46  if (value == ArbitraryCategory.INSTANCE)
47  {
48  element.SetAttributeValue(attribute.ElementName, ARBITRARY_CATEGORY_VALUE);
49  }
50  else
51  {
52  base.WriteTo(parent, element, attribute, value);
53  }
54  }
55 
56  protected override ICategory GetFromGuid(IProjectObject parent, XmlElement element, RedDotAttribute attribute,
57  Guid guid)
58  {
59  //after a deletion of a category, references to it can still be present in the system and
60  //thus we can't throw an exception but have to handle it like no category is assigned (RedDot seems to handle it that way).
61  ICategory result;
62  return parent.Project.Categories.TryGetByGuid(guid, out result) ? result : null;
63  }
64 
65  protected override ICategory GetFromName(IProjectObject parent, IXmlReadWriteWrapper element,
66  RedDotAttribute attribute, ICategory value)
67  {
68  ICategory category;
69  if (!parent.Project.Categories.TryGetByName(value.Name, out category))
70  {
71  throw new SmartAPIException(parent.Session.ServerLogin,
72  string.Format("Cannot find category {0} in project {1}", value.Name, parent));
73  }
74  return category;
75  }
76  }
77 }