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.plugins.ugc.enumerator;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.logger.AbstractLogEnabled;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028import org.apache.commons.lang.StringUtils;
029import org.apache.excalibur.source.SourceResolver;
030
031import org.ametys.plugins.workflow.support.WorkflowHelper;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.runtime.model.Enumerator;
034
035/**
036 * {@link Enumerator} for workflow enumeration
037 */
038public class WorkflowEnumerator extends AbstractLogEnabled implements Enumerator<String>, Contextualizable, Serviceable
039{
040    private static final String __PREFIX_FILE = "ugc-";
041    
042    /** The cocoon context */
043    protected org.apache.cocoon.environment.Context _context;
044    
045    /** the source resolver */
046    protected SourceResolver _srcResolver;
047    
048    /** The workflow helper */
049    protected WorkflowHelper _workflowHelper;
050    
051    @Override
052    public void contextualize(Context context) throws ContextException 
053    {
054        _context = (org.apache.cocoon.environment.Context) context.get(org.apache.cocoon.Constants.CONTEXT_ENVIRONMENT_CONTEXT);
055    }
056    
057    @Override
058    public void service(ServiceManager manager) throws ServiceException 
059    {
060        _srcResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
061        _workflowHelper = (WorkflowHelper) manager.lookup(WorkflowHelper.ROLE);
062    }
063    
064    @Override
065    public Map<String, I18nizableText> getTypedEntries() throws Exception
066    {
067        return _getThemes();
068    }
069    
070    @Override
071    public I18nizableText getEntry(String value) throws Exception
072    {
073        Map<String, I18nizableText> entries = _getWorkflows();
074        return entries.get(value);
075    }
076    
077    private Map<String, I18nizableText> _getThemes ()
078    {
079        return _getWorkflows();
080    }
081    
082    /**
083     * Retrieves the workflows begin by 'workflow-ugc'
084     * @return The map of workflows
085     */
086    protected Map<String, I18nizableText> _getWorkflows()
087    {
088        Map<String, I18nizableText> workflows = new LinkedHashMap<>();
089        
090        for (String workflowName : _workflowHelper.getWorkflowNames())
091        {
092            if (StringUtils.startsWith(workflowName, __PREFIX_FILE))
093            {
094                String i18nKey = "WORKFLOW_" + workflowName;
095                I18nizableText i18nText = new I18nizableText("application", i18nKey);
096                
097                workflows.put(workflowName, i18nText);
098            }
099        }
100        
101        return workflows;
102    }
103}