Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. /*
  2. * commercetools GmbH
  3. *
  4. * Copyright (c) 2006 commercetools
  5. * All rights reserved.
  6. *
  7. * This software is the confidential and proprietary information of commercetools
  8. * ("Confidential Information"). You shall not disclose such Confidential
  9. * Information and shall use it only in accordance with the terms of the
  10. * license agreement you entered into with commercetools.
  11. */
  12. package test;
  13.  
  14. import java.io.File;
  15. import java.io.PrintWriter;
  16. import java.text.DecimalFormat;
  17. import java.time.ZoneId;
  18. import java.time.ZonedDateTime;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.concurrent.TimeUnit;
  22.  
  23. import javax.annotation.Nullable;
  24. import javax.money.NumberValue;
  25.  
  26. import io.sphere.sdk.client.BlockingSphereClient;
  27. import io.sphere.sdk.client.SphereClient;
  28. import io.sphere.sdk.client.SphereClientFactory;
  29. import io.sphere.sdk.orders.Order;
  30. import io.sphere.sdk.orders.queries.OrderQuery;
  31. import io.sphere.sdk.payments.Payment;
  32. import io.sphere.sdk.payments.messages.PaymentTransactionStateChangedMessage;
  33. import io.sphere.sdk.payments.queries.PaymentByIdGet;
  34. import io.sphere.sdk.queries.PagedQueryResult;
  35.  
  36. public class OrderExport {
  37. private Integer minutesOverlapping;
  38. private static final String MESSAGETYPE = "PaymentTransactionStateChanged";
  39. private static List<PaymentTransactionStateChangedMessage> messages = Collections.emptyList();
  40. private static boolean wasInitialQueried = false;
  41. private static long total = 1l;
  42. private static long offset = 0L;
  43. private static final int RESULTSPERPAGE = 100;
  44. private static final int PAGEOVERLAP = 5;
  45.  
  46. private static OrderQuery orderQuery;
  47. private static BlockingSphereClient client;
  48. private static String lastmodified;
  49. static ZonedDateTime createdAt = ZonedDateTime.of(2017, 02, 26, 00, 00, 00, 00, ZoneId.of("Z"));
  50.  
  51. public static void main(String[] args) {
  52. client = createSphereClient();
  53.  
  54. try {
  55.  
  56. File export = new File("carhartt_orderexport.csv");
  57.  
  58. PrintWriter writer = new PrintWriter(export);
  59. String header = "orderId;orderNumber;orderState;totalNet;totalGross;shippingrate;currency";
  60. writeFile(header, writer);
  61.  
  62. while (offset < total) {
  63.  
  64. List<Order> orders = getOrders();
  65. for (Order order : orders) {
  66. String exportLine = exportOrder(order);
  67. writeFile(exportLine, writer);
  68.  
  69. }
  70. offset = offset + RESULTSPERPAGE;
  71. }
  72. writer.close();
  73. System.out.println("Location of the export:" + export.getAbsolutePath() + "/" + export.getName());
  74. System.out.println("####################done##################");
  75. client.close();
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79.  
  80. }
  81.  
  82. private static void writeFile(String line, PrintWriter writer) {
  83. writer.println(line);
  84. System.out.println(line);
  85. }
  86.  
  87. private static String exportOrder(Order order) {
  88. String orderId = order.getId();
  89. String orderNumber = order.getOrderNumber();
  90. String orderState = order.getOrderState() != null ? order.getOrderState().name() : "";
  91. String totalNet = formatNumber(order.getTaxedPrice().getTotalNet().getNumber());
  92. String totalGross = formatNumber(order.getTaxedPrice().getTotalGross().getNumber());
  93. String currency = order.getTotalPrice().getCurrency().getCurrencyCode();
  94. String shippingrate = order.getShippingInfo() != null ? formatNumber(order.getShippingInfo().getPrice().getNumber()) : "";
  95. String orderLine = String.format("%s;%s;%s;%s;%s;%s;%s;", orderId, orderNumber, orderState, totalNet, totalGross, shippingrate, currency);
  96. return orderLine;
  97. }
  98.  
  99. static String formatNumber(NumberValue value) {
  100. DecimalFormat df = new DecimalFormat("#.00");
  101. return df.format(value.doubleValue());
  102. }
  103.  
  104. private static BlockingSphereClient createSphereClient() {
  105. final SphereClientFactory factory = SphereClientFactory.of();
  106. final SphereClient asyncSphereClient = factory.createClient("xxxx", // replace with your
  107. // project key
  108. "xxx", // replace with your client id
  109. "xxx"); // replace with your client secret
  110. return BlockingSphereClient.of(asyncSphereClient, 30, TimeUnit.SECONDS);
  111.  
  112. }
  113.  
  114. private static List<Order> getOrders() {
  115.  
  116. buildQuery();
  117. final PagedQueryResult<Order> result = client.executeBlocking(orderQuery);
  118. // Get the total workload from first Query
  119. total = result.getTotal();
  120. return result.getResults();
  121. }
  122.  
  123. // Due to eventual consistency messages could be created with a delay. Fetching several minutes prior last Timestamp
  124. private static void buildQuery() {
  125. orderQuery = OrderQuery.of().withSort(m -> m.lastModifiedAt().sort().asc()).withOffset(offset).withLimit(RESULTSPERPAGE);
  126.  
  127. orderQuery = orderQuery.plusExpansionPaths(m -> m.paymentInfo().payments());
  128. orderQuery = orderQuery.plusExpansionPaths(m -> m.shippingInfo().shippingMethod());
  129. // orderQuery = orderQuery.plusPredicates(m -> m.createdAt().isGreaterThan(createdAt));
  130.  
  131. }
  132.  
  133. @Nullable
  134. private static Payment getCorrespondingPayment(final PaymentTransactionStateChangedMessage message) {
  135. final String paymentId = message.getResource().getId();
  136.  
  137. final PaymentByIdGet paymentByIdGet = PaymentByIdGet.of(paymentId);
  138. return client.executeBlocking(paymentByIdGet);
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement