Class PetStore
Pet/Order/User store seeded from classpath JSON.
Backed by ConcurrentHashMaps; pet and order IDs auto-assign via AtomicLong starting from
the highest seeded ID plus one (or 1 if no seeds existed). Seed files live on the classpath at
petstore/init/{Pets,Orders,Users}.json and are loaded lazily on first access.
This is a sample/demo store — there is no persistence, no transactions, and no replication. Restart wipes state back to the seeded baseline (and clears the audit trail).
Every successful create, update or delete is recorded in the audit trail; see getAudit().
See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringActor recorded for mutations made through the/petstoreAPI.static final InMemoryBeanQueryContext<AuditEntry>Query context over the audit trail.static final InMemoryBeanQueryContext<PendingChange>Query context over staged changes.static final intThe most petsrestock(java.util.Map<org.apache.juneau.petstore.dto.Species, java.lang.Integer>, java.lang.String)adds for one species in one call.static final InMemoryBeanQueryContext<Order>Query context over orders.static final InMemoryBeanQueryContext<Pet>Query context over pets;tagsandphotoare not searchable.static final InMemoryBeanQueryContext<User>Query context over users;passwordis write-only onUser, so it is neither searchable nor a column. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclock()createOrder(Order order) Creates a new order.createOrder(Order order, String actor) Creates an order and records aCREATEaudit entry.Creates a new pet.Creates a pet and records aCREATEaudit entry.createUser(User user) Creates a new user.createUser(User user, String actor) Creates a user and records aCREATEaudit entry.voiddeleteOrder(long id) Deletes an order by ID.voiddeleteOrder(long id, String actor) Deletes an order and records aDELETEaudit entry.voiddeleteOrder(long id, String actor, String reason) Cancels an order, recording the reason as theDELETEaudit detail.voiddeletePet(long id) Deletes a pet by ID.voidDeletes a pet and records aDELETEaudit entry.voiddeleteUser(String username) Deletes a user by username.voiddeleteUser(String username, String actor) Deletes a user and records aDELETEaudit entry.getAudit()Returns the audit trail.getOrder(long id) Returns an order by ID.Returns all orders.getPet(long id) Returns a pet by ID.getPets()Returns all pets.Returns a user by username.getUsers()Returns all users.voidload(Collection<Pet> newPets, Collection<Order> newOrders, Collection<User> newUsers, Collection<AuditEntry> newAudit) Bulk-loads rows with their ids preserved, writing no audit.Returns the staging area for console inline edits (pet detail to Pending changes).Opens a query session over a snapshot of the pets.Appends an audit entry stamped with this store's clock.Adds a batch of available pets, one per requested count, and records one summaryRESTOCKaudit entry.updateOrder(Order order) Updates an existing order.updateOrder(Order order, String actor) Replaces an order and records anUPDATEaudit entry.Updates an existing pet.Replaces a pet and records anUPDATEaudit entry.updateUser(User user) Updates an existing user.updateUser(User user, String actor) Replaces a user and records anUPDATEaudit entry.
-
Field Details
-
ACTOR_API
Actor recorded for mutations made through the/petstoreAPI.- See Also:
-
MAX_RESTOCK_PER_SPECIES
The most petsrestock(java.util.Map<org.apache.juneau.petstore.dto.Species, java.lang.Integer>, java.lang.String)adds for one species in one call.- See Also:
-
PET_QUERY
Query context over pets;tagsandphotoare not searchable. -
ORDER_QUERY
Query context over orders. -
USER_QUERY
Query context over users;passwordis write-only onUser, so it is neither searchable nor a column. -
AUDIT_QUERY
Query context over the audit trail. -
CHANGE_QUERY
Query context over staged changes.
-
-
Constructor Details
-
PetStore
public PetStore()Constructor using the system UTC clock.Eagerly loads the bundled classpath seed data. Seeding writes no audit.
-
PetStore
Constructor.- Parameters:
clock- The clock used to timestamp audit entries. Must not benull .
-
-
Method Details
-
clock
- Returns:
- The clock this store timestamps with.
-
getPets
Returns all pets.- Returns:
- All pets, in arbitrary order. Never
null .
-
getPet
Returns a pet by ID.- Parameters:
id- The pet ID.- Returns:
- The pet, or
null if not found.
-
createPet
Creates a new pet.Assigns the next available ID, ignoring any caller-supplied ID.
- Parameters:
pet- The pet to create. Must not benull .- Returns:
- The created pet (same instance, with assigned ID).
-
createPet
Creates a pet and records aCREATEaudit entry.- Parameters:
pet- The pet. Its ID is overwritten. Must not benull .actor- Who is creating it, e.g."console:alice".- Returns:
- The stored pet.
-
restock
Adds a batch of available pets, one per requested count, and records one summaryRESTOCKaudit entry.Example:
var added =store .restock(Map.of (Species.DOG , 2, Species.CAT , 1),"job:restock" );// 3 new pets named "DOG #101", "DOG #102", "CAT #103" (species + store-unique id), plus one RESTOCK audit entry. - Parameters:
perSpecies- How many pets of each species to add. Must not benull ; each count must be0..MAX_RESTOCK_PER_SPECIES.actor- Who is restocking, e.g."job:restock:console:alice".- Returns:
- The created pets.
- Throws:
IllegalArgumentException- If the map, a species, or a count is null, negative, or above the cap. Nothing is added.
-
updatePet
Updates an existing pet.- Parameters:
pet- The pet to update. Must not benull ; must carry an ID matching an existing pet.- Returns:
- The updated pet.
- Throws:
PetstoreNotFoundException- If no pet with the given ID exists.
-
updatePet
Replaces a pet and records anUPDATEaudit entry.- Parameters:
pet- The pet. Must not benull .actor- Who is updating it.- Returns:
- The stored pet.
- Throws:
PetstoreNotFoundException- If no pet with the given ID exists.
-
deletePet
Deletes a pet by ID.- Parameters:
id- The pet ID.- Throws:
PetstoreNotFoundException- If no pet with the given ID exists.
-
deletePet
Deletes a pet and records aDELETEaudit entry.- Parameters:
id- The pet ID.actor- Who is deleting it.- Throws:
PetstoreNotFoundException- If no pet with the given ID exists.
-
getOrders
Returns all orders.- Returns:
- All orders, in arbitrary order. Never
null .
-
getOrder
Returns an order by ID.- Parameters:
id- The order ID.- Returns:
- The order, or
null if not found.
-
createOrder
Creates a new order.Assigns the next available ID, ignoring any caller-supplied ID.
- Parameters:
order- The order to create. Must not benull .- Returns:
- The created order (same instance, with assigned ID).
-
createOrder
Creates an order and records aCREATEaudit entry.- Parameters:
order- The order. Its ID is overwritten. Must not benull .actor- Who is creating it.- Returns:
- The stored order.
-
updateOrder
Updates an existing order.- Parameters:
order- The order to update. Must not benull ; must carry an ID matching an existing order.- Returns:
- The updated order.
- Throws:
PetstoreNotFoundException- If no order with the given ID exists.
-
updateOrder
Replaces an order and records anUPDATEaudit entry.- Parameters:
order- The order. Must not benull .actor- Who is updating it.- Returns:
- The stored order.
- Throws:
PetstoreNotFoundException- If no order with the given ID exists.
-
deleteOrder
Deletes an order by ID.- Parameters:
id- The order ID.- Throws:
PetstoreNotFoundException- If no order with the given ID exists.
-
deleteOrder
Deletes an order and records aDELETEaudit entry.- Parameters:
id- The order ID.actor- Who is deleting it.- Throws:
PetstoreNotFoundException- If no order with the given ID exists.
-
deleteOrder
Cancels an order, recording the reason as theDELETEaudit detail.- Parameters:
id- The order ID.actor- Who is cancelling it.reason- Why, ornull or blank for none (the audit detail is then the order status).- Throws:
PetstoreNotFoundException- If no order with the given ID exists.
-
getUsers
Returns all users.- Returns:
- All users, in arbitrary order. Never
null .
-
getUser
Returns a user by username.- Parameters:
username- The username (primary key).- Returns:
- The user, or
null if not found.
-
createUser
Creates a new user.- Parameters:
user- The user to create. Must not benull ; must carry a non-null username not already in use.- Returns:
- The created user.
- Throws:
IllegalArgumentException- If the username is already in use.
-
createUser
Creates a user and records aCREATEaudit entry.- Parameters:
user- The user. Must carry a non-null username not already in use.actor- Who is creating it.- Returns:
- The stored user.
- Throws:
IllegalArgumentException- If the username is already in use.
-
updateUser
Updates an existing user.- Parameters:
user- The user to update. Must not benull ; must carry a username matching an existing user.- Returns:
- The updated user.
- Throws:
PetstoreNotFoundException- If no user with the given username exists.
-
updateUser
Replaces a user and records anUPDATEaudit entry.The password is write-only and never read back, so a
null or blank password on the replacement keeps the stored one.- Parameters:
user- The user. Must carry a username matching an existing user.actor- Who is updating it.- Returns:
- The stored user.
- Throws:
PetstoreNotFoundException- If no user with the given username exists.
-
deleteUser
Deletes a user by username.- Parameters:
username- The username.- Throws:
PetstoreNotFoundException- If no user with the given username exists.
-
deleteUser
Deletes a user and records aDELETEaudit entry.- Parameters:
username- The username.actor- Who is deleting it.- Throws:
PetstoreNotFoundException- If no user with the given username exists.
-
getAudit
Returns the audit trail.- Returns:
- An immutable snapshot, oldest first. Never
null .
-
recordAudit
public AuditEntry recordAudit(String actor, String entity, String entityId, String action, String detail) Appends an audit entry stamped with this store's clock.Public so console operations that are not plain CRUD (restock, apply/discard pending changes) are audited too.
- Parameters:
actor- Who did it.entity- The entity kind.entityId- The entity id.action- The action.detail- A short detail, ornull .- Returns:
- The new entry.
-
pendingChanges
Returns the staging area for console inline edits (pet detail to Pending changes).- Returns:
- The pending-changes staging area. Never
null .
-
load
public void load(Collection<Pet> newPets, Collection<Order> newOrders, Collection<User> newUsers, Collection<AuditEntry> newAudit) Bulk-loads rows with their ids preserved, writing no audit. Used byPetstoreSeed.Id counters advance past the highest loaded id, so later creates never collide.
- Parameters:
newPets- Pets to add or replace.newOrders- Orders to add or replace.newUsers- Users to add or replace.newAudit- Audit rows to append, in order.
-
queryPets
Opens a query session over a snapshot of the pets.Example:
try (var s =store .queryPets()) {return DataTablesQuery.run (req ,s ); }- Returns:
- A new session; close it.
-
queryOrders
- Returns:
- A new session over a snapshot of the orders; close it.
-
queryUsers
- Returns:
- A new session over a snapshot of the users; close it.
-
queryAudit
- Returns:
- A new session over a snapshot of the audit trail; close it.
-
queryPendingChanges
- Returns:
- A new session over a snapshot of the staged changes; close it.
-