Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDP-5671 added script to allow bulk cancellation of orders in GBF #126

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/org/broadinstitute/dsm/model/gbf/CancelRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.broadinstitute.dsm.model.gbf;

import com.google.gson.annotations.SerializedName;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few model classes to match the GBF APIs


public class CancelRequest {

@SerializedName("orderNumber")
private String orderNumber;

public CancelRequest(String orderNumber) {
this.orderNumber = orderNumber;
}
}
34 changes: 34 additions & 0 deletions src/main/java/org/broadinstitute/dsm/model/gbf/CancelResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.broadinstitute.dsm.model.gbf;

import com.google.gson.annotations.SerializedName;

public class CancelResponse {

@SerializedName("orderNumber")
private String orderNumber;

@SerializedName("orderStatus")
private String orderStatus;

@SerializedName("success")
private Boolean success;

@SerializedName("errorMessage")
private String errorMessage;

public String getOrderNumber() {
return orderNumber;
}

public String getOrderStatus() {
return orderStatus;
}

public boolean wasSuccessful() {
return Boolean.TRUE.equals(success);
}

public String getErrorMessage() {
return errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.broadinstitute.dsm.util.externalShipper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.broadinstitute.dsm.model.gbf.CancelResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CancelGBFOrderCLI {

private static final Logger logger = LoggerFactory.getLogger(CancelGBFOrderCLI.class);


public static void main(String[] args) {
String cancelUrl = args[0];
String apiKey = args[1];
String ordersFile = args[2];
FileReader ordersReader = null;
List<String> ordersToCancel = new ArrayList<>();

try {
ordersReader = new FileReader(new File(ordersFile));
ordersToCancel = IOUtils.readLines(ordersReader);
} catch(IOException e) {
logger.error("Could read orders from file " + ordersFile,e);
}

if (ordersReader != null) {
CancelResponse cancelResponse = null;
for (String orderNumber : ordersToCancel) {
try {
cancelResponse = GBFRequestUtil.cancelOrder(orderNumber, cancelUrl, apiKey);
if (cancelResponse.wasSuccessful()) {
logger.info("Cancelled order {}", orderNumber);
} else {
logger.error("Could not cancel order {} due to {}",orderNumber, cancelResponse.getErrorMessage());
}
} catch(Exception e) {
logger.error("Could not cancel order {}", orderNumber, e);
}
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.api.client.http.HttpStatusCodes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.NonNull;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -96,15 +97,17 @@ public class GBFRequestUtil implements ExternalShipper {

private static Executor blindTrustEverythingExecutor;

public GBFRequestUtil() {
static {
initTrust();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this change, even if you were calling a static method, you would have had to new up an object, lest you would see an NPE because various static methods assume that blindTrustEverythingExecutor has been initialized. Now it gets initialized automagically.

}

private static synchronized void initTrust() {
try {
if (blindTrustEverythingExecutor == null) {
blindTrustEverythingExecutor = Executor.newInstance(SecurityUtil.buildHttpClient());
}
}
catch (Exception e) {
} catch (Exception e) {
logger.error("Starting up the blindTrustEverythingExecutor ", e);
System.exit(-3);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit scary--a constructor that calls System.exit() can bring down the entire app!

}
}

Expand Down Expand Up @@ -615,4 +618,23 @@ public static void markKitRequestAsOrdered(ArrayList<KitRequest> kitRequests) {

}

public static CancelResponse cancelOrder(String orderNumber, String cancelUrl, String apiKey) throws Exception {
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
CancelRequest cancelRequest = new CancelRequest(orderNumber);
logger.info("About to cancel GBF order {}", orderNumber);
CancelResponse cancelResponse = executePost(CancelResponse.class, cancelUrl, gson.toJson(cancelRequest), apiKey);

if (cancelResponse != null) {
if (cancelResponse.wasSuccessful()) {
logger.info("Cancelled GBF order {}", orderNumber);
} else {
logger.error("Could not cancel GBF order " + orderNumber + " due to " + cancelResponse.getErrorMessage());
}
} else {
logger.error("Got no response after cancelling GBF order " + orderNumber);
}
return cancelResponse;
}


}