Skip to content

Commit 114bca1

Browse files
Eugene Bochilointrofog
authored andcommitted
Add ability to create producer line from product process events
DEVSIX-5662
1 parent 5153486 commit 114bca1

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

commons/src/main/java/com/itextpdf/commons/actions/producer/ProducerBuilder.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.commons.actions.producer;
2424

25+
import com.itextpdf.commons.actions.AbstractITextConfigurationEvent;
26+
import com.itextpdf.commons.actions.AbstractProductProcessITextEvent;
2527
import com.itextpdf.commons.actions.confirmations.ConfirmedEventWrapper;
28+
import com.itextpdf.commons.actions.processors.ITextProductEventProcessor;
2629
import com.itextpdf.commons.exceptions.CommonsExceptionMessageConstant;
2730
import com.itextpdf.commons.logs.CommonsLogMessageConstant;
2831
import com.itextpdf.commons.utils.MessageFormatUtil;
2932

33+
import java.util.ArrayList;
3034
import java.util.Collections;
3135
import java.util.HashMap;
3236
import java.util.List;
@@ -39,9 +43,11 @@ This file is part of the iText (R) project.
3943
/**
4044
* Class is used for producer line building.
4145
*/
42-
public final class ProducerBuilder {
46+
public final class ProducerBuilder extends AbstractITextConfigurationEvent {
4347
private static final Logger LOGGER = LoggerFactory.getLogger(ProducerBuilder.class);
4448

49+
private static final ProducerBuilder INSTANCE = new ProducerBuilder();
50+
4551
private static final String CURRENT_DATE = "currentDate";
4652
private static final String USED_PRODUCTS = "usedProducts";
4753
private static final String COPYRIGHT_SINCE = "copyrightSince";
@@ -75,27 +81,49 @@ public final class ProducerBuilder {
7581
private ProducerBuilder() { }
7682

7783
/**
78-
* Modifies an old producer line according to events registered for the document. Format of the
79-
* new producer line will be defined by the first event in the list. Placeholder will be
80-
* replaced and merged all together
84+
* Modifies an old producer line according to events registered for the document.
85+
* Events can be either wrapped with {@link ConfirmedEventWrapper} or not.
86+
* Format of the new producer line will be defined by the first event in the list.
87+
* Placeholder will be replaced and merged all together
8188
*
82-
* @param events list of events wrapped with {@link ConfirmedEventWrapper} registered for
83-
* the document
84-
* @param oldProducer is an old producer line. If <code>null</code> or empty, will be replaced
89+
* @param events list of events registered for the document
90+
* @param oldProducer old producer line. If <code>null</code> or empty, will be replaced
8591
* with a new one. Otherwise new line will be attached with
8692
* <code>modified using</code> prefix. If old producer line already contains
8793
* <code>modified using</code> substring, it will be overriden with a new one
8894
* @return modified producer line
8995
*/
90-
public static String modifyProducer(List<ConfirmedEventWrapper> events, String oldProducer) {
91-
final String newProducer = buildProducer(events);
96+
public static String modifyProducer(List<? extends AbstractProductProcessITextEvent> events, String oldProducer) {
97+
List<ConfirmedEventWrapper> confirmedEvents = new ArrayList<>();
98+
if (events != null) {
99+
for (AbstractProductProcessITextEvent event : events) {
100+
if (event instanceof ConfirmedEventWrapper) {
101+
confirmedEvents.add((ConfirmedEventWrapper) event);
102+
} else {
103+
ITextProductEventProcessor processor = INSTANCE.getActiveProcessor(event.getProductName());
104+
confirmedEvents.add(new ConfirmedEventWrapper(event, processor.getUsageType(), processor.getProducer()));
105+
}
106+
}
107+
}
108+
109+
final String newProducer = buildProducer(confirmedEvents);
92110
if (oldProducer == null || oldProducer.isEmpty()) {
93111
return newProducer;
94112
} else {
95113
return oldProducer + MODIFIED_USING + newProducer;
96114
}
97115
}
98116

117+
/**
118+
* Configuration events for util internal purposes are not expected to be sent.
119+
*
120+
* @throws IllegalStateException on every method call
121+
*/
122+
@Override
123+
protected void doAction() {
124+
throw new IllegalStateException("Configuration events for util internal purposes are not expected to be sent");
125+
}
126+
99127
private static String buildProducer(List<ConfirmedEventWrapper> events) {
100128
if (events == null || events.isEmpty()) {
101129
throw new IllegalArgumentException(CommonsExceptionMessageConstant.NO_EVENTS_WERE_REGISTERED_FOR_THE_DOCUMENT);

commons/src/test/java/com/itextpdf/commons/actions/producer/ProducerBuilderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.commons.actions.producer;
2424

25+
import com.itextpdf.commons.actions.AbstractProductProcessITextEvent;
2526
import com.itextpdf.commons.actions.confirmations.ConfirmedEventWrapper;
2627
import com.itextpdf.commons.actions.data.ProductData;
2728
import com.itextpdf.commons.actions.sequence.SequenceId;
@@ -55,7 +56,7 @@ public void emptyEventsProducerLineTest() {
5556
junitExpectedException.expect(IllegalArgumentException.class);
5657
junitExpectedException.expectMessage(CommonsExceptionMessageConstant.NO_EVENTS_WERE_REGISTERED_FOR_THE_DOCUMENT);
5758

58-
ProducerBuilder.modifyProducer(null, null);
59+
ProducerBuilder.modifyProducer((List<AbstractProductProcessITextEvent>)null, null);
5960
}
6061

6162
@Test

0 commit comments

Comments
 (0)