/
RemoveIshDocumentObj.cs
210 lines (192 loc) · 11.5 KB
/
RemoveIshDocumentObj.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 2014 All Rights Reserved by the SDL Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Management.Automation;
using Trisoft.ISHRemote.DocumentObj25ServiceReference;
using Trisoft.ISHRemote.Objects;
using Trisoft.ISHRemote.Objects.Public;
using Trisoft.ISHRemote.Exceptions;
namespace Trisoft.ISHRemote.Cmdlets.DocumentObj
{
/// <summary>
/// <para type="synopsis">The Remove-IshDocumentObj cmdlet removes the document objects that are passed through the pipeline or determined via provided parameters This commandlet allows to remove all types of objects (Illustrations, Maps, etc. ), except for publication (outputs).
/// For publication (outputs) you need to use Remove-IshPublicationOutput.</para>
/// <para type="description">The Remove-IshDocumentObj cmdlet removes the document objects that are passed through the pipeline or determined via provided parameters This commandlet allows to remove all types of objects (Illustrations, Maps, etc. ), except for publication (outputs).
/// For publication (outputs) you need to use Remove-IshPublicationOutput.</para>
/// </summary>
/// <example>
/// <code>
/// $ishSession = New-IshSession -WsBaseUrl "https://example.com/InfoShareWS/" -PSCredential Admin
/// Get-IshFolder -FolderPath $folderCmdletRootPath -Recurse |
/// Get-IshFolderContent |
/// Remove-IshDocumentObj -Force
/// </code>
/// <para>New-IshSession will submit into SessionState, so it can be reused by this cmdlet. Removes all content objects from the listed folder and its subfolders.</para>
/// </example>
[Cmdlet(VerbsCommon.Remove, "IshDocumentObj", SupportsShouldProcess = true)]
public sealed class RemoveIshDocumentObj : DocumentObjCmdlet
{
/// <summary>
/// <para type="description">The IshSession variable holds the authentication and contract information. This object can be initialized using the New-IshSession cmdlet.</para>
/// </summary>
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = "ParameterGroup")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = "IshObjectsGroup")]
[ValidateNotNullOrEmpty]
public IshSession IshSession { get; set; }
/// <summary>
/// <para type="description">The logical identifier of the document object</para>
/// </summary>
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = false, ParameterSetName = "ParameterGroup"), ValidateNotNullOrEmpty]
public string LogicalId { get; set; }
/// <summary>
/// <para type="description">The version of the document object</para>
/// </summary>
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = false, ParameterSetName = "ParameterGroup"), ValidateNotNullOrEmpty]
public string Version { get; set; }
/// <summary>
/// <para type="description">The language of the document object</para>
/// </summary>
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = "ParameterGroup"), ValidateNotNullOrEmpty]
public string Lng { get; set; }
/// <summary>
/// <para type="description">The resolution of the document object</para>
/// </summary>
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = "ParameterGroup"), ValidateNotNullOrEmpty]
public string Resolution { get; set; }
/// <summary>
/// <para type="description">The required current metadata of the document object. This parameter can be used to avoid that users override changes done by other users. The cmdlet will check whether the fields provided in this parameter still have the same values in the repository:</para>
/// <para type="description">If the metadata is still the same, the document object is removed.</para>
/// <para type="description">If the metadata is not the same anymore, an error is given and the document object is not removed.</para>
/// </summary>
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = "ParameterGroup")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = "IshObjectsGroup")]
[ValidateNotNullOrEmpty]
public IshField[] RequiredCurrentMetadata { get; set; }
/// <summary>
/// <para type="description">Array with the objects to remove. This array can be passed through the pipeline or explicitly passed via the parameter.</para>
/// </summary>
/// TODO [Should] Promote parameter IshObject to IshObject[] processing
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = "IshObjectsGroup")]
public IshObject IshObject { get; set; }
/// <summary>
/// <para type="description">When the Force switch is set, after deleting the language object, the version object and logical object will be deleted if they don't have any language objects anymore</para>
/// </summary>
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = "ParameterGroup")]
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, ParameterSetName = "IshObjectsGroup")]
[ValidateNotNullOrEmpty]
public SwitchParameter Force { get; set; }
protected override void BeginProcessing()
{
if (IshSession == null) { IshSession = (IshSession)SessionState.PSVariable.GetValue(ISHRemoteSessionStateIshSession); }
if (IshSession == null) { throw new ArgumentException(ISHRemoteSessionStateIshSessionException); }
WriteDebug($"Using IshSession[{IshSession.Name}] from SessionState.{ISHRemoteSessionStateIshSession}");
base.BeginProcessing();
}
/// <summary>
/// Process the Remove-IshDocumentObj commandlet.
/// </summary>
/// <exception cref="TrisoftAutomationException"></exception>
/// <exception cref="Exception"></exception>
protected override void ProcessRecord()
{
try
{
// 1. Validating the input
WriteDebug("Validating");
var logicalIdsVersionsCollection = new NameValueCollection();
WriteDebug("Removing");
IshFields requiredCurrentMetadata = new IshFields(RequiredCurrentMetadata);
if (ParameterSetName == "IshObjectsGroup")
{
long lngRef = Convert.ToInt64(IshObject.ObjectRef[Enumerations.ReferenceType.Lng]);
WriteDebug($"Removing lngCardId[{lngRef}]");
if (ShouldProcess(Convert.ToString(lngRef)))
{
IshSession.DocumentObj25.DeleteByIshLngRef(lngRef, requiredCurrentMetadata.ToXml());
}
logicalIdsVersionsCollection.Add(IshObject.IshRef, IshObject.IshFields.GetFieldValue("VERSION", Enumerations.Level.Version, Enumerations.ValueType.Value));
}
else
{
string resolution = Resolution ?? "";
string lng = Lng ?? "";
if (lng.Length > 0)
{
// only delete when you have a language, lower the version delete (so empty lng) is handled
WriteDebug($"Removing LogicalId[{LogicalId}] Version[{Version}] Lng[{lng}] Resolution[{resolution}]");
if (ShouldProcess(LogicalId + "=" + Version + "=" + lng + "=" + resolution))
{
IshSession.DocumentObj25.Delete(
LogicalId,
Version,
lng,
resolution,
requiredCurrentMetadata.ToXml());
}
}
logicalIdsVersionsCollection.Add(LogicalId, Version);
}
if (Force.IsPresent && logicalIdsVersionsCollection.Count > 0 && IshSession.ServerIshVersion.MajorVersion < 15)
{
var xmlIshObjects = IshSession.DocumentObj25.RetrieveMetadata(logicalIdsVersionsCollection.AllKeys.ToArray(),
StatusFilter.ISHNoStatusFilter, "", "<ishfields><ishfield name='VERSION' level='version'/><ishfield name='DOC-LANGUAGE' level='lng'/></ishfields>");
List<IshObject> retrievedObjects = new List<IshObject>();
retrievedObjects.AddRange(new IshObjects(xmlIshObjects).Objects);
// Delete versions which do not have any language card anymore
foreach (string logicalId in logicalIdsVersionsCollection.AllKeys)
{
var versions = logicalIdsVersionsCollection.GetValues(logicalId).Distinct();
foreach (var version in versions)
{
var versionWithLanguageObjects = retrievedObjects.FindAll(o => (o.IshFields.GetFieldValue("VERSION", Enumerations.Level.Version, Enumerations.ValueType.Value) == version) && o.IshRef == logicalId);
if (versionWithLanguageObjects.Count == 0)
{
if (ShouldProcess(LogicalId + "=" + version + "=" + "="))
{
IshSession.DocumentObj25.Delete(logicalId, version, "", "", "");
}
}
}
}
// Delete logical cards which do not have any languages anymore
foreach (string logicalId in logicalIdsVersionsCollection.AllKeys)
{
var logicalObjectsWithLanguages = retrievedObjects.FindAll(o => o.IshRef == logicalId);
if (logicalObjectsWithLanguages.Count == 0)
{
if (ShouldProcess(LogicalId + "=" + "=" + "="))
{
IshSession.DocumentObj25.Delete(logicalId, "", "", "", "");
}
}
}
}
WriteVerbose("returned object count[0]");
}
catch (TrisoftAutomationException trisoftAutomationException)
{
ThrowTerminatingError(new ErrorRecord(trisoftAutomationException, base.GetType().Name, ErrorCategory.InvalidOperation, null));
}
catch (Exception exception)
{
ThrowTerminatingError(new ErrorRecord(exception, base.GetType().Name, ErrorCategory.NotSpecified, null));
}
}
}
}