SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ConverterHelper.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 erminas.SmartAPI.CMS.Project;
17 using erminas.SmartAPI.CMS.Project.ContentClasses;
18 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
19 using erminas.SmartAPI.CMS.Project.Folder;
20 using erminas.SmartAPI.Exceptions;
21 
22 namespace erminas.SmartAPI.CMS.Converter
23 {
24  internal static class ConverterHelper
25  {
26  public static void CheckReadOnly<T>(IAttributeConverter<T> converter, IProjectObject po,
27  RedDotAttribute attribute) where T : IProjectObject
28  {
29  if (converter.IsReadOnly || attribute.IsReadOnly)
30  {
31  throw new SmartAPIException(po.Session.ServerLogin,
32  string.Format("Writing to attribute {0} is forbidden", attribute.Description));
33  }
34  }
35 
36  public static void EnsureValidProjectObject(IProjectObject parent)
37  {
38  if (parent == null)
39  {
40  throw new SmartAPIInternalException("Converter called with invalid project object");
41  }
42  }
43 
44  internal static bool AreFromTheSameProject<T>(IProjectObject projectOfTarget, T value)
45  where T : class, IProjectObject
46  {
47  return projectOfTarget.Session == value.Session && projectOfTarget.Project.Equals(value.Project);
48  }
49 
50  internal static IContentClassElement GetEquivalentContentClassElementFromOtherProject(IContentClassElement cc,
51  IProject otherProject)
52  {
53  var folderName = cc.ContentClass.Folder.Name;
54  IContentClassFolder otherFolder;
55  if (!otherProject.ContentClassFolders.TryGetByName(folderName, out otherFolder))
56  {
57  throw new SmartAPIException(otherProject.Session.ServerLogin,
58  string.Format("Missing content class folder {0} for project {1}", folderName,
59  otherProject));
60  }
61 
62  IContentClass otherContentClass;
63  if (!otherFolder.ContentClasses.TryGetByName(cc.ContentClass.Name, out otherContentClass))
64  {
65  throw new SmartAPIException(otherProject.Session.ServerLogin,
66  string.Format("Missing content class {0} in folder {1} for project {2}",
67  cc.ContentClass.Name, folderName, otherProject));
68  }
69 
70  IContentClassElement otherElement;
71  if (!otherContentClass.Elements.TryGetByName(cc.Name, out otherElement))
72  {
73  throw new SmartAPIException(otherProject.Session.ServerLogin,
74  string.Format(
75  "Missing element {3} for content class {0} in folder {1} for project {2}",
76  cc.ContentClass.Name, folderName, otherProject, cc.Name));
77  }
78 
79  return otherElement;
80  }
81  }
82 }