SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IWorkflowPreassignable.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.Linq;
19 using System.Xml;
20 using erminas.SmartAPI.CMS.Project.Workflows;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
23 
24 namespace erminas.SmartAPI.CMS.Project.ContentClasses.Elements
25 {
27  {
28  }
29 
30  public interface IWorkflowAssignments
31  {
32  void CreateAndConnectContentWorkflow(string workflowName, params string[] languageVariants);
33 
34  void CreateAndConnectContentWorkflow(string workflowName, IEnumerable<ILanguageVariant> languageVariants);
35 
36  void CreateAndConnectStructuralworkflow(string workflowName);
37  void DisconnectAllWorkflows();
38  IPreassignedWorkflow GetContentWorkflowFor(string languageVariantId);
39  IPreassignedWorkflow GetContentWorkflowFor(ILanguageVariant languageVariant);
40  IPreassignedWorkflow GetContentWorkflowForCurrentLanguageVariant();
41  void InvalidateCache();
42  void SetContentWorkflow(IWorkflow workflow, IEnumerable<ILanguageVariant> languageVariants);
43  void SetContentWorkflow(IWorkflow workflow, params string[] languageVariantIds);
44  IPreassignedWorkflow StructuralWorkflow { get; set; }
45  }
46 
47  internal class WorkflowAssignments : IWorkflowAssignments
48  {
49  private readonly IWorkflowAssignable _element;
50 
51  private readonly Dictionary<ILanguageVariant, IPreassignedWorkflow> _preassignedContentWorkflows =
52  new Dictionary<ILanguageVariant, IPreassignedWorkflow>();
53 
54  private bool _isStructuralWorkflowLoaded;
55  private PreassignedWorkflow _preassignedStructuralWorkflow;
56 
57  internal WorkflowAssignments(IWorkflowAssignable element)
58  {
59  _element = element;
60  }
61 
62  public void CreateAndConnectContentWorkflow(string workflowName, params string[] languageVariants)
63  {
64  CreateAndConnectContentWorkflow(workflowName,
65  languageVariants.Select(
66  curVariant => _element.Project.LanguageVariants[curVariant]));
67  }
68 
69  public void CreateAndConnectContentWorkflow(string workflowName, IEnumerable<ILanguageVariant> languageVariants)
70  {
71  const bool IS_STRUCTURAL_WORKFLOW = false;
72  ExecuteCreateAndPreassignWorkflow(workflowName, languageVariants, IS_STRUCTURAL_WORKFLOW);
73  }
74 
75  public void CreateAndConnectStructuralworkflow(string workflowName)
76  {
77  const bool IS_STRUCTURAL_WORKFLOW = true;
78  ExecuteCreateAndPreassignWorkflow(workflowName, new[] {_element.Project.LanguageVariants.Current},
79  IS_STRUCTURAL_WORKFLOW);
80  }
81 
82  public void DisconnectAllWorkflows()
83  {
84  if (StructuralWorkflow != null)
85  {
86  StructuralWorkflow.DisconnectFromLinkCompletely();
87  }
88  var workflows = new HashSet<Guid>();
89  foreach (var curLang in _element.Project.LanguageVariants)
90  {
91  var workflow = GetContentWorkflowFor(curLang);
92  if (workflow == null)
93  {
94  continue;
95  }
96  if (workflows.Contains(workflow.Guid))
97  {
98  continue;
99  }
100  workflow.DisconnectFromLinkCompletely();
101  workflows.Add(workflow.Guid);
102  }
103  InvalidateCache();
104  }
105 
106  public IPreassignedWorkflow GetContentWorkflowFor(string languageVariantId)
107  {
108  ILanguageVariant languageVariant = _element.Project.LanguageVariants[languageVariantId];
109  return GetContentWorkflowFor(languageVariant);
110  }
111 
112  public IPreassignedWorkflow GetContentWorkflowFor(ILanguageVariant languageVariant)
113  {
114  IPreassignedWorkflow workflow;
115  if (!_preassignedContentWorkflows.TryGetValue(languageVariant, out workflow))
116  {
117  workflow = GetPreassignedContentWorkflow(languageVariant);
118 
119  if (workflow == null)
120  {
121  _preassignedContentWorkflows[languageVariant] = null;
122  return null;
123  }
124 
125  foreach (var curLanguage in workflow.LanguageVariantsPreassignedTo)
126  {
127  _preassignedContentWorkflows[curLanguage] = workflow;
128  }
129  }
130  return workflow;
131  }
132 
133  public IPreassignedWorkflow GetContentWorkflowForCurrentLanguageVariant()
134  {
135  return GetContentWorkflowFor(_element.Project.LanguageVariants.Current);
136  }
137 
138  public void InvalidateCache()
139  {
140  _preassignedStructuralWorkflow = null;
141  _isStructuralWorkflowLoaded = false;
142  _preassignedContentWorkflows.Clear();
143  }
144 
145  public void SetContentWorkflow(IWorkflow workflow, IEnumerable<ILanguageVariant> languageVariants)
146  {
147  if (workflow.IsStructureWorkflow)
148  {
149  throw new SmartAPIException(_element.Session.ServerLogin,
150  "Workflow for preassignment is a structural workflow, although a content workflow is expected");
151  }
152 
153  ExecutePreassignWorkflow(workflow, languageVariants);
154  }
155 
156  public void SetContentWorkflow(IWorkflow workflow, params string[] languageVariantIds)
157  {
158  var languageVariants =
159  languageVariantIds.Select(
160  curLanguageVariantId => _element.Project.LanguageVariants[curLanguageVariantId]);
161  SetContentWorkflow(workflow, languageVariants);
162  }
163 
164  public IPreassignedWorkflow StructuralWorkflow
165  {
166  get
167  {
168  if (!_isStructuralWorkflowLoaded)
169  {
170  _preassignedStructuralWorkflow = LoadStructuralWorkflow();
171  _isStructuralWorkflowLoaded = true;
172  }
173  return _preassignedStructuralWorkflow;
174  }
175  set
176  {
177  if (!value.IsStructureWorkflow)
178  {
179  throw new SmartAPIException(_element.Session.ServerLogin,
180  "Workflow for preassignment is not a structural workflow");
181  }
182 
183  ExecutePreassignWorkflow(value, null);
184  }
185  }
186 
187  private void ExecuteCreateAndPreassignWorkflow(string workflowName,
188  IEnumerable<ILanguageVariant> languageVariants, bool isStructural)
189  {
190  const string CREATE_AND_ASSIGN_WORKFLOW =
191  @"<WORKFLOW sessionkey=""{0}""><LINK guid=""{1}"" action=""assign""><WORKFLOW action=""addnew"" structureworkflow=""{2}"" guid="""" name=""{3}""><LANGUAGEVARIANTS>{4}</LANGUAGEVARIANTS></WORKFLOW></LINK></WORKFLOW>";
192  var session = _element.Project.Session;
193  string query = CREATE_AND_ASSIGN_WORKFLOW.RQLFormat(session.SessionKey, _element, isStructural, workflowName,
194  languageVariants);
195  session.ExecuteRQLRaw(query, RQL.IODataFormat.LogonGuidOnly);
196  }
197 
198  private PreassignedWorkflow ExecuteLoadWorkflow(string LOAD_WORKFLOW)
199  {
200  IProject project = _element.Project;
201  var xmlDoc = project.ExecuteRQL(LOAD_WORKFLOW.RQLFormat(project.Session.SessionKey, _element));
202  var workflowElement = (XmlElement) xmlDoc.SelectSingleNode("//WORKFLOW[@guid!='']");
203 
204  return workflowElement != null
205  ? new PreassignedWorkflow(_element, new Workflow(project, workflowElement))
206  : null;
207  }
208 
209  private void ExecutePreassignWorkflow(IWorkflow workflow, IEnumerable<ILanguageVariant> languageVariants)
210  {
211  const string PREASSIGN_WORKFLOW =
212  @"<WORKFLOW sessionkey=""{0}""><LINK guid=""{1}"" action=""assign""><WORKFLOW action=""addnew"" structureworkflow=""{2}"" guid=""{3}"" name=""{4}""><LANGUAGEVARIANTS>{5}</LANGUAGEVARIANTS></WORKFLOW></LINK></WORKFLOW>";
213 
214  var session = _element.Project.Session;
215  session.ExecuteRQLRaw(
216  PREASSIGN_WORKFLOW.RQLFormat(session.SessionKey, _element, workflow.IsStructureWorkflow, workflow,
217  workflow.Name, languageVariants), RQL.IODataFormat.LogonGuidOnly);
218 
219  InvalidateCache();
220  }
221 
222  private PreassignedWorkflow GetPreassignedContentWorkflow(ILanguageVariant languageVariant)
223  {
224  using (new LanguageContext(languageVariant))
225  {
226  return LoadPreassignedContentWorkflow();
227  }
228  }
229 
230  private PreassignedWorkflow LoadPreassignedContentWorkflow()
231  {
232  const string LOAD_CONTENT_WORKFLOW =
233  @"<WORKFLOW sessionkey=""{0}""><LINK guid=""{1}""><WORKFLOW action=""load"" /></LINK></WORKFLOW>";
234  return ExecuteLoadWorkflow(LOAD_CONTENT_WORKFLOW);
235  }
236 
237  private PreassignedWorkflow LoadStructuralWorkflow()
238  {
239  const string LOAD_WORKFLOW =
240  @"<WORKFLOW sessionkey=""{0}""><LINK guid=""{1}""><WORKFLOW action=""load"" structureworkflow=""1""/></LINK></WORKFLOW>";
241  return ExecuteLoadWorkflow(LOAD_WORKFLOW);
242  }
243  }
244 }