2012-01-24 123 views
-2

我被要求處理此後端預定作業,該作業將某些客戶數據(從電子商務數據庫)導出到自定義格式文本文件。下面的代碼是我發現的。Java將數據導出到(自定義格式)文本文件

我只想刪除它,但我不能。我可以在不改變它的情況下改進它嗎?

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; 
    } 

} 
+5

你對代碼有什麼問題?你想改善什麼?我們不會爲您分析代碼。 – madth3

+0

所有那些試圖抓住......那些無效的控制......它看起來很糟糕。我沒有要求你分析它 – user1168098

+0

如果你指出你想要改進的地方,我們可以將它移到Code Review,這將更適合這類問題。 – casperOne

回答

0

你說你想改善它,你想刪除它,但你不能。我不確定你爲什麼不能。我也不明白你爲什麼要刪除它。但在我看來,像我曾經擁有過的那種態度在我讀到Martin Fowler的重構。如果你還沒有,我會強烈建議你閱讀那本書。

無需重寫全部代碼,當然可以改進此代碼(或任何代碼)。最明顯的改進是通過創建一些實用方法來消除create方法中的一些重複代碼,然後將create方法拆分爲幾個更小的方法和模板方法。

另外,在create方法中存在一個有問題的代碼位,它將客戶的名稱轉換爲UTF-8字節流,然後返回到字符串中。我無法想象那是爲了什麼。最後,如果客戶爲空,它將返回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 " "; 
     } 
    } 
} 

我還注意到,如果客戶數據(例如,電子郵件地址)的任何字段恰好在其中包含分號,那麼您的自定義格式文本文件格式存在問題,因爲這是您的分隔符。我相信這是一個已知的問題?

+0

非常感謝@david conrad。那是我想要的那種方法。當我說我想刪除它時,我顯然意味着我不喜歡看到做一些像BeanToString一樣簡單的事情(可能不是正確的方式)。非常感謝您的重構版本。看起來更清晰。我會看看這本書。 – user1168098

+0

儘管如此,@ user1168098? –

+0

顯然「投票」需要15點聲望 – user1168098

相關問題