Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * commercetools GmbH
- *
- * Copyright (c) 2006 commercetools
- * All rights reserved.
- *
- * This software is the confidential and proprietary information of commercetools
- * ("Confidential Information"). You shall not disclose such Confidential
- * Information and shall use it only in accordance with the terms of the
- * license agreement you entered into with commercetools.
- */
- package test;
- import java.io.File;
- import java.io.PrintWriter;
- import java.text.DecimalFormat;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.util.Collections;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- import javax.annotation.Nullable;
- import javax.money.NumberValue;
- import io.sphere.sdk.client.BlockingSphereClient;
- import io.sphere.sdk.client.SphereClient;
- import io.sphere.sdk.client.SphereClientFactory;
- import io.sphere.sdk.orders.Order;
- import io.sphere.sdk.orders.queries.OrderQuery;
- import io.sphere.sdk.payments.Payment;
- import io.sphere.sdk.payments.messages.PaymentTransactionStateChangedMessage;
- import io.sphere.sdk.payments.queries.PaymentByIdGet;
- import io.sphere.sdk.queries.PagedQueryResult;
- public class OrderExport {
- private Integer minutesOverlapping;
- private static final String MESSAGETYPE = "PaymentTransactionStateChanged";
- private static List<PaymentTransactionStateChangedMessage> messages = Collections.emptyList();
- private static boolean wasInitialQueried = false;
- private static long total = 1l;
- private static long offset = 0L;
- private static final int RESULTSPERPAGE = 100;
- private static final int PAGEOVERLAP = 5;
- private static OrderQuery orderQuery;
- private static BlockingSphereClient client;
- private static String lastmodified;
- static ZonedDateTime createdAt = ZonedDateTime.of(2017, 02, 26, 00, 00, 00, 00, ZoneId.of("Z"));
- public static void main(String[] args) {
- client = createSphereClient();
- try {
- File export = new File("carhartt_orderexport.csv");
- PrintWriter writer = new PrintWriter(export);
- String header = "orderId;orderNumber;orderState;totalNet;totalGross;shippingrate;currency";
- writeFile(header, writer);
- while (offset < total) {
- List<Order> orders = getOrders();
- for (Order order : orders) {
- String exportLine = exportOrder(order);
- writeFile(exportLine, writer);
- }
- offset = offset + RESULTSPERPAGE;
- }
- writer.close();
- System.out.println("Location of the export:" + export.getAbsolutePath() + "/" + export.getName());
- System.out.println("####################done##################");
- client.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void writeFile(String line, PrintWriter writer) {
- writer.println(line);
- System.out.println(line);
- }
- private static String exportOrder(Order order) {
- String orderId = order.getId();
- String orderNumber = order.getOrderNumber();
- String orderState = order.getOrderState() != null ? order.getOrderState().name() : "";
- String totalNet = formatNumber(order.getTaxedPrice().getTotalNet().getNumber());
- String totalGross = formatNumber(order.getTaxedPrice().getTotalGross().getNumber());
- String currency = order.getTotalPrice().getCurrency().getCurrencyCode();
- String shippingrate = order.getShippingInfo() != null ? formatNumber(order.getShippingInfo().getPrice().getNumber()) : "";
- String orderLine = String.format("%s;%s;%s;%s;%s;%s;%s;", orderId, orderNumber, orderState, totalNet, totalGross, shippingrate, currency);
- return orderLine;
- }
- static String formatNumber(NumberValue value) {
- DecimalFormat df = new DecimalFormat("#.00");
- return df.format(value.doubleValue());
- }
- private static BlockingSphereClient createSphereClient() {
- final SphereClientFactory factory = SphereClientFactory.of();
- final SphereClient asyncSphereClient = factory.createClient("xxxx", // replace with your
- // project key
- "xxx", // replace with your client id
- "xxx"); // replace with your client secret
- return BlockingSphereClient.of(asyncSphereClient, 30, TimeUnit.SECONDS);
- }
- private static List<Order> getOrders() {
- buildQuery();
- final PagedQueryResult<Order> result = client.executeBlocking(orderQuery);
- // Get the total workload from first Query
- total = result.getTotal();
- return result.getResults();
- }
- // Due to eventual consistency messages could be created with a delay. Fetching several minutes prior last Timestamp
- private static void buildQuery() {
- orderQuery = OrderQuery.of().withSort(m -> m.lastModifiedAt().sort().asc()).withOffset(offset).withLimit(RESULTSPERPAGE);
- orderQuery = orderQuery.plusExpansionPaths(m -> m.paymentInfo().payments());
- orderQuery = orderQuery.plusExpansionPaths(m -> m.shippingInfo().shippingMethod());
- // orderQuery = orderQuery.plusPredicates(m -> m.createdAt().isGreaterThan(createdAt));
- }
- @Nullable
- private static Payment getCorrespondingPayment(final PaymentTransactionStateChangedMessage message) {
- final String paymentId = message.getResource().getId();
- final PaymentByIdGet paymentByIdGet = PaymentByIdGet.of(paymentId);
- return client.executeBlocking(paymentByIdGet);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement