Skip to content

Commit

Permalink
Revert "[2.26.x] updated RTF transformer formatting and now omits nul…
Browse files Browse the repository at this point in the history
…l attributes from… (#6744)"

This reverts commit 22d0f89.
  • Loading branch information
alexabird committed Sep 13, 2024
1 parent 41fe113 commit 64f8e5d
Show file tree
Hide file tree
Showing 7 changed files with 1,436 additions and 635 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.tutego.jrtf.RtfPara;
import com.tutego.jrtf.RtfPicture;
import com.tutego.jrtf.RtfRow;
import com.tutego.jrtf.RtfSectionFormatAndHeaderFooter;
import com.tutego.jrtf.RtfText;
import ddf.catalog.data.Metacard;
import ddf.catalog.transformer.output.rtf.model.ExportCategory;
Expand All @@ -52,41 +51,62 @@ public class RtfTemplate {
private static final Logger LOGGER = LoggerFactory.getLogger(RtfTemplate.class);

private static final int MINIMUM_VALID_IMAGE_DATA_SIZE = 10;

static class Builder {
private List<RtfCategory> categories;
private Metacard metacard;

public Builder withCategories(List<RtfCategory> categories) {
this.categories = categories;

return this;
}

public Builder withMetacard(Metacard metacard) {
this.metacard = metacard;

return this;
}

public RtfTemplate build() {
return new RtfTemplate(this.categories, this.metacard);
}
}

private final List<RtfCategory> categories;
private final Metacard metacard;
private Function<String, Function<byte[], InputStream>> memoizeForImageData =
metacardId -> data -> fromBytes(metacardId, data);
private Function<String, Function<Map.Entry, RtfRow>> memoizeForRowData =
metacardId -> entry -> appendProperty(metacardId, entry);

private RtfTemplate(List<RtfCategory> categories, Metacard metacard) {
this.categories = categories;
this.metacard = metacard;
}

public Rtf rtf(Rtf doc) {
doc.section(
RtfSectionFormatAndHeaderFooter.noBreak(),
p(font(1, bold(this.metacard.getTitle()))).alignCentered());
doc.section(p(), p(), p(font(1, bold(this.metacard.getTitle()))).alignCentered());

this.categories.forEach(exportCategory -> appendSection(doc, exportCategory, this.metacard));
doc.p();

return doc;
}

private Function<String, Function<Map.Entry, RtfRow>> memoizeForRowData =
metacardId -> entry -> appendProperty(metacardId, entry);

private Function<String, Function<byte[], InputStream>> memoizeForImageData =
metacardId -> data -> fromBytes(metacardId, data);

private void appendSection(Rtf rtf, RtfCategory category, Metacard metacard) {
rtf.p();
rtf.p(bold(category.getTitle()));

Function<Map.Entry, RtfRow> appendPropertyFunction = memoizeForRowData.apply(metacard.getId());

Collection<RtfPara> rows =
category.toExportMap(metacard).entrySet().stream()
.map(appendPropertyFunction)
.collect(Collectors.toList());

if (rows.isEmpty()) {
return;
}
rtf.p();
rtf.p(bold(category.getTitle()));
rtf.section(RtfSectionFormatAndHeaderFooter.noBreak(), rows.toArray(new RtfPara[rows.size()]));
rtf.section(rows);
}

private RtfRow appendProperty(
Expand All @@ -102,18 +122,10 @@ private RtfRow appendProperty(
.map(this::imageFromStream)
.orElse(text(EMPTY_VALUE));

return row(entry.getKey(), picture)
.topCellBorder()
.rightCellBorder()
.leftCellBorder()
.bottomCellBorder();
return row(entry.getKey(), picture);
}

return row(entry.getKey(), entry.getValue().getValue())
.bottomCellBorder()
.leftCellBorder()
.rightCellBorder()
.topCellBorder();
return row(entry.getKey(), entry.getValue().getValue());
}

private RtfText imageFromStream(InputStream stream) {
Expand All @@ -130,26 +142,4 @@ private InputStream fromBytes(String metacardId, byte[] data) {

return new ByteArrayInputStream(data);
}

static class Builder {

private List<RtfCategory> categories;
private Metacard metacard;

public Builder withCategories(List<RtfCategory> categories) {
this.categories = categories;

return this;
}

public Builder withMetacard(Metacard metacard) {
this.metacard = metacard;

return this;
}

public RtfTemplate build() {
return new RtfTemplate(this.categories, this.metacard);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package ddf.catalog.transformer.output.rtf.model;

import ddf.catalog.data.Attribute;
import ddf.catalog.data.AttributeDescriptor;
import ddf.catalog.data.Metacard;
import ddf.catalog.data.types.Core;
import java.util.AbstractMap;
Expand All @@ -23,7 +22,6 @@
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;

public class ExportCategory implements RtfCategory {
Expand Down Expand Up @@ -91,7 +89,6 @@ public void setAttributes(List<String> attributes) {
@Override
public Map<String, ExportValue> toExportMap(Metacard metacard) {
return attributes.stream()
.filter(a -> isNonEmptyValue(metacard, a))
.map(
key ->
new AbstractMap.SimpleEntry<>(
Expand All @@ -114,6 +111,10 @@ private String attributeKeyFrom(String key) {
}

private ExportValue attributeExportValueFrom(String attributeKey, Attribute attribute) {
if (attributeKey == null || attribute == null) {
return emptyValue();
}

if (attributeKey.equals(Core.THUMBNAIL)) {
byte[] image = (byte[]) attribute.getValue();

Expand All @@ -136,27 +137,4 @@ private ExportValue simpleValue(Attribute attribute) {
Optional.ofNullable(attribute.getValue()).map(Object::toString).orElse(null),
ValueType.SIMPLE);
}

private static boolean isNonEmptyValue(Metacard metacard, String attrName) {
final AttributeDescriptor descriptor =
metacard.getMetacardType().getAttributeDescriptor(attrName);
final Attribute attribute = metacard.getAttribute(attrName);
switch (descriptor.getType().getAttributeFormat()) {
case STRING:
case XML:
case GEOMETRY:
return attribute != null && StringUtils.isNotEmpty((String) attribute.getValue());
case INTEGER:
case LONG:
case DOUBLE:
case FLOAT:
case SHORT:
case DATE:
case BOOLEAN:
case BINARY:
return attribute != null && attribute.getValue() != null;
default:
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
import static org.mockito.Mockito.when;

import ddf.catalog.data.Attribute;
import ddf.catalog.data.AttributeDescriptor;
import ddf.catalog.data.AttributeType;
import ddf.catalog.data.Metacard;
import ddf.catalog.data.MetacardType;
import ddf.catalog.data.impl.BasicTypes;
import ddf.catalog.data.types.Core;
import ddf.catalog.transformer.output.rtf.model.ExportCategory;
import ddf.catalog.transformer.output.rtf.model.RtfCategory;
Expand Down Expand Up @@ -125,8 +121,6 @@ Metacard createMockMetacard(String title) {

Metacard createMockMetacard(String title, Attribute mediaAttribute) {
Metacard metacard = mock(Metacard.class);
MetacardType mockMetacardType = createMockMetacardType();
when(metacard.getMetacardType()).thenReturn(mockMetacardType);
when(metacard.getTitle()).thenReturn(title);

when(metacard.getId()).thenReturn("mock-id");
Expand Down Expand Up @@ -174,30 +168,4 @@ Attribute createSimpleAttribute() {

return mockAttribute;
}

MetacardType createMockMetacardType() {
MetacardType mockType = mock(MetacardType.class);
AttributeDescriptor mockThumbnailDesc =
createMockAttributeDescriptor(Core.THUMBNAIL, BasicTypes.BINARY_TYPE);
when(mockType.getAttributeDescriptor(Core.THUMBNAIL)).thenReturn(mockThumbnailDesc);
AttributeDescriptor mockEmptyDesc =
createMockAttributeDescriptor(EMPTY_ATTRIBUTE, BasicTypes.STRING_TYPE);
when(mockType.getAttributeDescriptor(EMPTY_ATTRIBUTE)).thenReturn(mockEmptyDesc);
AttributeDescriptor mockSimpleDesc =
createMockAttributeDescriptor(SIMPLE_ATTRIBUTE, BasicTypes.STRING_TYPE);
when(mockType.getAttributeDescriptor(SIMPLE_ATTRIBUTE)).thenReturn(mockSimpleDesc);
AttributeDescriptor mockExtendedDesc =
createMockAttributeDescriptor(EXTENDED_ATTRIBUTE, BasicTypes.STRING_TYPE);
when(mockType.getAttributeDescriptor(EXTENDED_ATTRIBUTE)).thenReturn(mockExtendedDesc);
AttributeDescriptor mockUnknownDesc =
createMockAttributeDescriptor(UNKNOWN_ATTRIBUTE, BasicTypes.INTEGER_TYPE);
when(mockType.getAttributeDescriptor(UNKNOWN_ATTRIBUTE)).thenReturn(mockUnknownDesc);
return mockType;
}

AttributeDescriptor createMockAttributeDescriptor(String name, AttributeType type) {
AttributeDescriptor mockDescriptor = mock(AttributeDescriptor.class);
when(mockDescriptor.getType()).thenReturn(type);
return mockDescriptor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

import com.tutego.jrtf.Rtf;
import ddf.catalog.data.Metacard;
import ddf.catalog.data.types.Core;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -60,48 +57,4 @@ public void testBuildingRtfFromTemplate() {
finishedDoc,
containsString(METACARD_TITLE));
}

@Test
public void testBuildingRtfWithEmptyCategoryFromTemplate() {
when(mockMetacard.getAttribute(Core.THUMBNAIL)).thenReturn(null);
when(mockMetacard.getAttribute(EMPTY_ATTRIBUTE)).thenReturn(null);
when(mockMetacard.getAttribute(SIMPLE_ATTRIBUTE)).thenReturn(null);
when(mockMetacard.getAttribute(EXTENDED_ATTRIBUTE)).thenReturn(null);
RtfTemplate template =
new RtfTemplate.Builder().withMetacard(mockMetacard).withCategories(MOCK_CATEGORY).build();

assertThat("Template cannot be null", template, notNullValue());
assertThat("There should be 5 categories", MOCK_CATEGORY.get(0).getAttributes(), hasSize(5));

Rtf doc = Rtf.rtf();

Rtf generatedTemplate = template.rtf(doc);

assertThat("Rtf template instance cannot be null", generatedTemplate, notNullValue());

String finishedDoc = generatedTemplate.out().toString();

assertThat("RTF output cannot be null", finishedDoc, notNullValue());
assertThat("RTF document must start with {\\rtf1", finishedDoc, startsWith("{\\rtf1"));
assertThat(
String.format("RTF document must contain section with title: %s", METACARD_TITLE),
finishedDoc,
containsString(METACARD_TITLE));
assertThat(
String.format("RTF document must NOT contain section with title: %s", Core.THUMBNAIL),
finishedDoc,
not(containsString(Core.THUMBNAIL)));
assertThat(
String.format("RTF document must NOT contain section with title: %s", EMPTY_ATTRIBUTE),
finishedDoc,
not(containsString(EMPTY_ATTRIBUTE)));
assertThat(
String.format("RTF document must NOT contain section with title: %s", SIMPLE_ATTRIBUTE),
finishedDoc,
not(containsString(SIMPLE_ATTRIBUTE)));
assertThat(
String.format("RTF document must NOT contain section with title: %s", EXTENDED_ATTRIBUTE),
finishedDoc,
not(containsString(EXTENDED_ATTRIBUTE)));
}
}
Loading

0 comments on commit 64f8e5d

Please sign in to comment.