Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 7.51 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Java export data to (custom-format) text file [closed]
  2. public class AConverter implements CustomerConverter {
  3.  
  4.     protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);
  5.  
  6.     private final static String SEPARATOR = ";";
  7.     private final static String CR = "n";
  8.  
  9.     public String create(Customer customer) {
  10.  
  11.         if (customer == null)
  12.             return null;
  13.  
  14.         LOG.info("Exporting customer, uidpk: {}, userid: {}", customer.getUidPk(), customer.getUserId());
  15.  
  16.         StringBuilder buf = new StringBuilder();
  17.  
  18.         buf.append("<HEAD>");
  19.         buf.append(SEPARATOR);
  20.         buf.append(String.valueOf(customer.getUidPk()));
  21.         buf.append(SEPARATOR);
  22.         byte[] fullName = null;
  23.         try {
  24.             fullName = customer.getFullName().getBytes("UTF-8");
  25.         } catch (UnsupportedEncodingException e1) {
  26.             fullName = customer.getFullName().getBytes();
  27.         }
  28.         String name = null;
  29.         try {
  30.             name = new String(fullName, "UTF-8");
  31.         } catch (UnsupportedEncodingException e) {
  32.             name = customer.getFullName();
  33.         }
  34.         buf.append(limitString(name, 40));
  35.         buf.append(SEPARATOR);
  36.         final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
  37.         if (preferredShippingAddress != null) {
  38.             final String street1 = preferredShippingAddress.getStreet1();
  39.             if (street1 != null) {
  40.                 buf.append(limitString(street1, 40));
  41.             }
  42.         } else {
  43.             buf.append(" ");
  44.         }
  45.         buf.append(SEPARATOR);
  46.  
  47.         final String addressStr = buildAddressString(customer);
  48.         buf.append(limitString(addressStr, 40));
  49.         buf.append(SEPARATOR);
  50.         buf.append(limitString(customer.getEmail(), 80));
  51.         buf.append(SEPARATOR);
  52.         if (preferredShippingAddress!=null && preferredShippingAddress.getStreet2() != null) {
  53.             buf.append(limitString(preferredShippingAddress.getStreet2(), 40));
  54.         } else {
  55.             buf.append(" ");
  56.         }
  57.         buf.append(SEPARATOR);
  58.         buf.append(limitString(customer.getPhoneNumber(), 25));
  59.         buf.append(SEPARATOR);
  60.         if (preferredShippingAddress!=null) {
  61.             if(preferredShippingAddress.getCountry()!=null) {
  62.                 buf.append(preferredShippingAddress.getCountry());
  63.             } else {
  64.                 buf.append(" ");
  65.             }
  66.         } else {
  67.             buf.append(" ");
  68.         }
  69.         buf.append(SEPARATOR);
  70.         if (preferredShippingAddress!=null) {
  71.             if(preferredShippingAddress.getCountry()!=null) {
  72.                 buf.append(preferredShippingAddress.getCountry());
  73.             } else {
  74.                 buf.append(" ");
  75.             }
  76.         } else {
  77.             buf.append(" ");
  78.         }
  79.         buf.append(SEPARATOR);
  80.  
  81.         String fodselsnummer = " ";
  82.         try {
  83.             Map<String, AttributeValue> profileValueMap = customer.getProfileValueMap();
  84.             AttributeValue attributeValue = profileValueMap.get("CODE");
  85.             fodselsnummer = attributeValue.getStringValue();
  86.         } catch (Exception e) {
  87.         }
  88.         buf.append(fodselsnummer);
  89.         buf.append(CR);
  90.         final String string = buf.toString();
  91.  
  92.         return string;
  93.  
  94.     }
  95.  
  96.     private String buildAddressString(Customer customer) {
  97.         final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
  98.         if (preferredShippingAddress != null) {
  99.             final String zipOrPostalCode = preferredShippingAddress.getZipOrPostalCode();
  100.             final String city = preferredShippingAddress.getCity();
  101.             if (zipOrPostalCode != null && city != null) {
  102.                 return zipOrPostalCode + " " + city;
  103.             } else if(zipOrPostalCode == null && city != null) {
  104.                 return city;
  105.             } else if(zipOrPostalCode != null && city == null) {
  106.                 return zipOrPostalCode;
  107.             }
  108.         }
  109.         return " ";
  110.     }
  111.  
  112.     private String limitString(String value, int numOfChars) {
  113.         if (value != null && value.length() > numOfChars)
  114.             return value.substring(0, numOfChars);
  115.         else
  116.             return value;
  117.     }
  118.  
  119. }
  120.        
  121. public class AConverter implements CustomerConverter {
  122.     protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);
  123.  
  124.     private final static String SEPARATOR = ";";
  125.     private final static String CR = "n";
  126.  
  127.     public String create(Customer customer) {
  128.         if (customer == null) throw new IllegalArgumentException("no cust");
  129.  
  130.         LOG.info("Exporting customer, uidpk: {}, userid: {}",
  131.                 customer.getUidPk(), customer.getUserId());
  132.  
  133.         StringBuilder buf = new StringBuilder();
  134.         doHead(buf, customer);
  135.         doAddress(buf, customer);
  136.         doTail(buf, customer);
  137.         return buf.toString();
  138.     }
  139.  
  140.     private void doHead(StringBuilder buf, Customer customer) {
  141.         append(buf, "<HEAD>");
  142.         append(buf, String.valueOf(customer.getUidPk()));
  143.         append(buf, limitTo(40, customer.getFullName()));
  144.     }
  145.  
  146.     private void doAddress(StringBuilder buf, Customer customer) {
  147.         append(buf, limitTo(40, street1of(customer)));
  148.         append(buf, limitTo(40, addressOf(customer)));
  149.         append(buf, limitTo(80, customer.getEmail()));
  150.         append(buf, limitTo(40, street2of(customer)));
  151.         append(buf, limitTo(25, customer.getPhoneNumber()));
  152.         append(buf, countryOf(customer));
  153.         append(buf, countryOf(customer));
  154.     }
  155.  
  156.     private void doTail(StringBuilder buf, Customer customer) {
  157.         buf.append(fodselsnummerOf(customer));
  158.         buf.append(CR);
  159.     }
  160.  
  161.     private void append(StringBuilder buf, String s) {
  162.         buf.append(s).append(SEPARATOR);
  163.     }
  164.  
  165.     private String street1of(Customer customer) {
  166.         final CustomerAddress shipto = customer.getPreferredShippingAddress();
  167.         if (shipto == null) return " ";
  168.         if (shipto.getStreet1() != null) return shipto.getStreet1();
  169.         return " ";
  170.     }
  171.  
  172.     private String street2of(Customer customer) {
  173.         final CustomerAddress shipto = customer.getPreferredShippingAddress();
  174.         if (shipto == null) return " ";
  175.         if (shipto.getStreet2() != null) return shipto.getStreet2();
  176.         return " ";
  177.     }
  178.  
  179.     private String addressOf(Customer customer) {
  180.         final CustomerAddress shipto = customer.getPreferredShippingAddress();
  181.         if (shipto == null) return " ";
  182.  
  183.         final String post = preferredShippingAddress.getZipOrPostalCode();
  184.         final String city = preferredShippingAddress.getCity();
  185.  
  186.         if (post != null && city != null) return post + " " + city;
  187.         if (post == null && city != null) return city;
  188.         if (post != null && city == null) return post;
  189.         return " ";
  190.     }
  191.  
  192.     private String countryOf(Customer customer) {
  193.         final CustomerAddress shipto = customer.getPreferredShippingAddress();
  194.         if (shipto == null) return " ";
  195.         if (shipto.getCountry() != null) return shipto.getCountry();
  196.         return " ";
  197.     }
  198.  
  199.     private String limitTo(int numOfChars, String value) {
  200.         if (value != null && value.length() > numOfChars)
  201.             return value.substring(0, numOfChars);
  202.         return value;
  203.     }
  204.  
  205.     private String fodelsnummerOf(Customer customer) {
  206.         try {
  207.             Map<String, AttributeValue> profileValueMap =
  208.                 customer.getProfileValueMap();
  209.             AttributeValue attributeValue = profileValueMap.get("CODE");
  210.             return attributeValue.getStringValue();
  211.         } catch (Exception e) {
  212.             return " ";
  213.         }
  214.     }
  215. }