001/*
002 *  Copyright 2010 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, softwares
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.web.glyph;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.parameters.Parameters;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.acting.ServiceableAction;
028import org.apache.cocoon.environment.ObjectModelHelper;
029import org.apache.cocoon.environment.Redirector;
030import org.apache.cocoon.environment.Request;
031import org.apache.cocoon.environment.SourceResolver;
032import org.apache.commons.lang.StringUtils;
033
034import org.ametys.core.cocoon.JSonReader;
035
036/**
037 * This class is the action used for display glyph of skin
038 */
039public class SkinGlyphSourceAction extends ServiceableAction
040{
041    private SkinGlyphSourceManager _skinGlyphManager;
042
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        _skinGlyphManager = (SkinGlyphSourceManager) smanager.lookup(SkinGlyphSourceManager.ROLE);
047    }
048
049    @Override
050    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
051    {
052        Request request = ObjectModelHelper.getRequest(objectModel);
053        String skinName = (String) request.getAttribute("skin");
054
055        Map<String, Object> result = new HashMap<>();
056
057        if (StringUtils.isNotBlank(skinName))
058        {
059            List<Map<String, String>> glyphs = new ArrayList<>();
060
061            Set<String> glyphClassNames = _skinGlyphManager.getGlyphes(skinName);
062            for (String glyphClassName : glyphClassNames)
063            {
064                Map<String, String> glyph = new HashMap<>();
065                glyph.put("cssClassName", glyphClassName);
066                glyphs.add(glyph);
067            }
068            result.put("glyphs", glyphs);
069        }
070        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
071        return EMPTY_MAP;
072    }
073}