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.nuxeo.ecm.web.resources.api.Resource;
022import org.nuxeo.ecm.web.resources.api.ResourceContextImpl;
023import org.nuxeo.ecm.web.resources.api.ResourceType;
024import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
025import org.nuxeo.runtime.api.Framework;
026import org.nuxeo.theme.styling.service.ThemeStylingService;
027import org.nuxeo.theme.styling.service.descriptors.PageDescriptor;
028
029import ro.isdc.wro.model.WroModel;
030import ro.isdc.wro.model.factory.WroModelFactory;
031import ro.isdc.wro.model.group.Group;
032
033/**
034 * Generates a {@link WroModel} using contributions to the {@link ThemeStylingService} service.
035 * <p>
036 * Maps {@link PageResourceBundle} elements to wro groups, and map their ordered resources (with resolved dependencies)
037 * to wro resources.
038 *
039 * @since 7.10
040 */
041public class NuxeoWroPageModelFactory extends NuxeoWroModelFactory implements WroModelFactory {
042
043    @Override
044    public WroModel create() {
045        WroModel model = new WroModel();
046        ThemeStylingService ts = Framework.getService(ThemeStylingService.class);
047        WebResourceManager ws = Framework.getService(WebResourceManager.class);
048        ResourceContextImpl rcontext = new ResourceContextImpl();
049
050        List<PageDescriptor> pages = ts.getPages();
051        for (PageDescriptor page : pages) {
052            String groupName = page.getName();
053            Group group = new Group(groupName);
054            List<String> bundleNames = page.getResourceBundles();
055            for (String bundleName : bundleNames) {
056                List<Resource> resources = ws.getResources(rcontext, bundleName, ResourceType.any.name());
057                if (resources != null) {
058                    for (Resource resource : resources) {
059                        ro.isdc.wro.model.resource.Resource wr = toWroResource(bundleName, resource);
060                        if (wr != null) {
061                            group.addResource(wr);
062                        }
063                    }
064                }
065            }
066            model.addGroup(group);
067        }
068
069        return model;
070    }
071
072    public void destroy() {
073    }
074
075}