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.plugins.thesaurus.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.ui.SimpleMenu;
027import org.ametys.plugins.repository.AmetysObjectIterable;
028import org.ametys.plugins.thesaurus.Thesaurus;
029import org.ametys.plugins.thesaurus.ThesaurusDAO;
030
031/**
032 * This element creates a menu with one gallery item per thesaurus. 
033 */
034public class ThesaurusGallery extends SimpleMenu
035{
036    private ThesaurusDAO _thesaurusDAO;
037    
038    private boolean _lazyConfigured;
039    
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _thesaurusDAO = (ThesaurusDAO) smanager.lookup(ThesaurusDAO.ROLE);
045    }
046    
047    @Override
048    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
049    {
050        if (!_lazyConfigured)
051        {
052            AmetysObjectIterable<Thesaurus> thesaurii = _thesaurusDAO.getThesaurii();
053
054            List<Object> thesauriiParams = new ArrayList<>();
055                
056            @SuppressWarnings("unchecked")
057            Map<String, Object> commonConfig = (Map<String, Object>) this._script.getParameters().get("items-config");
058            
059            for (Thesaurus thesaurus : thesaurii)
060            {
061                Map<String, Object> parameters = new HashMap<>(commonConfig);
062                
063                String id = this.getId() + "-" + thesaurus.getId().substring("thesaurus://".length());
064                parameters.put("id", id);
065                parameters.put("label", thesaurus.getLabel());
066                parameters.put("description", thesaurus.getLabel());
067                parameters.put("thesaurusId", thesaurus.getId());
068                
069                thesauriiParams.add(parameters);
070            }
071            _script.getParameters().put("thesaurii", thesauriiParams);
072            
073            _lazyConfigured = true;
074        }
075        
076        return super.getScripts(ignoreRights, contextParameters);
077    }
078}