001/* 002 * Copyright 2025 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.trash; 017 018import org.ametys.cms.trash.element.TrashElementDAO; 019import org.ametys.plugins.repository.AmetysObject; 020import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 021import org.ametys.plugins.repository.trash.TrashableAmetysObject; 022import org.ametys.web.repository.SiteAwareAmetysObject; 023import org.ametys.web.repository.site.Site; 024 025/** 026 * Add supports for a trash by site 027 */ 028public class WebTrashElementDAO extends TrashElementDAO 029{ 030 /** 031 * Get the trash root for the object. 032 * The root can be the root for all element shared between sites 033 * or the root for the site of the element 034 */ 035 @Override 036 protected ModifiableTraversableAmetysObject getOrCreateRoot(TrashableAmetysObject ametysObject) 037 { 038 // If the object is site aware use it, otherwise, try to guess the site based on the path 039 String siteName = ametysObject instanceof SiteAwareAmetysObject siteAO 040 ? siteAO.getSiteName() 041 : _getParentSiteName(ametysObject); 042 043 return siteName != null 044 ? _getOrCreateSiteRoot(siteName) // get the root for the site 045 : super.getOrCreateRoot(ametysObject); // get the root for the shared object 046 } 047 048 private ModifiableTraversableAmetysObject _getOrCreateSiteRoot(String siteName) 049 { 050 return executeInTrashSession( 051 session -> 052 { 053 ModifiableTraversableAmetysObject root = _resolver.resolveByPath("/", session); 054 ModifiableTraversableAmetysObject sitesTrash = getOrCreateCollection(root, "ametys-internal:sites-trash"); 055 return getOrCreateCollection(sitesTrash, siteName); 056 } 057 ); 058 } 059 060 // Search the sitename on parents of the object 061 private String _getParentSiteName(AmetysObject ametysObject) 062 { 063 if (ametysObject instanceof Site site) 064 { 065 return site.getName(); 066 } 067 else if (ametysObject != null) 068 { 069 return _getParentSiteName(ametysObject.getParent()); 070 } 071 else 072 { 073 return null; 074 } 075 } 076}