# Orders refused before they start

## What you want to do

The website now sends orders before they are finished: Quin's arrives without an order number, so nothing can name its journey, and order A-16 without the customer's name, so the logging service has nothing to file its lines under. You want such an order stopped before anything is done for it, and to know why.

## The code

The shop is [chapter 35](https://itinera-dev.org/book/java/35-giving-the-rescue-step-a-service-of-its-own.md)'s, with the order's number now an `Optional<String>` in its initial data, since the website may leave it out. An empty number adds no key, as an empty discount doesn't in [chapter 20](https://itinera-dev.org/book/java/20-an-optional-input.md):

Java, from the program [`ch36_orders_refused_before_they_start`](https://github.com/itinera-dev/book/blob/main/java/examples/ch36_orders_refused_before_they_start.java)

```java
/**
 * What the shop knows about an order when its journey starts. An order with no number or no card
 * leaves it out.
 */
record NewOrder(
    Optional<String> order,
    String customer,
    long price,
    Optional<Long> discount,
    Parcel parcel,
    Optional<String> card,
    boolean giftCard) {
  /** An order with a number, without a discount, that isn't a gift card. */
  NewOrder(String order, String customer, long price, Parcel parcel, String card) {
    this(Optional.of(order), customer, price, Optional.empty(), parcel, Optional.of(card), false);
  }

  /** An order without a number or a discount, that isn't a gift card. */
  NewOrder(String customer, long price, Parcel parcel, String card) {
    this(Optional.empty(), customer, price, Optional.empty(), parcel, Optional.of(card), false);
  }
}

```

`Parcel` is [chapter 21](https://itinera-dev.org/book/java/21-deciding-where-a-steps-data-comes-from.md)'s. When the log receives the customer's name, it asks the logging service to file the journey's lines under it, and lets the service's exception out if it refuses:

Java, from the program [`ch36_orders_refused_before_they_start`](https://github.com/itinera-dev/book/blob/main/java/examples/ch36_orders_refused_before_they_start.java)

```java
@Override
public void setGlobalMetadata(String key, Object value) {
  if (key.equals("customer")) service.fileUnder(value instanceof String name ? name : "");
  metadata.put(key, value);
}

```

The faked logging service refuses an empty name with its exception, `NoCustomer`. The rest of the log is [chapter 33](https://itinera-dev.org/book/java/33-giving-every-reporter-what-it-needs.md)'s, with [chapter 35](https://itinera-dev.org/book/java/35-giving-the-rescue-step-a-service-of-its-own.md)'s `detail`.

For each order, `main` first prints a heading, saying what the order lacks:

Java, from the program [`ch36_orders_refused_before_they_start`](https://github.com/itinera-dev/book/blob/main/java/examples/ch36_orders_refused_before_they_start.java)

```java
/** What an order lacks, as a heading for what its journey prints. */
static String heading(NewOrder order) {
  if (order.order().isEmpty()) return order.customer() + "'s order, without a number";
  return "order " + order.order().get() + ", without a customer's name";
}

```

Then it creates the order's instance, runs it, and says why the order went no further at either point. The instance is a `WorkflowInstance<Sync>`, from `org.itineradev.itinera.instance` and `org.itineradev.itinera.workflow`, declared before the `try` so that the run after it can use it:

Java, from the program [`ch36_orders_refused_before_they_start`](https://github.com/itinera-dev/book/blob/main/java/examples/ch36_orders_refused_before_they_start.java)

```java
WorkflowInstance<Sync> instance;
try {
  instance =
      new Orders("email")
          .instance()
          .reportersGlobalMetadata("customer", order.customer())
          .initialData(order)
          .create();
} catch (IllegalStateException error) {
  System.out.println("no instance: " + error.getMessage());
  continue;
}
try {
  followUp(executor.run(instance));
} catch (Refusal refusal) {
  System.out.println("the journey was refused: " + refusal.getMessage());
}
```

`Orders` is the workflow [chapter 23](https://itinera-dev.org/book/java/23-undoing-a-charge-when-the-shipping-fails.md) declares, with [chapter 35](https://itinera-dev.org/book/java/35-giving-the-rescue-step-a-service-of-its-own.md)'s provider for `cards`, `executor` is made before the loop, as in [chapter 24](https://itinera-dev.org/book/java/24-running-many-journeys.md), and `followUp` is [chapter 34](https://itinera-dev.org/book/java/34-saying-which-attempt-took-the-money.md)'s. It prints:

What `ch36_orders_refused_before_they_start` prints, recorded in [`ch36_orders_refused_before_they_start.txt`](https://github.com/itinera-dev/book/blob/main/java/expected/ch36_orders_refused_before_they_start.txt)

```text
Quin's order, without a number:
no instance: The ID generator threw while the instance was created: an order without a number
order A-16, without a customer's name:
the journey was refused: run refused an instance: the global metadata could not be set: the logging service files no line without a customer's name
```

## How it works

1. **An instance isn't created without its journey ID.** The ID generator of [chapter 11](https://itinera-dev.org/book/java/11-naming-a-journey.md) names each journey after its order's number, and throws an `IllegalStateException` when there is none. Quin's order has no number, so `create()` throws one in turn, whose message says that the ID generator threw, followed by the generator's own message. With no instance, there is nothing to run.
2. **An executor can refuse an instance.** `run` throws a `Refusal` for a journey that can't start, as [chapter 24](https://itinera-dev.org/book/java/24-running-many-journeys.md) showed. Before the first event, the executor gives every reporter the journey ID, then the instance's entries of global metadata, as [chapter 33](https://itinera-dev.org/book/java/33-giving-every-reporter-what-it-needs.md) showed. The log threw on `customer`, so the executor refused the journey: the refusal's `getRefused()` is a `Refused.GlobalMetadataNotSet`, its message ends with the message of the log's exception, and `getRefused().getError()` holds the `NoCustomer` itself.
3. **A refused journey never started.** There is no `journey_started`, no step ran, and the bank was never called: unlike Pat's journey in [chapter 31](https://itinera-dev.org/book/java/31-stopping-a-journey-when-its-log-fails.md), aborted after the money was taken, A-16's has nothing to undo. There is no `JourneyResult` either, since there was no journey to end.
4. **The order is still the shop's to deal with.** `main` learns why from what `create()` and `run` threw. Once the website sends what was missing, a new instance runs the order, as [chapter 24](https://itinera-dev.org/book/java/24-running-many-journeys.md) said.

These orders were stopped by what they lacked. A mistake in how the shop's workflow is put together, such as two steps under one name, would stop every order instead, however complete.

## Outcomes

We have shown how an order that can't be named or logged is stopped before its journey starts, and how the program learns why.

| The usual way                                                                                                   | With Itinera                                                                                            |
| --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
| An incomplete request often gets halfway through, past the payment, before something notices the missing field. | An order that can't be named, or whose journey a reporter can't take, is stopped before its first step. |
| Why a request never ran is often only a log line somewhere.                                                     | `create()` throws, and `run` throws a `Refusal`, saying why, to the program that called them.           |
