001/*
002 *  Copyright 2019 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.plugins.ugc.page;
017
018import java.util.Map;
019
020import org.apache.commons.lang3.LocaleUtils;
021
022import org.ametys.cms.repository.Content;
023import org.ametys.runtime.i18n.I18nizableText;
024import org.ametys.web.repository.page.Page;
025
026import com.google.common.collect.ImmutableMap;
027
028/**
029 * The information about a {@link UGCTransitionalPage}
030 */
031public interface TransitionalPageInformation
032{
033    /**
034     * Gets the unique key of the transitional page
035     * @return the unique key of the transitional page
036     */
037    String getKey();
038    
039    /**
040     * Gets the value of the classification attribute for the transitional page
041     * @return the value of the classification attribute for the transitional page
042     */
043    String getAttributeValue();
044    
045    /**
046     * Gets the title of the transitional page
047     * @return the title of the transitional page
048     */
049    String getTitle();
050    
051    /**
052     * Gets the information as a map
053     * @return the information as a map
054     */
055    default Map<String, String> getInfo()
056    {
057        return ImmutableMap.of(
058                UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE, getAttributeValue(),
059                UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE, getTitle());
060    }
061    
062    /**
063     * UGC pages are classified by a linked Content on their UG Content (for instance entries of a reference table)
064     */
065    static class TypeContent implements TransitionalPageInformation
066    {
067        private Content _content;
068        private Page _rootPage;
069        
070        TypeContent(Content content, Page rootPage)
071        {
072            _content = content;
073            _rootPage = rootPage;
074        }
075        
076        @Override
077        public String getKey()
078        {
079            return _content.getName();
080        }
081        
082        @Override
083        public String getAttributeValue()
084        {
085            return _content.getId();
086        }
087        
088        @Override
089        public String getTitle()
090        {
091            return _content.getTitle(LocaleUtils.toLocale(_rootPage.getSitemapName()));
092        }
093    }
094    
095    /**
096     * UGC pages are classified by entries of an enumerator
097     */
098    static class TypeEnum implements TransitionalPageInformation
099    {
100        private String _key;
101        private I18nizableText _text;
102        private UGCPageHandler _ugcPageHandler;
103        
104        TypeEnum(UGCPageHandler ugcPageHandler, String key, I18nizableText text)
105        {
106            _ugcPageHandler = ugcPageHandler;
107            _key = key;
108            _text = text;
109        }
110        
111        @Override
112        public String getKey()
113        {
114            return _key;
115        }
116        
117        @Override
118        public String getAttributeValue()
119        {
120            return _key;
121        }
122        
123        @Override
124        public String getTitle()
125        {
126            return _ugcPageHandler._i18nUtils.translate(_text);
127        }
128    }
129}