> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a first application

> Walkthrough of the Atoti Java SDK datastore sample, which loads trade P&L from CSV files into Datastore stores, builds a cube, and converts P&L to EUR with a Copper lookup on FX rates

The code shown on this page comes directly from the sample application, so it always matches the project. Before
starting, set up the development environment described in [Install](https://docs.activeviam.com/engine/java-sdk/6.2/start/install_and_start.md).

## What does the sample application do?

The application analyzes the profit and loss (P\&L) of a small trading portfolio over one working week.
The week runs from Monday 13 to Friday 17 October 2025.
Each trade has a P\&L in its own currency. The application converts these values to EUR with daily FX rates.

The application has four parts:

* **Dependencies**: Spring Boot starters that provide Atoti Server, Atoti UI and Atoti Admin UI.
* **Data**: three CSV files, loaded into the datastore at startup.
* **Database**: three stores that hold trades, books and FX rates.
* **Cube**: dimensions and measures that turn the stored data into P\&L figures.

## How to build and run the application

The application is a Spring Boot project. The development environment from [Install](https://docs.activeviam.com/engine/java-sdk/6.2/start/install_and_start.md) must be configured first.

1. Build the project. Run the following command from the project root directory.

   ```bash theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
   mvn clean install -DskipTests
   ```

2. Run the `com.activeviam.sample.datastore.AtotiLauncher` main class.

3. Open `http://localhost:9090/ui` in a browser. The Atoti UI login page appears.

The application exposes two interfaces on port 9090.

| Interface | URL |
| - | - |
| Atoti UI | `http://localhost:9090/ui` |
| Atoti Admin UI | `http://localhost:9090/admin/ui` |

The application defines two users by default.

| Username | Password | Access level |
| - | - | - |
| `admin` | `admin` | Full access |
| `user` | `user` | Read-only |

## Which dependencies does the application need?

An Atoti Server application is a Spring Boot application. The sample application declares Atoti starters as Maven dependencies in its `pom.xml` file.
The sample application uses three starters:

* `atoti-server-starter`: required. It provides the API to define the Atoti Datastore and cubes.
* `atoti-ui-starter`: embeds Atoti UI in the running application.
* `atoti-admin-ui-starter`: embeds Atoti Admin UI in the running application.

The following dependency declarations come from the sample `pom.xml` file.

```xml theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
<dependency>
    <groupId>com.activeviam.springboot</groupId>
    <artifactId>atoti-server-starter</artifactId>
    <version>${atoti-server.version}</version>
</dependency>
<dependency>
    <groupId>com.activeviam.springboot</groupId>
    <artifactId>atoti-ui-starter</artifactId>
    <version>${atoti-server.version}</version>
</dependency>
<dependency>
    <groupId>com.activeviam.springboot</groupId>
    <artifactId>atoti-admin-ui-starter</artifactId>
    <version>${atoti-server.version}</version>
</dependency>
```

The Spring Boot version must match the version that the Atoti Java SDK requires.
For the supported versions, see [dependencies](https://docs.activeviam.com/engine/java-sdk/6.2/release/dependencies.md).
For details about the main starter, see [The Atoti Server Starter](https://docs.activeviam.com/engine/java-sdk/6.2/starters/atoti_starter.md).

## Which data does the application load?

The application loads three CSV files at startup. The files are packaged as classpath resources:

* `trades.csv` holds one P\&L value per trade, risk factor, sensitivity and as-of date.
* `books.csv` describes the organization that each book belongs to.
* `fx_rates.csv` holds the daily rate to convert each currency to EUR.

The data is a small extract of a historical value-at-risk dataset. It contains 100 trades across 5 books.
The FX rates are illustrative, not market data.

The `SourceConfig` class defines the file locations as constants, as follows.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
public static final String TRADES_FILE = "data/trades.csv";
public static final String BOOKS_FILE = "data/books.csv";
public static final String FX_RATES_FILE = "data/fx_rates.csv";
```

`SourceConfig` creates one CSV topic per store from these locations.
For more on CSV loading, see [CSV source](https://docs.activeviam.com/engine/java-sdk/6.2/sources/csv_source.md).

## How is the database defined?

The database is the Atoti in-memory Datastore. The `DatastoreConfig` class defines three stores and one reference.

* The `Trades` and `Books` stores are linked by a reference.
* The `FxRates` store is isolated: no reference links it to the other stores.

Each store definition lists its fields and their types, and marks which fields form the store key.

### What does the Trades store hold?

* The `Trades` store is the fact table. Each record holds the P\&L of one trade for one risk factor, sensitivity and as-of date.
* The key fields are `AsOfDate`, `TradeId`, `RiskFactor` and `Sensitivity`.
* The `Currency` field holds the trade currency. The `PnL` field holds the P\&L in that currency.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
return StoreDescription.builder()
    .withStoreName(ModelConstants.TRADES_STORE_NAME)
    .withField(ModelConstants.TRADES_AS_OF_DATE, LOCAL_DATE)
    .asKeyField()
    .withField(ModelConstants.TRADES_TRADE_ID, STRING)
    .asKeyField()
    .withField(ModelConstants.TRADES_BOOK_ID, INT)
    .withField(ModelConstants.TRADES_RISK_CLASS, STRING)
    .withField(ModelConstants.TRADES_RISK_FACTOR, STRING)
    .asKeyField()
    .withField(ModelConstants.TRADES_SENSITIVITY, STRING)
    .asKeyField()
    .withField(ModelConstants.TRADES_CURRENCY, STRING)
    .withField(ModelConstants.TRADES_PNL, DOUBLE)
    .build();
```

### What does the Books store hold?

The `Books` store describes the organization of each book. Its key field is `BookId`.
The other fields place each book in a trading desk, a sub business unit and a business unit.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
return StoreDescription.builder()
    .withStoreName(ModelConstants.BOOKS_STORE_NAME)
    .withField(ModelConstants.BOOKS_BOOK_ID, INT)
    .asKeyField()
    .withField(ModelConstants.BOOKS_BOOK, STRING)
    .withField(ModelConstants.BOOKS_TRADING_DESK, STRING)
    .withField(ModelConstants.BOOKS_SUB_BUSINESS_UNIT, STRING)
    .withField(ModelConstants.BOOKS_BUSINESS_UNIT, STRING)
    .build();
```

### How are trades joined to books?

The `TradesToBooks` reference joins the `Trades` store to the `Books` store on the `BookId` field.
Through this reference, the cube can read book attributes for each trade.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
references.add(
    ReferenceDescription.builder()
        .fromStore(ModelConstants.TRADES_STORE_NAME)
        .toStore(ModelConstants.BOOKS_STORE_NAME)
        .withName(ModelConstants.TRADES_TO_BOOKS_REF)
        .withMapping(ModelConstants.TRADES_BOOK_ID, ModelConstants.BOOKS_BOOK_ID)
        .build());
```

### How are FX rates stored?

The `FxRates` store holds the rate to convert one unit of each currency to EUR, for each as-of date.
Its key fields are `AsOfDate` and `Currency`. The `RateToEUR` field holds the rate.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
return StoreDescription.builder()
    .withStoreName(ModelConstants.FX_RATES_STORE_NAME)
    .withField(ModelConstants.FX_RATES_AS_OF_DATE, LOCAL_DATE)
    .asKeyField()
    .withField(ModelConstants.FX_RATES_CURRENCY, STRING)
    .asKeyField()
    .withField(ModelConstants.FX_RATES_RATE_TO_EUR, DOUBLE)
    .build();
```

No reference links the `FxRates` store to the `Trades` store. The cube reads the rates through a lookup measure instead.
The section "How does the cube read FX rates?" on this page describes that measure.

For more on store definitions, see [Datastore configuration](https://docs.activeviam.com/engine/java-sdk/6.2/datastore/datastore_config.md).

## How is the cube built?

The `PivotManagerConfig` class defines one cube named `Trades`. The cube definition has three parts: a selection, dimensions and measures.

### Which data does the cube read?

The selection defines which store fields the cube can use. The sample selection starts from the `Trades` store.
The selection includes every field that the `Trades` store can reach through references.
The `Books` fields are therefore part of the selection, through the `TradesToBooks` reference.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
return StartBuilding.selection(datastoreSchema)
    .fromBaseStore(ModelConstants.TRADES_STORE_NAME)
    .withAllReachableFields()
    .build();
```

The `FxRates` store is not part of the selection, because no reference reaches it from the `Trades` store.

### Which dimensions does the cube expose?

The cube exposes four single-level hierarchies and two multi-level hierarchies.

The single-level hierarchies are `AsOfDate`, `TradeId`, `Currency` and `Sensitivity`.
Each one contains one level with the same name.

The two multi-level hierarchies are:

* The `Booking` hierarchy, in the `Organization` dimension. Its levels are `BusinessUnit`, `SubBusinessUnit`, `TradingDesk` and `Book`, from top to bottom.
* The `Risk factor` hierarchy, in the `Risk` dimension. Its levels are `RiskClass` and `RiskFactor`, from top to bottom.

The following code defines these dimensions.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
return builder
    .withSingleLevelDimensions(
        ModelConstants.TRADES_AS_OF_DATE,
        ModelConstants.TRADES_TRADE_ID,
        ModelConstants.TRADES_CURRENCY,
        ModelConstants.TRADES_SENSITIVITY)
    // Booking hierarchy: Business unit → Sub business unit → Trading desk → Book
    .withDimension(DIM_ORGANIZATION)
    .withHierarchy(HIERARCHY_BOOKING)
    .withLevel(ModelConstants.BOOKS_BUSINESS_UNIT)
    .withLevel(ModelConstants.BOOKS_SUB_BUSINESS_UNIT)
    .withLevel(ModelConstants.BOOKS_TRADING_DESK)
    .withLevel(ModelConstants.BOOKS_BOOK)
    // Risk hierarchy: Risk class → Risk factor
    .withDimension(DIM_RISK)
    .withHierarchy(HIERARCHY_RISK_FACTOR)
    .withLevel(ModelConstants.TRADES_RISK_CLASS)
    .withLevel(ModelConstants.TRADES_RISK_FACTOR);
```

For more on dimensions and hierarchies, see [Dimension and hierarchy configuration](https://docs.activeviam.com/engine/java-sdk/6.2/cube/hierarchy_configuration.md).

## How are the measures defined?

The cube defines three measures with the Copper API. The goal is a P\&L in EUR: `PnL.SUM` and `FX rate to EUR`
are the building blocks of `PnL (EUR)`.

| Measure | Definition |
| - | - |
| `PnL.SUM` | Sum of `PnL` in the trade currency. |
| `FX rate to EUR` | The `RateToEUR` value from the `FxRates` store, for the `AsOfDate` and `Currency` being queried. |
| `PnL (EUR)` | `PnL.SUM` multiplied by `FX rate to EUR` for each `AsOfDate` and `Currency`, then summed. |

### How is the P\&L summed?

The `PnL.SUM` measure sums the `PnL` field of the `Trades` store. The measure is placed in the `P&L` folder.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
Copper.sum(ModelConstants.TRADES_PNL)
    .as(MEASURE_PNL_SUM)
    .withinFolder(FOLDER_PNL)
    .withFormatter(DOUBLE_FORMATTER)
    .publish(context);
```

> [!NOTE]
>
> `PnL.SUM` does not convert currencies: it adds the `PnL` values as they are, each in the currency of its trade.
> Amounts in different currencies, such as USD and CHF, cannot be added as they are.

### How should the cube handle several currencies?

Two designs avoid adding amounts in different currencies. Each one changes what the users of the cube see.

| Design | What users see | What it needs |
| - | - | - |
| Slice on `Currency` | The cube shows one currency at a time. There is no total across currencies, not even for a desk that trades in several. A conversion measure such as `PnL (EUR)` shows the EUR value of the selected currency only. | Nothing more than `PnL.SUM`. |
| Convert to one currency | Totals at every level, all in the same currency. | FX rates and a conversion measure. |

Slicing means making `Currency` a [slicing hierarchy](https://docs.activeviam.com/concepts/cube/hierarchies.md#what-is-a-slicing-hierarchy).
The cube then shows one currency at a time, so `PnL.SUM` never adds currencies together.
If the user does not choose a currency, the cube shows a default one.

The choice depends on what the user needs to understand:

* **Convert** when the user needs totals across currencies, as in most P\&L and risk reporting.
* **Slice** when the user only needs to see the amounts in one currency at a time.

This guide converts the P\&L to EUR, because the goal is a total P\&L across all currencies. `Currency` is therefore an ordinary hierarchy, not a slicing one: users can still filter or expand on a currency, but nothing forces them to choose one.
Larger cubes often offer both: measures in the trade currency next to converted measures, and sometimes a slicing hierarchy that lets users choose the target currency.

To convert amounts to EUR, the cube needs the FX rates.

### How does the cube read FX rates?

The `FX rate to EUR` measure reads the `RateToEUR` field from the isolated `FxRates` store. The following code does this in two steps.

1. It joins the `FxRates` store to the cube. The `AsOfDate` and `Currency` fields of `FxRates` map to the `AsOfDate` and `Currency` levels.
2. A lookup measure returns the rate that matches the `AsOfDate` and `Currency` of each queried location. The measure is placed in the `FX` folder.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
final CopperStore fxRates =
    Copper.store(ModelConstants.FX_RATES_STORE_NAME)
        .joinToCube(UnlinkedCopperStore.JoinType.INNER)
        .withMapping(ModelConstants.FX_RATES_AS_OF_DATE, AS_OF_DATE_LEVEL)
        .withMapping(ModelConstants.FX_RATES_CURRENCY, CURRENCY_LEVEL);
// Looks up the rate for the AsOfDate and Currency of the queried location.
Copper.newLookupMeasure(fxRates.field(ModelConstants.FX_RATES_RATE_TO_EUR))
    .as(MEASURE_FX_RATE)
    .withinFolder(FOLDER_FX)
    .withFormatter(RATE_FORMATTER)
    .publish(context);
```

For more on joining isolated stores, see [Joining isolated stores in Copper](https://docs.activeviam.com/engine/java-sdk/6.2/copper/copper_join.md).

### How is the P\&L converted to EUR?

The `PnL (EUR)` measure gives the same result as converting each trade to EUR and then adding them up.
Because all trades with the same `AsOfDate` and `Currency` use the same rate, it first sums their `PnL` with
`PnL.SUM`, then multiplies that sum by the `FX rate to EUR` measure. It then adds these converted values
across all dates and currencies in the queried location.

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
Copper.sum(ModelConstants.TRADES_PNL)
    .multiply(Copper.measure(MEASURE_FX_RATE))
    .per(AS_OF_DATE_LEVEL, CURRENCY_LEVEL)
    .sum()
    .as(MEASURE_PNL_EUR)
    .withinFolder(FOLDER_PNL)
    .withFormatter(DOUBLE_FORMATTER)
    .publish(context);
```

Unlike `PnL.SUM`, the `PnL (EUR)` measure is meaningful at any level, because all its values are in EUR.

For more on Copper measures, see [Creating and publishing measures](https://docs.activeviam.com/engine/java-sdk/6.2/copper/copper_measures.md).

## Next steps

The finished project is in the `datastore` folder of the [atoti-java-sdk-install-and-start](https://github.com/activeviam/atoti-java-sdk-install-and-start) repository on GitHub.

To learn more about the components used on this page, see:

* [CSV source](https://docs.activeviam.com/engine/java-sdk/6.2/sources/csv_source.md)
* [Datastore configuration](https://docs.activeviam.com/engine/java-sdk/6.2/datastore/datastore_config.md)
* [Cube configuration](https://docs.activeviam.com/engine/java-sdk/6.2/cube/configuration.md)
* [Copper introduction](https://docs.activeviam.com/engine/java-sdk/6.2/copper/copper_intro.md)
* [Joining isolated stores in Copper](https://docs.activeviam.com/engine/java-sdk/6.2/copper/copper_join.md)
* [Creating and publishing measures](https://docs.activeviam.com/engine/java-sdk/6.2/copper/copper_measures.md)
* [Slicing hierarchies](https://docs.activeviam.com/concepts/cube/hierarchies.md#what-is-a-slicing-hierarchy)
