SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ElementReferenceConverter.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.ContentClasses;
20 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 
24 namespace erminas.SmartAPI.CMS.Converter
25 {
26  //TODO test with different servers etc
27  internal class ElementReferenceConverter : IAttributeConverter<IContentClassElement>
28  {
29  public IContentClassElement ConvertFrom(IProjectObject parent, XmlElement element, RedDotAttribute attribute)
30  {
31  Guid projectGuid, ccGuid, elementGuid;
32  if (!element.TryGetGuid("eltprojectguid", out projectGuid) ||
33  !element.TryGetGuid("elttemplateguid", out ccGuid) ||
34  !element.TryGetGuid("eltelementguid", out elementGuid))
35  {
36  return null;
37  }
38 
39  string langId = element.GetAttributeValue("eltlanguagevariantid");
40 
41  IProject project = parent.Session.ServerManager.Projects.GetByGuid(projectGuid);
42  IContentClass contentClass = project.ContentClasses.GetByGuid(ccGuid);
43 
44  return contentClass.Elements.GetByGuid(elementGuid);
45  }
46 
47  public bool IsReadOnly
48  {
49  get { return false; }
50  }
51 
52  public void WriteTo(IProjectObject parent, IXmlReadWriteWrapper element, RedDotAttribute attribute,
54  {
55  if (parent == null)
56  {
57  throw new SmartAPIInternalException(string.Format("{0}.WriteTo must be called from a project object",
58  GetType().Name));
59  }
60 
61  CheckReadOnly(parent.Project, attribute);
62 
63  if (value == null)
64  {
65  SetValuesToNull(element);
66  return;
67  }
68 
69  if (AreFromTheSameServer(parent, value))
70  {
71  SetValuesFromSameServer(element, value);
72  return;
73  }
74 
75  SetValuesFromOtherServer(parent, element, value);
76  }
77 
78  private static bool AreFromTheSameServer(IProjectObject parent, IContentClassElement value)
79  {
80  return parent.Session == value.Session;
81  }
82 
83  private void CheckReadOnly(IProject projectOfTarget, RedDotAttribute attribute)
84  {
85  if (IsReadOnly)
86  {
87  throw new SmartAPIException(projectOfTarget.Session.ServerLogin,
88  string.Format("Writing to attribute {0} is forbidden", attribute.Description));
89  }
90  }
91 
92  private static IContentClassElement GetReferencedElement(IProjectObject parent, IContentClassElement value)
93  {
94  var project = parent.Project.Session.ServerManager.Projects[value.ContentClass.Project.Name];
95  return ConverterHelper.GetEquivalentContentClassElementFromOtherProject(value, project);
96  }
97 
98  private static void SetValuesFromOtherServer(IProjectObject parent, IXmlReadWriteWrapper element,
100  {
101  try
102  {
103  var referencedElement = GetReferencedElement(parent, value);
104 
105  SetValuesFromSameServer(element, referencedElement);
106  } catch (Exception e)
107  {
108  throw new SmartAPIException(parent.Session.ServerLogin,
109  string.Format(
110  "Can't find project/content class/element {0}/{1}/{2} on server",
111  value.ContentClass.Project.Name, value.ContentClass.Name, value.Name), e);
112  }
113  }
114 
115  private static void SetValuesFromSameServer(IXmlReadWriteWrapper element, IContentClassElement value)
116  {
117  //element.SetAttributeValue("eltlanguagevariantid", element.GetAttributeValue());
118  element.SetAttributeValue("eltelementguid", value.Guid.ToRQLString());
119  element.SetAttributeValue("elttemplateguid", value.ContentClass.Guid.ToRQLString());
120  element.SetAttributeValue("eltprojectguid", value.Project.Guid.ToRQLString());
121  }
122 
123  private static void SetValuesToNull(IXmlReadWriteWrapper element)
124  {
125  //element.SetAttributeValue("eltlanguagevariantid", null);
126  element.SetAttributeValue("eltelementguid", null);
127  element.SetAttributeValue("elttemplateguid", null);
128  element.SetAttributeValue("eltprojectguid", null);
129  }
130  }
131 }