Skip to content

Commit 89df9ce

Browse files
Eugene Bochiloyulian-gaponenko
authored andcommitted
Add ability to create producer line from product process events
DEVSIX-5662 Autoported commit. Original commit hash: [0ce1fb2b3]
1 parent 764efdf commit 89df9ce

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

itext.tests/itext.commons.tests/itext/commons/actions/producer/ProducerBuilderTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ You should have received a copy of the GNU Affero General Public License
2222
*/
2323
using System;
2424
using System.Collections.Generic;
25+
using iText.Commons.Actions;
2526
using iText.Commons.Actions.Confirmations;
2627
using iText.Commons.Actions.Data;
2728
using iText.Commons.Actions.Sequence;
@@ -37,7 +38,7 @@ public class ProducerBuilderTest : ExtendedITextTest {
3738
[NUnit.Framework.Test]
3839
public virtual void EmptyEventsProducerLineTest() {
3940
NUnit.Framework.Assert.That(() => {
40-
ProducerBuilder.ModifyProducer(null, null);
41+
ProducerBuilder.ModifyProducer((IList<AbstractProductProcessITextEvent>)null, null);
4142
}
4243
, NUnit.Framework.Throws.InstanceOf<ArgumentException>().With.Message.EqualTo(CommonsExceptionMessageConstant.NO_EVENTS_WERE_REGISTERED_FOR_THE_DOCUMENT))
4344
;

itext/itext.commons/itext/commons/actions/producer/ProducerBuilder.cs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,22 @@ You should have received a copy of the GNU Affero General Public License
2626
using System.Text.RegularExpressions;
2727
using Microsoft.Extensions.Logging;
2828
using iText.Commons;
29+
using iText.Commons.Actions;
2930
using iText.Commons.Actions.Confirmations;
31+
using iText.Commons.Actions.Processors;
3032
using iText.Commons.Exceptions;
3133
using iText.Commons.Logs;
3234
using iText.Commons.Utils;
3335

3436
namespace iText.Commons.Actions.Producer {
3537
/// <summary>Class is used for producer line building.</summary>
36-
public sealed class ProducerBuilder {
38+
public sealed class ProducerBuilder : AbstractITextConfigurationEvent {
3739
private static readonly ILogger LOGGER = ITextLogManager.GetLogger(typeof(iText.Commons.Actions.Producer.ProducerBuilder
3840
));
3941

42+
private static readonly iText.Commons.Actions.Producer.ProducerBuilder INSTANCE = new iText.Commons.Actions.Producer.ProducerBuilder
43+
();
44+
4045
private const String CURRENT_DATE = "currentDate";
4146

4247
private const String USED_PRODUCTS = "usedProducts";
@@ -76,25 +81,36 @@ private ProducerBuilder() {
7681

7782
/// <summary>Modifies an old producer line according to events registered for the document.</summary>
7883
/// <remarks>
79-
/// Modifies an old producer line according to events registered for the document. Format of the
80-
/// new producer line will be defined by the first event in the list. Placeholder will be
81-
/// replaced and merged all together
82-
/// </remarks>
83-
/// <param name="events">
84-
/// list of events wrapped with
84+
/// Modifies an old producer line according to events registered for the document.
85+
/// Events can be either wrapped with
8586
/// <see cref="iText.Commons.Actions.Confirmations.ConfirmedEventWrapper"/>
86-
/// registered for
87-
/// the document
88-
/// </param>
87+
/// or not.
88+
/// Format of the new producer line will be defined by the first event in the list.
89+
/// Placeholder will be replaced and merged all together
90+
/// </remarks>
91+
/// <param name="events">list of events registered for the document</param>
8992
/// <param name="oldProducer">
90-
/// is an old producer line. If <c>null</c> or empty, will be replaced
93+
/// old producer line. If <c>null</c> or empty, will be replaced
9194
/// with a new one. Otherwise new line will be attached with
9295
/// <c>modified using</c> prefix. If old producer line already contains
9396
/// <c>modified using</c> substring, it will be overriden with a new one
9497
/// </param>
9598
/// <returns>modified producer line</returns>
96-
public static String ModifyProducer(IList<ConfirmedEventWrapper> events, String oldProducer) {
97-
String newProducer = BuildProducer(events);
99+
public static String ModifyProducer<_T0>(IList<_T0> events, String oldProducer)
100+
where _T0 : AbstractProductProcessITextEvent {
101+
IList<ConfirmedEventWrapper> confirmedEvents = new List<ConfirmedEventWrapper>();
102+
if (events != null) {
103+
foreach (AbstractProductProcessITextEvent @event in events) {
104+
if (@event is ConfirmedEventWrapper) {
105+
confirmedEvents.Add((ConfirmedEventWrapper)@event);
106+
}
107+
else {
108+
ITextProductEventProcessor processor = INSTANCE.GetActiveProcessor(@event.GetProductName());
109+
confirmedEvents.Add(new ConfirmedEventWrapper(@event, processor.GetUsageType(), processor.GetProducer()));
110+
}
111+
}
112+
}
113+
String newProducer = BuildProducer(confirmedEvents);
98114
if (oldProducer == null || String.IsNullOrEmpty(oldProducer)) {
99115
return newProducer;
100116
}
@@ -103,6 +119,12 @@ public static String ModifyProducer(IList<ConfirmedEventWrapper> events, String
103119
}
104120
}
105121

122+
/// <summary>Configuration events for util internal purposes are not expected to be sent.</summary>
123+
protected internal override void DoAction() {
124+
throw new InvalidOperationException("Configuration events for util internal purposes are not expected to be sent"
125+
);
126+
}
127+
106128
private static String BuildProducer(IList<ConfirmedEventWrapper> events) {
107129
if (events == null || events.IsEmpty()) {
108130
throw new ArgumentException(CommonsExceptionMessageConstant.NO_EVENTS_WERE_REGISTERED_FOR_THE_DOCUMENT);

0 commit comments

Comments
 (0)