001/*
002 *  Copyright 2010 Anyware Services
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 */
016package org.ametys.web.clientsideelement;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.ui.Callable;
027import org.ametys.core.ui.ClientSideElement;
028import org.ametys.core.ui.StaticClientSideElement;
029import org.ametys.plugins.explorer.applications.ExplorerApplicationExtensionPoint;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.web.repository.site.SiteManager;
032
033/**
034 * This implementation creates an element from a static configuration. This implementation is used by the explorer tool to retrieve the js and css files of applications.
035 */
036public class ExplorerClientSideElement extends StaticClientSideElement
037{
038    private ExplorerApplicationExtensionPoint _appExtPt;
039    private SiteManager _siteManager;
040    private boolean _initialized;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _appExtPt = (ExplorerApplicationExtensionPoint) smanager.lookup(ExplorerApplicationExtensionPoint.ROLE);
047        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
048    }
049    
050    private void _lazyInitialize(Map<String, Object> contextParameters)
051    {
052        if (!_initialized)
053        {
054            Set<String> ids = _appExtPt.getExtensionsIds();
055            for (String id : ids)
056            {
057                ClientSideElement application = _appExtPt.getExtension(id);
058                for (Script scriptApp : application.getScripts(contextParameters))
059                {
060                    if (scriptApp != null)
061                    {
062                        _script.getScriptFiles().addAll(scriptApp.getScriptFiles());
063                        _script.getCSSFiles().addAll(scriptApp.getCSSFiles());
064                    }
065                }
066            }
067            
068            _initialized = true;
069        }
070    }
071    
072    /**
073     * Get the root id of resources
074     * @param siteName The site name
075     * @return the root id
076     */
077    @Callable
078    public Map<String, Object> getRoot (String siteName)
079    {
080        Map<String, Object> result = new HashMap<>();
081        
082        AmetysObject resources = _siteManager.getSite(siteName).getRootResources();
083        result.put("root-id", resources.getId());
084        
085        return result;
086    }
087    
088    @Override
089    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
090    {
091        _lazyInitialize(contextParameters);
092        return super.getScripts(ignoreRights, contextParameters);
093    }
094}