Java导出数据到(自定义格式)文本文件

时间:2012-01-24 22:26:17

标签: java backend data-export

我被要求处理这个后端预定作业,该作业将一些客户数据(从电子商务数据库)导出到自定义格式文本文件。接下来的代码就是我找到的。

我只是想删除它,但我不能。我是否有可能在不改变它的情况下改进它?

public class AConverter implements CustomerConverter {

    protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);

    private final static String SEPARATOR = ";";
    private final static String CR = "\n";

    public String create(Customer customer) {

        if (customer == null)
            return null;

        LOG.info("Exporting customer, uidpk: {}, userid: {}", customer.getUidPk(), customer.getUserId());

        StringBuilder buf = new StringBuilder();

        buf.append("<HEAD>");
        buf.append(SEPARATOR);
        buf.append(String.valueOf(customer.getUidPk()));
        buf.append(SEPARATOR);
        byte[] fullName = null;
        try {
            fullName = customer.getFullName().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e1) {
            fullName = customer.getFullName().getBytes();
        }
        String name = null;
        try {
            name = new String(fullName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            name = customer.getFullName();
        }
        buf.append(limitString(name, 40));
        buf.append(SEPARATOR);
        final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
        if (preferredShippingAddress != null) {
            final String street1 = preferredShippingAddress.getStreet1();
            if (street1 != null) {
                buf.append(limitString(street1, 40));
            }
        } else {
            buf.append(" ");
        }
        buf.append(SEPARATOR);

        final String addressStr = buildAddressString(customer);
        buf.append(limitString(addressStr, 40));
        buf.append(SEPARATOR);
        buf.append(limitString(customer.getEmail(), 80));
        buf.append(SEPARATOR);
        if (preferredShippingAddress!=null && preferredShippingAddress.getStreet2() != null) {
            buf.append(limitString(preferredShippingAddress.getStreet2(), 40));
        } else {
            buf.append(" ");
        }
        buf.append(SEPARATOR);
        buf.append(limitString(customer.getPhoneNumber(), 25));
        buf.append(SEPARATOR);
        if (preferredShippingAddress!=null) {
            if(preferredShippingAddress.getCountry()!=null) {
                buf.append(preferredShippingAddress.getCountry());
            } else {
                buf.append(" ");
            }
        } else {
            buf.append(" ");
        }
        buf.append(SEPARATOR);
        if (preferredShippingAddress!=null) {
            if(preferredShippingAddress.getCountry()!=null) {
                buf.append(preferredShippingAddress.getCountry());
            } else {
                buf.append(" ");
            }
        } else {
            buf.append(" ");
        }
        buf.append(SEPARATOR);

        String fodselsnummer = " ";
        try {
            Map<String, AttributeValue> profileValueMap = customer.getProfileValueMap();
            AttributeValue attributeValue = profileValueMap.get("CODE");
            fodselsnummer = attributeValue.getStringValue();
        } catch (Exception e) {
        }
        buf.append(fodselsnummer);
        buf.append(CR);
        final String string = buf.toString();

        return string;

    }

    private String buildAddressString(Customer customer) {
        final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
        if (preferredShippingAddress != null) {
            final String zipOrPostalCode = preferredShippingAddress.getZipOrPostalCode();
            final String city = preferredShippingAddress.getCity();
            if (zipOrPostalCode != null && city != null) {
                return zipOrPostalCode + " " + city;
            } else if(zipOrPostalCode == null && city != null) {
                return city;
            } else if(zipOrPostalCode != null && city == null) {
                return zipOrPostalCode;
            }
        }
        return " ";
    }

    private String limitString(String value, int numOfChars) {
        if (value != null && value.length() > numOfChars)
            return value.substring(0, numOfChars);
        else
            return value;
    }

}

1 个答案:

答案 0 :(得分:0)

你说你想改进它,你想删除它,但你不能。我不确定你为什么不能。我也不明白你为什么要删除它。但在我看到Martin Fowler的 Refactoring 之前,这听起来像我曾经拥有的态度。如果你还没有,我强烈建议你阅读那本书。

当然可以在不重写所有内容的情况下改进此代码(或任何代码)。最明显的改进是通过创建一些实用程序方法来消除create方法中的一些重复代码,然后将create方法拆分为几个较小的方法 - 模板方法。

此外,create方法中存在一些可疑的代码,它将客户的名称转换为UTF-8字节流,然后再转换为字符串。我无法想象这是为了什么。最后,如果customer为null,则返回null。这不太可能是必要的或明智的。

为了好玩,我决定对这段代码进行一些重构。 (请注意,正确的重构涉及单元测试;我没有对此代码进行任何测试,甚至没有编译下面的代码,更不用说测试它了。)以下是一种可以重写此代码的方法:

public class AConverter implements CustomerConverter {
    protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);

    private final static String SEPARATOR = ";";
    private final static String CR = "\n";

    public String create(Customer customer) {
        if (customer == null) throw new IllegalArgumentException("no cust");

        LOG.info("Exporting customer, uidpk: {}, userid: {}",
                customer.getUidPk(), customer.getUserId());

        StringBuilder buf = new StringBuilder();
        doHead(buf, customer);
        doAddress(buf, customer);
        doTail(buf, customer);
        return buf.toString();
    }

    private void doHead(StringBuilder buf, Customer customer) {
        append(buf, "<HEAD>");
        append(buf, String.valueOf(customer.getUidPk()));
        append(buf, limitTo(40, customer.getFullName()));
    }

    private void doAddress(StringBuilder buf, Customer customer) {
        append(buf, limitTo(40, street1of(customer)));
        append(buf, limitTo(40, addressOf(customer)));
        append(buf, limitTo(80, customer.getEmail()));
        append(buf, limitTo(40, street2of(customer)));
        append(buf, limitTo(25, customer.getPhoneNumber()));
        append(buf, countryOf(customer));
        append(buf, countryOf(customer));
    }

    private void doTail(StringBuilder buf, Customer customer) {
        buf.append(fodselsnummerOf(customer));
        buf.append(CR);
    }

    private void append(StringBuilder buf, String s) {
        buf.append(s).append(SEPARATOR);
    }

    private String street1of(Customer customer) {
        final CustomerAddress shipto = customer.getPreferredShippingAddress();
        if (shipto == null) return " ";
        if (shipto.getStreet1() != null) return shipto.getStreet1();
        return " ";
    }

    private String street2of(Customer customer) {
        final CustomerAddress shipto = customer.getPreferredShippingAddress();
        if (shipto == null) return " ";
        if (shipto.getStreet2() != null) return shipto.getStreet2();
        return " ";
    }

    private String addressOf(Customer customer) {
        final CustomerAddress shipto = customer.getPreferredShippingAddress();
        if (shipto == null) return " ";

        final String post = preferredShippingAddress.getZipOrPostalCode();
        final String city = preferredShippingAddress.getCity();

        if (post != null && city != null) return post + " " + city;
        if (post == null && city != null) return city;
        if (post != null && city == null) return post;
        return " ";
    }

    private String countryOf(Customer customer) {
        final CustomerAddress shipto = customer.getPreferredShippingAddress();
        if (shipto == null) return " ";
        if (shipto.getCountry() != null) return shipto.getCountry();
        return " ";
    }

    private String limitTo(int numOfChars, String value) {
        if (value != null && value.length() > numOfChars)
            return value.substring(0, numOfChars);
        return value;
    }

    private String fodelsnummerOf(Customer customer) {
        try {
            Map<String, AttributeValue> profileValueMap =
                customer.getProfileValueMap();
            AttributeValue attributeValue = profileValueMap.get("CODE");
            return attributeValue.getStringValue();
        } catch (Exception e) {
            return " ";
        }
    }
}

我还注意到,如果客户数据的任何字段(例如电子邮件地址)中碰巧有分号,则自定义格式文本文件的格式存在问题,因为这是您的分隔符字符。我相信这是一个众所周知的问题?