001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.styleguide.service;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025
026import javax.faces.context.ExternalContext;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.common.utils.FileUtils;
031import org.nuxeo.ecm.styleguide.service.descriptors.IconDescriptor;
032import org.nuxeo.runtime.model.ComponentContext;
033import org.nuxeo.runtime.model.ComponentInstance;
034import org.nuxeo.runtime.model.DefaultComponent;
035
036/**
037 * @since 5.7
038 */
039public class StyleGuideServiceImpl extends DefaultComponent implements StyleGuideService {
040
041    private static final long serialVersionUID = 1L;
042
043    private static final Log log = LogFactory.getLog(StyleGuideServiceImpl.class);
044
045    protected StyleGuideIconRegistry iconsReg;
046
047    // Runtime Component API
048
049    @Override
050    public void activate(ComponentContext context) {
051        super.activate(context);
052        iconsReg = new StyleGuideIconRegistry();
053    }
054
055    @Override
056    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
057        if (contribution instanceof IconDescriptor) {
058            IconDescriptor icon = (IconDescriptor) contribution;
059            log.info(String.format("Register icon '%s'", icon.getPath()));
060            registerIcon(icon);
061            log.info(String.format("Done registering icon '%s'", icon.getPath()));
062        }
063    }
064
065    @Override
066    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
067        if (contribution instanceof IconDescriptor) {
068            IconDescriptor icon = (IconDescriptor) contribution;
069            log.info(String.format("Unregister icon '%s'", icon.getPath()));
070            unregisterIcon(icon);
071            log.info(String.format("Done unregistering icon '%s'", icon.getPath()));
072        }
073    }
074
075    protected void registerIcon(IconDescriptor icon) {
076        iconsReg.addContribution(icon);
077    }
078
079    protected void unregisterIcon(IconDescriptor icon) {
080        iconsReg.removeContribution(icon);
081    }
082
083    // Service API
084
085    @Override
086    public Map<String, List<IconDescriptor>> getIconsByCat(ExternalContext ctx, String path) {
087        List<String> iconPaths = resolvePaths(ctx, path);
088        Map<String, List<IconDescriptor>> res = new HashMap<String, List<IconDescriptor>>();
089        // add "unknown" cat by default
090        List<IconDescriptor> unknownCat = new ArrayList<IconDescriptor>();
091        res.put("unknown", unknownCat);
092        if (iconPaths != null) {
093            for (String iconPath : iconPaths) {
094                IconDescriptor desc = iconsReg.getIcon(iconPath);
095                if (desc == null) {
096                    desc = new IconDescriptor();
097                    desc.setPath(iconPath);
098                    desc.setLabel(FileUtils.getFileName(iconPath));
099                    desc.setCategories(Arrays.asList("unknown"));
100                }
101                if (Boolean.FALSE.equals(desc.getEnabled())) {
102                    continue;
103                }
104                List<String> cats = desc.getCategories();
105                if (cats != null) {
106                    for (String cat : cats) {
107                        if (!res.containsKey(cat)) {
108                            List<IconDescriptor> newCat = new ArrayList<IconDescriptor>();
109                            res.put(cat, newCat);
110                        }
111                        res.get(cat).add(desc);
112                    }
113                }
114            }
115        }
116        return res;
117    }
118
119    protected List<String> resolvePaths(ExternalContext ctx, String basePath) {
120        List<String> res = new ArrayList<String>();
121        Set<String> paths = ctx.getResourcePaths(basePath);
122        if (paths != null) {
123            for (String path : paths) {
124                if (path.endsWith("/")) {
125                    // resolve sub resources
126                    res.addAll(resolvePaths(ctx, path));
127                } else {
128                    res.add(path);
129                }
130            }
131        }
132        return res;
133    }
134
135}