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.odfweb.service.search;
017
018import java.util.Collection;
019import java.util.Collections;
020import java.util.function.Function;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
028import org.ametys.cms.search.query.Query;
029import org.ametys.odf.program.Program;
030import org.ametys.odf.program.ProgramFactory;
031import org.ametys.plugins.odfweb.repository.OdfPageResolver;
032import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap;
033import org.ametys.web.frontoffice.search.metamodel.Returnable;
034import org.ametys.web.frontoffice.search.metamodel.ReturnableExtensionPoint;
035import org.ametys.web.frontoffice.search.metamodel.ReturnableSaxer;
036import org.ametys.web.frontoffice.search.metamodel.impl.AbstractContentBasedReturnable;
037import org.ametys.web.frontoffice.search.metamodel.impl.PageReturnable;
038
039/**
040 * {@link Returnable} for {@link Program}s
041 */
042public class ProgramReturnable extends AbstractContentBasedReturnable
043{
044    /** Avalon Role */
045    public static final String ROLE = ProgramReturnable.class.getName();
046    
047    /** The additional parameter for indicating how subprograms are displayed */
048    public static final String PARAMETER_DISPLAY_SUBPROGRAMS = "displaySubprogram";
049    
050    /** The prefix for the ids of sorts */
051    protected static final String __PREFIX_ID = "ProgramReturnable$";
052
053    /** The context */
054    Context _avalonContext;
055    
056    private OdfPageResolver _odfPageResolver;
057    private ContentTypeExtensionPoint _cTypeEP;
058    private ReturnableExtensionPoint _returnableEP;
059    private PageReturnable _pageReturnable;
060    
061    
062    @Override
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        super.service(manager);
066        _odfPageResolver = (OdfPageResolver) manager.lookup(OdfPageResolver.ROLE);
067        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
068        _returnableEP = (ReturnableExtensionPoint) manager.lookup(ReturnableExtensionPoint.ROLE);
069    }
070    
071    @Override
072    protected Function<Query, Query> siteQueryJoiner()
073    {
074        // Programs contents are "no-site" contents
075        return null;
076    }
077    
078    /**
079     * Get the ODF page resolver
080     * @return the ODF page resolver
081     */
082    protected OdfPageResolver _getOdfPageResolver()
083    {
084        return _odfPageResolver;
085    }
086    
087    /**
088     * Get the {@link ContentTypeExtensionPoint}
089     * @return the {@link ContentTypeExtensionPoint}
090     */
091    protected ContentTypeExtensionPoint _getContentTypeEP()
092    {
093        return _cTypeEP;
094    }
095    
096    /**
097     * Get the {@link Returnable} for page
098     * @return the {@link PageReturnable}
099     */
100    protected PageReturnable _gePageReturnable()
101    {
102        if (_pageReturnable == null)
103        {
104            _pageReturnable = (PageReturnable) _returnableEP.getExtension(PageReturnable.ROLE);
105        }
106        return _pageReturnable;
107    }
108    
109    @Override
110    protected String associatedContentSearchableRole()
111    {
112        return ProgramSearchable.ROLE;
113    }
114    
115    @Override
116    public void contextualize(Context context) throws ContextException
117    {
118        super.contextualize(context);
119        _avalonContext = _context;
120    }
121    
122    @Override
123    public String getId()
124    {
125        return ROLE;
126    }
127    
128    @Override
129    protected Collection<String> getContentTypes(AdditionalParameterValueMap additionalParameterValues)
130    {
131        return Collections.singleton(ProgramFactory.PROGRAM_CONTENT_TYPE);
132    }
133    
134    @Override
135    public ReturnableSaxer getSaxer(Collection<Returnable> allReturnables, AdditionalParameterValueMap additionalParameterValues)
136    {
137        DisplaySubprogramMode displaySubprogramMode = getDisplaySubprogramMode(additionalParameterValues);
138        return new ProgramSaxer(_gePageReturnable(), this, displaySubprogramMode);
139    }
140    
141    static DisplaySubprogramMode getDisplaySubprogramMode(AdditionalParameterValueMap additionalParameterValues)
142    {
143        String displaySubprogramParamValue = additionalParameterValues.getValue(PARAMETER_DISPLAY_SUBPROGRAMS);
144        return DisplaySubprogramMode.valueOf(displaySubprogramParamValue.toUpperCase());
145    }
146    
147    @Override
148    protected String getDefinitionPrefix()
149    {
150        return __PREFIX_ID;
151    }
152    
153    static enum DisplaySubprogramMode
154    {
155        /** Display no subprogram */
156        NONE,
157        /** Display all subprograms */
158        ALL,
159        /** Display all subprograms with highlighting those which match the search criteria */
160        ALL_WITH_HIGHLIGHT,
161        /** Display matching subprograms only */
162        MATCHING_SEARCH_ONLY
163    }
164}