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.LinkedHashMap;
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;
030import org.ametys.core.ui.Callable;
031
032/**
033 * This element creates a menu with one gallery item per thesaurus. 
034 */
035public class ThesaurusGallery extends SimpleMenu
036{
037    private ThesaurusDAO _thesaurusDAO;
038    
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _thesaurusDAO = (ThesaurusDAO) smanager.lookup(ThesaurusDAO.ROLE);
044    }
045    
046    /**
047     * Get the gallery items
048     * @return The gallery items
049     */
050    @Callable
051    public Map<String, Object> getGalleryItems ()
052    {
053        Map<String, Object> items = new LinkedHashMap<>();
054        
055        ArrayList<Map<String, Object>> galleryItems = new ArrayList<>();
056        AmetysObjectIterable<Thesaurus> thesaurii = _thesaurusDAO.getThesaurii();
057        
058        @SuppressWarnings("unchecked")
059        Map<String, Object> commonConfig = (Map<String, Object>) this._script.getParameters().get("items-config");
060        
061        for (Thesaurus thesaurus : thesaurii)
062        {
063            Map<String, Object> parameters = new HashMap<>(commonConfig);
064            
065            String id = this.getId() + "-" + thesaurus.getId().substring("thesaurus://".length());
066            parameters.put("id", id);
067            parameters.put("label", thesaurus.getLabel());
068            parameters.put("description", thesaurus.getLabel());
069            parameters.put("thesaurusId", thesaurus.getId());
070            
071            galleryItems.add(parameters);
072        }
073        
074        items.put("gallery-items", galleryItems);
075        return items;
076    }
077}