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().getValueAsBoolean("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", _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_EXPLORER_SHARED"))); 107 result.put("path", "/dummy/" + name); 108 result.put("isShared", true); 109 return result; 110 } 111 112 @Override 113 public Map<String, Object> getResourceProperties(Resource resource) 114 { 115 Map<String, Object> infos = super.getResourceProperties(resource); 116 117 boolean isShared = ResourceHelper.isShared(resource); 118 infos.put("isShared", isShared); 119 120 if (isShared) 121 { 122 String sharedRoot = ResourceHelper.getSharedRootName(resource); 123 infos.put("sharedRoot", sharedRoot); 124 } 125 126 if (_getParentPage(resource) != null) 127 { 128 infos.put("rootOwnerType", "page"); 129 } 130 131 return infos; 132 } 133 134 @Override 135 public Map<String, Object> getExplorerNodeProperties(ExplorerNode node) 136 { 137 Map<String, Object> infos = super.getExplorerNodeProperties(node); 138 139 boolean isShared = ResourceHelper.isShared(node); 140 infos.put("isShared", isShared); 141 142 if (isShared) 143 { 144 String sharedRoot = ResourceHelper.getSharedRootName(node); 145 infos.put("sharedRoot", sharedRoot); 146 } 147 148 if (_getParentPage(node) != null) 149 { 150 infos.put("rootOwnerType", "page"); 151 } 152 153 return infos; 154 } 155 156 @Override 157 protected Set<String> getUserRights(ExplorerNode node) 158 { 159 Page parentPage = _getParentPage(node); 160 if (parentPage != null) 161 { 162 return _rightManager.getUserRights(_currentUserProvider.getUser(), parentPage); 163 } 164 165 return super.getUserRights(node); 166 } 167 168 /** 169 * Get the parent page of a resource or collection 170 * @param ao The resource or collection 171 * @return the parent page or null if the object is not part of page attachments 172 */ 173 protected Page _getParentPage (AmetysObject ao) 174 { 175 AmetysObject parent = ao.getParent(); 176 while (parent != null) 177 { 178 if (parent instanceof Page) 179 { 180 return (Page) parent; 181 } 182 parent = parent.getParent(); 183 } 184 return null; 185 } 186}