001/*
002 * (C) Copyright 2015 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-2.1.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.web.resources.wro.factory;
018
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.web.resources.api.Resource;
025import org.nuxeo.ecm.web.resources.api.ResourceBundle;
026import org.nuxeo.ecm.web.resources.api.ResourceContextImpl;
027import org.nuxeo.ecm.web.resources.api.ResourceType;
028import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
029import org.nuxeo.ecm.web.resources.wro.provider.NuxeoUriLocator;
030import org.nuxeo.runtime.api.Framework;
031
032import ro.isdc.wro.model.WroModel;
033import ro.isdc.wro.model.factory.WroModelFactory;
034import ro.isdc.wro.model.group.Group;
035
036/**
037 * Generates a {@link WroModel} using contributions to the {@link WebResourceManager} service.
038 * <p>
039 * Maps {@link ResourceBundle} elements to wro groups, and map their ordered resources (with resolved dependencies) to
040 * wro resources.
041 *
042 * @since 7.3
043 */
044public class NuxeoWroModelFactory implements WroModelFactory {
045
046    private static final Log log = LogFactory.getLog(NuxeoWroModelFactory.class);
047
048    @Override
049    public WroModel create() {
050        WroModel model = new WroModel();
051        WebResourceManager service = Framework.getService(WebResourceManager.class);
052        ResourceContextImpl rcontext = new ResourceContextImpl();
053        List<ResourceBundle> bundles = service.getResourceBundles();
054        for (ResourceBundle bundle : bundles) {
055            String groupName = bundle.getName();
056            Group group = new Group(groupName);
057            List<Resource> resources = service.getResources(rcontext, groupName, ResourceType.any.name());
058            if (resources != null) {
059                for (Resource resource : resources) {
060                    ro.isdc.wro.model.resource.Resource wr = toWroResource(groupName, resource);
061                    if (wr != null) {
062                        group.addResource(wr);
063                    }
064                }
065            }
066            model.addGroup(group);
067        }
068        return model;
069    }
070
071    protected ro.isdc.wro.model.resource.Resource toWroResource(String bundle, Resource resource) {
072        ro.isdc.wro.model.resource.ResourceType type = toWroResourceType(resource.getType());
073        if (type == null) {
074            if (log.isDebugEnabled()) {
075                log.debug(String.format("Cannot handle resource type '%s' for resource '%s'", resource.getType(),
076                        resource.getName()));
077            }
078            return null;
079        }
080        String uri = NuxeoUriLocator.getUri(resource);
081        if (uri == null) {
082            log.error(String.format("Cannot handle resource '%s' for bundle '%s': no uri resolved", resource.getName(),
083                    bundle));
084            return null;
085        }
086        ro.isdc.wro.model.resource.Resource res = ro.isdc.wro.model.resource.Resource.create(uri, type);
087        res.setMinimize(resource.isShrinkable());
088        return res;
089    }
090
091    protected ro.isdc.wro.model.resource.ResourceType toWroResourceType(String type) {
092        if (StringUtils.isBlank(type)) {
093            return null;
094        }
095        if (ResourceType.js.name().equals(type.toLowerCase())) {
096            return ro.isdc.wro.model.resource.ResourceType.JS;
097        }
098        if (ResourceType.css.name().equals(type.toLowerCase())) {
099            return ro.isdc.wro.model.resource.ResourceType.CSS;
100        }
101        return null;
102    }
103
104    public void destroy() {
105    }
106
107}