001/*
002 *  Copyright 2013 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.cms.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.activity.Disposable;
024import org.apache.avalon.framework.component.ComponentException;
025import org.apache.avalon.framework.configuration.Configuration;
026import org.apache.avalon.framework.configuration.ConfigurationException;
027import org.apache.avalon.framework.configuration.DefaultConfiguration;
028import org.apache.avalon.framework.configuration.MutableConfiguration;
029import org.apache.avalon.framework.context.Context;
030import org.apache.avalon.framework.context.ContextException;
031import org.apache.avalon.framework.context.Contextualizable;
032import org.apache.avalon.framework.service.ServiceException;
033import org.apache.avalon.framework.service.ServiceManager;
034
035import org.ametys.core.ui.StaticClientSideElement;
036import org.ametys.runtime.i18n.I18nizableText;
037import org.ametys.runtime.parameter.Enumerator;
038import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
039
040/**
041 * Simple Search tool HMI item.
042 * Only SAX content type enumerator values.
043 * TODO To remove
044 */
045public class SimpleSearchClientSideElement extends StaticClientSideElement implements Contextualizable, Disposable
046{
047    /** Manager component for enumerators */
048    protected ThreadSafeComponentManager<Enumerator> _enumeratorManager;
049    /** The service manager */
050    protected ServiceManager _manager;
051    /** The context. */
052    protected Context _context;
053    
054    private String _enumeratorToLookup;
055    
056    @Override
057    public void service(ServiceManager smanager) throws ServiceException
058    {
059        super.service(smanager);
060        _manager = smanager;
061    }
062    
063    @Override
064    public void contextualize(Context context) throws ContextException
065    {
066        _context = context;
067    }
068    
069    @Override
070    public void configure(Configuration configuration) throws ConfigurationException
071    {
072        _enumeratorManager = new ThreadSafeComponentManager<>();
073        _enumeratorManager.setLogger(getLogger());
074        _enumeratorManager.contextualize(_context);
075        _enumeratorManager.service(_manager);
076        
077        super.configure(configuration);
078        
079        _enumeratorToLookup = _configureContentTypeEnumerator();
080        
081        try
082        {
083            _enumeratorManager.initialize();
084        }
085        catch (Exception e)
086        {
087            throw new ConfigurationException("Unable to lookup parameter local components", configuration, e);
088        }
089    }
090    
091    @Override
092    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
093    {
094        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
095        Map<String, Object> parameters = new HashMap<>();
096        List<ScriptFile> cssFiles = new ArrayList<>();
097        List<ScriptFile> scriptFiles = new ArrayList<>();
098        for (Script script : scripts)
099        {
100            cssFiles.addAll(script.getCSSFiles());
101            scriptFiles.addAll(script.getScriptFiles());
102            parameters.putAll(script.getParameters());
103        }
104        Script script = new Script(this.getId(), _script.getScriptClassname(), scriptFiles, cssFiles, parameters);
105        
106        try
107        {
108            String className = _enumeratorToLookup;
109            try
110            {
111                Enumerator enumerator = _enumeratorManager.lookup(className);
112                setEnumeratorValues(script, enumerator, "content-type");
113            }
114            catch (ComponentException e)
115            {
116                getLogger().error("Unable to instantiate enumerator for class: " + className, e);
117            }
118        }
119        catch (Exception e)
120        {
121            getLogger().error("Unable to lookup parameter local components", e);
122        }
123        
124        List<Script> result = new ArrayList<>();
125        result.add(script);
126        return result;
127    }
128    
129    /**
130     * Configures the content type enumerator
131     * @return The content type enumerator role
132     * @throws ConfigurationException If the configuration has an issue
133     */
134    @SuppressWarnings("unchecked")
135    protected String _configureContentTypeEnumerator() throws ConfigurationException
136    {
137        String criterionName = "content-type";
138        String enumeratorClassName = "org.ametys.cms.contenttype.ContentTypeEnumerator";
139        String role = criterionName + "$" + enumeratorClassName;
140        
141        try
142        {
143            MutableConfiguration configuration = new DefaultConfiguration("custom-enumerator");
144            
145            MutableConfiguration allOption = new DefaultConfiguration("all-option");
146            allOption.setValue("concat");
147            MutableConfiguration excludePrivate = new DefaultConfiguration("exclude-private");
148            excludePrivate.setValue("true");
149            
150            configuration.addChild(allOption);
151            configuration.addChild(excludePrivate);
152            
153            Class enumeratorClass = Class.forName(enumeratorClassName);
154            _enumeratorManager.addComponent(_pluginName, null, role, enumeratorClass, configuration);
155        }
156        catch (Exception e)
157        {
158            throw new ConfigurationException("Unable to instantiate enumerator for class: " + enumeratorClassName, e);
159        }
160        
161        return role;
162    }
163    
164    /**
165     * Set the enumerator values in script parameters
166     * @param script The script into which set the parameters
167     * @param enumerator The enumerator
168     * @param prefix The parameters prefix
169     * @throws Exception If the enumerator has an issue 
170     */
171    protected void setEnumeratorValues(Script script, Enumerator enumerator, String prefix) throws Exception
172    {
173        Map<Object, I18nizableText> entries = enumerator.getEntries();
174        int index = 0;
175        
176        for (Object value : entries.keySet())
177        {
178            script.getParameters().put(prefix + "-" + index + "-value", value);
179            if (entries.get(value) != null)
180            {
181                script.getParameters().put(prefix + "-" + index + "-label", entries.get(value));
182            }
183            else
184            {
185                script.getParameters().put(prefix + "-" + "-label", value);
186            }
187            index++;
188        }
189    }
190
191    @Override
192    public void dispose()
193    {
194        _enumeratorManager.dispose();
195        _enumeratorManager = null;
196    }
197}