[[headless-access]]
= Headless access

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:

This section tackles the topic of enabling access to an Apache Causeway application directly, or at least, not through either the xref:vw:ROOT:about.adoc[Wicket] or xref:vro:ROOT:about.adoc[Restful] viewers.

There are a number of use-cases:

* implementing a REST end point that needs to reach into the Apache Causeway runtime

** this could be a custom UI
** this could be to integrate from other systems, eg a subscriber on a pub/sub event bus, pushing changes through an Apache Causeway domain model.

* calling from Quartz for a background batch process

Note that the calling thread runs in the same process space as the Apache Causeway domain object model (must be physically linked to the JAR files containing the domain classes).

The xref:docs:starters:simpleapp.adoc[SimpleApp] starter app demonstrates the pattern with a custom UI controller:

[source,java]
----
@RestController
@RequiredArgsConstructor(onConstructor_ = {@Inject})
class CustomController {

    private final InteractionService interactionService;
    private final TransactionalProcessor transactionalProcessor;
    private final SimpleObjects simpleObjects;

    @GetMapping("/custom/simpleObjects")
    List<SimpleObject> all() {
        return call("sven", simpleObjects::listAll)
                .orElse(Collections.<SimpleObject>emptyList());                                     // <3>
    }

    private <T> Optional<T> call(
            final String username,
            final Callable<T> callable) {

        return interactionService.call(
                InteractionContext.ofUserWithSystemDefaults(UserMemento.ofName(username)),          // <1>
                () -> transactionalProcessor.callWithinCurrentTransactionElseCreateNew(callable))
                .ifFailureFail()                                                                    // <2>
                .getValue();
    }
}
----
<.> the context of this interaction: who, when and where
<.> re-throws exception that has occurred, if any
<.> handles null case, if required

The API described here is reasonably low-level, allowing code to interact very directly with the Apache Causeway metamodel and runtime.
Such callers should be considered trusted: they do not (by default) honour any business rules eg implicit in the Causeway annotations or hide/disable/validate methods.

== Another example: Seed services

Another example is domain services that need to seed reference data during bootstrapping.
An example of this can be found xref:userguide:ROOT:domain-services.adoc#instantiating[here].
