[#auto-complete]
=== Auto-complete

: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:

The xref:refguide:applib:index/annotation/DomainObject.adoc#autoCompleteRepository[autoCompleteRepository()] element nominates a single method on a domain service as the fallback means for looking up instances of the domain object using a simple string.

For example, this might search for a customer by their name or number.
Or it could search for a country based on its ISO-3 code or user-friendly name.

[TIP]
====
If you require additional control - for example restricting the returned results based on the object being interacted with - then use the xref:refguide:applib-methods:prefixes.adoc#autoComplete[autoComplete...()] supporting method instead.
====

For example:

[source,java]
----
@DomainObject(
    autoCompleteRepository=CustomerRepository.class
)
public class Customer {
   ....
}
----

where:

[source,java]
----
@DomainService
public class CustomerRepository {
    List<Customer> autoComplete(String search);  // <1>
    ...
}
----
<1> is assumed to be called "autoComplete", and accepts a single string


(As noted above) the method invoked on the repository is assumed to be called "autoComplete".
The optional xref:refguide:applib:index/annotation/DomainObject.adoc#autoCompleteMethod[autoCompleteMethod()] element allows the name of this method to be overridden.

For example:

[source,java]
----
@DomainObject(
    autoCompleteRepository=Customers.class,
    autoCompleteAction="findByName"
)
public class Customer {
   ....
}
----

where in this case `findByName` might be an existing action already defined:

[source,java]
----
@DomainService(nature = NatureOfService.VIEW)
public class Customers {

    @Action(semantics=SemanticsOf.SAFE)
    public List<Customer> findByName(
        @MinLength(3)                       // <.>
        @ParameterLayout(named="name")
        String name);
    ...
}
----
<.> end-user must enter minimum number of characters to trigger the query

NOTE: The autocomplete "action" can also be a regular method, annotated using xref:refguide:applib:index/annotation/Programmatic.adoc[@Programmatic].
That is, it does not need to be part of the metamodel:
+
[source,java]
----
@DomainService(nature = NatureOfService.VIEW)
public class Customers {
    @Programmatic
    public List<Customer> findByName(
        @MinLength(3)
        String name);
    ...
}
----

