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