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