SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IPreassignedWorkflow.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.Collections.Generic;
18 using System.Collections.ObjectModel;
19 using System.Linq;
20 using System.Xml;
21 using erminas.SmartAPI.CMS.Project.Workflows;
22 using erminas.SmartAPI.Utils;
23 
24 namespace erminas.SmartAPI.CMS.Project.ContentClasses.Elements
25 {
26  public interface IPreassignedWorkflow : IWorkflow
27  {
28  void DisconnectFromLinkCompletely();
29  void DisconnectFromLinkForLanguages(IEnumerable<ILanguageVariant> languageVariants);
30  void DisconnectFromLinkForLanguages(params string[] languageVariants);
31  IWorkflowAssignable ElementPreassignedTo { get; }
32  IEnumerable<ILanguageVariant> LanguageVariantsPreassignedTo { get; }
33  }
34 
35  internal class PreassignedWorkflow : IPreassignedWorkflow
36  {
37  private readonly Workflow _workflow;
38  private ReadOnlyCollection<ILanguageVariant> _languageVariants;
39 
40  internal PreassignedWorkflow(IWorkflowAssignable elementPreassignedTo, Workflow workflow)
41  {
42  ElementPreassignedTo = elementPreassignedTo;
43  _workflow = workflow;
44  }
45 
46  public IWorkflowActions Actions
47  {
48  get { return _workflow.Actions; }
49  }
50 
51  public bool CanBeInherited
52  {
53  get { return _workflow.CanBeInherited; }
54  }
55 
56  public void Delete()
57  {
58  _workflow.Delete();
59  }
60 
61  public void DisconnectFromLinkCompletely()
62  {
63  DisconnectFromLinkForLanguages(LanguageVariantsPreassignedTo);
64  }
65 
66  public void DisconnectFromLinkForLanguages(IEnumerable<ILanguageVariant> languageVariants)
67  {
68  const string UNLINK_WORKFLOW =
69  @"<WORKFLOW sessionkey=""{0}""><LINK action=""unlink"" guid=""{1}""><WORKFLOW guid=""{2}""><LANGUAGEVARIANTS>{3}</LANGUAGEVARIANTS></WORKFLOW></LINK></WORKFLOW>";
70 
71  ISession session = ElementPreassignedTo.Session;
72 
73  string query = UNLINK_WORKFLOW.RQLFormat(session.SessionKey, ElementPreassignedTo, _workflow,
74  languageVariants);
75 
76  session.ExecuteRQLRaw(query, RQL.IODataFormat.LogonGuidOnly);
77 
78  InvalidateCache();
79  }
80 
81  public void DisconnectFromLinkForLanguages(params string[] languageVariants)
82  {
83  IProject project = ElementPreassignedTo.Project;
84 
85  IEnumerable<ILanguageVariant> languages =
86  languageVariants.Select(language => project.LanguageVariants[language]);
87  DisconnectFromLinkForLanguages(languages);
88  }
89 
90  public IWorkflowAssignable ElementPreassignedTo { get; private set; }
91 
92  public void EnsureInitialization()
93  {
94  _workflow.EnsureInitialization();
95  }
96 
97  public override bool Equals(object o)
98  {
99  var workflow = o as Workflow;
100  return workflow != null && workflow.Guid == Guid;
101  }
102 
103  public override int GetHashCode()
104  {
105  return _workflow.GetHashCode();
106  }
107 
108  public Guid Guid
109  {
110  get { return _workflow.Guid; }
111  set { _workflow.Guid = value; }
112  }
113 
114  public bool IsGlobal
115  {
116  get { return _workflow.IsGlobal; }
117  }
118 
119  public bool IsStructureWorkflow
120  {
121  get { return _workflow.IsStructureWorkflow; }
122  }
123 
124  public IEnumerable<ILanguageVariant> LanguageVariantsPreassignedTo
125  {
126  get
127  {
128  if (_languageVariants == null)
129  {
130  RefreshLanguageVariants();
131  }
132 
133  return _languageVariants;
134  }
135  }
136 
137  public string Name
138  {
139  get { return _workflow.Name; }
140  set { _workflow.Name = value; }
141  }
142 
143  public IProject Project
144  {
145  get { return _workflow.Project; }
146  }
147 
148  public void Refresh()
149  {
150  _workflow.Refresh();
151  RefreshLanguageVariants();
152  }
153 
154  public ISession Session
155  {
156  get { return _workflow.Session; }
157  }
158 
159  public XmlElement XmlElement
160  {
161  get { return _workflow.XmlElement; }
162  set { _workflow.XmlElement = value; }
163  }
164 
165  private ReadOnlyCollection<ILanguageVariant> GetPreAssignmentLanguageVariants()
166  {
167  const string LOAD_LANGUAGES =
168  @"<WORKFLOW guid=""{0}""><LANGUAGEVARIANTS action=""workflowexisting"" linkguid=""{1}""/></WORKFLOW>";
169 
170  IProject project = ElementPreassignedTo.Project;
171 
172  var xmlDoc = project.ExecuteRQL(LOAD_LANGUAGES.RQLFormat(_workflow, ElementPreassignedTo));
173  var languageVariants = xmlDoc.GetElementsByTagName("LANGUAGEVARIANT");
174 
175  return (from XmlElement curLanguage in languageVariants
176  select project.LanguageVariants[curLanguage.GetAttributeValue("language")]).ToList().AsReadOnly();
177  }
178 
179  private void InvalidateCache()
180  {
181  _languageVariants = null;
182  }
183 
184  private void RefreshLanguageVariants()
185  {
186  _languageVariants = GetPreAssignmentLanguageVariants();
187  }
188  }
189 }