001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.frontoffice.search.instance.model;
017
018import java.util.Optional;
019
020import org.ametys.plugins.repository.AmetysObjectResolver;
021import org.ametys.web.frontoffice.search.instance.SearchServiceInstance;
022import org.ametys.web.repository.page.Page;
023
024/**
025 * A link in a {@link SearchServiceInstance}
026 */
027public class Link
028{
029    // keep id instead of real page object and then re-resolve it to avoid RepositoryException about session closed
030    private String _targetPageId;
031    private String _title;
032    private AmetysObjectResolver _resolver;
033
034    /**
035     * Creates a Link of {@link SearchServiceInstance}
036     * @param targetPage the target page of the link. Can be null
037     * @param title the title of the link
038     * @param resolver The ametys object resolver
039     */
040    public Link(String targetPage, String title, AmetysObjectResolver resolver)
041    {
042        _targetPageId = targetPage;
043        _title = title;
044        _resolver = resolver;
045    }
046    
047    /**
048     * Gets the target page of the link
049     * @return the target page of the link
050     */
051    public Optional<Page> getTarget()
052    {
053        return Optional.ofNullable(_targetPageId)
054                .map(_resolver::resolveById);
055    }
056    
057    /**
058     * Gets the title of the link
059     * @return the title of the link
060     */
061    public String getTitle()
062    {
063        return _title;
064    }
065}