[[persisted-title]]
= Persisted Title

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



Normally the title of an object is not persisted to the database, rather it is recomputed automatically from underlying properties.
On occasion though you might want the title to also be persisted; either to make things easier for the DBA, or for an integration scenario, or to support full-text search.

We can implement this feature by either overriding the  xref:refguide:applib-methods:lifecycle.adoc[lifecycle methods] or (probably better) subscribing to the xref:refguide:applib-classes:events.adoc#lifecycle-events[lifecycle events].

In the design we discuss here we make it a responsibility of the entities to persist the title as a property, by implementing a `ObjectWithPersistedTitle` interface:

[source,java]
----
public interface ObjectWithPersistedTitle {
    @Programmatic                                   // <.>
    String getPersistedTitle();
    void setPersistedTitle(final String title);
}
----
<.> we don't want to expose this in the UI.

We can then define a subscribing domain service that leverage this.

[source,java]
----
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.apache.isis.applib.events.lifecycle.ObjectPersistingEvent;
import org.apache.isis.applib.events.lifecycle.ObjectUpdatingEvent;
import lombok.val;

@Service
public class TitlingService {

    @EventListener(ObjectPersistingEvent.class)
    public void on(ObjectPersistingEvent ev) {
        handle(ev.getSource());
    }

    @EventListener(ObjectUpdatingEvent.class)
    public void on(ObjectUpdatingEvent ev) {
        handle(ev.getSource());
    }

    private void handle(final Object persistentInstance) {
        if(persistentInstance instanceof ObjectWithPersistedTitle) {
            val objectWithPersistedTitle =
                (ObjectWithPersistedTitle) persistentInstance;
            objectWithPersistedTitle.setPersistedTitle(titleService.titleOf(objectWithPersistedTitle));
        }
    }

    @Inject
    TitleService titleService;
}
----


The above is probably the easiest and most straightforward design.
One could imagine other designs where the persisted title is stored elsewhere, such as a link:http://lucene.apache.org/[Apache Lucene] (or similar) database to allow for full-text searches.

