001/*
002 *  Copyright 2015 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.explorer;
017
018import java.util.ArrayList;
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;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.commons.lang.StringUtils;
028
029import org.ametys.plugins.explorer.ExplorerNode;
030import org.ametys.plugins.explorer.resources.Resource;
031import org.ametys.plugins.repository.AmetysObject;
032import org.ametys.runtime.config.Config;
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.web.repository.page.Page;
035import org.ametys.web.repository.site.SiteManager;
036
037
038/**
039 * This DAO overrides {@link org.ametys.cms.explorer.ExplorerResourcesDAO} to handle shared explorer resources and page attachments.
040 */
041public class ExplorerResourcesDAO extends org.ametys.cms.explorer.ExplorerResourcesDAO
042{
043    /** Site manager */
044    protected SiteManager _siteManager;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
051    }
052    
053    @Override
054    public List<ExplorerNode> getResourcesRootNodes()
055    {
056        Request request = ContextHelper.getRequest(_context);
057        String siteName = (String) request.getAttribute("siteName");
058        
059        List<ExplorerNode> rootNodes = new ArrayList<>();
060        
061        // Site root
062        if (StringUtils.isNotEmpty(siteName))
063        {
064            AmetysObject resourcesRootAO = _siteManager.getSite(siteName).getRootResources();
065            if (resourcesRootAO instanceof ExplorerNode)
066            {
067                rootNodes.add((ExplorerNode) resourcesRootAO);
068            }
069        }
070        
071        // Shared root
072        boolean useShared = Config.getInstance().getValue("resources.shared.folder");
073        if (useShared)
074        {
075            ExplorerNode sharedRoot = _resolver.resolveByPath(ResourceHelper.SHARED_RESOURCE_PATH + "/all");
076            rootNodes.add(sharedRoot);
077        }
078        
079        return rootNodes;
080    }
081    
082    @Override
083    public Map<String, Object> getDefaultInfoAsRootNode(ExplorerNode rootNode)
084    {
085        if (rootNode.getPath().startsWith(ResourceHelper.SHARED_RESOURCE_PATH))
086        {
087            return getSharedRootNodeInfo(rootNode);
088        }
089        return super.getDefaultInfoAsRootNode(rootNode);
090    }
091    
092    /**
093     * Get the necessary info for the shared root node.
094     * Can be used to construct a root node for a tree (client side).
095     * @param sharedRoot The shared root node
096     * @return A map which contains a set of default info (such as id, applicationId, path, type etc...)
097     */
098    public Map<String, Object> getSharedRootNodeInfo(ExplorerNode sharedRoot)
099    {
100        Map<String, Object> result = super.getDefaultInfoAsRootNode(sharedRoot);
101        
102        String name = "shared-resources-" + sharedRoot.getName();
103        result.put("name", name);
104        result.put("cls", "shared-root");
105        result.put("iconCls", "ametysicon-folder249 decorator-ametysicon-world91");
106        result.put("text", _getSharedRootNodeLabel());
107        result.put("path", "/dummy/" + name);
108        result.put("isShared", true);
109        return result;
110    }
111    
112    @Override
113    public I18nizableText getRootNodeLabel(ExplorerNode rootNode)
114    {
115        if (rootNode.getPath().startsWith(ResourceHelper.SHARED_RESOURCE_PATH))
116        {
117            return _getSharedRootNodeLabel();
118        }
119        return super.getRootNodeLabel(rootNode);
120    }
121
122    private I18nizableText _getSharedRootNodeLabel()
123    {
124        return new I18nizableText("plugin.web", "PLUGINS_WEB_EXPLORER_SHARED");
125    }
126    
127    @Override
128    public Map<String, Object> getResourceProperties(Resource resource)
129    {
130        Map<String, Object> infos = super.getResourceProperties(resource);
131        
132        boolean isShared = ResourceHelper.isShared(resource);
133        infos.put("isShared", isShared);
134
135        if (isShared)
136        {
137            String sharedRoot = ResourceHelper.getSharedRootName(resource);
138            infos.put("sharedRoot", sharedRoot);
139        }
140        
141        if (_getParentPage(resource) != null)
142        {
143            infos.put("rootOwnerType", "page");
144        }
145        
146        return infos;
147    }
148    
149    @Override
150    public Map<String, Object> getExplorerNodeProperties(ExplorerNode node)
151    {
152        Map<String, Object> infos = super.getExplorerNodeProperties(node);
153        
154        boolean isShared = ResourceHelper.isShared(node);
155        infos.put("isShared", isShared);
156        
157        if (isShared)
158        {
159            String sharedRoot = ResourceHelper.getSharedRootName(node);
160            infos.put("sharedRoot", sharedRoot);
161        }
162        
163        if (_getParentPage(node) != null)
164        {
165            infos.put("rootOwnerType", "page");
166        }
167        
168        return infos;
169    }
170    
171    @Override
172    protected Set<String> getUserRights(ExplorerNode node)
173    {
174        Page parentPage = _getParentPage(node);
175        if (parentPage != null)
176        {
177            return _rightManager.getUserRights(_currentUserProvider.getUser(), parentPage);
178        }
179        
180        return super.getUserRights(node);
181    }
182    
183    /**
184     * Get the parent page of a resource or collection
185     * @param ao The resource or collection
186     * @return the parent page or null if the object is not part of page attachments
187     */
188    protected Page _getParentPage (AmetysObject ao)
189    {
190        AmetysObject parent = ao.getParent();
191        while (parent != null)
192        {
193            if (parent instanceof Page)
194            {
195                return (Page) parent;
196            }
197            parent = parent.getParent();
198        }
199        return null;
200    }
201}