From 70121d99102ef1ceb002f2588ef5f5fe6f90e4b6 Mon Sep 17 00:00:00 2001 From: Hans Van Akelyen Date: Thu, 16 Jan 2025 11:15:08 +0100 Subject: [PATCH] allow setting a rounding mode when writing a textfile, fixes #4782 add documentation, #4782 some cleanup --- .../org/apache/hop/core/row/IValueMeta.java | 72 ++++++---- .../hop/core/row/ValueMetaDeserializer.java | 1 + .../hop/core/row/value/ValueMetaBase.java | 81 +++++++++++ .../hop/core/row/value/ValueMetaFactory.java | 1 + .../core/messages/messages_en_US.properties | 8 ++ .../pipeline/transforms/textfileoutput.adoc | 3 + .../ROOT/pages/snippets/rounding-types.adoc | 69 +++++++++ .../metadata/SchemaDefinitionEditor.java | 10 +- .../metadata/SchemaFieldDefinition.java | 135 +----------------- .../messages/messages_en_US.properties | 1 + .../textfileoutput/TextFileField.java | 86 ++--------- .../textfileoutput/TextFileOutputDialog.java | 16 ++- .../textfileoutput/TextFileOutputMeta.java | 6 + .../messages/messages_en_US.properties | 1 + .../TextFileOutputMetaTest.java | 3 +- .../textfileoutput/TextFileOutputTest.java | 14 +- 16 files changed, 266 insertions(+), 241 deletions(-) create mode 100644 docs/hop-user-manual/modules/ROOT/pages/snippets/rounding-types.adoc diff --git a/core/src/main/java/org/apache/hop/core/row/IValueMeta.java b/core/src/main/java/org/apache/hop/core/row/IValueMeta.java index 2d4121d66ae..0fdb22b0808 100644 --- a/core/src/main/java/org/apache/hop/core/row/IValueMeta.java +++ b/core/src/main/java/org/apache/hop/core/row/IValueMeta.java @@ -33,6 +33,7 @@ import java.util.Date; import java.util.Locale; import java.util.TimeZone; +import lombok.Getter; import org.apache.hop.core.Const; import org.apache.hop.core.database.DatabaseMeta; import org.apache.hop.core.database.IDatabase; @@ -280,9 +281,27 @@ enum TrimType implements IEnumHasCodeAndDescription { LEFT("left", BaseMessages.getString(PKG, "ValueMeta.TrimType.Left"), TRIM_TYPE_LEFT), RIGHT("right", BaseMessages.getString(PKG, "ValueMeta.TrimType.Right"), TRIM_TYPE_RIGHT), BOTH("both", BaseMessages.getString(PKG, "ValueMeta.TrimType.Both"), TRIM_TYPE_BOTH); - private final String code; - private final String description; - private final int type; + + /** + * -- GETTER -- Gets code + * + * @return value of code + */ + @Getter private final String code; + + /** + * -- GETTER -- Gets description + * + * @return value of description + */ + @Getter private final String description; + + /** + * -- GETTER -- Gets type + * + * @return value of type + */ + @Getter private final int type; TrimType(String code, String description, int type) { this.code = code; @@ -290,6 +309,12 @@ enum TrimType implements IEnumHasCodeAndDescription { this.type = type; } + /** The Rounding type codes */ + @Getter + public static final String[] roundingTypeCode = { + "unnecessary", "ceiling", "down", "floor", "half_down", "half_even", "half_up", "up" + }; + public static String[] getDescriptions() { return IEnumHasCodeAndDescription.getDescriptions(TrimType.class); } @@ -310,33 +335,6 @@ public static TrimType lookupType(int type) { } return NONE; } - - /** - * Gets code - * - * @return value of code - */ - public String getCode() { - return code; - } - - /** - * Gets description - * - * @return value of description - */ - public String getDescription() { - return description; - } - - /** - * Gets type - * - * @return value of type - */ - public int getType() { - return type; - } } /** Default integer length for hardcoded metadata integers */ @@ -465,6 +463,20 @@ static String getTypeDescription(int type) { */ void setTrimType(int trimType); + /** + * Gets the rounding type. + * + * @return the rounding type + */ + String getRoundingType(); + + /** + * Sets the rounding type. + * + * @param roundingType the new rounding type + */ + void setRoundingType(String roundingType); + /** * Gets the index. * diff --git a/core/src/main/java/org/apache/hop/core/row/ValueMetaDeserializer.java b/core/src/main/java/org/apache/hop/core/row/ValueMetaDeserializer.java index 9e58bfa6201..02780aed9f5 100644 --- a/core/src/main/java/org/apache/hop/core/row/ValueMetaDeserializer.java +++ b/core/src/main/java/org/apache/hop/core/row/ValueMetaDeserializer.java @@ -44,6 +44,7 @@ public IValueMeta deserialize( IValueMeta valueMeta = ValueMetaFactory.createValueMeta(name, type, length, precision); valueMeta.setTrimType(root.get("trimType").asInt()); valueMeta.setStorageType(root.get("storageType").asInt()); + valueMeta.setRoundingType(root.get("roundingType").asText()); valueMeta.setConversionMask(asString(root.get("conversionMask"))); JsonNode stringEncoding = root.get("stringEncoding"); if (!stringEncoding.isNull()) { diff --git a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java index 0120949ede8..014abee73ac 100644 --- a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java +++ b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java @@ -33,6 +33,7 @@ import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; +import java.math.RoundingMode; import java.net.InetAddress; import java.net.SocketTimeoutException; import java.nio.charset.Charset; @@ -58,6 +59,8 @@ import java.util.Objects; import java.util.TimeZone; import javax.annotation.Nullable; +import lombok.Getter; +import lombok.Setter; import org.apache.hop.core.Const; import org.apache.hop.core.database.DatabaseMeta; import org.apache.hop.core.database.IDatabase; @@ -175,6 +178,11 @@ public class ValueMetaBase implements IValueMeta { @HopMetadataProperty(key = "trim_type", intCodeConverter = TrimTypeCodeConverter.class) protected int trimType; + @Getter + @Setter + @HopMetadataProperty(key = "roundingType") + protected String roundingType; + // The storage-type isn't meant to be serialized as metadata protected int storageType; @@ -257,6 +265,23 @@ public class ValueMetaBase implements IValueMeta { BaseMessages.getString(PKG, "ValueMeta.TrimType.Both") }; + /** The Rounding type codes */ + public static final String[] roundingTypeCode = { + "unnecessary", "ceiling", "down", "floor", "half_down", "half_even", "half_up", "up" + }; + + /** The Rounding description */ + public static final String[] roundingTypeDesc = { + BaseMessages.getString(PKG, "ValueMeta.RoundingType.Unnecessary"), + BaseMessages.getString(PKG, "ValueMeta.RoundingType.Ceiling"), + BaseMessages.getString(PKG, "ValueMeta.RoundingType.Down"), + BaseMessages.getString(PKG, "ValueMeta.RoundingType.Floor"), + BaseMessages.getString(PKG, "ValueMeta.RoundingType.HalfDown"), + BaseMessages.getString(PKG, "ValueMeta.RoundingType.HalfEven"), + BaseMessages.getString(PKG, "ValueMeta.RoundingType.HalfUp"), + BaseMessages.getString(PKG, "ValueMeta.RoundingType.Up"), + }; + // endregion public ValueMetaBase() { @@ -439,6 +464,7 @@ public boolean equals(Object o) { && getPrecision() == that.getPrecision() && type == that.type && trimType == that.trimType + && roundingType == that.roundingType && storageType == that.storageType && collatorStrength == that.collatorStrength && caseInsensitive == that.caseInsensitive @@ -469,6 +495,7 @@ public int hashCode() { precision, type, trimType, + roundingType, storageType, origin, comments, @@ -1309,6 +1336,40 @@ public synchronized DecimalFormat getDecimalFormat(boolean useBigDecimal) { decimalFormat.setParseBigDecimal(useBigDecimal); DecimalFormatSymbols decimalFormatSymbols = decimalFormat.getDecimalFormatSymbols(); + // Set the Rounding mode + + if (roundingType != null) { + switch (roundingType) { + case "unnecessary": + decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); + break; + case "ceiling": + decimalFormat.setRoundingMode(RoundingMode.CEILING); + break; + case "down": + decimalFormat.setRoundingMode(RoundingMode.DOWN); + break; + case "floor": + decimalFormat.setRoundingMode(RoundingMode.FLOOR); + break; + case "half_down": + decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); + break; + case "half_even": + decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); + break; + case "half_up": + decimalFormat.setRoundingMode(RoundingMode.HALF_UP); + break; + case "up": + decimalFormat.setRoundingMode(RoundingMode.UP); + break; + default: + decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); + break; + } + } + if (!Utils.isEmpty(currencySymbol)) { decimalFormatSymbols.setCurrencySymbol(currencySymbol); } @@ -4958,6 +5019,26 @@ public static final String getTrimTypeDesc(int i) { return trimTypeDesc[i]; } + public static final String getRoundingTypeDesc(String code) { + for (int i = 0; i < roundingTypeCode.length; i++) { + if (roundingTypeCode[i].equalsIgnoreCase(code)) { + return roundingTypeDesc[i]; + } + } + // return half_even as default + return roundingTypeDesc[5]; + } + + public static final String getRoundingTypeCode(String desc) { + for (int i = 0; i < roundingTypeDesc.length; i++) { + if (roundingTypeCode[i].equalsIgnoreCase(desc)) { + return roundingTypeCode[i]; + } + } + // return half_even as default + return roundingTypeCode[5]; + } + /** * @return the conversionMetadata */ diff --git a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaFactory.java b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaFactory.java index 850a4924ee2..cb0b8194c81 100644 --- a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaFactory.java +++ b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaFactory.java @@ -94,6 +94,7 @@ public static void cloneInfo(IValueMeta source, IValueMeta target) throws HopPlu } target.setStringEncoding(source.getStringEncoding()); target.setTrimType(source.getTrimType()); + target.setRoundingType(source.getRoundingType()); target.setDateFormatLenient(source.isDateFormatLenient()); target.setDateFormatLocale(source.getDateFormatLocale()); target.setDateFormatTimeZone(source.getDateFormatTimeZone()); diff --git a/core/src/main/resources/org/apache/hop/core/messages/messages_en_US.properties b/core/src/main/resources/org/apache/hop/core/messages/messages_en_US.properties index 049e4689ca4..249eb045dca 100644 --- a/core/src/main/resources/org/apache/hop/core/messages/messages_en_US.properties +++ b/core/src/main/resources/org/apache/hop/core/messages/messages_en_US.properties @@ -103,3 +103,11 @@ ValueMeta.TrimType.Both=both ValueMeta.TrimType.Left=left ValueMeta.TrimType.None=none ValueMeta.TrimType.Right=right +ValueMeta.RoundingType.Ceiling=Ceiling +ValueMeta.RoundingType.Down=Down +ValueMeta.RoundingType.Floor=Floor +ValueMeta.RoundingType.HalfDown=Half Down +ValueMeta.RoundingType.HalfEven=Half Even +ValueMeta.RoundingType.HalfUp=Half Up +ValueMeta.RoundingType.Unnecessary=Unnecessary +ValueMeta.RoundingType.Up=Up diff --git a/docs/hop-user-manual/modules/ROOT/pages/pipeline/transforms/textfileoutput.adoc b/docs/hop-user-manual/modules/ROOT/pages/pipeline/transforms/textfileoutput.adoc index 69c5f4ce82d..195430529ce 100644 --- a/docs/hop-user-manual/modules/ROOT/pages/pipeline/transforms/textfileoutput.adoc +++ b/docs/hop-user-manual/modules/ROOT/pages/pipeline/transforms/textfileoutput.adoc @@ -144,8 +144,11 @@ See Number Formats for a complete description of format symbols. |Trim type|The trimming method to apply on the string. Note: Trimming only works when there is no field length given. |Null|If the value of the field is null, insert this string into the textfile +|Rounding Type| When writing numbers to a file you can specify the Rounding type being used by default `Half Even` is used, for more information look at the Rounding Types section |Get|Click to retrieve the list of fields from the input fields stream(s) |Minimal width|Alter the options in the fields tab in such a way that the resulting width of lines in the text file is minimal. So instead of save 0000001, we write 1, etc. String fields will no longer be padded to their specified length. |=== + +include::../../snippets/rounding-types.adoc[] \ No newline at end of file diff --git a/docs/hop-user-manual/modules/ROOT/pages/snippets/rounding-types.adoc b/docs/hop-user-manual/modules/ROOT/pages/snippets/rounding-types.adoc new file mode 100644 index 00000000000..634f6bd5fb0 --- /dev/null +++ b/docs/hop-user-manual/modules/ROOT/pages/snippets/rounding-types.adoc @@ -0,0 +1,69 @@ +//// +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +//// + +=== Rounding Types + +Rounding on Number and BigNumber data type fields is based on https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html[Java Rounding Mode] + +By default, rounding mode `Half Even` is used this Rounding mode will round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. + +Example: Rounding from 1 to 0 digits + +5.5 -> 6 + +2.5 -> 2 + +-2.5 -> -2 + +-5.5 -> -6 + +==== Unnecessary +Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. This mode will throw an error when you try to reduce the precision of a number + +==== Ceiling +Rounding mode to round towards positive infinity. + +==== Down +Rounding mode to round towards zero. + +==== Floor +Rounding mode to round towards negative infinity. + +==== Half Down +Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. + +==== Half Even +Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. + +==== Half Up +Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. + +==== Up +Rounding mode to round away from zero. + +==== Examples + +[options="header"] +|=== +| Input Number | Up |Down |Ceiling |Floor |Half Up |Half Down | Half Even | Unnecessary +|5.5|6|5|6|5|6|5|6|throw ArithmeticException +|2.5|3|2|3|2|3|2|2|throw ArithmeticException +|1.6|2|1|2|1|2|2|2|throw ArithmeticException +|1.1|2|1|2|1|1|1|1|throw ArithmeticException +|1.0|1|1|1|1|1|1|1|1 +|-1.0|-1|-1|-1|-1|-1|-1|-1|-1 +|-1.1|-2|-1|-1|-2|-1|-1|-1|throw ArithmeticException +|-1.6|-2|-1|-1|-2|-2|-2|-2|throw ArithmeticException +|-2.5|-3|-2|-3|-3|-3|-2|-2|throw ArithmeticException +|-5.5|-6|-5|-6|-6|-6|-5|-6|throw ArithmeticException +|=== \ No newline at end of file diff --git a/plugins/misc/static-schema/src/main/java/org/apache/hop/staticschema/metadata/SchemaDefinitionEditor.java b/plugins/misc/static-schema/src/main/java/org/apache/hop/staticschema/metadata/SchemaDefinitionEditor.java index a32d7a17c64..1fc4b5bef2a 100644 --- a/plugins/misc/static-schema/src/main/java/org/apache/hop/staticschema/metadata/SchemaDefinitionEditor.java +++ b/plugins/misc/static-schema/src/main/java/org/apache/hop/staticschema/metadata/SchemaDefinitionEditor.java @@ -201,7 +201,12 @@ public void createControl(Composite parent) { new ColumnInfo( BaseMessages.getString(PKG, "SchemaDefinitionDialog.Fields.Column.FieldComment"), ColumnInfo.COLUMN_TYPE_TEXT, - false) + false), + new ColumnInfo( + BaseMessages.getString(PKG, "SchemaDefinitionDialog.Fields.Column.RoundingType"), + ColumnInfo.COLUMN_TYPE_CCOMBO, + ValueMetaString.roundingTypeDesc, + true), }; wFields = @@ -257,6 +262,8 @@ public void setWidgetsContent() { item.setText(10, trimType); item.setText(11, Const.NVL(field.getComment(), "")); + String roundingType = ValueMetaBase.getRoundingTypeDesc(field.getRoundingType()); + item.setText(12, roundingType); } } @@ -283,6 +290,7 @@ public void getWidgetsContent(SchemaDefinition SchemaDefinition) { sfd.setIfNullValue(item.getText(9)); sfd.setTrimType(ValueMetaString.getTrimTypeByDesc(item.getText(10))); sfd.setComment(item.getText(11)); + sfd.setRoundingType(ValueMetaBase.getRoundingTypeCode(item.getText(11))); SchemaDefinition.getFieldDefinitions().add(sfd); } } diff --git a/plugins/misc/static-schema/src/main/java/org/apache/hop/staticschema/metadata/SchemaFieldDefinition.java b/plugins/misc/static-schema/src/main/java/org/apache/hop/staticschema/metadata/SchemaFieldDefinition.java index 18a783fa316..b8a5699cbb6 100644 --- a/plugins/misc/static-schema/src/main/java/org/apache/hop/staticschema/metadata/SchemaFieldDefinition.java +++ b/plugins/misc/static-schema/src/main/java/org/apache/hop/staticschema/metadata/SchemaFieldDefinition.java @@ -18,11 +18,15 @@ package org.apache.hop.staticschema.metadata; import java.io.Serializable; +import lombok.Getter; +import lombok.Setter; import org.apache.hop.core.exception.HopPluginException; import org.apache.hop.core.row.IValueMeta; import org.apache.hop.core.row.value.ValueMetaFactory; import org.apache.hop.metadata.api.HopMetadataProperty; +@Getter +@Setter public class SchemaFieldDefinition implements Serializable { @HopMetadataProperty private String name; @@ -47,6 +51,8 @@ public class SchemaFieldDefinition implements Serializable { @HopMetadataProperty private String comment; + @HopMetadataProperty private String roundingType; + public SchemaFieldDefinition() {} public SchemaFieldDefinition(String name, String hopType) { @@ -65,6 +71,7 @@ public IValueMeta getValueMeta() throws HopPluginException { || valueMeta.getType() == IValueMeta.TYPE_TIMESTAMP) { valueMeta.setConversionMask(formatMask); + valueMeta.setRoundingType(roundingType); } if (valueMeta.getType() == IValueMeta.TYPE_NUMBER @@ -79,132 +86,4 @@ public IValueMeta getValueMeta() throws HopPluginException { return valueMeta; } - - /** - * Gets name - * - * @return value of name - */ - public String getName() { - return name; - } - - /** - * @param name The name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * Gets hopType - * - * @return value of hopType - */ - public String getHopType() { - return hopType; - } - - /** - * @param hopType The hopType to set - */ - public void setHopType(String hopType) { - this.hopType = hopType; - } - - /** - * Gets length - * - * @return value of length - */ - public int getLength() { - return length; - } - - /** - * @param length The length to set - */ - public void setLength(int length) { - this.length = length; - } - - /** - * Gets precision - * - * @return value of precision - */ - public int getPrecision() { - return precision; - } - - /** - * @param precision The precision to set - */ - public void setPrecision(int precision) { - this.precision = precision; - } - - /** - * Gets formatMask - * - * @return value of formatMask - */ - public String getFormatMask() { - return formatMask; - } - - /** - * @param formatMask The formatMask to set - */ - public void setFormatMask(String formatMask) { - this.formatMask = formatMask; - } - - public String getCurrencySymbol() { - return currencySymbol; - } - - public void setCurrencySymbol(String currencySymbol) { - this.currencySymbol = currencySymbol; - } - - public String getDecimalSymbol() { - return decimalSymbol; - } - - public void setDecimalSymbol(String decimalSymbol) { - this.decimalSymbol = decimalSymbol; - } - - public String getGroupingSymbol() { - return groupingSymbol; - } - - public void setGroupingSymbol(String groupingSymbol) { - this.groupingSymbol = groupingSymbol; - } - - public int getTrimType() { - return trimType; - } - - public void setTrimType(int trimType) { - this.trimType = trimType; - } - - public String getIfNullValue() { - return ifNullValue; - } - - public void setIfNullValue(String ifNullValue) { - this.ifNullValue = ifNullValue; - } - - public String getComment() { - return comment; - } - - public void setComment(String comment) { - this.comment = comment; - } } diff --git a/plugins/misc/static-schema/src/main/resources/org/apache/hop/staticschema/metadata/messages/messages_en_US.properties b/plugins/misc/static-schema/src/main/resources/org/apache/hop/staticschema/metadata/messages/messages_en_US.properties index cff3f8ed778..8d8d4b299e7 100644 --- a/plugins/misc/static-schema/src/main/resources/org/apache/hop/staticschema/metadata/messages/messages_en_US.properties +++ b/plugins/misc/static-schema/src/main/resources/org/apache/hop/staticschema/metadata/messages/messages_en_US.properties @@ -32,6 +32,7 @@ SchemaDefinitionDialog.Fields.Column.FieldLength=Length SchemaDefinitionDialog.Fields.Column.FieldName=Field name SchemaDefinitionDialog.Fields.Column.FieldPrecision=Precision SchemaDefinitionDialog.Fields.Column.FieldTrimType=Trim Type +SchemaDefinitionDialog.Fields.Column.RoundingType=Rounding Type SchemaDefinitionDialog.Fields.Column.FieldType=Type SchemaDefinitionDialog.Fields.Label=Field definitions : SchemaDefinitionDialog.GetFields.Button=Get Fields diff --git a/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileField.java b/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileField.java index db4b7e051ee..77743ec404e 100644 --- a/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileField.java +++ b/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileField.java @@ -17,11 +17,15 @@ package org.apache.hop.pipeline.transforms.textfileoutput; +import lombok.Getter; +import lombok.Setter; import org.apache.hop.core.injection.Injection; import org.apache.hop.core.row.value.ValueMetaBase; import org.apache.hop.core.row.value.ValueMetaFactory; /** Describes a single field in a text file */ +@Getter +@Setter public class TextFileField implements Cloneable { @Injection(name = "OUTPUT_FIELDNAME", group = "OUTPUT_FIELDS") private String name; @@ -51,6 +55,8 @@ public class TextFileField implements Cloneable { private int trimType; + @Getter @Setter private String roundingType; + public TextFileField( String name, int type, @@ -60,7 +66,8 @@ public TextFileField( String currencySymbol, String decimalSymbol, String groupSymbol, - String nullString) { + String nullString, + String roundingType) { this.name = name; this.type = type; this.format = format; @@ -70,6 +77,7 @@ public TextFileField( this.decimalSymbol = decimalSymbol; this.groupingSymbol = groupSymbol; this.nullString = nullString; + this.roundingType = roundingType; } public TextFileField() {} @@ -96,26 +104,6 @@ public Object clone() { } } - public int getLength() { - return length; - } - - public void setLength(int length) { - this.length = length; - } - - public String getName() { - return name; - } - - public void setName(String fieldname) { - this.name = fieldname; - } - - public int getType() { - return type; - } - public String getTypeDesc() { return ValueMetaFactory.getValueMetaName(type); } @@ -129,67 +117,11 @@ public void setType(String typeDesc) { this.type = ValueMetaFactory.getIdForValueMeta(typeDesc); } - public String getFormat() { - return format; - } - - public void setFormat(String format) { - this.format = format; - } - - public String getGroupingSymbol() { - return groupingSymbol; - } - - public void setGroupingSymbol(String groupSymbol) { - this.groupingSymbol = groupSymbol; - } - - public String getDecimalSymbol() { - return decimalSymbol; - } - - public void setDecimalSymbol(String decimalSymbol) { - this.decimalSymbol = decimalSymbol; - } - - public String getCurrencySymbol() { - return currencySymbol; - } - - public void setCurrencySymbol(String currencySymbol) { - this.currencySymbol = currencySymbol; - } - - public int getPrecision() { - return precision; - } - - public void setPrecision(int precision) { - this.precision = precision; - } - - public String getNullString() { - return nullString; - } - - public void setNullString(String nullString) { - this.nullString = nullString; - } - @Override public String toString() { return name + ":" + getTypeDesc(); } - public int getTrimType() { - return trimType; - } - - public void setTrimType(int trimType) { - this.trimType = trimType; - } - @Injection(name = "OUTPUT_TRIM", group = "OUTPUT_FIELDS") public void setTrimTypeByDesc(String value) { this.trimType = ValueMetaBase.getTrimTypeByDesc(value); diff --git a/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputDialog.java b/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputDialog.java index 89e683122a2..732e1252b99 100644 --- a/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputDialog.java +++ b/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputDialog.java @@ -1149,7 +1149,12 @@ public void widgetSelected(SelectionEvent e) { new ColumnInfo( BaseMessages.getString(PKG, "TextFileOutputDialog.NullColumn.Column"), ColumnInfo.COLUMN_TYPE_TEXT, - false) + false), + new ColumnInfo( + BaseMessages.getString(PKG, "TextFileOutputDialog.RoundingTypeColumn.Column"), + ColumnInfo.COLUMN_TYPE_CCOMBO, + ValueMetaBase.roundingTypeDesc, + true), }; wFields = @@ -1295,6 +1300,9 @@ private void fillFieldsLayoutFromSchema() { item.setText( 9, Const.NVL(ValueMetaBase.getTrimTypeDesc(valueMeta.getTrimType()), "")); item.setText(10, Const.NVL(schemaFieldDefinition.getIfNullValue(), "")); + item.setText( + 11, + ValueMetaBase.getRoundingTypeDesc(schemaFieldDefinition.getRoundingType())); } } } @@ -1549,6 +1557,10 @@ public void getData() { if (field.getNullString() != null) { item.setText(10, field.getNullString()); } + String roundingType = ValueMetaBase.getRoundingTypeDesc(field.getRoundingType()); + if (roundingType != null) { + item.setText(11, roundingType); + } } wFields.optWidth(true); @@ -1618,7 +1630,7 @@ protected void saveInfoInMeta(TextFileOutputMeta tfoi) { field.setGroupingSymbol(item.getText(8)); field.setTrimType(ValueMetaBase.getTrimTypeByDesc(item.getText(9))); field.setNullString(item.getText(10)); - + field.setRoundingType(ValueMetaBase.getRoundingTypeCode(item.getText(11))); tfoi.getOutputFields()[i] = field; } } diff --git a/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputMeta.java b/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputMeta.java index 9086e160f29..06fbd92665b 100644 --- a/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputMeta.java +++ b/plugins/transforms/textfile/src/main/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputMeta.java @@ -717,6 +717,8 @@ protected void readData(Node transformNode, IHopMetadataProvider metadataProvide outputFields[i].setGroupingSymbol(XmlHandler.getTagValue(fnode, "group")); outputFields[i].setTrimType( ValueMetaString.getTrimTypeByCode(XmlHandler.getTagValue(fnode, "trim_type"))); + outputFields[i].setRoundingType( + Const.NVL(XmlHandler.getTagValue(fnode, "roundingType"), "half_even")); outputFields[i].setNullString(XmlHandler.getTagValue(fnode, "nullif")); outputFields[i].setLength(Const.toInt(XmlHandler.getTagValue(fnode, "length"), -1)); outputFields[i].setPrecision(Const.toInt(XmlHandler.getTagValue(fnode, "precision"), -1)); @@ -854,6 +856,7 @@ public void getFields( v.setCurrencySymbol(field.getCurrencySymbol()); v.setOutputPaddingEnabled(isPadded()); v.setTrimType(field.getTrimType()); + v.setRoundingType(field.getRoundingType()); if (!Utils.isEmpty(getEncoding())) { v.setStringEncoding(getEncoding()); } @@ -917,6 +920,9 @@ public String getXml() { retval .append(CONST_SPACES_LONG) .append(XmlHandler.addTagValue("trim_type", field.getTrimTypeCode())); + retval + .append(CONST_SPACES_LONG) + .append(XmlHandler.addTagValue("roundingType", field.getRoundingType())); retval .append(CONST_SPACES_LONG) .append(XmlHandler.addTagValue("length", field.getLength())); diff --git a/plugins/transforms/textfile/src/main/resources/org/apache/hop/pipeline/transforms/textfileoutput/messages/messages_en_US.properties b/plugins/transforms/textfile/src/main/resources/org/apache/hop/pipeline/transforms/textfileoutput/messages/messages_en_US.properties index 31b99c704c4..6e2e789378f 100644 --- a/plugins/transforms/textfile/src/main/resources/org/apache/hop/pipeline/transforms/textfileoutput/messages/messages_en_US.properties +++ b/plugins/transforms/textfile/src/main/resources/org/apache/hop/pipeline/transforms/textfileoutput/messages/messages_en_US.properties @@ -135,6 +135,7 @@ TextFileOutputDialog.SpecifyFormat.Label=Specify Date time format TextFileOutputDialog.SpecifyFormat.Tooltip=Specify Date time format TextFileOutputDialog.SplitEvery.Label=Split every ... rows TextFileOutputDialog.TrimTypeColumn.Column=Trim Type +TextFileOutputDialog.RoundingTypeColumn.Column=Rounding Type TextFileOutputDialog.TypeColumn.Column=Type TextFileOutputMeta.CheckResult.AllFieldsFound=All output fields are found in the input stream. TextFileOutputMeta.CheckResult.ExpectedInputError=No input received from other transforms\! diff --git a/plugins/transforms/textfile/src/test/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputMetaTest.java b/plugins/transforms/textfile/src/test/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputMetaTest.java index d371856bf67..ece4e7236d6 100644 --- a/plugins/transforms/textfile/src/test/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputMetaTest.java +++ b/plugins/transforms/textfile/src/test/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputMetaTest.java @@ -217,7 +217,8 @@ public TextFileField getTestObject() { currencySymbol, decimalSymbol, groupSymbol, - nullString); + nullString, + "floor"); } @Override diff --git a/plugins/transforms/textfile/src/test/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputTest.java b/plugins/transforms/textfile/src/test/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputTest.java index de37228ae7a..aef0d133730 100644 --- a/plugins/transforms/textfile/src/test/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputTest.java +++ b/plugins/transforms/textfile/src/test/java/org/apache/hop/pipeline/transforms/textfileoutput/TextFileOutputTest.java @@ -131,7 +131,16 @@ public void logError(String message, Object... arguments) { private TransformMockHelper transformMockHelper; private TextFileField textFileField = new TextFileField( - "Name", 2, EMPTY_STRING, 10, 20, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING); + "Name", + 2, + EMPTY_STRING, + 10, + 20, + EMPTY_STRING, + EMPTY_STRING, + EMPTY_STRING, + EMPTY_STRING, + "floor"); private TextFileField textFileField2 = new TextFileField( "Surname", @@ -142,7 +151,8 @@ public void logError(String message, Object... arguments) { EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, - EMPTY_STRING); + EMPTY_STRING, + "floor"); private TextFileField[] textFileFields = new TextFileField[] {textFileField, textFileField2}; private Object[] row = new Object[] {"some data", "another data"}; private Object[] row2 = new Object[] {"some data2", "another data2"};