001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.styleguide.service;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import javax.faces.context.ExternalContext;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.common.utils.FileUtils;
033import org.nuxeo.ecm.styleguide.service.descriptors.IconDescriptor;
034import org.nuxeo.runtime.model.ComponentContext;
035import org.nuxeo.runtime.model.ComponentInstance;
036import org.nuxeo.runtime.model.DefaultComponent;
037
038/**
039 * @since 5.7
040 */
041public class StyleGuideServiceImpl extends DefaultComponent implements StyleGuideService {
042
043    private static final long serialVersionUID = 1L;
044
045    private static final Log log = LogFactory.getLog(StyleGuideServiceImpl.class);
046
047    protected StyleGuideIconRegistry iconsReg;
048
049    // Runtime Component API
050
051    @Override
052    public void activate(ComponentContext context) {
053        super.activate(context);
054        iconsReg = new StyleGuideIconRegistry();
055    }
056
057    @Override
058    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
059        if (contribution instanceof IconDescriptor) {
060            IconDescriptor icon = (IconDescriptor) contribution;
061            log.info(String.format("Register icon '%s'", icon.getPath()));
062            registerIcon(icon);
063            log.info(String.format("Done registering icon '%s'", icon.getPath()));
064        }
065    }
066
067    @Override
068    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
069        if (contribution instanceof IconDescriptor) {
070            IconDescriptor icon = (IconDescriptor) contribution;
071            log.info(String.format("Unregister icon '%s'", icon.getPath()));
072            unregisterIcon(icon);
073            log.info(String.format("Done unregistering icon '%s'", icon.getPath()));
074        }
075    }
076
077    protected void registerIcon(IconDescriptor icon) {
078        iconsReg.addContribution(icon);
079    }
080
081    protected void unregisterIcon(IconDescriptor icon) {
082        iconsReg.removeContribution(icon);
083    }
084
085    // Service API
086
087    @Override
088    public Map<String, List<IconDescriptor>> getIconsByCat(ExternalContext ctx, String path) {
089        List<String> iconPaths = resolvePaths(ctx, path);
090        Map<String, List<IconDescriptor>> res = new HashMap<String, List<IconDescriptor>>();
091        // add "unknown" cat by default
092        List<IconDescriptor> unknownCat = new ArrayList<IconDescriptor>();
093        res.put("unknown", unknownCat);
094        if (iconPaths != null) {
095            for (String iconPath : iconPaths) {
096                IconDescriptor desc = iconsReg.getIcon(iconPath);
097                if (desc == null) {
098                    desc = new IconDescriptor();
099                    desc.setPath(iconPath);
100                    desc.setLabel(FileUtils.getFileName(iconPath));
101                    desc.setCategories(Arrays.asList("unknown"));
102                }
103                if (Boolean.FALSE.equals(desc.getEnabled())) {
104                    continue;
105                }
106                List<String> cats = desc.getCategories();
107                if (cats != null) {
108                    for (String cat : cats) {
109                        if (!res.containsKey(cat)) {
110                            List<IconDescriptor> newCat = new ArrayList<IconDescriptor>();
111                            res.put(cat, newCat);
112                        }
113                        res.get(cat).add(desc);
114                    }
115                }
116            }
117        }
118        return res;
119    }
120
121    protected List<String> resolvePaths(ExternalContext ctx, String basePath) {
122        List<String> res = new ArrayList<String>();
123        Set<String> paths = ctx.getResourcePaths(basePath);
124        if (paths != null) {
125            for (String path : paths) {
126                if (path.endsWith("/")) {
127                    // resolve sub resources
128                    res.addAll(resolvePaths(ctx, path));
129                } else {
130                    res.add(path);
131                }
132            }
133        }
134        return res;
135    }
136
137}