17 using System.Collections.Generic;
20 using erminas.SmartAPI.CMS.Project.Workflows;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils;
24 namespace erminas.SmartAPI.CMS.Project.ContentClasses.Elements
32 void CreateAndConnectContentWorkflow(
string workflowName, params
string[] languageVariants);
34 void CreateAndConnectContentWorkflow(
string workflowName, IEnumerable<ILanguageVariant> languageVariants);
36 void CreateAndConnectStructuralworkflow(
string workflowName);
37 void DisconnectAllWorkflows();
41 void InvalidateCache();
42 void SetContentWorkflow(
IWorkflow workflow, IEnumerable<ILanguageVariant> languageVariants);
43 void SetContentWorkflow(
IWorkflow workflow, params
string[] languageVariantIds);
47 internal class WorkflowAssignments : IWorkflowAssignments
49 private readonly IWorkflowAssignable _element;
51 private readonly Dictionary<ILanguageVariant, IPreassignedWorkflow> _preassignedContentWorkflows =
52 new Dictionary<ILanguageVariant, IPreassignedWorkflow>();
54 private bool _isStructuralWorkflowLoaded;
55 private PreassignedWorkflow _preassignedStructuralWorkflow;
57 internal WorkflowAssignments(IWorkflowAssignable element)
62 public void CreateAndConnectContentWorkflow(
string workflowName, params
string[] languageVariants)
64 CreateAndConnectContentWorkflow(workflowName,
65 languageVariants.Select(
66 curVariant => _element.Project.LanguageVariants[curVariant]));
69 public void CreateAndConnectContentWorkflow(
string workflowName, IEnumerable<ILanguageVariant> languageVariants)
71 const bool IS_STRUCTURAL_WORKFLOW =
false;
72 ExecuteCreateAndPreassignWorkflow(workflowName, languageVariants, IS_STRUCTURAL_WORKFLOW);
75 public void CreateAndConnectStructuralworkflow(
string workflowName)
77 const bool IS_STRUCTURAL_WORKFLOW =
true;
78 ExecuteCreateAndPreassignWorkflow(workflowName,
new[] {_element.Project.LanguageVariants.Current},
79 IS_STRUCTURAL_WORKFLOW);
82 public void DisconnectAllWorkflows()
84 if (StructuralWorkflow != null)
86 StructuralWorkflow.DisconnectFromLinkCompletely();
88 var workflows =
new HashSet<Guid>();
89 foreach (var curLang
in _element.Project.LanguageVariants)
91 var workflow = GetContentWorkflowFor(curLang);
96 if (workflows.Contains(workflow.Guid))
100 workflow.DisconnectFromLinkCompletely();
101 workflows.Add(workflow.Guid);
106 public IPreassignedWorkflow GetContentWorkflowFor(
string languageVariantId)
108 ILanguageVariant languageVariant = _element.Project.LanguageVariants[languageVariantId];
109 return GetContentWorkflowFor(languageVariant);
112 public IPreassignedWorkflow GetContentWorkflowFor(ILanguageVariant languageVariant)
114 IPreassignedWorkflow workflow;
115 if (!_preassignedContentWorkflows.TryGetValue(languageVariant, out workflow))
117 workflow = GetPreassignedContentWorkflow(languageVariant);
119 if (workflow == null)
121 _preassignedContentWorkflows[languageVariant] = null;
125 foreach (var curLanguage
in workflow.LanguageVariantsPreassignedTo)
127 _preassignedContentWorkflows[curLanguage] = workflow;
133 public IPreassignedWorkflow GetContentWorkflowForCurrentLanguageVariant()
135 return GetContentWorkflowFor(_element.Project.LanguageVariants.Current);
138 public void InvalidateCache()
140 _preassignedStructuralWorkflow = null;
141 _isStructuralWorkflowLoaded =
false;
142 _preassignedContentWorkflows.Clear();
145 public void SetContentWorkflow(
IWorkflow workflow, IEnumerable<ILanguageVariant> languageVariants)
150 "Workflow for preassignment is a structural workflow, although a content workflow is expected");
153 ExecutePreassignWorkflow(workflow, languageVariants);
156 public void SetContentWorkflow(
IWorkflow workflow, params
string[] languageVariantIds)
158 var languageVariants =
159 languageVariantIds.Select(
160 curLanguageVariantId => _element.Project.LanguageVariants[curLanguageVariantId]);
161 SetContentWorkflow(workflow, languageVariants);
164 public IPreassignedWorkflow StructuralWorkflow
168 if (!_isStructuralWorkflowLoaded)
170 _preassignedStructuralWorkflow = LoadStructuralWorkflow();
171 _isStructuralWorkflowLoaded =
true;
173 return _preassignedStructuralWorkflow;
177 if (!value.IsStructureWorkflow)
180 "Workflow for preassignment is not a structural workflow");
183 ExecutePreassignWorkflow(value, null);
187 private void ExecuteCreateAndPreassignWorkflow(
string workflowName,
188 IEnumerable<ILanguageVariant> languageVariants,
bool isStructural)
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,
195 session.ExecuteRQLRaw(query, RQL.IODataFormat.LogonGuidOnly);
198 private PreassignedWorkflow ExecuteLoadWorkflow(
string LOAD_WORKFLOW)
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!='']");
204 return workflowElement != null
205 ?
new PreassignedWorkflow(_element,
new Workflow(project, workflowElement))
209 private void ExecutePreassignWorkflow(
IWorkflow workflow, IEnumerable<ILanguageVariant> languageVariants)
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>";
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);
222 private PreassignedWorkflow GetPreassignedContentWorkflow(ILanguageVariant languageVariant)
226 return LoadPreassignedContentWorkflow();
230 private PreassignedWorkflow LoadPreassignedContentWorkflow()
232 const string LOAD_CONTENT_WORKFLOW =
233 @"<WORKFLOW sessionkey=""{0}""><LINK guid=""{1}""><WORKFLOW action=""load"" /></LINK></WORKFLOW>";
234 return ExecuteLoadWorkflow(LOAD_CONTENT_WORKFLOW);
237 private PreassignedWorkflow LoadStructuralWorkflow()
239 const string LOAD_WORKFLOW =
240 @"<WORKFLOW sessionkey=""{0}""><LINK guid=""{1}""><WORKFLOW action=""load"" structureworkflow=""1""/></LINK></WORKFLOW>";
241 return ExecuteLoadWorkflow(LOAD_WORKFLOW);