Search in sources :

Example 1 with TypedIdentifier

use of com.revolsys.identifier.TypedIdentifier in project com.revolsys.open by revolsys.

the class XbaseRecordWriter method writeField.

protected boolean writeField(final Record record, final XBaseFieldDefinition field) throws IOException {
    if (this.out == null) {
        return true;
    } else {
        final String fieldName = field.getFullName();
        Object value;
        if (isWriteCodeValues()) {
            value = record.getCodeValue(fieldName);
        } else {
            value = record.getValue(fieldName);
        }
        if (value instanceof SingleIdentifier) {
            final SingleIdentifier identifier = (SingleIdentifier) value;
            value = identifier.getValue(0);
        } else if (value instanceof TypedIdentifier) {
            final TypedIdentifier identifier = (TypedIdentifier) value;
            value = identifier.getIdentifier().getValue(0);
        }
        final int fieldLength = field.getLength();
        switch(field.getType()) {
            case XBaseFieldDefinition.NUMBER_TYPE:
                String numString = "";
                final DecimalFormat numberFormat = field.getNumberFormat();
                if (value == null) {
                    if (this.useZeroForNull) {
                        numString = numberFormat.format(0);
                    }
                } else {
                    if (value instanceof Number) {
                        Number number = (Number) value;
                        final int decimalPlaces = field.getDecimalPlaces();
                        if (decimalPlaces >= 0) {
                            if (number instanceof BigDecimal) {
                                final BigDecimal bigDecimal = new BigDecimal(number.toString());
                                number = bigDecimal.setScale(decimalPlaces, RoundingMode.HALF_UP);
                            } else if (number instanceof Double || number instanceof Float) {
                                final double doubleValue = number.doubleValue();
                                final double precisionScale = field.getPrecisionScale();
                                number = Doubles.makePrecise(precisionScale, doubleValue);
                            }
                        }
                        numString = numberFormat.format(number);
                    } else {
                        throw new IllegalArgumentException("Not a number " + fieldName + "=" + value);
                    }
                }
                final byte[] numberBytes = numString.getBytes();
                final int numLength = numberBytes.length;
                if (numLength > fieldLength) {
                    for (int i = 0; i < fieldLength; i++) {
                        this.recordBuffer.put((byte) '9');
                    }
                } else {
                    for (int i = numLength; i < fieldLength; i++) {
                        this.recordBuffer.put((byte) ' ');
                    }
                    this.recordBuffer.put(numberBytes);
                }
                return true;
            case XBaseFieldDefinition.FLOAT_TYPE:
                String floatString = "";
                if (value != null) {
                    floatString = value.toString();
                }
                final byte[] floatBytes = floatString.getBytes();
                final int floatLength = floatBytes.length;
                if (floatLength > fieldLength) {
                    for (int i = 0; i < fieldLength; i++) {
                        this.recordBuffer.put((byte) '9');
                    }
                } else {
                    for (int i = floatLength; i < fieldLength; i++) {
                        this.recordBuffer.put((byte) ' ');
                    }
                    this.recordBuffer.put(floatBytes);
                }
                return true;
            case XBaseFieldDefinition.CHARACTER_TYPE:
                String string = "";
                if (value != null) {
                    final Object value1 = value;
                    string = DataTypes.toString(value1);
                }
                final byte[] stringBytes = string.getBytes(this.charset);
                if (stringBytes.length >= fieldLength) {
                    this.recordBuffer.put(stringBytes, 0, fieldLength);
                } else {
                    this.recordBuffer.put(stringBytes);
                    for (int i = stringBytes.length; i < fieldLength; i++) {
                        this.recordBuffer.put((byte) ' ');
                    }
                }
                return true;
            case XBaseFieldDefinition.DATE_TYPE:
                if (value instanceof Date) {
                    final Date date = (Date) value;
                    final String dateString = Dates.format("yyyyMMdd", date);
                    this.recordBuffer.put(dateString.getBytes());
                } else if (value == null) {
                    this.recordBuffer.put("        ".getBytes());
                } else {
                    final byte[] dateBytes = value.toString().getBytes();
                    this.recordBuffer.put(dateBytes, 0, 8);
                }
                return true;
            case XBaseFieldDefinition.LOGICAL_TYPE:
                boolean logical = false;
                if (value instanceof Boolean) {
                    final Boolean boolVal = (Boolean) value;
                    logical = boolVal.booleanValue();
                } else if (value != null) {
                    logical = Boolean.valueOf(value.toString());
                }
                if (logical) {
                    this.recordBuffer.put((byte) 'T');
                } else {
                    this.recordBuffer.put((byte) 'F');
                }
                return true;
            default:
                return false;
        }
    }
}
Also used : SingleIdentifier(com.revolsys.identifier.SingleIdentifier) DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal) Date(java.util.Date) TypedIdentifier(com.revolsys.identifier.TypedIdentifier)

Aggregations

SingleIdentifier (com.revolsys.identifier.SingleIdentifier)1 TypedIdentifier (com.revolsys.identifier.TypedIdentifier)1 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 Date (java.util.Date)1